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

201 lines
7.5 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>time</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="time"></a> <a name="tag_03_775"></a><!-- time -->
<!--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_775_01"></a>NAME</h4>
<blockquote>time - get time</blockquote>
<h4><a name="tag_03_775_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/time.h.html">time.h</a>&gt;<br>
<br>
time_t time(time_t *</tt><i>tloc</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_775_03"></a>DESCRIPTION</h4>
<blockquote>
<div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between the
requirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers to
the ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div>
<p>The <i>time</i>() function shall return the value of time <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src=
"../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;in seconds since the Epoch. <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></p>
<p>The <i>tloc</i> argument points to an area where the return value is also stored. If <i>tloc</i> is a null pointer, no value is
stored.</p>
</blockquote>
<h4><a name="tag_03_775_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful completion, <i>time</i>() shall return the value of time. Otherwise, (<b>time_t</b>)-1 shall be returned.</p>
</blockquote>
<h4><a name="tag_03_775_05"></a>ERRORS</h4>
<blockquote>
<p>No errors are defined.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_775_06"></a>EXAMPLES</h4>
<blockquote>
<h5><a name="tag_03_775_06_01"></a>Getting the Current Time</h5>
<p>The following example uses the <i>time</i>() function to calculate the time elapsed, in seconds, since the Epoch, <a href=
"../functions/localtime.html"><i>localtime</i>()</a> to convert that value to a broken-down time, and <a href=
"../functions/asctime.html"><i>asctime</i>()</a> to convert the broken-down time values into a printable string.</p>
<pre>
<tt>#include &lt;stdio.h&gt;
#include &lt;time.h&gt;
<br>
int main(void)
{
time_t result;
<br>
result = time(NULL);
printf("%s%ju secs since the Epoch\n",
asctime(localtime(&amp;result)),
(uintmax_t)result);
return(0);
}
</tt>
</pre>
<p>This example writes the current time to <i>stdout</i> in a form like this:</p>
<pre>
<tt>Wed Jun 26 10:32:15 1996
835810335 secs since the Epoch
</tt>
</pre>
<h5><a name="tag_03_775_06_02"></a>Timing an Event</h5>
<p>The following example gets the current time, prints it out in the user's format, and prints the number of minutes to an event
being timed.</p>
<pre>
<tt>#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
...
time_t now;
int minutes_to_event;
...
time(&amp;now);
minutes_to_event = ...;
printf("The time is ");
puts(asctime(localtime(&amp;now)));
printf("There are %d minutes to the event.\n",
minutes_to_event);
...
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_775_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_775_08"></a>RATIONALE</h4>
<blockquote>
<p>The <i>time</i>() function returns a value in seconds (type <b>time_t</b>) while <a href=
"../functions/times.html"><i>times</i>()</a> returns a set of values in clock ticks (type <b>clock_t</b>). Some historical
implementations, such as 4.3 BSD, have mechanisms capable of returning more precise times (see below). A generalized timing scheme
to unify these various timing mechanisms has been proposed but not adopted.</p>
<p>Implementations in which <b>time_t</b> is a 32-bit signed integer (many historical implementations) fail in the year 2038.
IEEE&nbsp;Std&nbsp;1003.1-2001 does not address this problem. However, the use of the <b>time_t</b> type is mandated in order to
ease the eventual fix.</p>
<p>The use of the <a href="../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a> header instead of <a href=
"../basedefs/sys/types.h.html"><i>&lt;sys/types.h&gt;</i></a> allows compatibility with the ISO&nbsp;C standard.</p>
<p>Many historical implementations (including Version 7) and the 1984 /usr/group standard use <b>long</b> instead of <b>time_t</b>.
This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 uses the latter type in order to agree with the ISO&nbsp;C standard.</p>
<p>4.3 BSD includes <i>time</i>() only as an alternate function to the more flexible <a href=
"../functions/gettimeofday.html"><i>gettimeofday</i>()</a> function.</p>
</blockquote>
<h4><a name="tag_03_775_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>In a future version of this volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <b>time_t</b> is likely to be required to be capable of
representing times far in the future. Whether this will be mandated as a 64-bit type or a requirement that a specific date in the
future be representable (for example, 10000 AD) is not yet determined. Systems purchased after the approval of this volume of
IEEE&nbsp;Std&nbsp;1003.1-2001 should be evaluated to determine whether their lifetime will extend past 2038.</p>
</blockquote>
<h4><a name="tag_03_775_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="asctime.html"><i>asctime</i>()</a> , <a href="clock.html"><i>clock</i>()</a> , <a href="ctime.html"><i>ctime</i>()</a>
, <a href="difftime.html"><i>difftime</i>()</a> , <a href="gettimeofday.html"><i>gettimeofday</i>()</a> , <a href=
"gmtime.html"><i>gmtime</i>()</a> , <a href="localtime.html"><i>localtime</i>()</a> , <a href="mktime.html"><i>mktime</i>()</a> ,
<a href="strftime.html"><i>strftime</i>()</a> , <a href="strptime.html"><i>strptime</i>()</a> , <a href=
"utime.html"><i>utime</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_775_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<h4><a name="tag_03_775_12"></a>Issue 6</h4>
<blockquote>
<p>Extensions beyond the ISO&nbsp;C standard are marked.</p>
<p>The EXAMPLES, RATIONALE, and FUTURE DIRECTIONS sections are added.</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>