getdate(S)
converts user-definable date and/or time specifications pointed to by
string
into a
tm
structure.
The structure declaration is in the
time.h
header file (see also
ctime(S)).
User-supplied templates are used to parse and interpret the input string.
The user creates the templates,
which are text files identified via the environment variable
DATEMSK.
Each line in the template represents
an acceptable date and/or time specification
using some of the same field descriptors as the ones used by the command
date(C).
The first line in the template that matches the input specification
is used for interpretation and conversion into the internal time format.
The following field descriptors are supported:
%%
same as %
%a
abbreviated weekday name
%A
full weekday name
%b
abbreviated month name
%B
full month name
%c
the locale's date and time representation
%d
day of month (01-31; the leading 0 is optional)
%e
same as %d
%D
date as %m/%d/%y
%h
abbreviated month name
%H
hour (00-23)
%I
hour (01-12)
%m
month number (01-12)
%M
minute (00-59)
%n
same as Newline
%p
the locale's equivalent of either a.m. or p.m.
%r
the locale's representation of time in a.m. and p.m. notation.
In the POSIX locale, it is equivalent to %I:%M:%S %p.
%R
the locale's representation of time.
In the POSIX locale, it is equivalent to %H:%M.
%S
seconds (00-61). Leap seconds are allowed but are not predictable.
%t
same as tab
%T
time as %H:%M:%S
%w
weekday number (0-6, where Sunday = 0)
%x
the locale's date representation
%X
the locale's time representation
%y
year within century. When a century is not otherwise specified,
values in the range 69-99 refer to years in the twentieth
century (1969 to 1999 inclusive); values in the range 00-68
refer to years in the twenty-first century (2000 to 2068
inclusive).
%Y
year as ccyy (for example, 2004)
%Z
time zone name or, if no time zone exists, no characters. If
getdate( )
expects a different time zone than the one supplied by %Z,
it produces an invalid input specification error.
getdate( )
calculates the expected time zone
based on information supplied to the function,
such as the hour, day, and month.
The month and weekday names can consist of any combination of letters.
Case does not matter in strings entered by the user.
For example,
"Uhr" is equivalent to "uhr".
The user can ask for the input date or time specification
in a specific language by setting the
setlocale(S)
variables
LC_TIME and LC_CTYPE.
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
These are examples of valid input specifications for the template above:
getdate("10/1/95 4 PM");
getdate("Friday");
getdate("Friday September 19 1995, 10:30:30");
getdate("24,9,1994 10:30");
getdate("at monday the 1st of december in 1994");
getdate("run job at 3 PM, december 2nd");
If the
LANG
environment variable is set to
german,
the following is valid:
getdate("freitag den 10. oktober 1994 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/94")
%m/%d/%y
getdate("27.11.94")
%d.%m.%y
getdate("94-11-27")
%y-%m-%d
getdate("Friday 12:00:00")
%A %H:%M:%S
The following rules are applied for converting
the input specification into the internal format:
If %Z is scanned,
getdate( )
sets the broken-down time to the current time in the scanned time zone.
If %Z is not scanned,
getdate( )
makes it the current local time.
If only the weekday is specified,
getdate( )
assumes today
if the specified day is equal to the current day
and next week if it is less.
If only the month is specified,
getdate( )
assumes the current year
if the specified month is equal to the
current month and next year if it is less.
(The first day of month is assumed if no day is given.)
If no hour, minute, and second are specified,
getdate( )
assumes the current hour, minute, and second.
If no date is specified,
getdate( )
assumes today if the specified hour is greater than the current hour;
it assumes tomorrow if the specified hour is less than the current hour.
The following examples illustrate these rules.
Assume that the current date is
Mon Sep 22 12:19:47 EDT 1994
and that the LC_TIME and LANG
environment variables are not set.
Input
Line in Template
Date
Mon
%a
Mon Sep 22 12:19:47 EDT 1994
Sun
%a
Sun Sep 28 12:19:47 EDT 1994
Fri
%a
Fri Sep 26 12:19:47 EDT 1994
September
%B
Mon Sep 1 12:19:47 EDT 1994
January
%B
Thu Jan 1 12:19:47 EST 1995
December
%B
Mon Dec 1 12:19:47 EST 1994
Sep Mon
%b %a
Mon Sep 1 12:19:47 EDT 1994
Jan Fri
%b %a
Fri Jan 2 12:19:47 EST 1995
Dec Mon
%b %a
Mon Dec 1 12:19:47 EST 1994
Jan Wed 1997
%b %a %Y
Wed Jan 4 12:19:47 EST 1997
Fri 9
%a %H
Fri Sep 26 09:00:00 EDT 1994
Feb 10:30
%b %H:%S
Sun Feb 1 10:00:30 EST 1995
10:30
%H:%M
Tue Sep 23 10:30:00 EDT 1994
13:30
%H:%M
Mon Sep 22 13:30:00 EDT 1994
Return values
If successful,
getdate( )
returns a pointer to a structure
tm; otherwise, it returns
NULL and sets the global variable
getdate_err
to indicate the error.
Diagnostics
The following is a complete list of the
getdate_err
settings and their meanings.
1
The environment variable DATEMSK is null or undefined.
2
The template file cannot be opened for reading.
3
Failed to get file status information.
4
The template file is not a regular file.
5
An error is encountered while reading the template file.
6
malloc(S)
failed (not enough memory is available).
7
There is no line in the template that matches the input.
8
The input specification is invalid.
For example, it specifies February 31 or a time
that can not be represented in time_t
(the time in seconds since
1970-01-01 00:00 UTC.
Warning
Note the following:
Subsequent calls to
getdate( )
alter the contents of getdate_err.
Dates before 1970 or after 2037 are illegal.
getdate( )
makes explicit use of macros described in
ctype(S)
and is thus affected by the LC_CTYPE category of the current locale.
Other implementations of
getdate( )
might return char *.
If the time zone supplied by %Z is not the same as the time zone
getdate( )
expects, an invalid input specification error results.
getdate( )
expects a time zone based on information supplied
to the interface (such as hour, day, and month).
Files
/usr/lang/locale/time
language-specific printable files.
locale expands to
language/territory/codeset,
for example
french/france/8859.
/usr/lang/locale/ctype
codeset-specific printable files.
locale expands to
language/territory/codeset,
for example french/france/8859.