Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / OS_NS_time.cpp
blob3303e0588b401a031f6c636311148e287a453bad
1 #include "ace/OS_NS_time.h"
3 #if !defined (ACE_HAS_INLINED_OSCALLS)
4 # include "ace/OS_NS_time.inl"
5 #endif /* ACE_HAS_INLINED_OSCALLS */
7 #if defined (ACE_LACKS_STRPTIME)
8 # include "ace/os_include/os_ctype.h"
9 #endif /* ACE_LACKS_STRPTIME */
11 #include "ace/OS_NS_Thread.h"
12 #include "ace/Object_Manager_Base.h"
14 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
16 struct tm *
17 ACE_OS::localtime_r (const time_t *t, struct tm *res)
19 ACE_OS_TRACE ("ACE_OS::localtime_r");
20 #if defined (ACE_HAS_TR24731_2005_CRT)
21 ACE_SECURECRTCALL (localtime_s (res, t), struct tm *, 0, res);
22 return res;
23 #elif defined (ACE_LACKS_LOCALTIME_R)
24 ACE_OS_GUARD
26 ACE_UNUSED_ARG (res);
27 struct tm * res_ptr = 0;
28 ACE_OSCALL (::localtime (t), struct tm *, res_ptr);
29 if (res_ptr == 0)
30 return 0;
31 else
33 *res = *res_ptr;
34 return res;
36 #else
37 return ace_localtime_r_helper (t, res);
38 #endif /* ACE_HAS_TR24731_2005_CRT */
41 time_t
42 ACE_OS::mktime (struct tm *t)
44 ACE_OS_TRACE ("ACE_OS::mktime");
45 #if defined (ACE_HAS_THREADS) && !defined (ACE_HAS_MT_SAFE_MKTIME)
46 ACE_OS_GUARD
47 #endif /* ACE_HAS_THREADS && ! ACE_HAS_MT_SAFE_MKTIME */
49 return std::mktime (t);
52 #if defined (ACE_LACKS_STRPTIME)
53 char *
54 ACE_OS::strptime_emulation (const char *buf, const char *format, struct tm *tm)
56 int bi = 0;
57 int fi = 0;
58 bool percent = false;
60 if (!buf || !format)
61 return 0;
63 while (format[fi] != '\0')
65 if (percent)
67 percent = false;
68 switch (format[fi])
70 case '%': // an escaped %
71 if (buf[bi] == '%')
73 ++fi;
74 ++bi;
76 else
77 return const_cast<char*> (buf + bi);
78 break;
80 /* not supported yet: weekday via locale long/short names
81 case 'a': / * weekday via locale * /
82 / * FALL THROUGH * /
83 case 'A': / * long/short names * /
84 break;
87 /* not supported yet:
88 case 'b': / * month via locale * /
89 / * FALL THROUGH * /
90 case 'B': / * long/short names * /
91 / * FALL THROUGH * /
92 case 'h':
93 break;
96 /* not supported yet:
97 case 'c': / * %x %X * /
98 break;
101 /* not supported yet:
102 case 'C': / * date & time - * /
103 / * locale long format * /
104 break;
107 case 'd': /* day of month (1-31) */
108 /* FALL THROUGH */
109 case 'e':
110 if (!ACE_OS::strptime_getnum
111 (buf + bi, &tm->tm_mday, &bi, &fi, 1, 31))
112 return const_cast<char*> (buf + bi);
114 break;
116 case 'D': /* %m/%d/%y */
117 if (!ACE_OS::strptime_getnum
118 (buf + bi, &tm->tm_mon, &bi, &fi, 1, 12))
119 return const_cast<char*> (buf + bi);
121 --fi;
122 tm->tm_mon--;
124 if (buf[bi] != '/')
125 return const_cast<char*> (buf + bi);
127 ++bi;
129 if (!ACE_OS::strptime_getnum
130 (buf + bi, &tm->tm_mday, &bi, &fi, 1, 31))
131 return const_cast<char*> (buf + bi);
133 --fi;
134 if (buf[bi] != '/')
135 return const_cast<char*> (buf + bi);
136 ++bi;
137 if (!ACE_OS::strptime_getnum
138 (buf + bi, &tm->tm_year, &bi, &fi, 0, 99))
139 return const_cast<char*> (buf + bi);
140 if (tm->tm_year < 69)
141 tm->tm_year += 100;
142 break;
144 case 'H': /* hour (0-23) */
145 /* FALL THROUGH */
146 case 'k':
147 if (!ACE_OS::strptime_getnum
148 (buf + bi, &tm->tm_hour, &bi, &fi, 0, 23))
149 return const_cast<char*> (buf + bi);
150 break;
152 /* not supported yet:
153 case 'I': / * hour (0-12) * /
154 / * FALL THROUGH * /
155 case 'l':
156 break;
159 case 'j': /* day of year (0-366) */
160 if (!ACE_OS::strptime_getnum
161 (buf + bi, &tm->tm_yday, &bi, &fi, 1, 366))
162 return const_cast<char*> (buf + bi);
164 tm->tm_yday--;
165 break;
167 case 'm': /* an escaped % */
168 if (!ACE_OS::strptime_getnum
169 (buf + bi, &tm->tm_mon, &bi, &fi, 1, 12))
170 return const_cast<char*> (buf + bi);
172 tm->tm_mon--;
173 break;
175 case 'M': /* minute (0-59) */
176 if (!ACE_OS::strptime_getnum
177 (buf + bi, &tm->tm_min, &bi, &fi, 0, 59))
178 return const_cast<char*> (buf + bi);
180 break;
182 /* not supported yet:
183 case 'p': / * am or pm for locale * /
184 break;
187 /* not supported yet:
188 case 'r': / * %I:%M:%S %p * /
189 break;
192 case 'R': /* %H:%M */
193 if (!ACE_OS::strptime_getnum
194 (buf + bi, &tm->tm_hour, &bi, &fi, 0, 23))
195 return const_cast<char*> (buf + bi);
197 --fi;
198 if (buf[bi] != ':')
199 return const_cast<char*> (buf + bi);
200 ++bi;
201 if (!ACE_OS::strptime_getnum
202 (buf + bi, &tm->tm_min, &bi, &fi, 0, 59))
203 return const_cast<char*> (buf + bi);
205 break;
207 case 'S': /* seconds (0-61) */
208 if (!ACE_OS::strptime_getnum
209 (buf + bi, &tm->tm_sec, &bi, &fi, 0, 61))
210 return const_cast<char*> (buf + bi);
211 break;
213 case 'T': /* %H:%M:%S */
214 if (!ACE_OS::strptime_getnum
215 (buf + bi, &tm->tm_hour, &bi, &fi, 0, 23))
216 return const_cast<char*> (buf + bi);
218 --fi;
219 if (buf[bi] != ':')
220 return const_cast<char*> (buf + bi);
221 ++bi;
222 if (!ACE_OS::strptime_getnum
223 (buf + bi, &tm->tm_min, &bi, &fi, 0, 59))
224 return const_cast<char*> (buf + bi);
226 --fi;
227 if (buf[bi] != ':')
228 return const_cast<char*> (buf + bi);
229 ++bi;
230 if (!ACE_OS::strptime_getnum
231 (buf + bi, &tm->tm_sec, &bi, &fi, 0, 61))
232 return const_cast<char*> (buf + bi);
234 break;
236 case 'w': /* day of week (0=Sun-6) */
237 if (!ACE_OS::strptime_getnum
238 (buf + bi, &tm->tm_wday, &bi, &fi, 0, 6))
239 return const_cast<char*> (buf + bi);
241 break;
243 /* not supported yet: date, based on locale
244 case 'x': / * date, based on locale * /
245 break;
248 /* not supported yet:
249 case 'X': / * time, based on locale * /
250 break;
253 case 'y': /* the year - 1900 (0-99) */
254 if (!ACE_OS::strptime_getnum
255 (buf + bi, &tm->tm_year, &bi, &fi, 0, 99))
256 return const_cast<char*> (buf + bi);
258 if (tm->tm_year < 69)
259 tm->tm_year += 100;
260 break;
262 case 'Y': /* the full year (1999) */
263 if (!ACE_OS::strptime_getnum
264 (buf + bi, &tm->tm_year, &bi, &fi, 0, 0))
265 return const_cast<char*> (buf + bi);
267 tm->tm_year -= 1900;
268 break;
270 default: /* unrecognised */
271 return const_cast<char*> (buf + bi);
272 } /* switch (format[fi]) */
275 else
276 { /* if (percent) */
277 if (format[fi] == '%')
279 percent = true;
280 ++fi;
282 else
284 if (format[fi] == buf[bi])
286 ++fi;
287 ++bi;
289 else
290 return const_cast<char*> (buf + bi);
292 } /* if (percent) */
293 } /* while (format[fi] */
295 return const_cast<char*> (buf + bi);
299 ACE_OS::strptime_getnum (const char *buf,
300 int *num,
301 int *bi,
302 int *fi,
303 int min,
304 int max)
306 int i = 0, tmp = 0;
308 while (isdigit (buf[i]))
310 tmp = (tmp * 10) + (buf[i] - '0');
311 if (max && (tmp > max))
312 return 0;
313 ++i;
316 if (tmp < min)
317 return 0;
318 else if (i)
320 *num = tmp;
321 (*fi)++;
322 *bi += i;
323 return 1;
325 else
326 return 0;
328 #endif /* ACE_LACKS_STRPTIME */
330 ACE_END_VERSIONED_NAMESPACE_DECL