home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / asctime.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  134 lines

  1. /***
  2. *asctime.c - convert date/time structure to ASCII string
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Contains asctime() - convert a date/time structure to ASCII string.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <time.h>
  13. #include <internal.h>
  14. #include <mtdll.h>
  15. #ifdef _MT
  16. #include <malloc.h>
  17. #include <stddef.h>
  18. #endif  /* _MT */
  19. #include <tchar.h>
  20. #include <dbgint.h>
  21.  
  22. #define _ASCBUFSIZE   26
  23. static _TSCHAR buf[_ASCBUFSIZE];
  24.  
  25. /*
  26. ** This prototype must be local to this file since the procedure is static
  27. */
  28.  
  29. static _TSCHAR * __cdecl store_dt(_TSCHAR *, int);
  30.  
  31. static _TSCHAR * __cdecl store_dt (
  32.         REG1 _TSCHAR *p,
  33.         REG2 int val
  34.         )
  35. {
  36.         *p++ = (_TSCHAR)(_T('0') + val / 10);
  37.         *p++ = (_TSCHAR)(_T('0') + val % 10);
  38.         return(p);
  39. }
  40.  
  41.  
  42. /***
  43. *char *asctime(time) - convert a structure time to ascii string
  44. *
  45. *Purpose:
  46. *       Converts a time stored in a struct tm to a charcater string.
  47. *       The string is always exactly 26 characters of the form
  48. *               Tue May 01 02:34:55 1984\n\0
  49. *
  50. *Entry:
  51. *       struct tm *time - ptr to time structure
  52. *
  53. *Exit:
  54. *       returns pointer to static string with time string.
  55. *
  56. *Exceptions:
  57. *
  58. *******************************************************************************/
  59.  
  60. _TSCHAR * __cdecl _tasctime (
  61.         REG1 const struct tm *tb
  62.         )
  63. {
  64. #ifdef _MT
  65.  
  66.         _ptiddata ptd = _getptd();
  67.  
  68.         REG2 _TSCHAR *p;                        /* will point to asctime buffer */
  69.         _TSCHAR *retval;                        /* holds retval pointer */
  70.  
  71. #else  /* _MT */
  72.  
  73.         REG2 _TSCHAR *p = buf;
  74.  
  75. #endif  /* _MT */
  76.  
  77.         int day, mon;
  78.         int i;
  79.  
  80. #ifdef _MT
  81.  
  82.         /* Use per thread buffer area (malloc space, if necessary) */
  83.  
  84. #ifdef _UNICODE
  85.         if ( (ptd->_wasctimebuf != NULL) || ((ptd->_wasctimebuf =
  86.             (wchar_t *)_malloc_crt(_ASCBUFSIZE * sizeof(wchar_t))) != NULL) )
  87.                 p = ptd->_wasctimebuf;
  88. #else  /* _UNICODE */
  89.         if ( (ptd->_asctimebuf != NULL) || ((ptd->_asctimebuf =
  90.             (char *)_malloc_crt(_ASCBUFSIZE * sizeof(char))) != NULL) )
  91.                 p = ptd->_asctimebuf;
  92. #endif  /* _UNICODE */
  93.         else
  94.                 p = buf;        /* error: use static buffer */
  95.  
  96.         retval = p;                     /* save return value for later */
  97.  
  98. #endif  /* _MT */
  99.  
  100.         /* copy day and month names into the buffer */
  101.  
  102.         day = tb->tm_wday * 3;          /* index to correct day string */
  103.         mon = tb->tm_mon * 3;           /* index to correct month string */
  104.         for (i=0; i < 3; i++,p++) {
  105.                 *p = *(__dnames + day + i);
  106.                 *(p+4) = *(__mnames + mon + i);
  107.         }
  108.  
  109.         *p = _T(' ');                   /* blank between day and month */
  110.  
  111.         p += 4;
  112.  
  113.         *p++ = _T(' ');
  114.         p = store_dt(p, tb->tm_mday);   /* day of the month (1-31) */
  115.         *p++ = _T(' ');
  116.         p = store_dt(p, tb->tm_hour);   /* hours (0-23) */
  117.         *p++ = _T(':');
  118.         p = store_dt(p, tb->tm_min);    /* minutes (0-59) */
  119.         *p++ = _T(':');
  120.         p = store_dt(p, tb->tm_sec);    /* seconds (0-59) */
  121.         *p++ = _T(' ');
  122.         p = store_dt(p, 19 + (tb->tm_year/100)); /* year (after 1900) */
  123.         p = store_dt(p, tb->tm_year%100);
  124.         *p++ = _T('\n');
  125.         *p = _T('\0');
  126.  
  127.  
  128. #ifdef _MT
  129.         return (retval);
  130. #else  /* _MT */
  131.         return ((_TSCHAR *) buf);
  132. #endif  /* _MT */
  133. }
  134.