GETDATE(3) | Library Functions Manual | GETDATE(3) |
getdate
— convert
user format date and time
#include
<time.h>
extern int getdate_err;
struct tm *
getdate
(const
char *string);
The
getdate
()
function converts user-definable date and/or time specifications pointed to
by string to a tm structure. The
tm structure is defined in the
⟨time.h⟩ header.
User-supplied templates are used to parse and interpret the input
string. The templates are text files created by the user and identified via
the environment variable DATEMSK
. Each line in the
template represents an acceptable date and/or time specification using
conversion specifications similar to those used by
strftime(3) and
strptime(3). Dates before 1902 and
after 2037 are illegal. The first line in the template that matches the
input specification is used for interpretation and conversion into the
internal time format.
The following conversion specifications are supported:
%%
%a
%A
%b
%B
%c
%C
%d
%D
%e
%h
%H
%I
%j
%m
%M
%n
%p
%r
%R
%S
%t
%T
%U
%w
%W
%x
%X
%y
%Y
%Z
Some conversion specifications can be modified by the E and O modifier characters to indicate that an alternative format or specification should be used rather than the one normally used by the unmodified specification. If the alternative format or specification does not exist in the current locale, the behavior be as if the unmodified conversion specification were used.
%Ec
%EC
%Ex
%EX
%Ey
%EY
%Od
%Oe
%OH
%OI
%Om
%OM
%OS
%OU
%Ow
%OW
%Oy
The following rules are applied for converting the input specification into the internal format:
A conversion specification that is an ordinary character is executed by scanning the next character from the buffer. If the character scanned from the buffer differs from the one comprising the conversion specification, the specification fails, and the differing and subsequent characters remain unscanned.
A series of conversion specifications composed of
‘%n
’,
‘%t
’, white space characters, or any
combination is executed by scanning up to the first character that is not
white space (which remains unscanned), or until no more characters can be
scanned.
Any other conversion specification is executed by
scanning characters until a character matching the next conversion
specification is scanned, or until no more characters can be scanned. These
characters, except the one matching the next conversion specification, are
then compared to the locale values associated with the conversion specifier.
If a match is found, values for the appropriate tm
structure members are set to values corresponding to the locale information.
If no match is found,
getdate
()
fails and no more characters are scanned.
The month names, weekday names, era names, and alternative numeric
symbols can consist of any combination of upper and lower case letters. The
user can request that the input date or time specification be in a specific
language by setting the LC_TIME
category using
setlocale(3).
If successful, getdate
() returns a pointer
to a tm structure; otherwise, it returns
NULL
and sets the global variable
getdate_err to indicate the error. Subsequent calls to
getdate
() alter the contents of
getdate_err.
The following is a complete list of the getdate_err settings and their meanings:
The
getdate
()
function makes explicit use of macros described on the
ctype(3) manual page.
Example 1: Examples of the getdate
()
function.
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 above template:
getdate("10/1/87 4 PM") getdate("Friday") getdate("Friday September 19 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")
LANG
environment variable is set to de (German),
the following is valid:
getdate("freitag den 10. oktober 1986 10.30 Uhr")
Local time and date specification are also supported. The following examples show how local date and time specification can be defined in the template.
Invocation | Line in Template |
getdate("11/27/86") | %m/%d/%y |
getdate("27.11.86") | %d.%m.%y |
getdate("86-11-27") | %y-%m-%d |
getdate("Friday 12:00:00") | %A %H:%M:%S |
The following examples illustrate the Internal Format Conversion
rules. Assume that the current date is Mon Sep 22 12:19:47
EDT 1986
and the LANG
environment variable is
not set.
Input | Template Line | Date |
Mon | %a | Mon Sep 22 12:19:48 EDT 1986 |
Sun | %a | Sun Sep 28 12:19:49 EDT 1986 |
Fri | %a | Fri Sep 26 12:19:49 EDT 1986 |
September | %B | Mon Sep 1 12:19:49 EDT 1986 |
January | %B | Thu Jan 1 12:19:49 EST 1987 |
December | %B | Mon Dec 1 12:19:49 EDT 1986 |
Sep Mon | %b %a | Mon Sep 1 12:19:50 EDT 1986 |
Jan Fri | %b %a | Fri Jan 2 12:19:50 EST 1987 |
Dec Mon | %b %a | Mon Dec 1 12:19:50 EST 1986 |
Jan Wed 1989 | %b %a %Y | Wed Jan 4 12:19:51 EST 1989 |
Fri 9 | %a %H | Fri Sep 26 09:00:00 EDT 1986 |
Feb 10:30 | %b %H:%S | Sun Feb 1 10:00:30 EST 1987 |
10:30 | %H:%M | Tue Sep 23 10:30:00 EDT 1986 |
13:30 | %H:%M | Mon Sep 22 13:30:00 EDT 1986 |
ctype(3), mktime(3), setlocale(3), strftime(3), strptime(3), environ(5)
January 3, 2004 | macOS 15.0 |