timecore.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
  14. int nDST)
  15. {
  16. struct tm atm;
  17. atm.tm_sec = nSec;
  18. atm.tm_min = nMin;
  19. atm.tm_hour = nHour;
  20. ASSERT(nDay >= 1 && nDay <= 31);
  21. atm.tm_mday = nDay;
  22. ASSERT(nMonth >= 1 && nMonth <= 12);
  23. atm.tm_mon = nMonth - 1; // tm_mon is 0 based
  24. ASSERT(nYear >= 1900);
  25. atm.tm_year = nYear - 1900; // tm_year is 1900 based
  26. atm.tm_isdst = nDST;
  27. m_time = mktime(&atm);
  28. ASSERT(m_time != -1); // indicates an illegal input time
  29. }
  30. CTime::CTime(const SYSTEMTIME& sysTime, int nDST)
  31. {
  32. if (sysTime.wYear < 1900)
  33. {
  34. time_t time0 = 0L;
  35. CTime timeT(time0);
  36. *this = timeT;
  37. }
  38. else
  39. {
  40. CTime timeT(
  41. (int)sysTime.wYear, (int)sysTime.wMonth, (int)sysTime.wDay,
  42. (int)sysTime.wHour, (int)sysTime.wMinute, (int)sysTime.wSecond,
  43. nDST);
  44. *this = timeT;
  45. }
  46. }
  47. CTime::CTime(const FILETIME& fileTime, int nDST)
  48. {
  49. // first convert file time (UTC time) to local time
  50. FILETIME localTime;
  51. if (!FileTimeToLocalFileTime(&fileTime, &localTime))
  52. {
  53. m_time = 0;
  54. return;
  55. }
  56. // then convert that time to system time
  57. SYSTEMTIME sysTime;
  58. if (!FileTimeToSystemTime(&localTime, &sysTime))
  59. {
  60. m_time = 0;
  61. return;
  62. }
  63. // then convert the system time to a time_t (C-runtime local time)
  64. CTime timeT(sysTime, nDST);
  65. *this = timeT;
  66. }
  67. CTime CTime::CreateForCurrentTime()
  68. // return the current system time
  69. {
  70. return CTime(::time(NULL));
  71. }
  72. struct tm* CTime::GetLocalTm(struct tm* ptm) const
  73. {
  74. if (ptm != NULL)
  75. {
  76. struct tm* ptmTemp = localtime(&m_time);
  77. if (ptmTemp == NULL)
  78. return NULL; // indicates the m_time was not initialized!
  79. *ptm = *ptmTemp;
  80. return ptm;
  81. }
  82. else
  83. return localtime(&m_time);
  84. }
  85. /////////////////////////////////////////////////////////////////////////////