459 lines
12 KiB
HTML
459 lines
12 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of GETDATE</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>GETDATE</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
getdate, getdate_r - convert a date-plus-time string to broken-down time
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/time.h">time.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>struct tm *getdate(const char *</B><I>string</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
<B>extern int getdate_err;</B>
|
|
|
|
<P>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/time.h">time.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>int getdate_r(const char *</B><I>string</I><B>, struct tm *</B><I>res</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>getdate</B>():
|
|
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
_XOPEN_SOURCE >= 500
|
|
|
|
</DL>
|
|
|
|
<BR>
|
|
|
|
<B>getdate_r</B>():
|
|
|
|
|
|
<DL COMPACT><DT id="2"><DD>
|
|
_GNU_SOURCE
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The function
|
|
<B>getdate</B>()
|
|
|
|
converts a string representation of a date and time,
|
|
contained in the buffer pointed to by
|
|
<I>string</I>,
|
|
|
|
into a broken-down time.
|
|
The broken-down time is stored in a
|
|
<I>tm</I>
|
|
|
|
structure, and a pointer to this
|
|
structure is returned as the function result.
|
|
This
|
|
<I>tm</I>
|
|
|
|
structure is allocated in static storage,
|
|
and consequently it will be overwritten by further calls to
|
|
<B>getdate</B>().
|
|
|
|
<P>
|
|
|
|
In contrast to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strptime">strptime</A></B>(3),
|
|
|
|
(which has a
|
|
<I>format</I>
|
|
|
|
argument),
|
|
<B>getdate</B>()
|
|
|
|
uses the formats found in the file
|
|
whose full pathname is given in the environment variable
|
|
<B>DATEMSK</B>.
|
|
|
|
The first line in the file that matches the given input string
|
|
is used for the conversion.
|
|
<P>
|
|
|
|
The matching is done case insensitively.
|
|
Superfluous whitespace, either in the pattern or in the string to
|
|
be converted, is ignored.
|
|
<P>
|
|
|
|
The conversion specifications that a pattern can contain are those given for
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strptime">strptime</A></B>(3).
|
|
|
|
One more conversion specification is specified in POSIX.1-2001:
|
|
<DL COMPACT>
|
|
<DT id="3"><B>%Z</B>
|
|
|
|
<DD>
|
|
Timezone name.
|
|
|
|
|
|
|
|
This is not implemented in glibc.
|
|
</DL>
|
|
<P>
|
|
|
|
When
|
|
<B>%Z</B>
|
|
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?3+localtime">localtime</A></B>(3)).
|
|
|
|
<P>
|
|
|
|
When only the day of the week is given,
|
|
the day is taken to be the first such day
|
|
on or after today.
|
|
<P>
|
|
|
|
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.
|
|
<P>
|
|
|
|
When no hour, minute and second are given, the current
|
|
hour, minute and second are taken.
|
|
<P>
|
|
|
|
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.
|
|
<P>
|
|
|
|
<B>getdate_r</B>()
|
|
|
|
is a GNU extension that provides a reentrant version of
|
|
<B>getdate</B>().
|
|
|
|
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
|
|
<I>res</I>.
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
When successful,
|
|
<B>getdate</B>()
|
|
|
|
returns a pointer to a
|
|
<I>struct tm</I>.
|
|
|
|
Otherwise, it returns NULL and sets the global variable
|
|
<I>getdate_err</I>
|
|
|
|
to one of the error numbers shown below.
|
|
Changes to
|
|
<I>errno</I>
|
|
|
|
are unspecified.
|
|
<P>
|
|
|
|
On success
|
|
<B>getdate_r</B>()
|
|
|
|
returns 0;
|
|
on error it returns one of the error numbers shown below.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
The following errors are returned via
|
|
<I>getdate_err</I>
|
|
|
|
(for
|
|
<B>getdate</B>())
|
|
|
|
or as the function result (for
|
|
<B>getdate_r</B>()):
|
|
|
|
<DL COMPACT>
|
|
<DT id="4"><B>1</B>
|
|
|
|
<DD>
|
|
The
|
|
<B>DATEMSK</B>
|
|
|
|
environment variable is not defined, or its value is an empty string.
|
|
<DT id="5"><B>2</B>
|
|
|
|
<DD>
|
|
The template file specified by
|
|
<B>DATEMSK</B>
|
|
|
|
cannot be opened for reading.
|
|
<DT id="6"><B>3</B>
|
|
|
|
<DD>
|
|
Failed to get file status information.
|
|
|
|
<DT id="7"><B>4</B>
|
|
|
|
<DD>
|
|
The template file is not a regular file.
|
|
<DT id="8"><B>5</B>
|
|
|
|
<DD>
|
|
An error was encountered while reading the template file.
|
|
<DT id="9"><B>6</B>
|
|
|
|
<DD>
|
|
Memory allocation failed (not enough memory available).
|
|
|
|
<DT id="10"><B>7</B>
|
|
|
|
<DD>
|
|
There is no line in the file that matches the input.
|
|
<DT id="11"><B>8</B>
|
|
|
|
<DD>
|
|
Invalid input specification.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ENVIRONMENT</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="12"><B>DATEMSK</B>
|
|
|
|
<DD>
|
|
File containing format patterns.
|
|
<DT id="13"><B>TZ</B>, <B>LC_TIME</B>
|
|
|
|
<DD>
|
|
Variables used by
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strptime">strptime</A></B>(3).
|
|
|
|
</DL>
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getdate</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Unsafe race:getdate env locale<BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getdate_r</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe env locale<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The POSIX.1 specification for
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strptime">strptime</A></B>(3)
|
|
|
|
contains conversion specifications using the
|
|
<B>%E</B>
|
|
|
|
or
|
|
<B>%O</B>
|
|
|
|
modifier, while such specifications are not given for
|
|
<B>getdate</B>().
|
|
|
|
In glibc,
|
|
<B>getdate</B>()
|
|
|
|
is implemented using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strptime">strptime</A></B>(3),
|
|
|
|
so that precisely the same conversions are supported by both.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below calls
|
|
<B>getdate</B>()
|
|
|
|
for each of its command-line arguments,
|
|
and for each call displays the values in the fields of the returned
|
|
<I>tm</I>
|
|
|
|
structure.
|
|
The following shell session demonstrates the operation of the program:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> TFILE=$PWD/tfile</B>
|
|
|
|
$<B> echo '%A' > $TFILE </B> # Full name of the day of the week
|
|
|
|
$<B> echo '%T' >> $TFILE</B> # ISO date (YYYY-MM-DD)
|
|
|
|
$<B> echo '%F' >> $TFILE</B> # Time (HH:MM:SS)
|
|
|
|
$<B> date</B>
|
|
|
|
$<B> export DATEMSK=$TFILE</B>
|
|
|
|
$<B> ./a.out Tuesday '2009-12-28' '12:22:33'</B>
|
|
|
|
Sun Sep 7 06:03:36 CEST 2008
|
|
Call 1 ("Tuesday") succeeded:
|
|
<BR> tm_sec = 36
|
|
<BR> tm_min = 3
|
|
<BR> tm_hour = 6
|
|
<BR> tm_mday = 9
|
|
<BR> tm_mon = 8
|
|
<BR> tm_year = 108
|
|
<BR> tm_wday = 2
|
|
<BR> tm_yday = 252
|
|
<BR> tm_isdst = 1
|
|
Call 2 ("2009-12-28") succeeded:
|
|
<BR> tm_sec = 36
|
|
<BR> tm_min = 3
|
|
<BR> tm_hour = 6
|
|
<BR> tm_mday = 28
|
|
<BR> tm_mon = 11
|
|
<BR> tm_year = 109
|
|
<BR> tm_wday = 1
|
|
<BR> tm_yday = 361
|
|
<BR> tm_isdst = 0
|
|
Call 3 ("12:22:33") succeeded:
|
|
<BR> tm_sec = 33
|
|
<BR> tm_min = 22
|
|
<BR> tm_hour = 12
|
|
<BR> tm_mday = 7
|
|
<BR> tm_mon = 8
|
|
<BR> tm_year = 108
|
|
<BR> tm_wday = 0
|
|
<BR> tm_yday = 250
|
|
<BR> tm_isdst = 1
|
|
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/time.h">time.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct tm *tmp;
|
|
<BR> int j;
|
|
<P>
|
|
<BR> for (j = 1; j < argc; j++) {
|
|
<BR> tmp = getdate(argv[j]);
|
|
<P>
|
|
<BR> if (tmp == NULL) {
|
|
<BR> printf("Call %d failed; getdate_err = %d\n",
|
|
<BR> j, getdate_err);
|
|
<BR> continue;
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("Call %d (\"%s\") succeeded:\n", j, argv[j]);
|
|
<BR> printf(" tm_sec = %d\n", tmp->tm_sec);
|
|
<BR> printf(" tm_min = %d\n", tmp->tm_min);
|
|
<BR> printf(" tm_hour = %d\n", tmp->tm_hour);
|
|
<BR> printf(" tm_mday = %d\n", tmp->tm_mday);
|
|
<BR> printf(" tm_mon = %d\n", tmp->tm_mon);
|
|
<BR> printf(" tm_year = %d\n", tmp->tm_year);
|
|
<BR> printf(" tm_wday = %d\n", tmp->tm_wday);
|
|
<BR> printf(" tm_yday = %d\n", tmp->tm_yday);
|
|
<BR> printf(" tm_isdst = %d\n", tmp->tm_isdst);
|
|
<BR> }
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+time">time</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+localtime">localtime</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+setlocale">setlocale</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strftime">strftime</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strptime">strptime</A></B>(3)
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="14"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="15"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="16"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="17"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="18"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="19"><A HREF="#lbAG">ENVIRONMENT</A><DD>
|
|
<DT id="20"><A HREF="#lbAH">ATTRIBUTES</A><DD>
|
|
<DT id="21"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="22"><A HREF="#lbAJ">NOTES</A><DD>
|
|
<DT id="23"><A HREF="#lbAK">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="24"><A HREF="#lbAL">Program source</A><DD>
|
|
</DL>
|
|
<DT id="25"><A HREF="#lbAM">SEE ALSO</A><DD>
|
|
<DT id="26"><A HREF="#lbAN">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:44 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|