6.10. Time and Date RoutinesThe basic time service provided by the UNIX kernel counts the number of seconds that have passed since the Epoch: 00:00:00 January 1, 1970, Coordinated Universal Time (UTC). In Section 1.10, we said that these seconds are represented in a time_t data type, and we call them calendar times . These calendar times represent both the time and the date. The UNIX System has always differed from other operating systems in (a) keeping time in UTC instead of the local time, (b) automatically handling conversions, such as daylight saving time, and (c) keeping the time and date as a single quantity.The time function returns the current time and date.
int gettimeofday(struct timeval *restrict tp , void ![]() | ||
Returns: 0 always |
time_t tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
Once we have the integer value that counts the number of seconds since the Epoch, we normally call one of the other time functions to convert it to a human-readable time and date. Figure 6.8 shows the relationships between the various time functions.
Figure 6.8. Relationship of the various time functions

int tm_sec; /* seconds after the minute: [0 - 60] */
int tm_min; /* minutes after the hour: [0 - 59] */
int tm_hour; /* hours after midnight: [0 - 23] */
int tm_mday; /* day of the month: [1 - 31] */
int tm_mon; /* months since January: [0 - 11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday: [0 - 6] */
int tm_yday; /* days since January 1: [0 - 365] */
int tm_isdst; /* daylight saving time flag: <0, 0, >0 */
};
The reason that the seconds can be greater than 59 is to allow for a leap second. Note that all the fields except the day of the month are 0-based. The daylight saving time flag is positive if daylight saving time is in effect, 0 if it's not in effect, and negative if the information isn't available.In previous versions of the Single UNIX Specification, double leap seconds were allowed. Thus, the valid range of values for the tm_sec member was 061. The formal definition of UTC doesn't allow for double leap seconds, so the valid range for seconds is now defined to be 060.
#include <time.h> struct tm *gmtime(const time_t *calptr ); struct tm *localtime(const time_t *calptr ); |
Both return: pointer to broken-down time |
#include <time.h> time_t mktime(struct tm *tmptr ); |
Returns: calendar time if OK, 1 on error |
#include <time.h> char *asctime(const struct tm *tmptr ); char *ctime(const time_t *calptr ); |
Both return: pointer to null-terminated string |
#include <time.h> size_t strftime(char *restrict buf , size_t maxsize , const char *restrict format , const struct tm *restrict tmptr ); |
Returns: number of characters stored in array if room, 0 otherwise |
Format | 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:38 2004 |
%C | year/100: [0099] | 20 |
%d | day of the month: [0131] | 10 |
%D | date [MM/DD/YY] | 02/10/04 |
%e | day of month (single digit preceded by space) [131] | 10 |
%F | ISO 8601 date format [YYYYMMDD] | 2004-02-10 |
%g | last two digits of ISO 8601 week-based year [0099] | 04 |
%G | ISO 8601 week-based year | 2004 |
%h | same as %b | Feb |
%H | hour of the day (24-hour format): [0023] | 18 |
%I | hour of the day (12-hour format): [0112] | 06 |
%j | day of the year: [001366] | 041 |
%m | month: [0112] | 02 |
%M | minute: [0059] | 27 |
%n | newline character | |
%p | AM/PM | PM |
%r | locale's time (12-hour format) | 06:27:38 PM |
%R | same as "%H:%M" | 18:27 |
%S | second: [0060] | 38 |
%t | horizontal tab character | |
%T | same as "%H:%M:%S" | 18:27:38 |
%u | ISO 8601 weekday [Monday=1, 17] | 2 |
%U | Sunday week number: [0053] | 06 |
%V | ISO 8601 week number: [0153] | 07 |
%w | weekday: [0=Sunday, 06] | 2 |
%W | Monday week number: [0053] | 06 |
%x | date | 02/10/04 |
%X | time | 18:27:38 |
%y | last two digits of year: [0099] | 04 |
%Y | year | 2004 |
%z | offset from UTC in ISO 8601 format | -0500 |
%Z | time zone name | EST |
%% | translates to a percent sign | % |