[6916] Fixed typos in spell checking code.
[getmangos.git] / dep / ACE_wrappers / ace / OS_NS_time.cpp
blobb68c657ed4c4183b62aee7cbaab3264ea8b0c2c7
1 // $Id: OS_NS_time.cpp 82693 2008-09-09 11:37:41Z johnnyw $
3 #include "ace/OS_NS_time.h"
5 ACE_RCSID(ace, OS_NS_time, "$Id: OS_NS_time.cpp 82693 2008-09-09 11:37:41Z johnnyw $")
7 #if !defined (ACE_HAS_INLINED_OSCALLS)
8 # include "ace/OS_NS_time.inl"
9 #endif /* ACE_HAS_INLINED_OSCALLS */
11 #if defined (ACE_LACKS_STRPTIME)
12 # include "ace/os_include/os_ctype.h"
13 #endif /* ACE_LACKS_STRPTIME */
15 #include "ace/OS_NS_Thread.h"
16 #include "ace/Object_Manager_Base.h"
18 #if defined (ACE_HAS_WINCE)
19 # include "ace/OS_NS_stdio.h" /* Need ACE_OS::sprintf() */
21 namespace
23 ACE_TCHAR const * const ACE_OS_day_of_week_name[] =
25 ACE_TEXT ("Sun"),
26 ACE_TEXT ("Mon"),
27 ACE_TEXT ("Tue"),
28 ACE_TEXT ("Wed"),
29 ACE_TEXT ("Thu"),
30 ACE_TEXT ("Fri"),
31 ACE_TEXT ("Sat")
34 ACE_TCHAR const * const ACE_OS_month_name[] =
36 ACE_TEXT ("Jan"),
37 ACE_TEXT ("Feb"),
38 ACE_TEXT ("Mar"),
39 ACE_TEXT ("Apr"),
40 ACE_TEXT ("May"),
41 ACE_TEXT ("Jun"),
42 ACE_TEXT ("Jul"),
43 ACE_TEXT ("Aug"),
44 ACE_TEXT ("Sep"),
45 ACE_TEXT ("Oct"),
46 ACE_TEXT ("Nov"),
47 ACE_TEXT ("Dec")
50 static ACE_TCHAR const ACE_OS_CTIME_R_FMTSTR[] = ACE_TEXT ("%3s %3s %02d %02d:%02d:%02d %04d\n");
51 } /* end blank namespace */
52 #endif /* ACE_HAS_WINCE */
54 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
56 # if defined (ACE_HAS_WINCE)
57 ACE_TCHAR *
58 ACE_OS::ctime_r (const time_t *clock, ACE_TCHAR *buf, int buflen)
60 // buflen must be at least 26 wchar_t long.
61 if (buflen < 26) // Again, 26 is a magic number.
63 errno = ERANGE;
64 return 0;
66 // This is really stupid, converting FILETIME to timeval back and
67 // forth. It assumes FILETIME and DWORDLONG are the same structure
68 // internally.
69 ULARGE_INTEGER _100ns;
70 _100ns.QuadPart = (DWORDLONG) *clock * 10000 * 1000
71 + ACE_Time_Value::FILETIME_to_timval_skew;
72 FILETIME file_time;
73 file_time.dwLowDateTime = _100ns.LowPart;
74 file_time.dwHighDateTime = _100ns.HighPart;
76 FILETIME localtime;
77 SYSTEMTIME systime;
78 FileTimeToLocalFileTime (&file_time, &localtime);
79 FileTimeToSystemTime (&localtime, &systime);
80 ACE_OS::sprintf (buf, ACE_OS_CTIME_R_FMTSTR,
81 ACE_OS_day_of_week_name[systime.wDayOfWeek],
82 ACE_OS_month_name[systime.wMonth - 1],
83 systime.wDay,
84 systime.wHour,
85 systime.wMinute,
86 systime.wSecond,
87 systime.wYear);
88 return buf;
90 # endif /* ACE_HAS_WINCE */
92 # if defined (ACE_LACKS_DIFFTIME)
93 double
94 ACE_OS::difftime (time_t t1, time_t t0)
96 /* return t1 - t0 in seconds */
97 struct tm tms[2], *ptms[2], temp;
98 double seconds;
99 int swap = 0;
101 /* extract the tm structure from time_t */
102 ptms[1] = ::gmtime_r (&t1, &tms[1]);
103 if (ptms[1] == 0) return 0.0;
105 ptms[0] = ::gmtime_r (&t0, &tms[0]);
106 if (ptms[0] == 0) return 0.0;
108 /* make sure t1 is > t0 */
109 if (tms[1].tm_year < tms[0].tm_year)
110 swap = 1;
111 else if (tms[1].tm_year == tms[0].tm_year)
113 if (tms[1].tm_yday < tms[0].tm_yday)
114 swap = 1;
115 else if (tms[1].tm_yday == tms[0].tm_yday)
117 if (tms[1].tm_hour < tms[0].tm_hour)
118 swap = 1;
119 else if (tms[1].tm_hour == tms[0].tm_hour)
121 if (tms[1].tm_min < tms[0].tm_min)
122 swap = 1;
123 else if (tms[1].tm_min == tms[0].tm_min)
125 if (tms[1].tm_sec < tms[0].tm_sec)
126 swap = 1;
132 if (swap)
133 temp = tms[0], tms[0] = tms[1], tms[1] = temp;
135 seconds = 0.0;
136 if (tms[1].tm_year > tms[0].tm_year)
138 // Accumulate the time until t[0] catches up to t[1]'s year.
139 seconds = 60 - tms[0].tm_sec;
140 tms[0].tm_sec = 0;
141 tms[0].tm_min += 1;
142 seconds += 60 * (60 - tms[0].tm_min);
143 tms[0].tm_min = 0;
144 tms[0].tm_hour += 1;
145 seconds += 60*60 * (24 - tms[0].tm_hour);
146 tms[0].tm_hour = 0;
147 tms[0].tm_yday += 1;
149 # define ISLEAPYEAR(y) ((y)&3u?0:(y)%25u?1:(y)/25u&12?0:1)
151 if (ISLEAPYEAR(tms[0].tm_year))
152 seconds += 60*60*24 * (366 - tms[0].tm_yday);
153 else
154 seconds += 60*60*24 * (365 - tms[0].tm_yday);
156 tms[0].tm_yday = 0;
157 tms[0].tm_year += 1;
159 while (tms[1].tm_year > tms[0].tm_year)
161 if (ISLEAPYEAR(tms[0].tm_year))
162 seconds += 60*60*24 * 366;
163 else
164 seconds += 60*60*24 * 365;
166 tms[0].tm_year += 1;
169 # undef ISLEAPYEAR
172 else
174 // Normalize
175 if (tms[1].tm_sec < tms[0].tm_sec)
177 if (tms[1].tm_min == 0)
179 if (tms[1].tm_hour == 0)
181 tms[1].tm_yday -= 1;
182 tms[1].tm_hour += 24;
184 tms[1].tm_hour -= 1;
185 tms[1].tm_min += 60;
187 tms[1].tm_min -= 1;
188 tms[1].tm_sec += 60;
190 tms[1].tm_sec -= tms[0].tm_sec;
192 if (tms[1].tm_min < tms[0].tm_min)
194 if (tms[1].tm_hour == 0)
196 tms[1].tm_yday -= 1;
197 tms[1].tm_hour += 24;
199 tms[1].tm_hour -= 1;
200 tms[1].tm_min += 60;
202 tms[1].tm_min -= tms[0].tm_min;
204 if (tms[1].tm_hour < tms[0].tm_hour)
206 tms[1].tm_yday -= 1;
207 tms[1].tm_hour += 24;
209 tms[1].tm_hour -= tms[0].tm_hour;
211 tms[1].tm_yday -= tms[0].tm_yday;
214 // accumulate the seconds
215 seconds += tms[1].tm_sec;
216 seconds += 60 * tms[1].tm_min;
217 seconds += 60*60 * tms[1].tm_hour;
218 seconds += 60*60*24 * tms[1].tm_yday;
220 return seconds;
222 # endif /* ACE_LACKS_DIFFTIME */
224 struct tm *
225 ACE_OS::localtime_r (const time_t *t, struct tm *res)
227 ACE_OS_TRACE ("ACE_OS::localtime_r");
228 #if defined (ACE_HAS_REENTRANT_FUNCTIONS)
229 # if defined (DIGITAL_UNIX)
230 ACE_OSCALL_RETURN (::_Plocaltime_r (t, res), struct tm *, 0);
231 # else
232 ACE_OSCALL_RETURN (::localtime_r (t, res), struct tm *, 0);
233 # endif /* DIGITAL_UNIX */
234 #elif defined (ACE_HAS_TR24731_2005_CRT)
235 ACE_SECURECRTCALL (localtime_s (res, t), struct tm *, 0, res);
236 return res;
237 #elif !defined (ACE_HAS_WINCE)
238 ACE_OS_GUARD
240 ACE_UNUSED_ARG (res);
241 struct tm * res_ptr = 0;
242 ACE_OSCALL (::localtime (t), struct tm *, 0, res_ptr);
243 if (res_ptr == 0)
244 return 0;
245 else
247 *res = *res_ptr;
248 return res;
250 #elif defined (ACE_HAS_WINCE)
251 // This is really stupid, converting FILETIME to timeval back and
252 // forth. It assumes FILETIME and DWORDLONG are the same structure
253 // internally.
255 TIME_ZONE_INFORMATION pTz;
257 const unsigned short int __mon_yday[2][13] =
259 /* Normal years. */
260 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
261 /* Leap years. */
262 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
265 ULARGE_INTEGER _100ns;
266 ::GetTimeZoneInformation (&pTz);
268 _100ns.QuadPart = (DWORDLONG) *t * 10000 * 1000 + ACE_Time_Value::FILETIME_to_timval_skew;
269 FILETIME file_time;
270 file_time.dwLowDateTime = _100ns.LowPart;
271 file_time.dwHighDateTime = _100ns.HighPart;
273 FILETIME localtime;
274 SYSTEMTIME systime;
275 FileTimeToLocalFileTime (&file_time, &localtime);
276 FileTimeToSystemTime (&localtime, &systime);
278 res->tm_hour = systime.wHour;
280 if(pTz.DaylightBias!=0)
281 res->tm_isdst = 1;
282 else
283 res->tm_isdst = 1;
285 int iLeap;
286 iLeap = (res->tm_year % 4 == 0 && (res->tm_year% 100 != 0 || res->tm_year % 400 == 0));
287 // based on leap select which group to use
289 res->tm_mday = systime.wDay;
290 res->tm_min = systime.wMinute;
291 res->tm_mon = systime.wMonth - 1;
292 res->tm_sec = systime.wSecond;
293 res->tm_wday = systime.wDayOfWeek;
294 res->tm_yday = __mon_yday[iLeap][systime.wMonth] + systime.wDay;
295 res->tm_year = systime.wYear;// this the correct year but bias the value to start at the 1900
296 res->tm_year = res->tm_year - 1900;
298 return res;
299 #else
300 // @@ Same as ACE_OS::localtime (), you need to implement it
301 // yourself.
302 ACE_UNUSED_ARG (t);
303 ACE_UNUSED_ARG (res);
304 ACE_NOTSUP_RETURN (0);
305 #endif /* ACE_HAS_REENTRANT_FUNCTIONS */
308 time_t
309 ACE_OS::mktime (struct tm *t)
311 ACE_OS_TRACE ("ACE_OS::mktime");
312 # if defined (ACE_HAS_WINCE)
313 SYSTEMTIME t_sys;
314 FILETIME t_file;
315 t_sys.wSecond = t->tm_sec;
316 t_sys.wMinute = t->tm_min;
317 t_sys.wHour = t->tm_hour;
318 t_sys.wDay = t->tm_mday;
319 t_sys.wMonth = t->tm_mon + 1; // SYSTEMTIME is 1-indexed, tm is 0-indexed
320 t_sys.wYear = t->tm_year + 1900; // SYSTEMTIME is real; tm is since 1900
321 t_sys.wDayOfWeek = t->tm_wday; // Ignored in below function call.
322 if (SystemTimeToFileTime (&t_sys, &t_file) == 0)
323 return -1;
324 ACE_Time_Value tv (t_file);
325 return tv.sec ();
326 # else
327 # if defined (ACE_HAS_THREADS) && !defined (ACE_HAS_MT_SAFE_MKTIME)
328 ACE_OS_GUARD
329 # endif /* ACE_HAS_THREADS && ! ACE_HAS_MT_SAFE_MKTIME */
331 ACE_OSCALL_RETURN (ACE_STD_NAMESPACE::mktime (t), time_t, (time_t) -1);
332 # endif /* ACE_HAS_WINCE */
335 #if defined (ACE_HAS_POWERPC_TIMER) && defined (ghs)
336 void
337 ACE_OS::readPPCTimeBase (u_long &most, u_long &least)
339 ACE_OS_TRACE ("ACE_OS::readPPCTimeBase");
341 // This function can't be inline because it depends on the arguments
342 // being in particular registers (r3 and r4), in conformance with the
343 // EABI standard. It would be nice if we knew how to put the variable
344 // names directly into the assembler instructions . . .
345 asm("aclock:");
346 asm("mftb r5,TBU");
347 asm("mftb r6,TBL");
348 asm("mftb r7,TBU");
349 asm("cmpw r5,r7");
350 asm("bne aclock");
352 asm("stw r5, 0(r3)");
353 asm("stw r6, 0(r4)");
355 #endif /* ACE_HAS_POWERPC_TIMER && ghs */
357 #if defined (ACE_LACKS_STRPTIME) && !defined (ACE_REFUSE_STRPTIME_EMULATION)
358 char *
359 ACE_OS::strptime_emulation (const char *buf, const char *format, struct tm *tm)
361 int bi = 0;
362 int fi = 0;
363 bool percent = false;
365 if (!buf || !format)
366 return 0;
368 ACE_OS::memset (tm, 0, sizeof (struct tm));
370 while (format[fi] != '\0')
372 if (percent)
374 percent = false;
375 switch (format[fi])
377 case '%': // an escaped %
378 if (buf[bi] == '%')
380 ++fi;
381 ++bi;
383 else
384 return const_cast<char*> (buf + bi);
385 break;
387 /* not supported yet: weekday via locale long/short names
388 case 'a': / * weekday via locale * /
389 / * FALL THROUGH * /
390 case 'A': / * long/short names * /
391 break;
394 /* not supported yet:
395 case 'b': / * month via locale * /
396 / * FALL THROUGH * /
397 case 'B': / * long/short names * /
398 / * FALL THROUGH * /
399 case 'h':
400 break;
403 /* not supported yet:
404 case 'c': / * %x %X * /
405 break;
408 /* not supported yet:
409 case 'C': / * date & time - * /
410 / * locale long format * /
411 break;
414 case 'd': /* day of month (1-31) */
415 /* FALL THROUGH */
416 case 'e':
417 if (!ACE_OS::strptime_getnum
418 (buf + bi, &tm->tm_mday, &bi, &fi, 1, 31))
419 return const_cast<char*> (buf + bi);
421 break;
423 case 'D': /* %m/%d/%y */
424 if (!ACE_OS::strptime_getnum
425 (buf + bi, &tm->tm_mon, &bi, &fi, 1, 12))
426 return const_cast<char*> (buf + bi);
428 --fi;
429 tm->tm_mon--;
431 if (buf[bi] != '/')
432 return const_cast<char*> (buf + bi);
434 ++bi;
436 if (!ACE_OS::strptime_getnum
437 (buf + bi, &tm->tm_mday, &bi, &fi, 1, 31))
438 return const_cast<char*> (buf + bi);
440 --fi;
441 if (buf[bi] != '/')
442 return const_cast<char*> (buf + bi);
443 ++bi;
444 if (!ACE_OS::strptime_getnum
445 (buf + bi, &tm->tm_year, &bi, &fi, 0, 99))
446 return const_cast<char*> (buf + bi);
447 if (tm->tm_year < 69)
448 tm->tm_year += 100;
449 break;
451 case 'H': /* hour (0-23) */
452 /* FALL THROUGH */
453 case 'k':
454 if (!ACE_OS::strptime_getnum
455 (buf + bi, &tm->tm_hour, &bi, &fi, 0, 23))
456 return const_cast<char*> (buf + bi);
457 break;
459 /* not supported yet:
460 case 'I': / * hour (0-12) * /
461 / * FALL THROUGH * /
462 case 'l':
463 break;
466 case 'j': /* day of year (0-366) */
467 if (!ACE_OS::strptime_getnum
468 (buf + bi, &tm->tm_yday, &bi, &fi, 1, 366))
469 return const_cast<char*> (buf + bi);
471 tm->tm_yday--;
472 break;
474 case 'm': /* an escaped % */
475 if (!ACE_OS::strptime_getnum
476 (buf + bi, &tm->tm_mon, &bi, &fi, 1, 12))
477 return const_cast<char*> (buf + bi);
479 tm->tm_mon--;
480 break;
482 case 'M': /* minute (0-59) */
483 if (!ACE_OS::strptime_getnum
484 (buf + bi, &tm->tm_min, &bi, &fi, 0, 59))
485 return const_cast<char*> (buf + bi);
487 break;
489 /* not supported yet:
490 case 'p': / * am or pm for locale * /
491 break;
494 /* not supported yet:
495 case 'r': / * %I:%M:%S %p * /
496 break;
499 case 'R': /* %H:%M */
500 if (!ACE_OS::strptime_getnum
501 (buf + bi, &tm->tm_hour, &bi, &fi, 0, 23))
502 return const_cast<char*> (buf + bi);
504 --fi;
505 if (buf[bi] != ':')
506 return const_cast<char*> (buf + bi);
507 ++bi;
508 if (!ACE_OS::strptime_getnum
509 (buf + bi, &tm->tm_min, &bi, &fi, 0, 59))
510 return const_cast<char*> (buf + bi);
512 break;
514 case 'S': /* seconds (0-61) */
515 if (!ACE_OS::strptime_getnum
516 (buf + bi, &tm->tm_sec, &bi, &fi, 0, 61))
517 return const_cast<char*> (buf + bi);
518 break;
520 case 'T': /* %H:%M:%S */
521 if (!ACE_OS::strptime_getnum
522 (buf + bi, &tm->tm_hour, &bi, &fi, 0, 23))
523 return const_cast<char*> (buf + bi);
525 --fi;
526 if (buf[bi] != ':')
527 return const_cast<char*> (buf + bi);
528 ++bi;
529 if (!ACE_OS::strptime_getnum
530 (buf + bi, &tm->tm_min, &bi, &fi, 0, 59))
531 return const_cast<char*> (buf + bi);
533 --fi;
534 if (buf[bi] != ':')
535 return const_cast<char*> (buf + bi);
536 ++bi;
537 if (!ACE_OS::strptime_getnum
538 (buf + bi, &tm->tm_sec, &bi, &fi, 0, 61))
539 return const_cast<char*> (buf + bi);
541 break;
543 case 'w': /* day of week (0=Sun-6) */
544 if (!ACE_OS::strptime_getnum
545 (buf + bi, &tm->tm_wday, &bi, &fi, 0, 6))
546 return const_cast<char*> (buf + bi);
548 break;
550 /* not supported yet: date, based on locale
551 case 'x': / * date, based on locale * /
552 break;
555 /* not supported yet:
556 case 'X': / * time, based on locale * /
557 break;
560 case 'y': /* the year - 1900 (0-99) */
561 if (!ACE_OS::strptime_getnum
562 (buf + bi, &tm->tm_year, &bi, &fi, 0, 99))
563 return const_cast<char*> (buf + bi);
565 if (tm->tm_year < 69)
566 tm->tm_year += 100;
567 break;
569 case 'Y': /* the full year (1999) */
570 if (!ACE_OS::strptime_getnum
571 (buf + bi, &tm->tm_year, &bi, &fi, 0, 0))
572 return const_cast<char*> (buf + bi);
574 tm->tm_year -= 1900;
575 break;
577 default: /* unrecognised */
578 return const_cast<char*> (buf + bi);
579 } /* switch (format[fi]) */
582 else
583 { /* if (percent) */
584 if (format[fi] == '%')
586 percent = true;
587 ++fi;
589 else
591 if (format[fi] == buf[bi])
593 ++fi;
594 ++bi;
596 else
597 return const_cast<char*> (buf + bi);
599 } /* if (percent) */
600 } /* while (format[fi] */
602 return const_cast<char*> (buf + bi);
606 ACE_OS::strptime_getnum (const char *buf,
607 int *num,
608 int *bi,
609 int *fi,
610 int min,
611 int max)
613 int i = 0, tmp = 0;
615 while (isdigit (buf[i]))
617 tmp = (tmp * 10) + (buf[i] - '0');
618 if (max && (tmp > max))
619 return 0;
620 ++i;
623 if (tmp < min)
624 return 0;
625 else if (i)
627 *num = tmp;
628 (*fi)++;
629 *bi += i;
630 return 1;
632 else
633 return 0;
635 #endif /* ACE_LACKS_STRPTIME && !ACE_REFUSE_STRPTIME_EMULATION */
637 ACE_END_VERSIONED_NAMESPACE_DECL