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 UNIX and indicate some of the deeper issues I should also consider when using dates and times?
The time() function is defined in time. h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.
The date command under UNIX displays date and time. You can use the same command set date and time. You must be the super-user (root) to change the date and time on Unix like operating systems. The date command shows the date and time read from the kernel clock.
Unix time is a date-time format used to express the number of milliseconds that have elapsed since January 1, 1970 00:00:00 (UTC). Unix time does not handle the extra seconds that occur on the extra day of leap years.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With