NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ENVIRONMENT |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |EXAMPLES |SEE ALSO |COLOPHON | |
getdate(3) Library Functions Manualgetdate(3)getdate, getdate_r - convert a date-plus-time string to broken- down time
Standard C library (libc,-lc)
#include <time.h>struct tm *getdate(const char *string);extern int getdate_err;int getdate_r(const char *restrictstring, struct tm *restrictres); Feature Test Macro Requirements for glibc (seefeature_test_macros(7)):getdate(): _XOPEN_SOURCE >= 500getdate_r(): _GNU_SOURCE
The functiongetdate() converts a string representation of a date and time, contained in the buffer pointed to bystring, into a broken-down time. The broken-down time is stored in atm structure, and a pointer to this structure is returned as the function result. Thistm structure is allocated in static storage, and consequently it will be overwritten by further calls togetdate(). In contrast tostrptime(3), (which has aformat argument),getdate() uses the formats found in the file whose full pathname is given in the environment variableDATEMSK. The first line in the file that matches the given input string is used for the conversion. The matching is done case insensitively. Superfluous whitespace, either in the pattern or in the string to be converted, is ignored. The conversion specifications that a pattern can contain are those given forstrptime(3). One more conversion specification is specified in POSIX.1-2001:%ZTimezone name. This is not implemented in glibc. When%Zis given, the structure containing the broken-down time is initialized with values corresponding to the current time in the given timezone. Otherwise, the structure is initialized to the broken-down time corresponding to the current local time (as by a call tolocaltime(3)). When only the day of the week is given, the day is taken to be the first such day on or after today. When only the month is given (and no year), the month is taken to be the first such month equal to or after the current month. If no day is given, it is the first day of the month. When no hour, minute, and second are given, the current hour, minute, and second are taken. If no date is given, but we know the hour, then that hour is taken to be the first such hour equal to or after the current hour.getdate_r() is a GNU extension that provides a reentrant version ofgetdate(). Rather than using a global variable to report errors and a static buffer to return the broken down time, it returns errors via the function result value, and returns the resulting broken-down time in the caller-allocated buffer pointed to by the argumentres.
When successful,getdate() returns a pointer to astruct tm. Otherwise, it returns NULL and sets the global variablegetdate_err to one of the error numbers shown below. Changes toerrno are unspecified. On successgetdate_r() returns 0; on error it returns one of the error numbers shown below.
The following errors are returned viagetdate_err (forgetdate()) or as the function result (forgetdate_r()):1TheDATEMSKenvironment variable is not defined, or its value is an empty string.2The template file specified byDATEMSKcannot be opened for reading.3Failed to get file status information.4The template file is not a regular file.5An error was encountered while reading the template file.6Memory allocation failed (not enough memory available).7There is no line in the file that matches the input.8Invalid input specification.
DATEMSK File containing format patterns.TZLC_TIME Variables used bystrptime(3).
For an explanation of the terms used in this section, seeattributes(7). ┌─────────────┬───────────────┬──────────────────────────────────┐ │Interface│Attribute│Value│ ├─────────────┼───────────────┼──────────────────────────────────┤ │getdate() │ Thread safety │ MT-Unsafe race:getdate env │ │ │ │ locale │ ├─────────────┼───────────────┼──────────────────────────────────┤ │getdate_r() │ Thread safety │ MT-Safe env locale │ └─────────────┴───────────────┴──────────────────────────────────┘
The POSIX.1 specification forstrptime(3) contains conversion specifications using the%Eor%Omodifier, while such specifications are not given forgetdate(). In glibc,getdate() is implemented usingstrptime(3), so that precisely the same conversions are supported by both.
POSIX.1-2008.
POSIX.1-2001.
The program below callsgetdate() for each of its command-line arguments, and for each call displays the values in the fields of the returnedtm structure. The following shell session demonstrates the operation of the program: $TFILE=$PWD/tfile $echo '%A' > $TFILE# Full name of the day of the week $echo '%T' >> $TFILE# Time (HH:MM:SS) $echo '%F' >> $TFILE# ISO date (YYYY-MM-DD) $date $export DATEMSK=$TFILE $./a.out Tuesday '2009-12-28' '12:22:33' Sun Sep 7 06:03:36 CEST 2008 Call 1 ("Tuesday") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 9 tm_mon = 8 tm_year = 108 tm_wday = 2 tm_yday = 252 tm_isdst = 1 Call 2 ("2009-12-28") succeeded: tm_sec = 36 tm_min = 3 tm_hour = 6 tm_mday = 28 tm_mon = 11 tm_year = 109 tm_wday = 1 tm_yday = 361 tm_isdst = 0 Call 3 ("12:22:33") succeeded: tm_sec = 33 tm_min = 22 tm_hour = 12 tm_mday = 7 tm_mon = 8 tm_year = 108 tm_wday = 0 tm_yday = 250 tm_isdst = 1Program source #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { struct tm *tmp; for (size_t j = 1; j < argc; j++) { tmp = getdate(argv[j]); if (tmp == NULL) { printf("Call %zu failed; getdate_err = %d\n", j, getdate_err); continue; } printf("Call %zu (\"%s\") succeeded:\n", j, argv[j]); printf(" tm_sec = %d\n", tmp->tm_sec); printf(" tm_min = %d\n", tmp->tm_min); printf(" tm_hour = %d\n", tmp->tm_hour); printf(" tm_mday = %d\n", tmp->tm_mday); printf(" tm_mon = %d\n", tmp->tm_mon); printf(" tm_year = %d\n", tmp->tm_year); printf(" tm_wday = %d\n", tmp->tm_wday); printf(" tm_yday = %d\n", tmp->tm_yday); printf(" tm_isdst = %d\n", tmp->tm_isdst); } exit(EXIT_SUCCESS); }time(2),localtime(3),setlocale(3),strftime(3),strptime(3)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17getdate(3)Pages that refer to this page:strptime(3)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |