Date/Time Basics

A calendar time is a point in the time continuum, for example November 4, 1990 at 18:02.5 UTC. Sometimes this is called “absolute time”. We don't speak of a “date”, because that is inherent in a calendar time. An interval is a contiguous part of the time continuum between two calendar times, for example the hour between 9:00 and 10:00 on July 4, 1980. An elapsed time is the length of an interval, for example, 35 minutes. People sometimes sloppily use the word “interval” to refer to the elapsed time of some interval. An amount of time is a sum of elapsed times, which need not be of any specific intervals. For example, the amount of time it takes to read a book might be 9 hours, independently of when and in how many sittings it is read.

A period is the elapsed time of an interval between two events, especially when they are part of a sequence of regularly repeating events. CPU time is like calendar time, except that it is based on the subset of the time continuum when a particular process is actively using a CPU. CPU time is, therefore, relative to a process. Processor time is an amount of time that a CPU is in use. In fact, it's a basic system resource, since there's a limit to how much can exist in any given interval (that limit is the elapsed time of the interval times the number of CPUs in the processor). People often call this CPU time, but we reserve the latter term in this manual for the definition above.

Calendar Time

We can represent calendar time two ways:

The relationship among the various time data types and functions is shown in the following diagram.

Dashed lines show functions that are affected by the time zone (TZ) environment variable

Simple Calendar Time


#include <time.h>

time_t time (time_t *result)

The time function returns the current calendar time as a value of type time_t. If the argument result is not a null pointer, the calendar time value is also stored in *result. If the current calendar time is not available, the value (time_t)(-1) is returned.

The data type time_t is used to represent simple time. Sometimes, it also represents an elapsed time. When interpreted as a calendar time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time. (This calendar time is sometimes referred to as the epoch.)

Note that a simple time has no concept of local time zone. Calendar Time T is the same instant in time regardless of where on the globe the computer is.

Broken-down Time

Broken-down time is a binary representation of calendar time separated into year, month, day, and so on. Broken-down time values are not useful for calculations, but they are useful for printing human readable time information.

A broken-down time value is always relative to a choice of time zone.

The data type struct tm (declared in the header file time.h) is used to represent a broken-down time. The structure contains at least the following members, which can appear in any order.

MembersDescription
int tm_secThis is the number of full seconds since the top of the minute (normally in the range 0 through 59, but the actual upper limit is 60, to allow for leap seconds if leap second support is available).
int tm_minThis is the number of full minutes since the top of the hour (in the range 0 through 59).
int tm_hourThis is the number of full hours past midnight (in the range 0 through 23).
int tm_mdayThis is the ordinal day of the month (in the range 1 through 31). Watch out for this one! As the only ordinal number in the structure, it is inconsistent with the rest of the structure.
int tm_monThis is the number of full calendar months since the beginning of the year (in the range 0 through 11). Watch out for this one! People usually use ordinal numbers for month-of-year (where January = 1).
int tm_yearThis is the number of full calendar years since 1900.
int tm_wdayThis is the number of full days since Sunday (in the range 0 through 6).
int tm_ydayThis is the number of full days since the beginning of the year (in the range 0 through 365).
int tm_isdstThis is a flag that indicates whether Daylight Saving Time is (or was, or will be) in effect at the time described. The value is positive if Daylight Saving Time is in effect, zero if it is not, and negative if the information is not available.

Conversion between Calendar Time and Broken-down Time


#include <time.h>

struct tm * localtime (const time_t *time)
struct tm * gmtime (const time_t *time)
time_t mktime (struct tm *brokentime)

— Function: struct tm * localtime (const time_t *time)

The localtime function converts the simple time pointed to by time to broken-down time representation, expressed relative to the user's specified time zone.

Calling localtime has one other effect: it sets the variable tzname with information about the current time zone. See Time Zone Functions.

— Function: struct tm * gmtime (const time_t *time)

This function is similar to localtime, except that the broken-down time is expressed as Coordinated Universal Time (UTC) (formerly called Greenwich Mean Time (GMT)) rather than relative to a local time zone.

— Function: time_t mktime (struct tm *brokentime)

The mktime function is used to convert a broken-down time structure to a simple time representation. It also “normalizes” the contents of the broken-down time structure, by filling in the day of week and day of year based on the other date and time components.

