man-pages/man3/getdate.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
getdate, getdate_r - convert a date-plus-time string to broken-down time
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;</B>
<P>
<B>struct tm *getdate(const char *</B><I>string</I><B>);</B>
<P>
<B>extern int getdate_err;</B>
<P>
<B>#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;</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&nbsp;&gt;=&nbsp;500
</DL>
<BR>
<B>getdate_r</B>():
<DL COMPACT><DT id="2"><DD>
_GNU_SOURCE
</DL>
<A NAME="lbAD">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAJ">&nbsp;</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">&nbsp;</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' &gt; $TFILE </B> # Full name of the day of the week
$<B> echo '%T' &gt;&gt; $TFILE</B> # ISO date (YYYY-MM-DD)
$<B> echo '%F' &gt;&gt; $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 (&quot;Tuesday&quot;) succeeded:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_sec&nbsp;&nbsp;&nbsp;=&nbsp;36
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_min&nbsp;&nbsp;&nbsp;=&nbsp;3
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_hour&nbsp;&nbsp;=&nbsp;6
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_mday&nbsp;&nbsp;=&nbsp;9
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_mon&nbsp;&nbsp;&nbsp;=&nbsp;8
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_year&nbsp;&nbsp;=&nbsp;108
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_wday&nbsp;&nbsp;=&nbsp;2
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_yday&nbsp;&nbsp;=&nbsp;252
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_isdst&nbsp;=&nbsp;1
Call 2 (&quot;2009-12-28&quot;) succeeded:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_sec&nbsp;&nbsp;&nbsp;=&nbsp;36
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_min&nbsp;&nbsp;&nbsp;=&nbsp;3
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_hour&nbsp;&nbsp;=&nbsp;6
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_mday&nbsp;&nbsp;=&nbsp;28
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_mon&nbsp;&nbsp;&nbsp;=&nbsp;11
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_year&nbsp;&nbsp;=&nbsp;109
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_wday&nbsp;&nbsp;=&nbsp;1
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_yday&nbsp;&nbsp;=&nbsp;361
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_isdst&nbsp;=&nbsp;0
Call 3 (&quot;12:22:33&quot;) succeeded:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_sec&nbsp;&nbsp;&nbsp;=&nbsp;33
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_min&nbsp;&nbsp;&nbsp;=&nbsp;22
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_hour&nbsp;&nbsp;=&nbsp;12
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_mday&nbsp;&nbsp;=&nbsp;7
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_mon&nbsp;&nbsp;&nbsp;=&nbsp;8
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_year&nbsp;&nbsp;=&nbsp;108
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_wday&nbsp;&nbsp;=&nbsp;0
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_yday&nbsp;&nbsp;=&nbsp;250
<BR>&nbsp;&nbsp;&nbsp;&nbsp;tm_isdst&nbsp;=&nbsp;1
<A NAME="lbAL">&nbsp;</A>
<H3>Program source</H3>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&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quot;&nbsp;&nbsp;&nbsp;&nbsp;tm_isdst&nbsp;=&nbsp;%d\n&quot;,&nbsp;tmp-&gt;tm_isdst);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAM">&nbsp;</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">&nbsp;</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">&nbsp;</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>