Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/getdate.html
2024-02-19 00:21:47 -05:00

648 lines
17 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>getdate</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="getdate"></a> <a name="tag_03_216"></a><!-- getdate -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_216_01"></a>NAME</h4>
<blockquote>getdate - convert user format date and time</blockquote>
<h4><a name="tag_03_216_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> #include &lt;<a href="../basedefs/time.h.html">time.h</a>&gt;<br>
<br>
struct tm *getdate(const char *</tt><i>string</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div>
<tt><br>
</tt></blockquote>
<h4><a name="tag_03_216_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>getdate</i>() function shall convert a string representation of a date or time into a broken-down time.</p>
<p>The external variable or macro <i>getdate_err</i> is used by <i>getdate</i>() to return error values.</p>
<p>Templates are used to parse and interpret the input string. The templates are contained in a text file identified by the
environment variable <i>DATEMSK .</i> The <i>DATEMSK</i> variable should be set to indicate the full pathname of the file that
contains the templates. The first line in the template that matches the input specification is used for interpretation and
conversion into the internal time format.</p>
<p>The following conversion specifications shall be supported:</p>
<dl compact>
<dt><tt>%%</tt></dt>
<dd>Equivalent to <tt>%</tt> .</dd>
<dt><tt>%a</tt></dt>
<dd>Abbreviated weekday name.</dd>
<dt><tt>%A</tt></dt>
<dd>Full weekday name.</dd>
<dt><tt>%b</tt></dt>
<dd>Abbreviated month name.</dd>
<dt><tt>%B</tt></dt>
<dd>Full month name.</dd>
<dt><tt>%c</tt></dt>
<dd>Locale's appropriate date and time representation.</dd>
<dt><tt>%C</tt></dt>
<dd>Century number [00,99]; leading zeros are permitted but not required.</dd>
<dt><tt>%d</tt></dt>
<dd>Day of month [01,31]; the leading 0 is optional.</dd>
<dt><tt>%D</tt></dt>
<dd>Date as <tt>%m</tt> / <tt>%d</tt> / <tt>%y</tt> .</dd>
<dt><tt>%e</tt></dt>
<dd>Equivalent to <tt>%d</tt> .</dd>
<dt><tt>%h</tt></dt>
<dd>Abbreviated month name.</dd>
<dt><tt>%H</tt></dt>
<dd>Hour [00,23].</dd>
<dt><tt>%I</tt></dt>
<dd>Hour [01,12].</dd>
<dt><tt>%m</tt></dt>
<dd>Month number [01,12].</dd>
<dt><tt>%M</tt></dt>
<dd>Minute [00,59].</dd>
<dt><tt>%n</tt></dt>
<dd>Equivalent to &lt;newline&gt;.</dd>
<dt><tt>%p</tt></dt>
<dd>Locale's equivalent of either AM or PM.</dd>
<dt><tt>%r</tt></dt>
<dd>The locale's appropriate representation of time in AM and PM notation. In the POSIX locale, this shall be equivalent to
<tt>%I</tt> : <tt>%M</tt> : <tt>%S</tt> <tt>%p</tt> .</dd>
<dt><tt>%R</tt></dt>
<dd>Time as <tt>%H</tt> : <tt>%M</tt> .</dd>
<dt><tt>%S</tt></dt>
<dd>Seconds [00,60]. The range goes to 60 (rather than stopping at 59) to allow positive leap seconds to be expressed. Since leap
seconds cannot be predicted by any algorithm, leap second data must come from some external source.</dd>
<dt><tt>%t</tt></dt>
<dd>Equivalent to &lt;tab&gt;.</dd>
<dt><tt>%T</tt></dt>
<dd>Time as <tt>%H</tt> : <tt>%M</tt> : <tt>%S</tt> .</dd>
<dt><tt>%w</tt></dt>
<dd>Weekday number (Sunday = [0,6]).</dd>
<dt><tt>%x</tt></dt>
<dd>Locale's appropriate date representation.</dd>
<dt><tt>%X</tt></dt>
<dd>Locale's appropriate time representation.</dd>
<dt><tt>%y</tt></dt>
<dd>Year within century. When a century is not otherwise specified, values in the range [69,99] shall refer to years 1969 to 1999
inclusive, and values in the range [00,68] shall refer to years 2000 to 2068 inclusive. <basefont size="2">
<dl>
<dt><b>Note:</b></dt>
<dd>It is expected that in a future version of IEEE&nbsp;Std&nbsp;1003.1-2001 the default century inferred from a 2-digit year will
change. (This would apply to all commands accepting a 2-digit year as input.)</dd>
</dl>
<basefont size="3"></dd>
<dt><tt>%Y</tt></dt>
<dd>Year as <tt>"ccyy"</tt> (for example, 2001).</dd>
<dt><tt>%Z</tt></dt>
<dd>Timezone name or no characters if no timezone exists. If the timezone supplied by <tt>%Z</tt> is not the timezone that
<i>getdate</i>() expects, an invalid input specification error shall result. The <i>getdate</i>() function calculates an expected
timezone based on information supplied to the function (such as the hour, day, and month).</dd>
</dl>
<p>The match between the template and input specification performed by <i>getdate</i>() shall be case-insensitive.</p>
<p>The month and weekday names can consist of any combination of upper and lowercase letters. The process can request that the
input date or time specification be in a specific language by setting the <i>LC_TIME</i> category (see <a href=
"setlocale.html"><i>setlocale</i>()</a> ).</p>
<p>Leading zeros are not necessary for the descriptors that allow leading zeros. However, at most two digits are allowed for those
descriptors, including leading zeros. Extra whitespace in either the template file or in <i>string</i> shall be ignored.</p>
<p>The results are undefined if the conversion specifications <tt>%c</tt> , <tt>%x</tt> , and <tt>%X</tt> include unsupported
conversion specifications.</p>
<p>The following rules apply for converting the input specification into the internal format:</p>
<ul>
<li>
<p>If <tt>%Z</tt> is being scanned, then <i>getdate</i>() shall initialize the broken-down time to be the current time in the
scanned timezone. Otherwise, it shall initialize the broken-down time based on the current local time as if <a href=
"../functions/localtime.html"><i>localtime</i>()</a> had been called.</p>
</li>
<li>
<p>If only the weekday is given, the day chosen shall be the day, starting with today and moving into the future, which first
matches the named day.</p>
</li>
<li>
<p>If only the month (and no year) is given, the month chosen shall be the month, starting with the current month and moving into
the future, which first matches the named month. The first day of the month shall be assumed if no day is given.</p>
</li>
<li>
<p>If no hour, minute, and second are given, the current hour, minute, and second shall be assumed.</p>
</li>
<li>
<p>If no date is given, the hour chosen shall be the hour, starting with the current hour and moving into the future, which first
matches the named hour.</p>
</li>
</ul>
<p>If a conversion specification in the DATEMSK file does not correspond to one of the conversion specifications above, the
behavior is unspecified.</p>
<p>The <i>getdate</i>() function need not be reentrant. A function that is not required to be reentrant is not required to be
thread-safe.</p>
</blockquote>
<h4><a name="tag_03_216_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>getdate</i>() shall return a pointer to a <b>struct tm</b>. Otherwise, it shall return a null
pointer and set <i>getdate_err</i> to indicate the error.</p>
</blockquote>
<h4><a name="tag_03_216_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>getdate</i>() function shall fail in the following cases, setting <i>getdate_err</i> to the value shown in the list
below. Any changes to <i>errno</i> are unspecified.</p>
<ol>
<li>
<p>The <i>DATEMSK</i> environment variable is null or undefined.</p>
</li>
<li>
<p>The template file cannot be opened for reading.</p>
</li>
<li>
<p>Failed to get file status information.</p>
</li>
<li>
<p>The template file is not a regular file.</p>
</li>
<li>
<p>An I/O error is encountered while reading the template file.</p>
</li>
<li>
<p>Memory allocation failed (not enough memory available).</p>
</li>
<li>
<p>There is no line in the template that matches the input.</p>
</li>
<li>
<p>Invalid input specification. For example, February 31; or a time is specified that cannot be represented in a <b>time_t</b>
(representing the time in seconds since the Epoch).</p>
</li>
</ol>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_216_06"></a>EXAMPLES</h4>
<blockquote>
<ol>
<li>
<p>The following example shows the possible contents of a template:</p>
<pre>
<tt>%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
</tt>
</pre>
</li>
<li>
<p>The following are examples of valid input specifications for the template in Example 1:</p>
<pre>
<tt>getdate("10/1/87 4 PM");
getdate("Friday");
getdate("Friday September 18, 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");
</tt>
</pre>
<p>If the <i>LC_TIME</i> category is set to a German locale that includes <i>freitag</i> as a weekday name and <i>oktober</i> as a
month name, the following would be valid:</p>
<pre>
<tt>getdate("freitag den 10. oktober 1986 10.30 Uhr");
</tt>
</pre>
</li>
<li>
<p>The following example shows how local date and time specification can be defined in the template:</p>
<center>
<table border="1" cellpadding="3" align="center">
<tr valign="top">
<th align="center">
<p class="tent"><b>Invocation</b></p>
</th>
<th align="center">
<p class="tent"><b>Line in Template</b></p>
</th>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">getdate("11/27/86")</p>
</td>
<td align="left">
<p class="tent">%m/%d/%y</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">getdate("27.11.86")</p>
</td>
<td align="left">
<p class="tent">%d.%m.%y</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">getdate("86-11-27")</p>
</td>
<td align="left">
<p class="tent">%y-%m-%d</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">getdate("Friday 12:00:00")</p>
</td>
<td align="left">
<p class="tent">%A %H:%M:%S</p>
</td>
</tr>
</table>
</center>
</li>
<li>
<p>The following examples help to illustrate the above rules assuming that the current date is Mon Sep 22 12:19:47 EDT 1986 and the
<i>LC_TIME</i> category is set to the default C locale:</p>
<center>
<table border="1" cellpadding="3" align="center">
<tr valign="top">
<th align="center">
<p class="tent"><b>Input</b></p>
</th>
<th align="center">
<p class="tent"><b>Line in Template</b></p>
</th>
<th align="center">
<p class="tent"><b>Date</b></p>
</th>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Mon</p>
</td>
<td align="left">
<p class="tent">%a</p>
</td>
<td align="left">
<p class="tent">Mon Sep 22 12:19:47 EDT 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Sun</p>
</td>
<td align="left">
<p class="tent">%a</p>
</td>
<td align="left">
<p class="tent">Sun Sep 28 12:19:47 EDT 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Fri</p>
</td>
<td align="left">
<p class="tent">%a</p>
</td>
<td align="left">
<p class="tent">Fri Sep 26 12:19:47 EDT 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">September</p>
</td>
<td align="left">
<p class="tent">%B</p>
</td>
<td align="left">
<p class="tent">Mon Sep 1 12:19:47 EDT 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">January</p>
</td>
<td align="left">
<p class="tent">%B</p>
</td>
<td align="left">
<p class="tent">Thu Jan 1 12:19:47 EST 1987</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">December</p>
</td>
<td align="left">
<p class="tent">%B</p>
</td>
<td align="left">
<p class="tent">Mon Dec 1 12:19:47 EST 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Sep Mon</p>
</td>
<td align="left">
<p class="tent">%b %a</p>
</td>
<td align="left">
<p class="tent">Mon Sep 1 12:19:47 EDT 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Jan Fri</p>
</td>
<td align="left">
<p class="tent">%b %a</p>
</td>
<td align="left">
<p class="tent">Fri Jan 2 12:19:47 EST 1987</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Dec Mon</p>
</td>
<td align="left">
<p class="tent">%b %a</p>
</td>
<td align="left">
<p class="tent">Mon Dec 1 12:19:47 EST 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Jan Wed 1989</p>
</td>
<td align="left">
<p class="tent">%b %a %Y</p>
</td>
<td align="left">
<p class="tent">Wed Jan 4 12:19:47 EST 1989</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Fri 9</p>
</td>
<td align="left">
<p class="tent">%a %H</p>
</td>
<td align="left">
<p class="tent">Fri Sep 26 09:00:00 EDT 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">Feb 10:30</p>
</td>
<td align="left">
<p class="tent">%b %H:%S</p>
</td>
<td align="left">
<p class="tent">Sun Feb 1 10:00:30 EST 1987</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">10:30</p>
</td>
<td align="left">
<p class="tent">%H:%M</p>
</td>
<td align="left">
<p class="tent">Tue Sep 23 10:30:00 EDT 1986</p>
</td>
</tr>
<tr valign="top">
<td align="left">
<p class="tent">13:30</p>
</td>
<td align="left">
<p class="tent">%H:%M</p>
</td>
<td align="left">
<p class="tent">Mon Sep 22 13:30:00 EDT 1986</p>
</td>
</tr>
</table>
</center>
</li>
</ol>
</blockquote>
<h4><a name="tag_03_216_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>Although historical versions of <i>getdate</i>() did not require that <a href=
"../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a> declare the external variable <i>getdate_err</i>, this volume of
IEEE&nbsp;Std&nbsp;1003.1-2001 does require it. The standard developers encourage applications to remove declarations of
<i>getdate_err</i> and instead incorporate the declaration by including <a href=
"../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a>.</p>
<p>Applications should use <tt>%Y</tt> (4-digit years) in preference to <tt>%y</tt> (2-digit years).</p>
</blockquote>
<h4><a name="tag_03_216_08"></a>RATIONALE</h4>
<blockquote>
<p>In standard locales, the conversion specifications <tt>%c</tt> , <tt>%x</tt> , and <tt>%X</tt> do not include unsupported
conversion specifiers and so the text regarding results being undefined is not a problem in that case.</p>
</blockquote>
<h4><a name="tag_03_216_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_216_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="ctime.html"><i>ctime</i>()</a> , <a href="localtime.html"><i>localtime</i>()</a> , <a href=
"setlocale.html"><i>setlocale</i>()</a> , <a href="strftime.html"><i>strftime</i>()</a> , <a href="times.html"><i>times</i>()</a> ,
the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_216_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 4, Version 2.</p>
</blockquote>
<h4><a name="tag_03_216_12"></a>Issue 5</h4>
<blockquote>
<p>Moved from X/OPEN UNIX extension to BASE.</p>
<p>The last paragraph of the DESCRIPTION is added.</p>
<p>The <tt>%C</tt> conversion specification is added, and the exact meaning of the <tt>%y</tt> conversion specification is
clarified in the DESCRIPTION.</p>
<p>A note indicating that this function need not be reentrant is added to the DESCRIPTION.</p>
<p>The <tt>%R</tt> conversion specification is changed to follow historical practice.</p>
</blockquote>
<h4><a name="tag_03_216_13"></a>Issue 6</h4>
<blockquote>
<p>The DESCRIPTION is updated to refer to &quot;seconds since the Epoch&quot; rather than &quot;seconds since 00:00:00 UTC (Coordinated
Universal Time), January 1 1970&quot; for consistency with other <i>time</i> functions.</p>
<p>The description of <tt>%S</tt> is updated so that the valid range is [00,60] rather than [00,61].</p>
<p>The DESCRIPTION is updated to refer to conversion specifications instead of field descriptors for consistency with other
functions.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>