Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.0 KiB

  1. //
  2. // MODULE: SafeTime.cpp
  3. //
  4. // PURPOSE: threadsafe wrappers for some standard time-related calls.
  5. //
  6. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  7. //
  8. // AUTHOR: Joe Mabel
  9. //
  10. // ORIGINAL DATE: 10-12-98
  11. //
  12. // NOTES:
  13. // 1. gmtime, mktime, and localtime all use a single statically allocated tm structure
  14. // for the conversion. Each call to one of these routines destroys the result of the
  15. // previous call. Obviously, that's not threadsafe.
  16. // 2. Right now this only deals with localtime, because we're not using the other 2 fns.
  17. // If we need to use gmtime or mktime, they'll need to be built analogously, using the
  18. // same mutex.
  19. // 3. _tasctime uses a single, statically allocated buffer to hold its return string.
  20. // Each call to this function destroys the result of the previous call.
  21. //
  22. // Version Date By Comments
  23. //--------------------------------------------------------------------
  24. // V3.0 10-12-98 JM
  25. //
  26. #include "stdafx.h"
  27. #include "SafeTime.h"
  28. #include "BaseException.h"
  29. #include "Event.h"
  30. #include "apiwraps.h"
  31. CMutexOwner CSafeTime::s_mx(_T("SafeTime"));
  32. //////////////////////////////////////////////////////////////////////
  33. // CSafeTime
  34. //////////////////////////////////////////////////////////////////////
  35. CSafeTime::CSafeTime(time_t time) :
  36. m_time(time)
  37. {
  38. }
  39. CSafeTime::~CSafeTime()
  40. {
  41. }
  42. // return local time as a struct tm
  43. struct tm CSafeTime::LocalTime()
  44. {
  45. struct tm tmLocal;
  46. WAIT_INFINITE( s_mx.Handle() );
  47. tmLocal = *(localtime(&m_time));
  48. ::ReleaseMutex(s_mx.Handle());
  49. return tmLocal;
  50. }
  51. // return GMT as a struct tm
  52. struct tm CSafeTime::GMTime()
  53. {
  54. struct tm tmLocal;
  55. WAIT_INFINITE( s_mx.Handle() );
  56. tmLocal = *(gmtime(&m_time));
  57. ::ReleaseMutex(s_mx.Handle());
  58. return tmLocal;
  59. }
  60. CString CSafeTime::StrLocalTime(LPCTSTR invalid_time /*=_T("Invalid Date/Time")*/)
  61. {
  62. CString str;
  63. WAIT_INFINITE( s_mx.Handle() );
  64. if (m_time)
  65. str = _tasctime(localtime(&m_time));
  66. else
  67. str = invalid_time;
  68. ::ReleaseMutex(s_mx.Handle());
  69. return str;
  70. }