
Starting off very simple I have decided on a new project. For this post, I will start off with a total of 25 lines of code in C, to generate an executable, that will print out the Linux Date-Time in something akin to ISO 1806 Date-Time, something that should also be readable by Javascript( Nodejs ) as a JSON object.
The C code should in of its self be very readable, and thus should also not need much in the way of an explanation. My idea here, is to keep different aspects of the project I have in mind, in small digestible “chunks” so that anyone who cares to join along, can. As with anything else, if you’re unfamiliar with time.h, or any of the time based data types. Feel free to use google, as there is a wealth of information waiting for you on the internet.
As for the rest of the project, I will be trying to work in various subjects, and technologies to keep the project as a whole interesting, and reusable. Attempting to keep the “back end API” in C as much as possible. Since the backend as of right now will be running on an Embedded Linux ARM based system with a minimal amount of resources. Where I intend a sort of “middleware” server to pick up the shared data with NodeJS, and serve it out to clients with REACT.
Anyway, on to the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int hours, minutes, seconds, day, month, year;
time_t now;
time(&now);
struct tm *local = gmtime(&now);
hours = local->tm_hour; // get hours since midnight (0-23)
minutes = local->tm_min; // get minutes passed after the hour (0-59)
seconds = local->tm_sec; // get seconds passed after a minute (0-59)
day = local->tm_mday; // get day of month (1 to 31)
month = local->tm_mon + 1; // get month of year (0 to 11)
year = local->tm_year + 1900; // get year since 1900
printf("{Date: %02d-%02d-%d %02d:%02d:%02d}\n", year, month, day, hours, minutes, seconds);
return 0;
}
Then once compiled:
gcc -o test iso8601.c
./test
{Date: 2022-07-15 19:26:02}
As for the current simplicity of the project, enjoy it while it lasts. This project in whole will get much more difficult / complex as I (we) progress.