Jon Skeet spoke of the complexity of programming dates and times at the 2009 DevDays in London.
Can you give me an introduction to the ANSI C date/time functions on
A date/time can be in two formats:
The date/time functions and types are declared in the time.h header file.
Time can be stored as a whole number or as an instance of a structure:
as a number using the time_t arithmetic type – to store calendar time as the number of seconds elapsed since the UNIX epoch January 1, 1970 00:00:00
using the structure timeval – to store calendar time as the number of seconds and nanoseconds elapsed since the UNIX epoch January 1, 1970 00:00:00
using the structure tm to store localtime, it contains attributes such as the following:
tm_hour
tm_min
tm_isdst
The tm_isdst attribute above is used to indicate Daylight Saving Time (DST). If the value is positive it is DST, if the value is 0 it is not DST.
#include <stdio.h>
#include <time.h>
int main ( int argc, char *argv[] )
{
time_t now;
now = time ( NULL );
printf ( "It’s %ld seconds since January 1, 1970 00:00:00", (long) now );
return 0;
}
In the program above the function time reads the UNIX system time, subtracts that from January 1, 1970 00:00:00 (the UNIX epoch) and returns its result in seconds.
#include <stdio.h>
#include <time.h>
int main ( int argc, char *argv[] )
{
time_t now;
struct tm *lcltime;
now = time ( NULL );
lcltime = localtime ( &now );
printf ( "The time is %d:%d\n", lcltime->tm_hour, lcltime->tm_min );
return 0;
}
In the program above the function localtime converts the elapsed time in seconds from the UNIX epoch into the broken-down time. localtime reads the UNIX environment TZ (through a call to the tzset function) to return the time relative to the timezone and to set the tm_isdst attribute.
A typical setting of the TZ variable in UNIX (using bash) would be as follows:
export TZ=GMT
or
export TZ=US/Eastern
#include <stdio.h>
#include <time.h>
int main ( int argc, char *argv[] )
{
time_t now;
struct tm *gmt;
char formatted_gmt [50];
now = time ( NULL );
gmt = gmtime ( &now );
strftime ( formatted_gmt, sizeof(formatted_gmt), "%I:%M %p", gmt );
printf ( "The time is %s\n", formatted_gmt );
return 0;
}
In the program above the function strftime provides specialised formatting of dates.