1 /*-
2 ***********************************************************************
3 *
4 * $Id: time.c,v 1.18 2014/07/18 06:40:44 mavrik Exp $
5 *
6 ***********************************************************************
7 *
8 * Copyright 2000-2014 The FTimes Project, All Rights Reserved.
9 *
10 ***********************************************************************
11 */
12 #include "all-includes.h"
13
14 /*-
15 ***********************************************************************
16 *
17 * TimeGetTimeValue
18 *
19 ***********************************************************************
20 */
21 int
TimeGetTimeValue(struct timeval * psTimeValue)22 TimeGetTimeValue(struct timeval *psTimeValue)
23 {
24 #ifdef WIN32
25 FILETIME sFileTimeNow = { 0 };
26 __int64 i64Now = 0;
27 #endif
28
29 psTimeValue->tv_sec = 0;
30 psTimeValue->tv_usec = 0;
31 #ifdef WIN32
32 GetSystemTimeAsFileTime(&sFileTimeNow);
33 i64Now = (((__int64) sFileTimeNow.dwHighDateTime) << 32) | sFileTimeNow.dwLowDateTime;
34 i64Now -= UNIX_EPOCH_IN_NT_TIME;
35 i64Now /= 10;
36 psTimeValue->tv_sec = (long) (i64Now / 1000000);
37 psTimeValue->tv_usec = (long) (i64Now % 1000000);
38 return (ER_OK);
39 #else
40 return gettimeofday(psTimeValue, NULL);
41 #endif
42 }
43
44
45 /*-
46 ***********************************************************************
47 *
48 * TimeGetTimeValueAsDouble
49 *
50 ***********************************************************************
51 */
52 double
TimeGetTimeValueAsDouble(void)53 TimeGetTimeValueAsDouble(void)
54 {
55 struct timeval sTimeValue;
56
57 TimeGetTimeValue(&sTimeValue);
58 return (double) sTimeValue.tv_sec + (double) sTimeValue.tv_usec * 0.000001;
59 }
60
61
62 /*-
63 ***********************************************************************
64 *
65 * TimeGetTime
66 *
67 ***********************************************************************
68 */
69 time_t
TimeGetTime(char * pcDate,char * pcTime,char * pcZone,char * pcDateTime)70 TimeGetTime(char *pcDate, char *pcTime, char *pcZone, char *pcDateTime)
71 {
72 int iCount;
73 time_t timeValue;
74
75 if ((timeValue = time(NULL)) < 0)
76 {
77 return ER;
78 }
79
80 if (pcTime != NULL)
81 {
82 iCount = strftime(pcTime, FTIMES_TIME_SIZE, FTIMES_RUNTIME_FORMAT, localtime(&timeValue));
83 if (iCount < 0 || iCount > FTIMES_TIME_SIZE - 1)
84 {
85 return ER;
86 }
87 }
88
89 if (pcDate != NULL)
90 {
91 iCount = strftime(pcDate, FTIMES_TIME_SIZE, FTIMES_RUNDATE_FORMAT, localtime(&timeValue));
92 if (iCount < 0 || iCount > FTIMES_TIME_SIZE - 1)
93 {
94 return ER;
95 }
96 }
97
98 if (pcZone != NULL)
99 {
100 iCount = strftime(pcZone, FTIMES_ZONE_SIZE, FTIMES_RUNZONE_FORMAT, localtime(&timeValue));
101 if (iCount < 0 || iCount > FTIMES_ZONE_SIZE - 1)
102 {
103 return ER;
104 }
105 }
106
107 /*-
108 *********************************************************************
109 *
110 * As a matter of convention, datetime values are in GMT. This
111 * prevents confusion with filenames and Integrity Server
112 * transactions. If localtime was used instead, then issues with
113 * things like Daylight Savings Time can arise. For example, when
114 * Daylight Savings Time falls back, it is possible to clobber
115 * old output files because there would be an hour of time for
116 * which output filenames could match existing names.
117 *
118 *********************************************************************
119 */
120 if (pcDateTime != NULL)
121 {
122 iCount = strftime(pcDateTime, FTIMES_TIME_SIZE, "%Y%m%d%H%M%S", gmtime(&timeValue));
123 if (iCount < 0 || iCount > FTIMES_TIME_SIZE - 1)
124 {
125 return ER;
126 }
127 }
128
129 return timeValue;
130 }
131
132
133 #ifdef UNIX
134 /*-
135 ***********************************************************************
136 *
137 * TimeFormatTime
138 *
139 ***********************************************************************
140 */
141 int
TimeFormatTime(time_t * pTimeValue,char * pcTime)142 TimeFormatTime(time_t *pTimeValue, char *pcTime)
143 {
144 int iCount;
145
146 /*-
147 *********************************************************************
148 *
149 * Constraint all time stamps are relative to GMT. In practice, this
150 * means we use gmtime instead of localtime.
151 *
152 *********************************************************************
153 */
154
155 pcTime[0] = 0;
156
157 iCount = strftime(pcTime, FTIMES_TIME_SIZE, FTIMES_TIME_FORMAT, gmtime(pTimeValue));
158
159 if (iCount != FTIMES_TIME_FORMAT_SIZE - 1)
160 {
161 return ER;
162 }
163
164 return ER_OK;
165 }
166 #endif
167
168
169 #ifdef WIN32
170 /*-
171 ***********************************************************************
172 *
173 * TimeFormatTime
174 *
175 ***********************************************************************
176 */
177 int
TimeFormatTime(FILETIME * psFileTime,char * pcTime)178 TimeFormatTime(FILETIME *psFileTime, char *pcTime)
179 {
180 int iCount;
181 SYSTEMTIME sSystemTime;
182
183 pcTime[0] = 0;
184
185 if (!FileTimeToSystemTime(psFileTime, &sSystemTime))
186 {
187 return ER;
188 }
189
190 iCount = snprintf(pcTime, FTIMES_TIME_FORMAT_SIZE, FTIMES_TIME_FORMAT,
191 sSystemTime.wYear, sSystemTime.wMonth, sSystemTime.wDay,
192 sSystemTime.wHour, sSystemTime.wMinute, sSystemTime.wSecond,
193 sSystemTime.wMilliseconds);
194
195 if (iCount < FTIMES_TIME_FORMAT_SIZE - 3 || iCount > FTIMES_TIME_FORMAT_SIZE - 1)
196 {
197 return ER;
198 }
199
200 return ER_OK;
201 }
202
203
204 /*-
205 ***********************************************************************
206 *
207 * TimeFormatOutOfBandTime
208 *
209 ***********************************************************************
210 */
211 int
TimeFormatOutOfBandTime(FILETIME * psFileTime,char * pcTime)212 TimeFormatOutOfBandTime(FILETIME *psFileTime, char *pcTime)
213 {
214 int iCount;
215 SYSTEMTIME sSystemTime;
216
217 pcTime[0] = 0;
218
219 if (!FileTimeToSystemTime(psFileTime, &sSystemTime))
220 {
221 return ER;
222 }
223
224 iCount = snprintf(pcTime, FTIMES_OOB_TIME_FORMAT_SIZE, FTIMES_OOB_TIME_FORMAT,
225 sSystemTime.wYear, sSystemTime.wMonth, sSystemTime.wDay,
226 sSystemTime.wHour, sSystemTime.wMinute, sSystemTime.wSecond,
227 sSystemTime.wMilliseconds);
228
229 if (iCount < FTIMES_OOB_TIME_FORMAT_SIZE - 3 || iCount > FTIMES_OOB_TIME_FORMAT_SIZE - 1)
230 {
231 return ER;
232 }
233
234 return ER_OK;
235 }
236 #endif
237