Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.7 KiB

  1. /***
  2. *asctime.c - convert date/time structure to ASCII string
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Contains asctime() - convert a date/time structure to ASCII string.
  8. *
  9. *Revision History:
  10. * 03-??-84 RLB Module created
  11. * 05-??-84 DCW Removed use of sprintf, to avoid loading stdio
  12. * functions
  13. * 04-13-87 JCR Added "const" to declarations
  14. * 05-21-87 SKS Declare the static buffer and helper routines as NEAR
  15. * Replace store_year() with in-line code
  16. *
  17. * 11-24-87 WAJ allocated a static buffer for each thread.
  18. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  19. * 05-24-88 PHG Merged DLL and normal versions; Removed initializers to
  20. * save memory
  21. * 06-06-89 JCR 386 mthread support
  22. * 03-20-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  23. * <cruntime.h>, removed #include <register.h>, fixed
  24. * the copyright and removed some leftover 16-bit support.
  25. * Also, cleaned up the formatting a bit.
  26. * 08-16-90 SBM Compiles cleanly with -W3
  27. * 10-04-90 GJF New-style function declarators.
  28. * 07-17-91 GJF Multi-thread support for Win32 [_WIN32_].
  29. * 02-17-93 GJF Changed for new _getptd().
  30. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  31. * 11-01-93 CFW Enable Unicode variant, rip out Cruiser.
  32. * 09-06-94 CFW Replace MTHREAD with _MT.
  33. * 01-10-95 CFW Debug CRT allocs.
  34. * 02-09-95 GJF Replaced WPRFLAG with _UNICODE.
  35. * 12-12-01 BWT Replace _getptd with _getptd_noexit and deal with error
  36. *
  37. *******************************************************************************/
  38. #include <cruntime.h>
  39. #include <time.h>
  40. #include <internal.h>
  41. #include <mtdll.h>
  42. #ifdef _MT
  43. #include <malloc.h>
  44. #include <stddef.h>
  45. #endif
  46. #include <tchar.h>
  47. #include <dbgint.h>
  48. #define _ASCBUFSIZE 26
  49. static _TSCHAR buf[_ASCBUFSIZE];
  50. /*
  51. ** This prototype must be local to this file since the procedure is static
  52. */
  53. static _TSCHAR * __cdecl store_dt(_TSCHAR *, int);
  54. static _TSCHAR * __cdecl store_dt (
  55. REG1 _TSCHAR *p,
  56. REG2 int val
  57. )
  58. {
  59. *p++ = (_TSCHAR)(_T('0') + val / 10);
  60. *p++ = (_TSCHAR)(_T('0') + val % 10);
  61. return(p);
  62. }
  63. /***
  64. *char *asctime(time) - convert a structure time to ascii string
  65. *
  66. *Purpose:
  67. * Converts a time stored in a struct tm to a charcater string.
  68. * The string is always exactly 26 characters of the form
  69. * Tue May 01 02:34:55 1984\n\0
  70. *
  71. *Entry:
  72. * struct tm *time - ptr to time structure
  73. *
  74. *Exit:
  75. * returns pointer to static string with time string.
  76. *
  77. *Exceptions:
  78. *
  79. *******************************************************************************/
  80. _TSCHAR * __cdecl _tasctime (
  81. REG1 const struct tm *tb
  82. )
  83. {
  84. REG2 _TSCHAR *p = buf; /* will point to asctime buffer */
  85. #ifdef _MT
  86. _TSCHAR *retval; /* holds retval pointer */
  87. _ptiddata ptd = _getptd_noexit();
  88. #endif
  89. int day, mon;
  90. int i;
  91. #ifdef _MT
  92. /* Use per thread buffer area (malloc space, if necessary) */
  93. if (ptd) {
  94. #ifdef _UNICODE
  95. if ( (ptd->_wasctimebuf != NULL) || ((ptd->_wasctimebuf =
  96. (wchar_t *)_malloc_crt(_ASCBUFSIZE * sizeof(wchar_t))) != NULL) )
  97. p = ptd->_wasctimebuf;
  98. #else
  99. if ( (ptd->_asctimebuf != NULL) || ((ptd->_asctimebuf =
  100. (char *)_malloc_crt(_ASCBUFSIZE * sizeof(char))) != NULL) )
  101. p = ptd->_asctimebuf;
  102. #endif
  103. }
  104. retval = p; /* save return value for later */
  105. #endif
  106. /* copy day and month names into the buffer */
  107. day = tb->tm_wday * 3; /* index to correct day string */
  108. mon = tb->tm_mon * 3; /* index to correct month string */
  109. for (i=0; i < 3; i++,p++) {
  110. *p = *(__dnames + day + i);
  111. *(p+4) = *(__mnames + mon + i);
  112. }
  113. *p = _T(' '); /* blank between day and month */
  114. p += 4;
  115. *p++ = _T(' ');
  116. p = store_dt(p, tb->tm_mday); /* day of the month (1-31) */
  117. *p++ = _T(' ');
  118. p = store_dt(p, tb->tm_hour); /* hours (0-23) */
  119. *p++ = _T(':');
  120. p = store_dt(p, tb->tm_min); /* minutes (0-59) */
  121. *p++ = _T(':');
  122. p = store_dt(p, tb->tm_sec); /* seconds (0-59) */
  123. *p++ = _T(' ');
  124. p = store_dt(p, 19 + (tb->tm_year/100)); /* year (after 1900) */
  125. p = store_dt(p, tb->tm_year%100);
  126. *p++ = _T('\n');
  127. *p = _T('\0');
  128. #ifdef _POSIX_
  129. /* Date should be padded with spaces instead of zeroes. */
  130. if (_T('0') == buf[8])
  131. buf[8] = _T(' ');
  132. #endif
  133. #ifdef _MT
  134. return (retval);
  135. #else
  136. return ((_TSCHAR *) buf);
  137. #endif
  138. }