The mktime function ignores the specified contents of the tm_wday and tm_yday members of the broken-down time structure. It uses the values of the other components to determine the calendar time; it's permissible for these components to have unnormalized values outside their normal ranges. The last thing that mktime does is adjust the components of the brokentime structure (including the tm_wday and tm_yday).

If the specified broken-down time cannot be represented as a simple time, mktime returns a value of (time_t)(-1) and does not modify the contents of brokentime.

Calling mktime also sets the variable tzname with information about the current time zone. See Time Zone Functions.

Formatting Calendar Time

The functions described in this section format calendar time values as strings.


#include <time.h>

char * asctime (const struct tm *brokentime)
char * ctime (const time_t *time)
size_t strftime (char *s, size_t size, const char *template, const struct tm *brokentime)

— Function: char * asctime (const struct tm *brokentime)

The asctime function converts the broken-down time value that brokentime points to into a string in a standard format:

          "Tue May 21 13:46:22 1991\n"
     

The abbreviations for the days of week are: `Sun', `Mon', `Tue', `Wed', `Thu', `Fri', and `Sat'.

The abbreviations for the months are: `Jan', `Feb', `Mar', `Apr', `May', `Jun', `Jul', `Aug', `Sep', `Oct', `Nov', and `Dec'.

The return value points to a statically allocated string, which might be overwritten by subsequent calls to asctime or ctime. (But no other library function overwrites the contents of this string.)

— Function: char * ctime (const time_t *time)

The ctime function is similar to asctime, except that you specify the calendar time argument as a time_t simple time value rather than in broken-down local time format. It is equivalent to

          asctime (localtime (time))
     

ctime sets the variable tzname, because localtime does so.

— Function: size_t strftime (char *s, size_t size, const char *template, const struct tm *brokentime)

This function is similar to the sprintf function, but the conversion specifications that can appear in the format template are specialized for printing components of the date and time brokentime according to the locale currently specified for time conversion (see Locales).

Ordinary characters appearing in the template are copied to the output string s. Conversion specifiers are introduced by a `%' character, followed by a format specifier taken from the following list.

CodeDescriptionExample
%aabbreviated weekday nameTue
%Afull weekday nameTuesday
%babbreviated month nameFeb
%Bfull month nameFebruary
%cdate and timeTue Feb 10 18:27:39 2004
%Ccentury (year/100)20
%dday of the month (01 through 31)10
%Ddate (%m/%d/%y)02/10/04
%eday of the month padded with blank (range 1 through 31)10
%F ISO 8601 date u format %Y-%m-%d.2004-02-10
%glast two digits of ISO 8601 week-based year04
%GISO 8601 week-based year2004
%habbreviated month name(same as %b)Feb
%Hhour of the day (24-hour clock) [00 through 23]18
%Ihour of the day (12-hour clock) [01 through 12]06
%jday of the year [001 through 366]041
%mmonth [01 through 12]02
%M minute [ 00 through 59]27
%n(newline) character (\n)
%pAM or PM. Noon is PMPM
%rlocale's time (12-hour clock)06:27:38 PM
%Rsame as %H:%M18:27
%Sseconds [ 00 through 60]38
%thorizontal tab character \t
%Tsame as %H:%M:%S18:27:38
%uISO weekday [1 through 7], Monday being 12
%USunday week number [ 00 through 53]06
%VISO 8601 week number [ 01 through 53]07
%wweek day [ 0 through 6], Sunday being 02
%WMonday week number[00 through 53]06
%xdate02/10/04
%Xtime18:27:38
%ylast two digits of year [ 00 through 99]04
%Yyear2004
%zoffset from UTC in ISO format-0500
%ZThe time zone abbreviationEST
%%translates to a percent sign%

References

Date and Time (from the GNU C Library Reference Manual).

W. Richard Stevens and Stephen A. Rago, Advanced Programming in the UNIX Environment, Second Edition, Addison Wesley, 2005. ISBN 0-201-43307-9.
p 173-177. The diagram above is from p 174. The conversion table is from p 177.

Neil Matthew and Richard Stone, Beginning Linux Programming, Third Edition,
Wrox, 2004. ISBN 0-7645-4497-7. p 146-153.


Maintained by John Loomis, last updated 6 September 2006