📄 time.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-2004 IEEE and 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, 2004 Edition<br>Copyright © 2001-2004 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 <<a href="../basedefs/time.h.html">time.h</a>><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 C standard. Any conflict between therequirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers tothe ISO 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"> 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 isstored.</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 <stdio.h>#include <time.h><br>int main(void){time_t result;<br> result = time(NULL); printf("%s%ju secs since the Epoch\n", asctime(localtime(&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 1996835810335 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 eventbeing timed.</p><pre><tt>#include <time.h>#include <stdio.h>...time_t now;int minutes_to_event;...time(&now);minutes_to_event = ...;printf("The time is ");puts(asctime(localtime(&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 historicalimplementations, such as 4.3 BSD, have mechanisms capable of returning more precise times (see below). A generalized timing schemeto 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 Std 1003.1-2001 does not address this problem. However, the use of the <b>time_t</b> type is mandated in order toease the eventual fix.</p><p>The use of the <a href="../basedefs/time.h.html"><i><time.h></i></a> header instead of <a href="../basedefs/sys/types.h.html"><i><sys/types.h></i></a> allows compatibility with the ISO 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 Std 1003.1-2001 uses the latter type in order to agree with the ISO 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 Std 1003.1-2001, <b>time_t</b> is likely to be required to be capable ofrepresenting times far in the future. Whether this will be mandated as a 64-bit type or a requirement that a specific date in thefuture be representable (for example, 10000 AD) is not yet determined. Systems purchased after the approval of this volume ofIEEE Std 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 Std 1003.1-2001, <a href="../basedefs/time.h.html"><i><time.h></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 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 size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -