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