14616d0f9Sopenharmony_cinewctime(3) Library Functions Manual newctime(3) 24616d0f9Sopenharmony_ci 34616d0f9Sopenharmony_ciNAME 44616d0f9Sopenharmony_ci asctime, ctime, difftime, gmtime, localtime, mktime - convert date and 54616d0f9Sopenharmony_ci time 64616d0f9Sopenharmony_ci 74616d0f9Sopenharmony_ciSYNOPSIS 84616d0f9Sopenharmony_ci #include <time.h> 94616d0f9Sopenharmony_ci 104616d0f9Sopenharmony_ci extern char *tzname[]; /* (optional) */ 114616d0f9Sopenharmony_ci 124616d0f9Sopenharmony_ci [[deprecated]] char *ctime(time_t const *clock); 134616d0f9Sopenharmony_ci 144616d0f9Sopenharmony_ci char *ctime_r(time_t const *clock, char *buf); 154616d0f9Sopenharmony_ci 164616d0f9Sopenharmony_ci double difftime(time_t time1, time_t time0); 174616d0f9Sopenharmony_ci 184616d0f9Sopenharmony_ci [[deprecated]] char *asctime(struct tm const *tm); 194616d0f9Sopenharmony_ci 204616d0f9Sopenharmony_ci char *asctime_r(struct tm const *restrict tm, 214616d0f9Sopenharmony_ci char *restrict result); 224616d0f9Sopenharmony_ci 234616d0f9Sopenharmony_ci struct tm *localtime(time_t const *clock); 244616d0f9Sopenharmony_ci 254616d0f9Sopenharmony_ci struct tm *localtime_r(time_t const *restrict clock, 264616d0f9Sopenharmony_ci struct tm *restrict result); 274616d0f9Sopenharmony_ci 284616d0f9Sopenharmony_ci struct tm *localtime_rz(timezone_t restrict zone, 294616d0f9Sopenharmony_ci time_t const *restrict clock, 304616d0f9Sopenharmony_ci struct tm *restrict result); 314616d0f9Sopenharmony_ci 324616d0f9Sopenharmony_ci struct tm *gmtime(time_t const *clock); 334616d0f9Sopenharmony_ci 344616d0f9Sopenharmony_ci struct tm *gmtime_r(time_t const *restrict clock, 354616d0f9Sopenharmony_ci struct tm *restrict result); 364616d0f9Sopenharmony_ci 374616d0f9Sopenharmony_ci time_t mktime(struct tm *tm); 384616d0f9Sopenharmony_ci 394616d0f9Sopenharmony_ci time_t mktime_z(timezone_t restrict zone, 404616d0f9Sopenharmony_ci struct tm *restrict tm); 414616d0f9Sopenharmony_ci 424616d0f9Sopenharmony_ci cc ... -ltz 434616d0f9Sopenharmony_ci 444616d0f9Sopenharmony_ciDESCRIPTION 454616d0f9Sopenharmony_ci The ctime function converts a long integer, pointed to by clock, and 464616d0f9Sopenharmony_ci returns a pointer to a string of the form 474616d0f9Sopenharmony_ci Thu Nov 24 18:22:48 1986\n\0 484616d0f9Sopenharmony_ci Years requiring fewer than four characters are padded with leading 494616d0f9Sopenharmony_ci zeroes. For years longer than four characters, the string is of the 504616d0f9Sopenharmony_ci form 514616d0f9Sopenharmony_ci Thu Nov 24 18:22:48 81986\n\0 524616d0f9Sopenharmony_ci with five spaces before the year. These unusual formats are designed 534616d0f9Sopenharmony_ci to make it less likely that older software that expects exactly 26 544616d0f9Sopenharmony_ci bytes of output will mistakenly output misleading values for out-of- 554616d0f9Sopenharmony_ci range years. 564616d0f9Sopenharmony_ci 574616d0f9Sopenharmony_ci The *clock timestamp represents the time in seconds since 1970-01-01 584616d0f9Sopenharmony_ci 00:00:00 Coordinated Universal Time (UTC). The POSIX standard says 594616d0f9Sopenharmony_ci that timestamps must be nonnegative and must ignore leap seconds. Many 604616d0f9Sopenharmony_ci implementations extend POSIX by allowing negative timestamps, and can 614616d0f9Sopenharmony_ci therefore represent timestamps that predate the introduction of UTC and 624616d0f9Sopenharmony_ci are some other flavor of Universal Time (UT). Some implementations 634616d0f9Sopenharmony_ci support leap seconds, in contradiction to POSIX. 644616d0f9Sopenharmony_ci 654616d0f9Sopenharmony_ci The ctime function is deprecated starting in C23. Callers can use 664616d0f9Sopenharmony_ci localtime_r and strftime instead. 674616d0f9Sopenharmony_ci 684616d0f9Sopenharmony_ci The localtime and gmtime functions return pointers to "tm" structures, 694616d0f9Sopenharmony_ci described below. The localtime function corrects for the time zone and 704616d0f9Sopenharmony_ci any time zone adjustments (such as Daylight Saving Time in the United 714616d0f9Sopenharmony_ci States). After filling in the "tm" structure, localtime sets the 724616d0f9Sopenharmony_ci tm_isdst'th element of tzname to a pointer to a string that's the time 734616d0f9Sopenharmony_ci zone abbreviation to be used with localtime's return value. 744616d0f9Sopenharmony_ci 754616d0f9Sopenharmony_ci The gmtime function converts to Coordinated Universal Time. 764616d0f9Sopenharmony_ci 774616d0f9Sopenharmony_ci The asctime function converts a time value contained in a "tm" 784616d0f9Sopenharmony_ci structure to a string, as shown in the above example, and returns a 794616d0f9Sopenharmony_ci pointer to the string. This function is deprecated starting in C23. 804616d0f9Sopenharmony_ci Callers can use strftime instead. 814616d0f9Sopenharmony_ci 824616d0f9Sopenharmony_ci The mktime function converts the broken-down time, expressed as local 834616d0f9Sopenharmony_ci time, in the structure pointed to by tm into a calendar time value with 844616d0f9Sopenharmony_ci the same encoding as that of the values returned by the time function. 854616d0f9Sopenharmony_ci The original values of the tm_wday and tm_yday components of the 864616d0f9Sopenharmony_ci structure are ignored, and the original values of the other components 874616d0f9Sopenharmony_ci are not restricted to their normal ranges. (A positive or zero value 884616d0f9Sopenharmony_ci for tm_isdst causes mktime to presume initially that daylight saving 894616d0f9Sopenharmony_ci time respectively, is or is not in effect for the specified time. A 904616d0f9Sopenharmony_ci negative value for tm_isdst causes the mktime function to attempt to 914616d0f9Sopenharmony_ci divine whether daylight saving time is in effect for the specified 924616d0f9Sopenharmony_ci time; in this case it does not use a consistent rule and may give a 934616d0f9Sopenharmony_ci different answer when later presented with the same argument.) On 944616d0f9Sopenharmony_ci successful completion, the values of the tm_wday and tm_yday components 954616d0f9Sopenharmony_ci of the structure are set appropriately, and the other components are 964616d0f9Sopenharmony_ci set to represent the specified calendar time, but with their values 974616d0f9Sopenharmony_ci forced to their normal ranges; the final value of tm_mday is not set 984616d0f9Sopenharmony_ci until tm_mon and tm_year are determined. The mktime function returns 994616d0f9Sopenharmony_ci the specified calendar time; If the calendar time cannot be 1004616d0f9Sopenharmony_ci represented, it returns -1. 1014616d0f9Sopenharmony_ci 1024616d0f9Sopenharmony_ci The difftime function returns the difference between two calendar 1034616d0f9Sopenharmony_ci times, (time1 - time0), expressed in seconds. 1044616d0f9Sopenharmony_ci 1054616d0f9Sopenharmony_ci The ctime_r, localtime_r, gmtime_r, and asctime_r functions are like 1064616d0f9Sopenharmony_ci their unsuffixed counterparts, except that they accept an additional 1074616d0f9Sopenharmony_ci argument specifying where to store the result if successful. 1084616d0f9Sopenharmony_ci 1094616d0f9Sopenharmony_ci The localtime_rz and mktime_z functions are like their unsuffixed 1104616d0f9Sopenharmony_ci counterparts, except that they accept an extra initial zone argument 1114616d0f9Sopenharmony_ci specifying the timezone to be used for conversion. If zone is null, UT 1124616d0f9Sopenharmony_ci is used; otherwise, zone should be have been allocated by tzalloc and 1134616d0f9Sopenharmony_ci should not be freed until after all uses (e.g., by calls to strftime) 1144616d0f9Sopenharmony_ci of the filled-in tm_zone fields. 1154616d0f9Sopenharmony_ci 1164616d0f9Sopenharmony_ci Declarations of all the functions and externals, and the "tm" 1174616d0f9Sopenharmony_ci structure, are in the <time.h> header file. The structure (of type) 1184616d0f9Sopenharmony_ci struct tm includes the following fields: 1194616d0f9Sopenharmony_ci 1204616d0f9Sopenharmony_ci int tm_sec; /* seconds (0-60) */ 1214616d0f9Sopenharmony_ci int tm_min; /* minutes (0-59) */ 1224616d0f9Sopenharmony_ci int tm_hour; /* hours (0-23) */ 1234616d0f9Sopenharmony_ci int tm_mday; /* day of month (1-31) */ 1244616d0f9Sopenharmony_ci int tm_mon; /* month of year (0-11) */ 1254616d0f9Sopenharmony_ci int tm_year; /* year - 1900 */ 1264616d0f9Sopenharmony_ci int tm_wday; /* day of week (Sunday = 0) */ 1274616d0f9Sopenharmony_ci int tm_yday; /* day of year (0-365) */ 1284616d0f9Sopenharmony_ci int tm_isdst; /* is daylight saving time in effect? */ 1294616d0f9Sopenharmony_ci char *tm_zone; /* time zone abbreviation (optional) */ 1304616d0f9Sopenharmony_ci long tm_gmtoff; /* offset from UT in seconds (optional) */ 1314616d0f9Sopenharmony_ci 1324616d0f9Sopenharmony_ci The tm_isdst field is non-zero if daylight saving time is in effect. 1334616d0f9Sopenharmony_ci 1344616d0f9Sopenharmony_ci The tm_gmtoff field is the offset (in seconds) of the time represented 1354616d0f9Sopenharmony_ci from UT, with positive values indicating east of the Prime Meridian. 1364616d0f9Sopenharmony_ci The field's name is derived from Greenwich Mean Time, a precursor of 1374616d0f9Sopenharmony_ci UT. 1384616d0f9Sopenharmony_ci 1394616d0f9Sopenharmony_ci In struct tm the tm_zone and tm_gmtoff fields exist, and are filled in, 1404616d0f9Sopenharmony_ci only if arrangements to do so were made when the library containing 1414616d0f9Sopenharmony_ci these functions was created. Similarly, the tzname variable is 1424616d0f9Sopenharmony_ci optional; also, there is no guarantee that tzname will continue to 1434616d0f9Sopenharmony_ci exist in this form in future releases of this code. 1444616d0f9Sopenharmony_ci 1454616d0f9Sopenharmony_ciFILES 1464616d0f9Sopenharmony_ci /etc/localtime local timezone file 1474616d0f9Sopenharmony_ci /usr/share/zoneinfo timezone directory 1484616d0f9Sopenharmony_ci /usr/share/zoneinfo/posixrules default DST rules (obsolete) 1494616d0f9Sopenharmony_ci /usr/share/zoneinfo/GMT for UTC leap seconds 1504616d0f9Sopenharmony_ci 1514616d0f9Sopenharmony_ci If /usr/share/zoneinfo/GMT is absent, UTC leap seconds are loaded from 1524616d0f9Sopenharmony_ci /usr/share/zoneinfo/GMT0 if present. 1534616d0f9Sopenharmony_ci 1544616d0f9Sopenharmony_ciSEE ALSO 1554616d0f9Sopenharmony_ci getenv(3), newstrftime(3), newtzset(3), time(2), tzfile(5) 1564616d0f9Sopenharmony_ci 1574616d0f9Sopenharmony_ciNOTES 1584616d0f9Sopenharmony_ci The return values of asctime, ctime, gmtime, and localtime point to 1594616d0f9Sopenharmony_ci static data overwritten by each call. The tzname variable (once set) 1604616d0f9Sopenharmony_ci and the tm_zone field of a returned struct tm both point to an array of 1614616d0f9Sopenharmony_ci characters that can be freed or overwritten by later calls to the 1624616d0f9Sopenharmony_ci functions localtime, tzfree, and tzset, if these functions affect the 1634616d0f9Sopenharmony_ci timezone information that specifies the abbreviation in question. The 1644616d0f9Sopenharmony_ci remaining functions and data are thread-safe. 1654616d0f9Sopenharmony_ci 1664616d0f9Sopenharmony_ci The asctime, asctime_r, ctime, and ctime_r functions behave strangely 1674616d0f9Sopenharmony_ci for years before 1000 or after 9999. The 1989 and 1999 editions of the 1684616d0f9Sopenharmony_ci C Standard say that years from -99 through 999 are converted without 1694616d0f9Sopenharmony_ci extra spaces, but this conflicts with longstanding tradition and with 1704616d0f9Sopenharmony_ci this implementation. The 2011 edition says that the behavior is 1714616d0f9Sopenharmony_ci undefined if the year is before 1000 or after 9999. Traditional 1724616d0f9Sopenharmony_ci implementations of these two functions are restricted to years in the 1734616d0f9Sopenharmony_ci range 1900 through 2099. To avoid this portability mess, new programs 1744616d0f9Sopenharmony_ci should use strftime instead. 1754616d0f9Sopenharmony_ci 1764616d0f9Sopenharmony_ciTime Zone Database newctime(3) 177