1 /** @file
2     System-specific declarations and macros related to time.
3 
4     Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5     This program and the accompanying materials are licensed and made available under
6     the terms and conditions of the BSD License that accompanies this distribution.
7     The full text of the license may be found at
8     http://opensource.org/licenses/bsd-license.
9 
10     THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11     WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13     Copyright (c) 1982, 1986, 1993
14     The Regents of the University of California.  All rights reserved.
15 
16     Redistribution and use in source and binary forms, with or without
17     modification, are permitted provided that the following conditions
18     are met:
19     1. Redistributions of source code must retain the above copyright
20       notice, this list of conditions and the following disclaimer.
21     2. Redistributions in binary form must reproduce the above copyright
22       notice, this list of conditions and the following disclaimer in the
23       documentation and/or other materials provided with the distribution.
24     3. Neither the name of the University nor the names of its contributors
25       may be used to endorse or promote products derived from this software
26       without specific prior written permission.
27 
28     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38     SUCH DAMAGE.
39 
40     time.h  8.5 (Berkeley) 5/4/95
41     NetBSD: time.h,v 1.56 2006/06/18 21:09:24 uwe Exp
42  */
43 #ifndef _SYS_TIME_H_
44 #define _SYS_TIME_H_
45 
46 #include  <Uefi.h>
47 #include  <sys/featuretest.h>
48 #include  <sys/types.h>
49 
50 /*
51  * Traditional *nix structure returned by gettimeofday(2) system call,
52  * and used in other calls.
53  */
54 struct timeval {
55   LONG32    tv_sec;   /* seconds */
56   LONG32    tv_usec;  /* and microseconds */
57 };
58 
59 /*
60  * Structure defined by POSIX.1b to be like a timeval.
61  * This works within EFI since the times really are time_t.
62  */
63 struct timespec {
64   time_t  tv_sec;   /* seconds */
65   LONG32  tv_nsec;  /* and nanoseconds */
66 };
67 
68 #define TIMEVAL_TO_TIMESPEC(tv, ts) do {        \
69   (ts)->tv_sec = (tv)->tv_sec;          \
70   (ts)->tv_nsec = (tv)->tv_usec * 1000;       \
71 } while (/*CONSTCOND*/0)
72 
73 #define TIMESPEC_TO_TIMEVAL(tv, ts) do {        \
74   (tv)->tv_sec = (ts)->tv_sec;          \
75   (tv)->tv_usec = (ts)->tv_nsec / 1000;       \
76 } while (/*CONSTCOND*/0)
77 
78 /* Operations on timevals. */
79 #define timerclear(tvp)   (tvp)->tv_sec = (tvp)->tv_usec = 0
80 #define timerisset(tvp)   ((tvp)->tv_sec || (tvp)->tv_usec)
81 
82 #define timercmp(tvp, uvp, cmp)           \
83   (((tvp)->tv_sec == (uvp)->tv_sec) ?       \
84       ((tvp)->tv_usec cmp (uvp)->tv_usec) :     \
85       ((tvp)->tv_sec cmp (uvp)->tv_sec))
86 
87 #define timeradd(tvp, uvp, vvp)           \
88   do {                \
89     (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;    \
90     (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
91     if ((vvp)->tv_usec >= 1000000) {      \
92       (vvp)->tv_sec++;        \
93       (vvp)->tv_usec -= 1000000;      \
94     }             \
95   } while (/* CONSTCOND */ 0)
96 
97 #define timersub(tvp, uvp, vvp)           \
98   do {                \
99     (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;    \
100     (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
101     if ((vvp)->tv_usec < 0) {       \
102       (vvp)->tv_sec--;        \
103       (vvp)->tv_usec += 1000000;      \
104     }             \
105   } while (/* CONSTCOND */ 0)
106 
107 /* Operations on timespecs. */
108 #define timespecclear(tsp)    (tsp)->tv_sec = (tsp)->tv_nsec = 0
109 #define timespecisset(tsp)    ((tsp)->tv_sec || (tsp)->tv_nsec)
110 
111 #define timespeccmp(tsp, usp, cmp)          \
112   (((tsp)->tv_sec == (usp)->tv_sec) ?       \
113       ((tsp)->tv_nsec cmp (usp)->tv_nsec) :     \
114       ((tsp)->tv_sec cmp (usp)->tv_sec))
115 
116 #define timespecadd(tsp, usp, vsp)          \
117   do {                \
118     (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;    \
119     (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
120     if ((vsp)->tv_nsec >= 1000000000L) {      \
121       (vsp)->tv_sec++;        \
122       (vsp)->tv_nsec -= 1000000000L;      \
123     }             \
124   } while (/* CONSTCOND */ 0)
125 
126 #define timespecsub(tsp, usp, vsp)          \
127   do {                \
128     (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;    \
129     (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
130     if ((vsp)->tv_nsec < 0) {       \
131       (vsp)->tv_sec--;        \
132       (vsp)->tv_nsec += 1000000000L;      \
133     }             \
134   } while (/* CONSTCOND */ 0)
135 
136 /*
137  * Names of the interval timers, and structure
138  * defining a timer setting.
139  */
140 #define ITIMER_REAL     0
141 #define ITIMER_VIRTUAL  1
142 #define ITIMER_PROF     2
143 
144 struct  itimerval {
145   struct  timeval it_interval;  /* timer interval */
146   struct  timeval it_value; /* current value */
147 };
148 
149 /*
150  * Structure defined by POSIX.1b to be like a itimerval, but with
151  * timespecs. Used in the timer_*() system calls.
152  */
153 struct  itimerspec {
154   struct  timespec it_interval;
155   struct  timespec it_value;
156 };
157 
158 #define CLOCK_REALTIME  0
159 #define CLOCK_VIRTUAL   1
160 #define CLOCK_PROF      2
161 #define CLOCK_MONOTONIC 3
162 
163 #define TIMER_RELTIME   0x0 /* relative timer */
164 #define TIMER_ABSTIME   0x1 /* absolute timer */
165 
166 #if 0
167   #if (_POSIX_C_SOURCE - 0) >= 200112L || \
168       (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
169       (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
170     #include  <sys/select.h>
171   #endif
172 #endif  /* if 0 */
173 
174 #include  <sys/EfiCdefs.h>
175 #include  <time.h>
176 
177 /* Functions useful for dealing with EFI */
178 __BEGIN_DECLS
179 
180 /* Convert an EFI_TIME structure into a time_t value. */
181 time_t  Efi2Time( EFI_TIME *EfiBDtime);
182 
183 /* Convert a time_t value into an EFI_TIME structure.
184     It is the caller's responsibility to free the returned structure.
185 */
186 EFI_TIME *  Time2Efi(time_t OTime);
187 
188 /* Convert an EFI_TIME structure into a C Standard tm structure. */
189 void    Efi2Tm( EFI_TIME *EfiBDtime, struct tm *NewTime);
190 void    Tm2Efi( struct tm *BdTime, EFI_TIME *ETime);
191 
192 /* BSD compatibility functions */
193 int gettimeofday (struct timeval *tp, void *ignore);
194 /* POSIX compatibility functions */
195 int getitimer (int which, struct itimerval *value);
196 int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue);
197 
198 __END_DECLS
199 
200 #endif /* !_SYS_TIME_H_ */
201