1TIME(P) POSIX Programmer's Manual TIME(P)
2
3
4
6 time - get time
7
9 #include <time.h>
10
11 time_t time(time_t *tloc);
12
13
15 The time() function shall return the value of time in seconds since
16 the Epoch.
17
18 The tloc argument points to an area where the return value is also
19 stored. If tloc is a null pointer, no value is stored.
20
22 Upon successful completion, time() shall return the value of time. Oth‐
23 erwise, (time_t)-1 shall be returned.
24
26 No errors are defined.
27
28 The following sections are informative.
29
31 Getting the Current Time
32 The following example uses the time() function to calculate the time
33 elapsed, in seconds, since the Epoch, localtime() to convert that value
34 to a broken-down time, and asctime() to convert the broken-down time
35 values into a printable string.
36
37
38 #include <stdio.h>
39 #include <time.h>
40
41
42 int main(void)
43 {
44 time_t result;
45
46
47 result = time(NULL);
48 printf("%s%ju secs since the Epoch\n",
49 asctime(localtime(&result)),
50 (uintmax_t)result);
51 return(0);
52 }
53
54 This example writes the current time to stdout in a form like this:
55
56
57 Wed Jun 26 10:32:15 1996
58 835810335 secs since the Epoch
59
60 Timing an Event
61 The following example gets the current time, prints it out in the
62 user's format, and prints the number of minutes to an event being
63 timed.
64
65
66 #include <time.h>
67 #include <stdio.h>
68 ...
69 time_t now;
70 int minutes_to_event;
71 ...
72 time(&now);
73 minutes_to_event = ...;
74 printf("The time is ");
75 puts(asctime(localtime(&now)));
76 printf("There are %d minutes to the event.\n",
77 minutes_to_event);
78 ...
79
81 None.
82
84 The time() function returns a value in seconds (type time_t) while
85 times() returns a set of values in clock ticks (type clock_t). Some
86 historical implementations, such as 4.3 BSD, have mechanisms capable of
87 returning more precise times (see below). A generalized timing scheme
88 to unify these various timing mechanisms has been proposed but not
89 adopted.
90
91 Implementations in which time_t is a 32-bit signed integer (many his‐
92 torical implementations) fail in the year 2038. IEEE Std 1003.1-2001
93 does not address this problem. However, the use of the time_t type is
94 mandated in order to ease the eventual fix.
95
96 The use of the <time.h> header instead of <sys/types.h> allows compati‐
97 bility with the ISO C standard.
98
99 Many historical implementations (including Version 7) and the 1984
100 /usr/group standard use long instead of time_t. This volume of
101 IEEE Std 1003.1-2001 uses the latter type in order to agree with the
102 ISO C standard.
103
104 4.3 BSD includes time() only as an alternate function to the more flex‐
105 ible gettimeofday() function.
106
108 In a future version of this volume of IEEE Std 1003.1-2001, time_t is
109 likely to be required to be capable of representing times far in the
110 future. Whether this will be mandated as a 64-bit type or a requirement
111 that a specific date in the future be representable (for example, 10000
112 AD) is not yet determined. Systems purchased after the approval of
113 this volume of IEEE Std 1003.1-2001 should be evaluated to determine
114 whether their lifetime will extend past 2038.
115
117 asctime() , clock() , ctime() , difftime() , gettimeofday() , gmtime()
118 , localtime() , mktime() , strftime() , strptime() , utime() , the Base
119 Definitions volume of IEEE Std 1003.1-2001, <time.h>
120
122 Portions of this text are reprinted and reproduced in electronic form
123 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
124 -- Portable Operating System Interface (POSIX), The Open Group Base
125 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
126 Electrical and Electronics Engineers, Inc and The Open Group. In the
127 event of any discrepancy between this version and the original IEEE and
128 The Open Group Standard, the original IEEE and The Open Group Standard
129 is the referee document. The original Standard can be obtained online
130 at http://www.opengroup.org/unix/online.html .
131
132
133
134IEEE/The Open Group 2003 TIME(P)