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

  1. /***
  2. *systime.c - _getsystime and _setsystime
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _getsystime() and _setsystime()
  8. *
  9. *******************************************************************************/
  10.  
  11.  
  12. #include <cruntime.h>
  13. #include <oscalls.h>
  14. #include <time.h>
  15.  
  16. /***
  17. *unsigned _getsystime(timestruc, milliseconds) - Get current system time
  18. *
  19. *Purpose:
  20. *
  21. *Entry:
  22.        struct tm * ptm - time structure
  23. *
  24. *Exit:
  25. *       milliseconds of current time
  26. *
  27. *Exceptions:
  28. *
  29. *******************************************************************************/
  30.  
  31. unsigned __cdecl _getsystime(struct tm * ptm)
  32. {
  33.     SYSTEMTIME  st;
  34.  
  35.     GetLocalTime(&st);
  36.  
  37.     ptm->tm_isdst       = -1;   /* mktime() computes whether this is */
  38.                                 /* during Standard or Daylight time. */
  39.     ptm->tm_sec         = (int)st.wSecond;
  40.     ptm->tm_min         = (int)st.wMinute;
  41.     ptm->tm_hour        = (int)st.wHour;
  42.     ptm->tm_mday        = (int)st.wDay;
  43.     ptm->tm_mon         = (int)st.wMonth - 1;
  44.     ptm->tm_year        = (int)st.wYear - 1900;
  45.     ptm->tm_wday        = (int)st.wDayOfWeek;
  46.  
  47.     /* Normalize uninitialized fields */
  48.     mktime(ptm);
  49.  
  50.     return (st.wMilliseconds);
  51. }
  52.  
  53. /***
  54. *unsigned _setsystime(timestruc, milliseconds) - Set new system time
  55. *
  56. *Purpose:
  57. *
  58. *Entry:
  59. *       struct tm * ptm - time structure
  60. *       unsigned milliseconds - milliseconds of current time
  61. *
  62. *Exit:
  63. *       0 if succeeds
  64. *       system error if fails
  65. *
  66. *Exceptions:
  67. *
  68. *******************************************************************************/
  69.  
  70. unsigned __cdecl _setsystime(struct tm * ptm, unsigned uMilliseconds)
  71. {
  72.     SYSTEMTIME  st;
  73.  
  74.     /* Normalize uninitialized fields */
  75.     mktime(ptm);
  76.  
  77.     st.wYear            = (WORD)(ptm->tm_year + 1900);
  78.     st.wMonth           = (WORD)(ptm->tm_mon + 1);
  79.     st.wDay             = (WORD)ptm->tm_mday;
  80.     st.wHour            = (WORD)(ptm->tm_hour);
  81.     st.wMinute          = (WORD)ptm->tm_min;
  82.     st.wSecond          = (WORD)ptm->tm_sec;
  83.     st.wMilliseconds    = (WORD)uMilliseconds;
  84.  
  85.     if (!SetLocalTime(&st)) {
  86.         return ((int)GetLastError());
  87.     }
  88.  
  89.     return (0);
  90. }
  91.  
  92.