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:
- We use the standard C library’s
time.hfor time-related functions. - The
time_ttype is used to represent time, andstruct tmis used for broken-down time. time()is used to get the current time.ctime()andasctime()are used to convert time to string representations.- We manually set fields in
struct tmto create a specific time. timegm()is used to convert astruct tmtotime_t(note: this function might not be available on all systems; you might need to usemktime()and adjust for timezone).- Comparison of times is done using
difftime(). - Duration calculations are done manually using the difference in seconds.
- Adding to or subtracting from a time is done by simple arithmetic on the
time_tvalue.
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_exampleThis will output the current time, the specified time, various components of the time, comparisons, and duration calculations, similar to the original example.
Comments powered by Disqus