timecore.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CTime - absolute time
  13. CTime::CTime(const SYSTEMTIME& sysTime)
  14. {
  15. if (sysTime.wYear < 1900)
  16. {
  17. time_t time0 = 0L;
  18. CTime timeT(time0);
  19. *this = timeT;
  20. }
  21. else
  22. {
  23. struct tm atm;
  24. atm.tm_sec = (int)sysTime.wSecond;
  25. atm.tm_min = (int)sysTime.wMinute;
  26. atm.tm_hour = (int)sysTime.wHour;
  27. int nDay = (int)sysTime.wDay;
  28. ASSERT(nDay >= 1 && nDay <= 31);
  29. atm.tm_mday = nDay;
  30. int nMonth = (int)sysTime.wMonth;
  31. ASSERT(nMonth >= 1 && nMonth <= 12);
  32. atm.tm_mon = nMonth - 1; // tm_mon is 0 based
  33. ASSERT(nYear >= 1900);
  34. atm.tm_year = (int)sysTime.wYear - 1900; // tm_year is 1900 based
  35. atm.tm_isdst = -1;
  36. m_time = mktime(&atm);
  37. ASSERT(m_time != -1); // indicates an illegal input time
  38. }
  39. }
  40. CTime::CTime(const FILETIME& fileTime)
  41. {
  42. // first convert file time (UTC time) to local time
  43. FILETIME localTime;
  44. if (!FileTimeToLocalFileTime(&fileTime, &localTime))
  45. {
  46. m_time = 0;
  47. return;
  48. }
  49. // then convert that time to system time
  50. SYSTEMTIME sysTime;
  51. if (!FileTimeToSystemTime(&localTime, &sysTime))
  52. {
  53. m_time = 0;
  54. return;
  55. }
  56. // then convert the system time to a time_t (C-runtime local time)
  57. CTime timeT(sysTime);
  58. *this = timeT;
  59. }
  60. CTime CTime::CreateForCurrentTime()
  61. // return the current system time
  62. {
  63. return CTime(::time(NULL));
  64. }
  65. struct tm* CTime::GetLocalTm(struct tm* ptm) const
  66. {
  67. if (ptm != NULL)
  68. {
  69. struct tm* ptmTemp = localtime(&m_time);
  70. if (ptmTemp == NULL)
  71. return NULL; // indicates the m_time was not initialized!
  72. *ptm = *ptmTemp;
  73. return ptm;
  74. }
  75. else
  76. return localtime(&m_time);
  77. }
  78. /////////////////////////////////////////////////////////////////////////////