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

  1. /***
  2. *wcsftime.c - String Format Time
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *******************************************************************************/
  9.  
  10.  
  11. #include <cruntime.h>
  12. #include <internal.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <wchar.h>
  16. #include <time.h>
  17. #include <dbgint.h>
  18.  
  19.  
  20. /***
  21. *size_t wcsftime(wstring, maxsize, format, timeptr) - Format a time string
  22. *
  23. *Purpose:
  24. *       The wcsftime functions is equivalent to to the strftime function, except
  25. *       that the argument 'wstring' specifies an array of a wide string into
  26. *       which the generated output is to be placed. The wcsftime acts as if
  27. *       strftime were called and the result string converted by mbstowcs().
  28. *       [ISO]
  29. *
  30. *Entry:
  31. *       wchar_t *wstring = pointer to output string
  32. *       size_t maxsize = max length of string
  33. *       const wchar_t *format = format control string
  34. *       const struct tm *timeptr = pointer to tb data structure
  35. *
  36. *Exit:
  37. *       !0 = If the total number of resulting characters including the
  38. *       terminating null is not more than 'maxsize', then return the
  39. *       number of wide chars placed in the 'wstring' array (not including the
  40. *       null terminator).
  41. *
  42. *       0 = Otherwise, return 0 and the contents of the string are
  43. *       indeterminate.
  44. *
  45. *Exceptions:
  46. *
  47. *******************************************************************************/
  48.  
  49. size_t __cdecl wcsftime (
  50.         wchar_t *wstring,
  51.         size_t maxsize,
  52.         const wchar_t *wformat,
  53.         const struct tm *timeptr
  54.         )
  55. {
  56.         size_t retval = 0;
  57.         char *format = NULL, *string = NULL;
  58.         int flen = wcslen(wformat) + 1;
  59.  
  60.         if ((string = (char *)_malloc_crt(sizeof(char) * maxsize * 2)) == NULL)
  61.             return 0;
  62.  
  63.         if ((format = (char *)_malloc_crt(sizeof(char) * flen * 2)) == NULL)
  64.             goto done;
  65.  
  66.         if (wcstombs(format, wformat, flen * 2) == -1)
  67.             goto done;
  68.  
  69.         if (strftime(string, maxsize * 2, format, timeptr))
  70.         {
  71.             if ((retval = mbstowcs(wstring, string, maxsize)) == -1)
  72.                 retval = 0;
  73.         }
  74.  
  75. done:
  76.         _free_crt(string);
  77.         _free_crt(format);
  78.         return retval;
  79. }
  80.  
  81.