Source code of Windows XP (NT5)
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.

166 lines
4.1 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. *
  36. *******************************************************************************/
  37. #include <cruntime.h>
  38. #include <time.h>
  39. #include <internal.h>
  40. #include <mtdll.h>
  41. #ifdef _MT
  42. #include <malloc.h>
  43. #include <stddef.h>
  44. #endif
  45. #include <tchar.h>
  46. #include <dbgint.h>
  47. #define _ASCBUFSIZE 26
  48. static _TSCHAR buf[_ASCBUFSIZE];
  49. /*
  50. ** This prototype must be local to this file since the procedure is static
  51. */
  52. static _TSCHAR * __cdecl store_dt(_TSCHAR *, int);
  53. static _TSCHAR * __cdecl store_dt (
  54. REG1 _TSCHAR *p,
  55. REG2 int val
  56. )
  57. {
  58. *p++ = (_TSCHAR)(_T('0') + val / 10);
  59. *p++ = (_TSCHAR)(_T('0') + val % 10);
  60. return(p);
  61. }
  62. /***
  63. *char *asctime(time) - convert a structure time to ascii string
  64. *
  65. *Purpose:
  66. * Converts a time stored in a struct tm to a charcater string.
  67. * The string is always exactly 26 characters of the form
  68. * Tue May 01 02:34:55 1984\n\0
  69. *
  70. *Entry:
  71. * struct tm *time - ptr to time structure
  72. *
  73. *Exit:
  74. * returns pointer to static string with time string.
  75. *
  76. *Exceptions:
  77. *
  78. *******************************************************************************/
  79. _TSCHAR * __cdecl _tasctime (
  80. REG1 const struct tm *tb
  81. )
  82. {
  83. #ifdef _MT
  84. _ptiddata ptd = _getptd();
  85. REG2 _TSCHAR *p; /* will point to asctime buffer */
  86. _TSCHAR *retval; /* holds retval pointer */
  87. #else
  88. REG2 _TSCHAR *p = buf;
  89. #endif
  90. int day, mon;
  91. int i;
  92. #ifdef _MT
  93. /* Use per thread buffer area (malloc space, if necessary) */
  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. else
  104. p = buf; /* error: use static buffer */
  105. retval = p; /* save return value for later */
  106. #endif
  107. /* copy day and month names into the buffer */
  108. day = tb->tm_wday * 3; /* index to correct day string */
  109. mon = tb->tm_mon * 3; /* index to correct month string */
  110. for (i=0; i < 3; i++,p++) {
  111. *p = *(__dnames + day + i);
  112. *(p+4) = *(__mnames + mon + i);
  113. }
  114. *p = _T(' '); /* blank between day and month */
  115. p += 4;
  116. *p++ = _T(' ');
  117. p = store_dt(p, tb->tm_mday); /* day of the month (1-31) */
  118. *p++ = _T(' ');
  119. p = store_dt(p, tb->tm_hour); /* hours (0-23) */
  120. *p++ = _T(':');
  121. p = store_dt(p, tb->tm_min); /* minutes (0-59) */
  122. *p++ = _T(':');
  123. p = store_dt(p, tb->tm_sec); /* seconds (0-59) */
  124. *p++ = _T(' ');
  125. p = store_dt(p, 19 + (tb->tm_year/100)); /* year (after 1900) */
  126. p = store_dt(p, tb->tm_year%100);
  127. *p++ = _T('\n');
  128. *p = _T('\0');
  129. #ifdef _POSIX_
  130. /* Date should be padded with spaces instead of zeroes. */
  131. if (_T('0') == buf[8])
  132. buf[8] = _T(' ');
  133. #endif
  134. #ifdef _MT
  135. return (retval);
  136. #else
  137. return ((_TSCHAR *) buf);
  138. #endif
  139. }