1 /**
2 * @file mprTime.c
3 * @brief Time handling for Windows
4 * @overview
5 */
6
7 /*
8 * @copy default
9 *
10 * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
11 *
12 * This software is distributed under commercial and open source licenses.
13 * You may use the GPL open source license described below or you may acquire
14 * a commercial license from Mbedthis Software. You agree to be fully bound
15 * by the terms of either license. Consult the LICENSE.TXT distributed with
16 * this software for full details.
17 *
18 * This software is open source; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2 of the License, or (at your
21 * option) any later version. See the GNU General Public License for more
22 * details at: http://www.mbedthis.com/downloads/gplLicense.html
23 *
24 * This program is distributed WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * This GPL license does NOT permit incorporating this software into
28 * proprietary programs. If you are unable to comply with the GPL, you must
29 * acquire a commercial license to use this software. Commercial licenses
30 * for this software and support services are available from Mbedthis
31 * Software at http://www.mbedthis.com
32 *
33 * @end
34 */
35
36 /********************************* Includes ***********************************/
37
38 #include "mpr.h"
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 /************************************ Code ************************************/
45 /*
46 * Returns time in seconds and milliseconds. This is NOT time-of-day.
47 */
48
mprGetTime(MprCtx ctx,MprTime * tp)49 MprTime *mprGetTime(MprCtx ctx, MprTime *tp)
50 {
51 FILETIME fileTime;
52 int64 now, base;
53
54 GetSystemTimeAsFileTime(&fileTime);
55
56 now = ((((int64) fileTime.dwHighDateTime) << BITS(uint)) +
57 ((int64) fileTime.dwLowDateTime));
58
59 /*
60 * Convert from 100-nanosec units to milliseconds
61 */
62 now = (now / 10000);
63
64 /*
65 * Adjust to be seconds since Jan 1 1970. Do this to be consistent with
66 * UNIX but not really required by the API definition.
67 */
68 base = ((UINT64(365) * 86400 * (1970 - 1601)) * 1000);
69 now -= base;
70 tp->sec = (uint) (now / 1000);
71 tp->msec = (uint) (now % 1000);
72
73 #if UNUSED
74 {
75 static int64 start;
76
77 if (start == 0) {
78 start = now;
79 }
80 if (now < start) {
81 mprLog(ctx, 0, "TIME WENT BACKWARDS");
82 mprLog(ctx, 0, "start %Ld", start);
83 mprLog(ctx, 0, "now %Ld", now);
84 }
85 mprLog(ctx, 0, "getTime %Ld", now);
86 start = now;
87 }
88 #endif
89
90 return tp;
91 }
92
93 /******************************************************************************/
94 /*
95 * Thread-safe wrapping of localtime
96 */
97
mprLocaltime(MprCtx ctx,struct tm * timep,time_t * now)98 struct tm *mprLocaltime(MprCtx ctx, struct tm *timep, time_t *now)
99 {
100 struct tm *tbuf;
101 mprGlobalLock(ctx);
102 tbuf = localtime(now);
103 *timep = *tbuf;
104 mprGlobalUnlock(ctx);
105
106 return timep;
107 }
108
109 /******************************************************************************/
110 /*
111 * Thread-safe wrapping of gmtime
112 */
113
mprGmtime(MprCtx ctx,time_t * now,struct tm * timep)114 struct tm *mprGmtime(MprCtx ctx, time_t *now, struct tm *timep)
115 {
116 struct tm *tbuf;
117 tbuf = gmtime(now);
118 *timep = *tbuf;
119
120 return timep;
121 }
122
123 /******************************************************************************/
124 /*
125 * Thread-safe wrapping of ctime
126 */
127
mprCtime(MprCtx ctx,char * buf,int bufsize,const time_t * timer)128 int mprCtime(MprCtx ctx, char *buf, int bufsize, const time_t *timer)
129 {
130 char *cp;
131 int len;
132
133 mprAssert(buf);
134
135 mprGlobalLock(ctx);
136
137 cp = ctime(timer);
138 if ((int) strlen(cp) >= bufsize) {
139 mprStrcpy(buf, bufsize, "WONT FIT");
140 mprAssert(0);
141 mprGlobalUnlock(ctx);
142 return MPR_ERR_WONT_FIT;
143 }
144
145 len = mprStrcpy(buf, bufsize, cp);
146 if (buf[len - 1] == '\n') {
147 buf[len - 1] = '\0';
148 }
149
150 mprGlobalUnlock(ctx);
151
152 return 0;
153 }
154
155 /******************************************************************************/
156 /*
157 * Thread-safe wrapping of asctime
158 */
159
mprAsctime(MprCtx ctx,char * buf,int bufsize,const struct tm * timeptr)160 int mprAsctime(MprCtx ctx, char *buf, int bufsize, const struct tm *timeptr)
161 {
162 char *cp;
163
164 mprAssert(buf);
165 mprGlobalLock(ctx);
166 cp = asctime(timeptr);
167 if ((int) strlen(cp) >= bufsize) {
168 mprAssert(0);
169 mprGlobalUnlock(ctx);
170 return MPR_ERR_WONT_FIT;
171 }
172 mprStrcpy(buf, bufsize, cp);
173 mprGlobalUnlock(ctx);
174
175 return strlen(buf);
176 }
177
178 /******************************************************************************/
179
180 #ifdef __cplusplus
181 }
182 #endif
183
184 /*
185 * Local variables:
186 * tab-width: 4
187 * c-basic-offset: 4
188 * End:
189 * vim:tw=78
190 * vim600: sw=4 ts=4 fdm=marker
191 * vim<600: sw=4 ts=4
192 */
193