GETDATE(3P)
๐ GETDATE(3P) ๐ โ convert user format date and time
okay bestie, this is GETDATE(3P) from the POSIX Programmer's Manual, and it's the girl who takes your messy little date string and turns it into a whole struct tm. she's iconic, she's picky, and she has eight ways to fail. let's get into it ๐ซถ
๐ PROLOG ๐
so, real talk: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so pls consult the corresponding Linux manual page for the deets on Linux behavior), or โ plot twist โ the interface may not be implemented on Linux at all. ๐
๐ NAME ๐
getdate โ convert user format date and time
๐ฆ SYNOPSIS ๐ฆ
#include <time.h>
struct tm *getdate(const char *string);
โจ DESCRIPTION โจ
The getdate() function shall convert a string representation of a date or time into a broken-down time. that's it, that's the whole job ๐
The external variable or macro getdate_err, which has type int, is used by getdate() to return error values. It is unspecified whether getdate_err is a macro or an identifier declared with external linkage, and whether or not it is a modifiable lvalue. If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name getdate_err, the behavior is undefined. ๐จ likeโฆ don't do that.
Templates are used to parse and interpret the input string. The templates are contained in a text file identified by the environment variable DATEMSK. The DATEMSK variable should be set to indicate the full pathname of the file that contains the templates. The first line in the template that matches the input specification is used for interpretation and conversion into the internal time format. (first match wins, no take-backs ๐ฏ)
The following conversion specifications shall be supported:
%%โ Equivalent to%.%aโ Abbreviated weekday name.%Aโ Full weekday name.%bโ Abbreviated month name.%Bโ Full month name.%cโ Locale's appropriate date and time representation.%Cโ Century number[00,99]; leading zeros are permitted but not required.%dโ Day of month[01,31]; the leading 0 is optional.%Dโ Date as%m/%d/%y.%eโ Equivalent to%d.%hโ Abbreviated month name.%Hโ Hour[00,23].%Iโ Hour[01,12].%mโ Month number[01,12].%Mโ Minute[00,59].%nโ Equivalent to<newline>.%pโ Locale's equivalent of either AM or PM.%rโ The locale's appropriate representation of time in AM and PM notation. In the POSIX locale, this shall be equivalent to%I:%M:%S %p.%Rโ Time as%H:%M.%Sโ Seconds[00,60]. The range goes to 60 (rather than stopping at 59) to allow positive leap seconds to be expressed. Since leap seconds cannot be predicted by any algorithm, leap second data must come from some external source. (60 seconds in a minute is NOT a typo, bestie, it's the leap second girlies ๐ฐ๏ธ)%tโ Equivalent to<tab>.%Tโ Time as%H:%M:%S.%wโ Weekday number (Sunday =[0,6]).%xโ Locale's appropriate date representation.%Xโ Locale's appropriate time representation.%yโ Year within century. When a century is not otherwise specified, values in the range[69,99]shall refer to years 1969 to 1999 inclusive, and values in the range[00,68]shall refer to years 2000 to 2068 inclusive.
Note: It is expected that in a future version of this standard the default century inferred from a 2-digit year will change. (This would apply to all commands accepting a 2-digit year as input.)
%Yโ Year as"ccyy"(for example, 2001).%Zโ Timezone name or no characters if no timezone exists. If the timezone supplied by%Zis not the timezone thatgetdate()expects, an invalid input specification error shall result. Thegetdate()function calculates an expected timezone based on information supplied to the function (such as the hour, day, and month).
The match between the template and input specification performed by getdate() shall be case-insensitive. ๐ค
The month and weekday names can consist of any combination of upper and lowercase letters. The process can request that the input date or time specification be in a specific language by setting the LC_TIME category (see setlocale()).
Leading zeros are not necessary for the descriptors that allow leading zeros. However, at most two digits are allowed for those descriptors, including leading zeros. Extra white space in either the template file or in string shall be ignored.
The results are undefined if the conversion specifications %c, %x, and %X include unsupported conversion specifications. ๐
The following rules apply for converting the input specification into the internal format:
- If
%Zis being scanned, thengetdate()shall initialize the broken-down time to be the current time in the scanned timezone. Otherwise, it shall initialize the broken-down time based on the current local time as iflocaltime()had been called. - If only the weekday is given, the day chosen shall be the day, starting with today and moving into the future, which first matches the named day.
- If only the month (and no year) is given, the month chosen shall be the month, starting with the current month and moving into the future, which first matches the named month. The first day of the month shall be assumed if no day is given.
- If no hour, minute, and second are given, the current hour, minute, and second shall be assumed.
- If no date is given, the hour chosen shall be the hour, starting with the current hour and moving into the future, which first matches the named hour.
If a conversion specification in the DATEMSK file does not correspond to one of the conversion specifications above, the behavior is unspecified. โ ๏ธ
The getdate() function need not be thread-safe. (so don't be out here sharing her across threads like she's communal property, okay? ๐ญ)
๐ RETURN VALUE ๐
Upon successful completion, getdate() shall return a pointer to a struct tm. Otherwise, it shall return a null pointer and set getdate_err to indicate the error. ๐
๐ฅ ERRORS ๐ฅ
The getdate() function shall fail in the following cases, setting getdate_err to the value shown in the list below. Any changes to errno are unspecified.
- The
DATEMSKenvironment variable is null or undefined. - The template file cannot be opened for reading.
- Failed to get file status information.
- The template file is not a regular file.
- An I/O error is encountered while reading the template file.
- Memory allocation failed (not enough memory available).
- There is no line in the template that matches the input.
- Invalid input specification. For example, February 31; or a time is specified that cannot be represented in a
time_t(representing the time in seconds since the Epoch).
The following sections are informative.
๐ธ EXAMPLES ๐ธ
- The following example shows the possible contents of a template:
%m
%A %B %d, %Y, %H:%M:%S
%A
%B
%m/%d/%y %I %p
%d,%m,%Y %H:%M
at %A the %dst of %B in %Y
run job at %I %p,%B %dnd
%A den %d. %B %Y %H.%M Uhr
- The following are examples of valid input specifications for the template in Example 1:
getdate("10/1/87 4 PM");
getdate("Friday");
getdate("Friday September 18, 1987, 10:30:30");
getdate("24,9,1986 10:30");
getdate("at monday the 1st of december in 1986");
getdate("run job at 3 PM, december 2nd");
If the LC_TIME category is set to a German locale that includes freitag as a weekday name and oktober as a month name, the following would be valid:
getdate("freitag den 10. oktober 1986 10.30 Uhr");
- The following example shows how local date and time specification can be defined in the template:
getdate("11/27/86")โ%m/%d/%ygetdate("27.11.86")โ%d.%m.%ygetdate("86-11-27")โ%y-%m-%dgetdate("Friday 12:00:00")โ%A %H:%M:%S
- The following examples help to illustrate the above rules assuming that the current date is
Mon Sep 22 12:19:47 EDT 1986and theLC_TIMEcategory is set to the default C or POSIX locale:
Monโ%aโMon Sep 22 12:19:47 EDT 1986Sunโ%aโSun Sep 28 12:19:47 EDT 1986Friโ%aโFri Sep 26 12:19:47 EDT 1986Septemberโ%BโMon Sep 1 12:19:47 EDT 1986Januaryโ%BโThu Jan 1 12:19:47 EST 1987Decemberโ%BโMon Dec 1 12:19:47 EST 1986Sep Monโ%b %aโMon Sep 1 12:19:47 EDT 1986Jan Friโ%b %aโFri Jan 2 12:19:47 EST 1987Dec Monโ%b %aโMon Dec 1 12:19:47 EST 1986Jan Wed 1989โ%b %a %YโWed Jan 4 12:19:47 EST 1989Fri 9โ%a %HโFri Sep 26 09:00:00 EDT 1986Feb 10:30โ%b %H:%SโSun Feb 1 10:00:30 EST 198710:30โ%H:%MโTue Sep 23 10:30:00 EDT 198613:30โ%H:%MโMon Sep 22 13:30:00 EDT 1986
๐ซถ APPLICATION USAGE ๐ซถ
Although historical versions of getdate() did not require that <time.h> declare the external variable getdate_err, this volume of POSIX.1โ2017 does require it. The standard developers encourage applications to remove declarations of getdate_err and instead incorporate the declaration by including <time.h>.
Applications should use %Y (4-digit years) in preference to %y (2-digit years). fr fr, 4 digits is the moment, 2 digits is the chaos ๐
๐ญ RATIONALE ๐ญ
ok so bestie, here's the tea โ โ in standard locales, those conversion specifications %c, %x, and %X do not include any unsupported conversion specifiers. which means the whole "results are undefined" situation we were spiraling about earlier? literally not a problem in that case. no cap. ๐ซถ
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. ๐
that's it. that's the whole section. the future holds absolutely nothing for this one, and honestly? iconic. โจ
๐ SEE ALSO ๐
go read these next, bestie, they're the whole friend group ๐ซ:
ctime()localtime()setlocale()strftime()times()
also the Base Definitions volume of POSIX.1โ2017, specifically <time.h> ๐
ยฉ๏ธ COPYRIGHT ยฉ๏ธ
ok so this is the part where we give credit where credit is due, and it's giving legal realness ๐:
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html ๐
ngl bestie, if you spot any typographical or formatting errors in this page, they were most likely introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html ๐ โ don't come for the original authors, come for the converters.
๐ and that's a wrap on GETDATE(3P), bestie!! ๐ this one's courtesy of IEEE / The Open Group, dated 2017 โญ โ go forth and format your dates responsibly, mwah ๐
โจ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms