Team Fortress 2 Source Code as on 22/4/2020
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.

187 lines
8.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Encapsulates real world (wall clock) time
  4. //
  5. //=============================================================================
  6. #ifndef RTIME_H
  7. #define RTIME_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #ifdef WIN32
  12. char* strptime(const char *s, const char *format, struct tm *tm);
  13. #endif
  14. #include <time.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. class CSTime;
  18. // Invalid time values
  19. const RTime32 k_RTime32Nil = 0;
  20. // time values between Nil and MinValid are available for special constants
  21. const RTime32 k_RTime32MinValid = 10;
  22. // infinite time value
  23. const RTime32 k_RTime32Infinite = 0x7FFFFFFF; //01-18-2038
  24. // Render buffer size
  25. const size_t k_RTimeRenderBufferSize = 25;
  26. // Flags for component fields. Longer durations must be > shorter ones
  27. // WARNING: DO NOT RENUMBER EXISTING VALUES - STORED IN DATABASE
  28. enum ETimeUnit
  29. {
  30. k_ETimeUnitNone = 0,
  31. k_ETimeUnitSecond = 1,
  32. k_ETimeUnitMinute = 2,
  33. k_ETimeUnitHour = 3,
  34. k_ETimeUnitDay = 4,
  35. k_ETimeUnitWeek = 5,
  36. k_ETimeUnitMonth = 6,
  37. k_ETimeUnitYear = 7,
  38. k_ETimeUnitForever
  39. };
  40. // CRTime
  41. // This is our primary real time structure.
  42. // It offers 1 second resolution beginning on January 1, 1970 (i.e unix time)
  43. // This represents wall clock time
  44. class CRTime
  45. {
  46. public:
  47. // default constructor initializes to the current time
  48. CRTime();
  49. CRTime( RTime32 nTime ) : m_nStartTime( nTime ), m_bGMT( false ) {}
  50. void SetToCurrentTime() { m_nStartTime = sm_nTimeCur; }
  51. void SetFromCurrentTime( int dSecOffset ) { m_nStartTime = sm_nTimeCur + dSecOffset; }
  52. // Amount of seconds that have passed between this CRTime being set and the current wall clock time.
  53. int CSecsPassed() const;
  54. // Time accessors
  55. RTime32 GetRTime32() const { return m_nStartTime; }
  56. void SetRTime32( RTime32 rTime32 ) { m_nStartTime = rTime32; }
  57. // Access our cached current time value
  58. static void UpdateRealTime();
  59. static void SetSystemClock( RTime32 nCurrentTime );
  60. static RTime32 RTime32TimeCur() { return sm_nTimeCur; }
  61. // Render
  62. const char *Render( char (&buf)[k_RTimeRenderBufferSize] ) const;
  63. static const char *Render( const RTime32 rTime32, char (&buf)[k_RTimeRenderBufferSize] );
  64. // Return a representation of the current system time
  65. static char *PchTimeCur() { return sm_rgchLocalTimeCur; }
  66. // Return a representation of the current system date
  67. static char *PchDateCur() { return sm_rgchLocalDateCur; }
  68. // comparisons against other CRTime objects
  69. bool operator==( const CRTime &val ) const { return val.m_nStartTime == m_nStartTime; }
  70. bool operator<( const CRTime &val ) const { return m_nStartTime < val.m_nStartTime; }
  71. bool operator<=( const CRTime &val ) const { return m_nStartTime <= val.m_nStartTime; }
  72. bool operator>( const CRTime &val ) const { return m_nStartTime > val.m_nStartTime; }
  73. bool operator>=( const CRTime &val ) const { return m_nStartTime >= val.m_nStartTime; }
  74. // comparisons against RTime32 numbers (to avoid implicit construct/destruct of a temporary CRTime)
  75. bool operator==( const RTime32 &val ) const { return m_nStartTime == val; }
  76. bool operator<( const RTime32 &val ) const { return m_nStartTime < val; }
  77. bool operator<=( const RTime32 &val ) const { return m_nStartTime <= val; }
  78. bool operator>( const RTime32 &val ) const { return m_nStartTime > val; }
  79. bool operator>=( const RTime32 &val ) const { return m_nStartTime >= val; }
  80. const CRTime& operator=( const RTime32 &val ) { m_nStartTime = val; return *this; }
  81. const CRTime& operator=( const CRTime &val ) { m_nStartTime = val.m_nStartTime; return *this; }
  82. const CRTime operator+( const int &nVal ) { return m_nStartTime + nVal; }
  83. // Add exactly X seconds to this time
  84. const CRTime& operator+=( const int &nVal ) { m_nStartTime += nVal; return *this; }
  85. // Component Details
  86. int GetYear() const;
  87. int GetMonth() const; // returns 0..11
  88. int GetDayOfYear() const;
  89. int GetDayOfMonth() const;
  90. int GetDayOfWeek() const;
  91. int GetHour() const;
  92. int GetMinute() const;
  93. int GetSecond() const;
  94. int GetISOWeekOfYear() const;
  95. // Handy references to nearby time boundaries
  96. static RTime32 RTime32BeginningOfDay( const RTime32 ); // at 00:00:00
  97. static RTime32 RTime32BeginningOfNextDay( const RTime32 ); // at 00:00:00
  98. static RTime32 RTime32FirstDayOfMonth( const RTime32 ); // at 00:00:00
  99. static RTime32 RTime32LastDayOfMonth( const RTime32 ); // at 00:00:00
  100. static RTime32 RTime32FirstDayOfNextMonth( const RTime32 ); // at 00:00:00
  101. static RTime32 RTime32LastDayOfNextMonth( const RTime32 ); // at 00:00:00
  102. static bool BIsLeapYear( int nYear );
  103. bool BIsLeapYear() const { return CRTime::BIsLeapYear( GetYear() ); }
  104. // Parse time using a format string with strptime format
  105. static RTime32 RTime32FromFmtString( const char *pchFmt, const char* pchValue );
  106. // Parse time from string in standard HTTP date format
  107. static RTime32 RTime32FromHTTPDateString( const char* pchValue );
  108. // Parse time from string RFC3339 format (assumes UTC)
  109. static RTime32 RTime32FromRFC3339UTCString( const char* pchValue );
  110. static const char* RTime32ToRFC3339UTCString( const RTime32 rTime32, char (&buf)[k_RTimeRenderBufferSize] );
  111. // parse time from string "YYYY-MM-DD hh:mm:ss" or just "YYYY-MM-DD", the ' ',':','-' are optional
  112. static RTime32 RTime32FromString( const char* pszValue );
  113. // turns RTime32 in a string like "YYYY-MM-DD hh:mm:ss"
  114. static const char* RTime32ToString( const RTime32 rTime32, char (&buf)[k_RTimeRenderBufferSize], bool bNoPunct = false, bool bOnlyDate = false );
  115. // turns RTime32 into a string like "Aug 21"
  116. static const char* RTime32ToDayString( const RTime32 rTime32, char (&buf)[k_RTimeRenderBufferSize], bool bGMT = false );
  117. // If the month only has K days, K < N, returns Kth day
  118. static RTime32 RTime32NthDayOfMonth( const RTime32, int nDay ); // at 00:00:00
  119. // Add X months but return the Nth day of that month. If the month only has K days, K < N, returns Kth day.
  120. static RTime32 RTime32MonthAddChooseDay( int nNthDayOfMonth, RTime32 rtime32StartDate, int nMonthsToAdd );
  121. RTime32 GetBeginningOfDay() const { return RTime32BeginningOfDay( m_nStartTime ); }
  122. RTime32 GetBeginningOfNextDay() const { return RTime32BeginningOfNextDay( m_nStartTime ); }
  123. RTime32 GetFirstDayOfMonth() const { return RTime32FirstDayOfMonth( m_nStartTime ); }
  124. RTime32 GetLastDayOfMonth() const { return RTime32LastDayOfMonth( m_nStartTime ); }
  125. RTime32 GetFirstDayOfNextMonth() const { return RTime32FirstDayOfNextMonth( m_nStartTime ); }
  126. RTime32 GetLastDayOfNextMonth() const { return RTime32LastDayOfNextMonth( m_nStartTime ); }
  127. RTime32 GetNthDayOfMonth( int nDay ) const { return RTime32NthDayOfMonth( m_nStartTime, nDay ); }
  128. RTime32 MonthAddChooseDay( int nNthDayOfMonth, int nMonthsToAdd ) const { return RTime32MonthAddChooseDay( nNthDayOfMonth, m_nStartTime, nMonthsToAdd ); }
  129. // Add or subtract N days, weeks, minutes, etc from the current time
  130. static RTime32 RTime32DateAdd( const RTime32, int nAmount, ETimeUnit eTimeAmountType );
  131. RTime32 DateAdd( int nAmount, ETimeUnit eTimeAmountType ) const { return RTime32DateAdd( m_nStartTime, nAmount, eTimeAmountType ); }
  132. // Compare two times, and return what the largest time boundary crossed between the two was.
  133. // Week boundaries do not line up with Month or Year boundaries, so you must rely on pbWeekChanged for them!
  134. static ETimeUnit FindTimeBoundaryCrossings( RTime32 unTime1, RTime32 unTime2, bool *pbWeekChanged );
  135. void SetToGMT( bool bUseGMT ) { m_bGMT = bUseGMT;}
  136. bool BIsGMT() const { return m_bGMT; }
  137. private:
  138. // prevent assignment or copy construction from the server time type
  139. const CRTime& operator=( const CSTime &val ) { return *this; }
  140. CRTime( const CSTime& ) {}
  141. RTime32 m_nStartTime; // the time store by this instance (wall clock, in seconds)
  142. static RTime32 sm_nTimeCur; // current system wide wall clock time
  143. static char sm_rgchLocalTimeCur[16]; // string version of time for logging
  144. static char sm_rgchLocalDateCur[16]; // string version of date for logging
  145. static RTime32 sm_nTimeLastSystemTimeUpdate; // last time we updated above two logging strings
  146. bool m_bGMT;
  147. };
  148. #endif // RTIME_H