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.
We can represent calendar time two ways:
time_t
data type) is a compact
representation, typically giving the number of seconds of elapsed time
since some implementation-specific base time.
struct tm
data
type) represents a calendar time as a set of components specifying the
year, month, and so on in the Gregorian calendar, for a specific time
zone. This calendar time representation is usually used only to
communicate with people.
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
#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 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.
#include <time.h> struct tm * localtime (const time_t *time) struct tm * gmtime (const time_t *time) time_t mktime (struct tm *brokentime) |
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 variabletzname
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 thetm_wday
andtm_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 thatmktime
does is adjust the components of the brokentime structure (including thetm_wday
andtm_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 variabletzname
with information about the current time zone. See Time Zone Functions.
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) |
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
orctime
. (But no other library function overwrites the contents of this string.)
— Function: char * ctime (const time_t *time)
The
ctime
function is similar toasctime
, except that you specify the calendar time argument as atime_t
simple time value rather than in broken-down local time format. It is equivalent toasctime (localtime (time))
ctime
sets the variabletzname
, becauselocaltime
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.
Code | Description | Example | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
%a | abbreviated weekday name | Tue | |||||||||||||||||||||||||||
%A | full weekday name | Tuesday | |||||||||||||||||||||||||||
%b | abbreviated month name | Feb | |||||||||||||||||||||||||||
%B | full month name | February | |||||||||||||||||||||||||||
%c | date and time | Tue Feb 10 18:27:39 2004 | |||||||||||||||||||||||||||
%C | century (year/100) | 20 | |||||||||||||||||||||||||||
%d | day of the month (01 through
31 ) | 10 | |||||||||||||||||||||||||||
%D | date (%m/%d/%y ) | 02/10/04 | |||||||||||||||||||||||||||
%e | day of the month padded with blank (range
1 through 31 ) | 10 | |||||||||||||||||||||||||||
%F | ISO 8601 date u format
%Y-%m-%d . | 2004-02-10 | |||||||||||||||||||||||||||
%g | last two digits of ISO 8601 week-based year | 04 | |||||||||||||||||||||||||||
%G | ISO 8601 week-based year | 2004 | |||||||||||||||||||||||||||
%h | abbreviated month name(same as
%b ) | Feb | |||||||||||||||||||||||||||
%H | hour of the day (24-hour clock) [00 through
23 ] | 18 | |||||||||||||||||||||||||||
%I | hour of the day (12-hour clock) [01 through
12 ] | 06 | |||||||||||||||||||||||||||
%j | day of the year [001 through
366 ] | 041 | |||||||||||||||||||||||||||
%m | month [01 through
12 ] | 02 | |||||||||||||||||||||||||||
%M | minute [ 00 through
59 ] | 27 | |||||||||||||||||||||||||||
%n | (newline) character
(\n ) | ||||||||||||||||||||||||||||
%p | AM or PM .
Noon is PM | PM | |||||||||||||||||||||||||||
%r | locale's time (12-hour clock) | 06:27:38 PM | |||||||||||||||||||||||||||
%R | same as %H:%M | 18:27 | |||||||||||||||||||||||||||
%S | seconds [ 00 through
60 ] | 38 | |||||||||||||||||||||||||||
%t | horizontal tab character
\t | ||||||||||||||||||||||||||||
%T | same as %H:%M:%S | 18:27:38 | |||||||||||||||||||||||||||
%u | ISO weekday [1 through
7 ], Monday being 1 | 2 | |||||||||||||||||||||||||||
%U | Sunday week number [ 00
through 53 ] | 06 | |||||||||||||||||||||||||||
%V | ISO 8601 week number [ 01
through 53] |
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