ctime(3) Library Functions Manual ctime(3) asctime, ctime, gmtime, localtime, mktime, asctime_r, ctime_r, gmtime_r, localtime_r - ASCII (libc -lc) #include char *asctime(const struct tm *tm); char *asctime_r(const struct tm *restrict tm, char buf[restrict 26]); char *ctime(const time_t *timep); char *ctime_r(const time_t *restrict timep, char buf[restrict 26]); struct tm *gmtime(const time_t *timep); struct tm *gmtime_r(const time_t *restrict timep, struct tm *restrict result); struct tm *localtime(const time_t *timep); struct tm *localtime_r(const time_t *restrict timep, struct tm *restrict result); time_t mktime(struct tm *tm); glibc ( feature_test_macros(7)): asctime_r(), ctime_r(), gmtime_r(), localtime_r(): _POSIX_C_SOURCE || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE ctime() gmtime() localtime() time_t . 1970-01-01 00:00:00 +0000 (UTC). asctime() mktime() . tm tm(3type). ctime(t) asctime(localtime(t)). t "Wed Jun 30 21:49:08 1993\n" "Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat". "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec". . tzname timezone daylight tzset(3). ctime_r() 26 . tzname timezone daylight. gmtime() timep (UTC). NULL . . gmtime_r() . localtime() timep . tzname timezone daylight tzset(3). . localtime_r() . tzname timezone daylight. asctime() tm ctime(). . asctime_r() 26 . mktime() . tm_wday tm_yday. tm_isdst mktime() (DST) tm: DST DST mktime() ( ) DST . timegm(3) UTC . mktime() tm : tm_wday tm_yday ( 40 9 ) tm_isdst ( ) 0 DST . tzname timezone daylight tzset(3). ( ) mktime() (time_t) -1 . gmtime() localtime() struct tm. gmtime_r() localtime_r() result. asctime() ctime() . asctime_r() ctime_r() buf. mktime() ( ) time_t. mktime() (time_t) -1 tm->tm_wday . NULL . errno . EOVERFLOW . attributes(7). +----------------------+------------------------------------+----------+ | | | | +----------------------+------------------------------------+----------+ |asctime() | | MT-Unsafe | | | | race:asctime | | | | locale | +----------------------+------------------------------------+----------+ |asctime_r() | | | | | | (locale) | | | | | | | | | | | | | | | | (MT-Safe) | +----------------------+------------------------------------+----------+ |ctime() | | MT-Unsafe | | | | race:tmbuf | | | | race:asctime | | | | env | | | | locale | +----------------------+------------------------------------+----------+ |ctime_r(), | | | |gmtime_r(), | | | |localtime_r(), | | | |mktime() | | | | | | | | | | (MT-Safe) | +----------------------+------------------------------------+----------+ |gmtime(), localtime() | | MT-Unsafe | | | | race:tmbuf | | | | env | | | | locale | +----------------------+------------------------------------+----------+ POSIX ctime_r() restrict glibc. glibc 0 tm_mday . POSIX.1 localtime() tzset(3) localtime_r() . tzset(3) localtime_r(). asctime() ctime() gmtime() localtime() mktime() C23 POSIX.1-2024. gmtime_r() localtime_r() POSIX.1-2024. asctime_r() ctime_r() . gmtime() localtime() mktime() C89, POSIX.1-1988. asctime() ctime() C89 POSIX.1-1988. C23 POSIX.1-2008 ( strftime(3)). gmtime_r() localtime_r() POSIX.1-1996. asctime_r() ctime_r() POSIX.1-1996. POSIX.1-2008. POSIX.1-2024 ( strftime(3)). asctime() ctime() gmtime() localtime() . asctime_r() ctime_r() gmtime_r() localtime_r() SUSv2. POSIX.1: " asctime() ctime() gmtime() localtime() : char. ." glibc. mktime() (time_t) -1 ( ). mktime() tm->tm_wday. . tm_isdst mktime() . mktime() . mktime() tm_isdst tm_isdst . tm_isdst . . mktime . . EINVAL ENOTUNIQ . : $ TZ=UTC ./a.out 1969 12 31 23 59 59 0; -1 $ $ export TZ=Europe/Madrid; $ $ ./a.out 2147483647 2147483647 00 00 00 00 -1; a.out: mktime: Value too large for defined data type $ $ ./a.out 2024 08 23 00 17 53 -1; 1724365073 $ ./a.out 2024 08 23 00 17 53 0; a.out: my_mktime: Invalid argument 1724368673 $ ./a.out 2024 08 23 00 17 53 1; 1724365073 $ $ ./a.out 2024 02 23 00 17 53 -1; 1708643873 $ ./a.out 2024 02 23 00 17 53 0; 1708643873 $ ./a.out 2024 02 23 00 17 53 1; a.out: my_mktime: Invalid argument 1708640273 $ $ ./a.out 2023 03 26 02 17 53 -1; a.out: my_mktime: Invalid argument 1679793473 $ $ ./a.out 2023 10 29 02 17 53 -1; a.out: my_mktime: Name not unique on network 1698542273 $ ./a.out 2023 10 29 02 17 53 0; 1698542273 $ ./a.out 2023 10 29 02 17 53 1; 1698538673 $ $ ./a.out 2023 02 29 12 00 00 -1; a.out: my_mktime: Invalid argument 1677668400 : mktime.c #include #include #include #include #include #include #include #define is_signed(T) ((T) -1 < 1) static time_t my_mktime(struct tm *tp); int main(int argc, char *argv[]) { char **p; time_t t; struct tm tm; if (argc != 8) { fprintf(stderr, "Usage: %s yyyy mm dd HH MM SS isdst\n", argv[0]); exit(EXIT_FAILURE); } p = &argv[1]; tm.tm_year = atoi(*p++) - 1900; tm.tm_mon = atoi(*p++) - 1; tm.tm_mday = atoi(*p++); tm.tm_hour = atoi(*p++); tm.tm_min = atoi(*p++); tm.tm_sec = atoi(*p++); tm.tm_isdst = atoi(*p++); errno = 0; tm.tm_wday = -1; t = my_mktime(&tm); if (tm.tm_wday == -1) err(EXIT_FAILURE, "mktime"); if (errno == EINVAL || errno == ENOTUNIQ) warn("my_mktime"); if (is_signed(time_t)) printf("%jd\n", (intmax_t) t); else printf("%ju\n", (uintmax_t) t); exit(EXIT_SUCCESS); } static time_t my_mktime(struct tm *tp) { int e, isdst; time_t t; struct tm tm; unsigned char wday[sizeof(tp->tm_wday)]; e = errno; tm = *tp; isdst = tp->tm_isdst; memcpy(wday, &tp->tm_wday, sizeof(wday)); tp->tm_wday = -1; t = mktime(tp); if (tp->tm_wday == -1) { memcpy(&tp->tm_wday, wday, sizeof(wday)); return -1; } if (isdst == -1) tm.tm_isdst = tp->tm_isdst; if ( tm.tm_sec != tp->tm_sec || tm.tm_min != tp->tm_min || tm.tm_hour != tp->tm_hour || tm.tm_mday != tp->tm_mday || tm.tm_mon != tp->tm_mon || tm.tm_year != tp->tm_year || tm.tm_isdst != tp->tm_isdst) { errno = EINVAL; return t; } if (isdst != -1) goto out; tm = *tp; tm.tm_isdst = !tm.tm_isdst; tm.tm_wday = -1; mktime(&tm); if (tm.tm_wday == -1) goto out; if (tm.tm_isdst != tp->tm_isdst) { errno = ENOTUNIQ; return t; } out: errno = e; return t; } date(1), gettimeofday(2), time(2), utime(2), clock(3), difftime(3), strftime(3), strptime(3), timegm(3), tzset(3), time(7) 3 . . : . 6.18 8 2026 ctime(3)