#include <time.h>
|
char *
ctime (const time_t *clock); |
double
difftime (time_t time1, time_t time0); |
char *
asctime (const struct tm *tm); |
struct tm *
localtime (const time_t *clock); |
struct tm *
gmtime (const time_t *clock); |
time_t
mktime (struct tm *tm); |
char *
ctime_r (const time_t *clock, char *buf); |
struct tm *
localtime_r (const time_t *clock, struct tm *result); |
struct tm *
gmtime_r (const time_t *clock, struct tm *result); |
char *
asctime_r (const struct tm *tm, char *buf); |
mktime ) in case an error was detected.
The function localtime converts the time value pointed at by clock, and returns a pointer to a "struct tm" (described below) which contains the broken-out time information for the value after adjusting for the current time zone (and any other factors such as Daylight Saving Time). Time zone adjustments are performed as specified by the TZ environment variable (see tzset The function localtime uses tzset to initialize time conversion information if tzset has not already been called by the process.
After filling in the tm structure,
localtime
sets the
tm_isdst’s Nth
element of
tzname
to a pointer to a
ASCII
string that is the time zone abbreviation to be
used with
localtime’s (return, value.);
The function gmtime similarly converts the time value, but without any time zone adjustment, and returns a pointer to a tm structure (described below).
The ctime function adjusts the time value for the current time zone in the same manner as localtime, and returns a pointer to a 26-character string of the form:
Thu Nov 24 18:22:48 1986\n\0
All the fields have constant width.
The ctime_r function provides the same functionality as ctime except the caller must provide the output buffer buf to store the result, which must be at least 26 characters long. The localtime_r and gmtime_r functions provide the same functionality as localtime and gmtime respectively, except the caller must provide the output buffer result.
The asctime function converts the broken down time in the structure tm pointed at by *tm to the form shown in the example above.
The asctime_r function provides the same functionality as asctime except the caller provide the output buffer buf to store the result, which must be at least 26 characters long.
The functions mktime converts the broken-down time in the structure pointed to by tm into a time value with the same encoding as that of the values returned by the time function (that is, seconds from the Epoch, UTC). The mktime function interprets the input structure according to the current timezone setting (see tzset
The original values of the tm_wday and tm_yday components of the structure are ignored, and the original values of the other components are not restricted to their normal ranges, and will be normalized if needed. For example, October 40 is changed into November 9, a tm_hour of -1 means 1 hour before midnight, tm_mday of 0 means the day preceding the current month, and tm_mon of -2 means 2 months before January of tm_year. (A positive or zero value for tm_isdst causes mktime to presume initially that summer time (for example, Daylight Saving Time) is or is not in effect for the specified time, respectively. A negative value for tm_isdst causes the mktime function to attempt to define whether summer time is in effect for the specified time. The tm_isdst and tm_gmtoff members are forced to zero by timegm.)
On successful completion, the values of the tm_wday and tm_yday components of the structure are set appropriately, and the other components are set to represent the specified calendar time, but with their values forced to their normal ranges; the final value of tm_mday is not set until tm_mon and tm_year are determined. The mktime function returns the specified calendar time; if the calendar time cannot be represented, it returns -1;
The difftime function returns the difference between two calendar times, ( time1 - time0), expressed in seconds.
External declarations as well as the tm structure definition are in the
#include <time.h> include file.
The tm structure includes at least the following fields:
int tm_sec; /* seconds (0 - 60) */ int tm_min; /* minutes (0 - 59) */ int tm_hour; /* hours (0 - 23) */ int tm_mday; /* day of month (1 - 31) */ int tm_mon; /* month of year (0 - 11) */ int tm_year; /* year - 1900 */ int tm_wday; /* day of week (Sunday = 0) */ int tm_yday; /* day of year (0 - 365) */ int tm_isdst; /* is summer time in effect? */ char *tm_zone; /* abbreviation of timezone name */ long tm_gmtoff; /* offset from UTC in seconds */ The field tm_isdst is non-zero if summer time is in effect. The field tm_gmtoff is the offset (in seconds) of the time represented from UTC, with positive values indicating east of the Prime Meridian. |
//Example usage of asctime,localtime and gmtime: #include <time.h> #include <stdio.h> int main(){ time_t t; struct tm *timeptr; char* asc_time; t = time (NULL); //Get current time in seconds from Epoc //Fill tm struct w.r.t localtime using localtime timeptr = localtime (&t); //Use this to convert it to a string indicating time w.r.t localtime asc_time = asctime (timeptr); printf ("Time from asctime w.r.t localtime : %s", asc_time); //Fill tm struct w.r.t GMT using gmtime timeptr = gmtime (&t); //Use this to convert it to a string indicating time w.r.t GMT asc_time = asctime (timeptr); printf ("Time from asctime w.r.t gmtime : %s", asc_time); return 0; }
Output
Time from asctime w.r.t localtime : Thu Jun 22 10:42:27 2007 Time from asctime w.r.t gmtime : Thu Jun 22 05:12:27 2007
//Example usage of ctime,mktime: #include <time.h> #include <stdio.h> int main(){ time_t t; struct tm timeptr; char* c_time; //Fill the tm struct with values timeptr.tm_year = 2001; timeptr.tm_mon = 6; timeptr.tm_mday = 4; timeptr.tm_hour = 0; timeptr.tm_min = 0; timeptr.tm_sec = 1; timeptr.tm_isdst = -1; t = mktime (&timeptr); //Call mktime to make time in seconds w.r.t epoc //Convert this to a string indicating time using ctime c_time = ctime (&t); printf ("Time from ctime : %s", c_time); return 0; }
Output
Time from ctime : Thu Jan 1 05:29:59 1970
//Example usage of difftime: #include <time.h> #include <unistd.h> #include <stdio.h> int main(){ time_t t0,t1,t2; //Set initial and final values t0 = 10; t1 = 20; t2 = difftime (t1, t0); //Find the time difference using difftime printf ("Result of difftime = %d", t2); return 0; }
Output
Result of difftime = 10
The asctime, ctime, difftime, gmtime, localtime, and mktime functions conform to -isoC, and conform to -p1003.1-96 provided the selected local timezone does not contain a leap-second table.
The asctime_r, ctime_r, gmtime_r, and localtime_r functions are expected to conform to -p1003.1-96 (again provided the selected local timezone does not contain a leap-second table).
The timegm function is not specified by any standard; its function cannot be completely emulated using the standard functions described above.
This manual page is derived from the time package contributed to Berkeley by Arthur Olson and which appeared in BSD 4.3.
The C Standard provides no mechanism for a program to modify its current local timezone setting, and the POSIX -standard method is not reentrant. (However, thread-safe implementations are provided in the POSIX threaded environment.)
The tm_zone field of a returned tm structure points to a static array of characters, which will also be overwritten by any subsequent calls (as well as by subsequent call to tzset
© 2005-2007 Nokia |