Time in C

#include <stdio.h>
#include <time.h>

int main() {
    // We'll start by getting the current time.
    time_t now = time(NULL);
    printf("%s", ctime(&now));

    // You can build a time struct by providing the
    // year, month, day, etc. Times are always associated
    // with a time zone.
    struct tm then = {0};
    then.tm_year = 2009 - 1900;  // Years since 1900
    then.tm_mon = 11 - 1;        // Months are 0-11
    then.tm_mday = 17;
    then.tm_hour = 20;
    then.tm_min = 34;
    then.tm_sec = 58;
    time_t then_time = timegm(&then);
    printf("%s", asctime(&then));

    // You can extract the various components of the time
    // value as expected.
    printf("%d\n", then.tm_year + 1900);
    printf("%d\n", then.tm_mon + 1);
    printf("%d\n", then.tm_mday);
    printf("%d\n", then.tm_hour);
    printf("%d\n", then.tm_min);
    printf("%d\n", then.tm_sec);

    // The day of the week is also available (0-6, Sunday = 0).
    printf("%d\n", then.tm_wday);

    // These functions compare two times, testing if the
    // first occurs before, after, or at the same time
    // as the second, respectively.
    printf("%d\n", difftime(now, then_time) > 0);
    printf("%d\n", difftime(now, then_time) < 0);
    printf("%d\n", difftime(now, then_time) == 0);

    // The difftime function returns a double representing
    // the interval between two times in seconds.
    double diff = difftime(now, then_time);
    printf("%f\n", diff);

    // We can compute the length of the duration in
    // various units.
    printf("%f\n", diff / 3600);     // Hours
    printf("%f\n", diff / 60);       // Minutes
    printf("%f\n", diff);            // Seconds

    // You can use addition to advance a time by a given
    // number of seconds, or subtraction to move backwards.
    time_t advanced = then_time + (time_t)diff;
    time_t backward = then_time - (time_t)diff;
    printf("%s", ctime(&advanced));
    printf("%s", ctime(&backward));

    return 0;
}

This C program demonstrates various time-related operations similar to the original example. Here’s a breakdown of the main differences and adaptations:

  1. We use the standard C library’s time.h for time-related functions.
  2. The time_t type is used to represent time, and struct tm is used for broken-down time.
  3. time() is used to get the current time.
  4. ctime() and asctime() are used to convert time to string representations.
  5. We manually set fields in struct tm to create a specific time.
  6. timegm() is used to convert a struct tm to time_t (note: this function might not be available on all systems; you might need to use mktime() and adjust for timezone).
  7. Comparison of times is done using difftime().
  8. Duration calculations are done manually using the difference in seconds.
  9. Adding to or subtracting from a time is done by simple arithmetic on the time_t value.

Note that C’s time functions are less feature-rich compared to Go’s, so some operations (like extracting nanoseconds or getting the time zone) are not directly available and would require additional work to implement.

To compile and run this program:

$ gcc time_example.c -o time_example
$ ./time_example

This will output the current time, the specified time, various components of the time, comparisons, and duration calculations, similar to the original example.