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?
Terminology
A date/time can be in two formats:
calendar time (a.k.a. simpletime) – time as an absolute value typically since some base time, often referred to as the Coordinated Universal Time
localtime (a.k.a. broken-down time) – a calendar time made up of components of year, month, day etc. which takes into account the local time zone including Daylight Saving Time if applicable.
Data types
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.
Program to print the current Coordinated Universal Time
#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.
Program to print the current local time
#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
Program to print the current formatted Greenwich Mean Time
#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.
Other issues to consider
Leap seconds
What should we do to prepare for 2038?
Related
Using C, I want to convert a UNIX Timestamp number to several usual date data.
How do I convert a UNIX timestamp like 12997424 to different numbers representing seconds, minutes, hours and days while using C?
Use gmtime or localtime from standard library. Prototypes are defined in time.h.
ADDED & EDITED:
So for example, the following code prints current timestamp, hour and minute:
#include <stdio.h>
#include <time.h>
void main() {
time_t t;
struct tm ttm;
t = time(NULL);
printf("Current timestamp: %d\n", t);
ttm = * localtime(&t);
printf("Current time: %02d:%02d\n", ttm.tm_hour, ttm.tm_min);
}
Here's an example of how to use localtime to convert time_t to tm as local time (credit goes to www.cplusplusreference.com):
#include <stdio.h>
#include <time.h>
int main() {
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
printf ("Current local time and date: %s", asctime(timeinfo));
return 0;
}
I am trying to create a Date header for an email.
The format is as follows:
DayOfWeek, Day ShortMonth Year Hours:Minutes:Seconds Offset (TimeZone)
So an example would be:
Fri, 19 Mar 2010 11:44:09 -0700 (PDT)
Its the TimeZone part (PDT in the example) that I'm trying to get. I realize that this will be different across systems, but I'd like a solution for all of them.
Thanks!
You are looking for %Z in strftime().
For example:
#include <stdio.h>
#include <time.h>
int main()
{
char buf[16];
time_t t = time(0);
strftime(buf, sizeof(buf), "%Z", localtime(&t));
printf("%s\n", buf);
return 0;
}
And it's C89, AFAIR (which means it's very portable).
Snippet from the C99 standard:
%Z is replaced by the locale’s time zone name or abbreviation, or by no characters if no time zone is determinable. [tm_isdst]
Sadly, as we noticed not every system gives reasonable values for it...
The info pages for the GNU date command contains this example:
For example, with the GNU date
command you can answer the question
"What time is it in New York when a
Paris clock shows 6:30am on October
31, 2004?" by using a date beginning
with `TZ="Europe/Paris"' as shown in
the following shell transcript:
$ export TZ="America/New_York"
$ date --date='TZ="Europe/Paris" 2004-10-31 06:30'
Sun Oct 31 01:30:00 EDT 2004
In this example, the '--date'
operand begins with its own 'TZ'
setting, so the rest of that operand
is processed according to
'Europe/Paris' rules, treating the
string 2004-10-31 06:30 as if it
were in Paris. However, since the
output of the date command is
processed according to the overall
time zone rules, it uses New York
time. (Paris was normally six hours
ahead of New York in 2004, but this
example refers to a brief Halloween
period when the gap was five hours.)
I am trying to accomplish essentially the same thing programatically in C without calling the date program millions of times. Basically I am looking for a way to take an arbitrary date and time in one timezone and convert it to the equivalent date and time in another timezone either directly or via conversion to and from UTC. I don't care about the formats of the input and output time as long as I can manipulate them using standard functions (strftime/strptime/mktime/etc).
The date program appears to accomplish this using complex routines internal to the coreutils package, I am looking for a way to do this in C using either standard POSIX/Linux routines or an external library. I looked at zoneinfo quite a bit which seemed promising but I cannot find any libraries to do anything useful with it.
I came up with a solution that seems to work on Linux with glibc, I don't know how portable this behavior is, any comments about portability or a better way to go about this would be welcome.
convert_time.c (No error checking for clarity):
#define _XOPEN_SOURCE
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
struct tm mytm = {0};
time_t mytime;
char buf[100];
mytm.tm_isdst = -1;
putenv(argv[1]);
tzset();
strptime(argv[2], "%Y-%m-%d %H:%M", &mytm);
mytime = mktime(&mytm);
putenv(argv[3]);
tzset();
localtime_r(&mytime, &mytm);
strftime(buf, 100, "%a, %d %b %Y %H:%M:%S %z(%Z)", &mytm);
puts(buf);
return 0;
}
The first argument is the source timezone (actually "TZ=Timezone" to pass to putenv), the second argument is the time in the specified format, the last argument is the destination timezone. I am using zoneinfo timezone names which glibc supports using the zoneinfo database.
The results of several DST test corner cases correspond to the results from the equivalent date command as well as this site which uses the zoneinfo database:
$ ./convert_time "TZ=America/New_York" "2005-05-31 06:30" "TZ=America/Indiana/Indianapolis"
Tue, 31 May 2005 05:30:00 -0500(EST)
$ ./convert_time "TZ=America/New_York" "2006-05-31 06:30" "TZ=America/Indiana/Indianapolis"
Wed, 31 May 2006 06:30:00 -0400(EDT)
$ ./convert_time "TZ=Europe/Paris" "2004-10-30 06:30" "TZ=America/New_York"
Sat, 30 Oct 2004 00:30:00 -0400(EDT)
$ ./convert_time "TZ=Europe/Paris" "2004-10-31 06:30" "TZ=America/New_York"
Sun, 31 Oct 2004 01:30:00 -0400(EDT)
$ ./convert_time "TZ=Europe/Paris" "2004-11-01 06:30" "TZ=America/New_York"
Mon, 01 Nov 2004 00:30:00 -0500(EST)
For time:
Get the GMT time with gmtime
Add/subtract the hours from time_t.tm_hour
Use mktime to renormalize
The date calculation will be similar but a little more complicated.
I was actually looking for the same answer you did, and this is the solution I came up with.
The following sample program takes the local system time and converts it to any timezone you want.
// compile: gcc snippet.c
// usage: ./a.out Europe/Berlin Pacific/Nauru Asia/Hong_Kong Asia/Kabul
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
int main(int argc, char *argv[])
{
char buf[80];
time_t tt = time(NULL);
struct tm tm;
int i;
if (!localtime_r(&tt, &tm)) {
printf("Could not convert time_t to tm struct. %s\n", strerror(errno));
return 1;
}
printf("time_t: %ld\n", tt);
printf("timezone: %ld, %s/%s, daylight:%d\n", timezone, tzname[0], tzname[1], daylight);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %z %Z", &tm);
printf("time: %s\n", buf);
for (i = 1; i < argc; ++i) {
printf("\ninput: %s\n", argv[i]);
snprintf(buf, sizeof(buf), "TZ=%s", argv[i]);
putenv(buf);
tzset();
printf("\ttimezone: %ld, %s/%s, daylight:%d\n", timezone, tzname[0], tzname[1], daylight);
if (!localtime_r(&tt, &tm)) {
printf("Could not convert time_t to tm struct. %s\n", strerror(errno));
return 1;
}
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %z %Z", &tm);
printf("\ttime: %s\n", buf);
}
return 0;
}
I am wondering is there any function that would return the current time in seconds, just 2 digits of seconds? I'm using gcc 4.4.2.
The following complete program shows you how to access the seconds value:
#include <stdio.h>
#include <time.h>
int main (int argc, char *argv[]) {
time_t now;
struct tm *tm;
now = time(0);
if ((tm = localtime (&now)) == NULL) {
printf ("Error extracting time stuff\n");
return 1;
}
printf ("%04d-%02d-%02d %02d:%02d:%02d\n",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return 0;
}
It outputs:
2010-02-11 15:58:29
How it works is as follows.
it calls time() to get the best approximation to the current time (usually number of seconds since the epoch but that's not actually mandated by the standard).
it then calls localtime() to convert that to a structure which contains the individual date and time fields, among other things.
at that point, you can just de-reference the structure to get the fields you're interested in (tm_sec in your case but I've shown a few of them).
Keep in mind you can also use gmtime() instead of localtime() if you want Greenwich time, or UTC for those too young to remember :-).
A more portable way to do this is to get the current time as a time_t struct:
time_t mytime = time((time_t*)0);
Retrieve a struct tm for this time_t:
struct tm *mytm = localtime(&mytime);
Examine the tm_sec member of mytm. Depending on your C library, there's no guarantee that the return value of time() is based on a number of seconds since the start of a minute.
You can get the current time with gettimeofday (C11), time (Linux), or localtime_r (POSIX); depending on what calendar & epoch you're interested. You can convert it to seconds elapsed after calendar epoch, or seconds of current minute, whichever you are after:
#include <stdio.h>
#include <string.h>
#include <time.h>
int main() {
time_t current_secs = time(NULL);
localtime_r(¤t_secs, ¤t_time);
char secstr[128] = {};
struct tm current_time;
strftime(secstr, sizeof secstr, "%S", ¤t_time);
fprintf(stdout, "The second: %s\n", secstr);
return 0;
}
You want to use gettimeofday:
man 2 gettimeofday
#include <stdio.h>
#include <sys/time.h>
int main (int argc, char **argv)
{
int iRet;
struct timeval tv;
iRet = gettimeofday (&tv, NULL); // timezone structure is obsolete
if (iRet == 0)
{
printf ("Seconds/USeconds since epoch: %d/%d\n",
(int)tv.tv_sec, (int)tv.tv_usec);
return 0;
}
else
{
perror ("gettimeofday");
}
return iRet;
}
This is better to use then time(0), because you get the useconds as well, atomically, which is the more common use case.
I represent dates using seconds (and microseconds) since 1970 as well as a time zone and dst flag. I want to print a representation of the date using strftime, but it uses the global value for timezone (extern long int timezone) that is picked up from the environment. How can I get strftime to print the zone of my choice?
The following program sets the UNIX environment variable TZ with your required timezone and then prints a formatted time using strftime.
In the example below the timezone is set to U.S. Pacific Time Zone .
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char *argv[])
{
struct tm *mt;
time_t mtt;
char ftime[10];
setenv("TZ", "PST8PDT", 1);
tzset();
mtt = time(NULL);
mt = localtime(&mtt);
strftime(ftime,sizeof(ftime),"%Z %H%M",mt);
printf("%s\n", ftime);
}
Change timezone via setting timezone global variable and use localtime to get the time you print via strftime.