The interface to getdate
is the simplest possible for a function
to parse a string and return the value. string is the input
string and the result is returned in a statically-allocated variable.
The details about how the string is processed are hidden from the user.
In fact, they can be outside the control of the program. Which formats
are recognized is controlled by the file named by the environment
variable DATEMSK
. This file should contain
lines of valid format strings which could be passed to strptime
.
The getdate
function reads these format strings one after the
other and tries to match the input string. The first line which
completely matches the input string is used.
Elements not initialized through the format string retain the values
present at the time of the getdate
function call.
The formats recognized by getdate
are the same as for
strptime
. See above for an explanation. There are only a few
extensions to the strptime
behavior:
It should be noted that the format in the template file need not only
contain format elements. The following is a list of possible format
strings (taken from the Unix standard):
%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
As you can see, the template list can contain very specific strings like
run job at %I %p,%B %dnd
. Using the above list of templates and
assuming the current time is Mon Sep 22 12:19:47 EDT 1986 we can obtain the
following results for the given input.
Input | Match | Result
|
Mon | %a | Mon Sep 22 12:19:47 EDT 1986
|
Sun | %a | Sun Sep 28 12:19:47 EDT 1986
|
Fri | %a | Fri Sep 26 12:19:47 EDT 1986
|
September | %B | Mon Sep 1 12:19:47 EDT 1986
|
January | %B | Thu Jan 1 12:19:47 EST 1987
|
December | %B | Mon Dec 1 12:19:47 EST 1986
|
Sep Mon | %b %a | Mon Sep 1 12:19:47 EDT 1986
|
Jan Fri | %b %a | Fri Jan 2 12:19:47 EST 1987
|
Dec Mon | %b %a | Mon Dec 1 12:19:47 EST 1986
|
Jan Wed 1989 | %b %a %Y | Wed Jan 4 12:19:47 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
|
The return value of the function is a pointer to a static variable of
type struct tm
, or a null pointer if an error occurred. The
result is only valid until the next getdate
call, making this
function unusable in multi-threaded applications.
The errno
variable is not changed. Error conditions are
stored in the global variable getdate_err
. See the
description above for a list of the possible error values.
Warning: The getdate
function should never be
used in SUID-programs. The reason is obvious: using the
DATEMSK
environment variable you can get the function to open
any arbitrary file and chances are high that with some bogus input
(such as a binary file) the program will crash.