getdate (3) - Linux Man Pages
getdate: convert a date-plus-time string to broken-down time
getdate, getdate_r - convert a date-plus-time string to broken-down time
struct tm *getdate(const char *string);
extern int getdate_err;
int getdate_r(const char *string, struct tm *res);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
- _XOPEN_SOURCE >= 500
DESCRIPTIONThe function getdate() converts a string representation of a date and time, contained in the buffer pointed to by string, into a broken-down time. The broken-down time is stored in a tm structure, and a pointer to this structure is returned as the function result. This tm structure is allocated in static storage, and consequently it will be overwritten by further calls to getdate().
In contrast to strptime(3), (which has a format argument), getdate() uses the formats found in the file whose full pathname is given in the environment variable DATEMSK. 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 for strptime(3). One more conversion specification is specified in POSIX.1-2001:
- Timezone name. This is not implemented in glibc.
When %Z is 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 to localtime(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 of getdate(). 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 argument res.
RETURN VALUEWhen successful, getdate() returns a pointer to a struct tm. Otherwise, it returns NULL and sets the global variable getdate_err to one of the error numbers shown below. Changes to errno are unspecified.
On success getdate_r() returns 0; on error it returns one of the error numbers shown below.
ERRORSThe following errors are returned via getdate_err (for getdate()) or as the function result (for getdate_r()):
- The DATEMSK environment variable is not defined, or its value is an empty string.
- The template file specified by DATEMSK cannot be opened for reading.
- Failed to get file status information.
- The template file is not a regular file.
- An error was encountered while reading the template file.
- Memory allocation failed (not enough memory available).
- There is no line in the file that matches the input.
- Invalid input specification.
- File containing format patterns.
- TZ, LC_TIME
- Variables used by strptime(3).
ATTRIBUTESFor an explanation of the terms used in this section, see attributes(7).
|getdate()||Thread safety||MT-Unsafe race:getdate env locale|
|getdate_r()||Thread safety||MT-Safe env locale|
CONFORMING TOPOSIX.1-2001, POSIX.1-2008.
NOTESThe POSIX.1 specification for strptime(3) contains conversion specifications using the %E or %O modifier, while such specifications are not given for getdate(). In glibc, getdate() is implemented using strptime(3), so that precisely the same conversions are supported by both.
EXAMPLESThe program below calls getdate() for each of its command-line arguments, and for each call displays the values in the fields of the returned tm structure. The following shell session demonstrates the operation of the program:
$ echo '%A' > $TFILE # Full name of the day of the week
$ echo '%T' >> $TFILE # ISO date (YYYY-MM-DD)
$ echo '%F' >> $TFILE # Time (HH:MM:SS)
$ export DATEMSK=$TFILE
$ ./a.out Tuesday '2009-12-28' '12:22:33'
Sun Sep 7 06:03:36 CEST 2008
Call 1 ("Tuesday") succeeded:
Program source#define _GNU_SOURCE #include <time.h> #include <stdio.h> #include <stdlib.h>
main(int argc, char *argv)
COLOPHONThis page is part of release 5.10 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.