Time.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // "$Id: Time.h 109610 2013-04-02 06:41:41Z qin_fenglin $"
  3. //
  4. // Copyright (c)1992-2007, ZheJiang Dahua Technology Stock CO.LTD.
  5. // All Rights Reserved.
  6. //
  7. // Description:
  8. // Revisions: Year-Month-Day SVN-Author Modification
  9. //
  10. #ifndef __INFRA3_TIME_WRAPPER_H__
  11. #define __INFRA3_TIME_WRAPPER_H__
  12. #include <string>
  13. #include "Defs.h"
  14. #include "String.h"
  15. #include "Function.h"
  16. namespace Dahua {
  17. namespace Infra {
  18. /// 时间结构
  19. struct SystemTime
  20. {
  21. int year; ///< 年。
  22. int month; ///< 月,January = 1, February = 2, and so on.
  23. int day; ///< 日。
  24. int wday; ///< 星期,Sunday = 0, Monday = 1, and so on
  25. int hour; ///< 时。
  26. int minute; ///< 分。
  27. int second; ///< 秒。
  28. int isdst; ///< 夏令时标识。
  29. };
  30. /// \class CTime
  31. /// \brief 时间类,实现了本地时间存取,运算,格式控制与打印等接口
  32. /// CTime类处理的时间都是本地时间,在GMT时间的基础上加了时区偏移。
  33. class INFRA_API CTime : public SystemTime
  34. {
  35. public:
  36. /// 时间修改回调函数
  37. typedef TFunction1<bool, SystemTime> ModifyProc;
  38. /// 日期顺序格式
  39. enum DateFormat
  40. {
  41. ymd,
  42. mdy,
  43. dmy
  44. };
  45. /// 格式化模式选项
  46. enum FormatMask
  47. {
  48. fmGeneral = 0, ///< 只指定是否显示以及起始值
  49. fmSeparator = 1, ///< 指定分隔符
  50. fmDateFormat = 2, ///< 指定年月日顺序
  51. fmHourFormat = 4, ///< 指定小时制式
  52. fmAll = fmSeparator | fmDateFormat | fmHourFormat ///< 指定所有格式选项
  53. };
  54. /// 缺省构造函数
  55. CTime( );
  56. /// 相对时间构造函数
  57. CTime(uint64_t time);
  58. /// 普通时间构造函数
  59. CTime( int vyear, int vmonth, int vday, int vhour, int vmin, int vsec);
  60. /// 得到相对时间,相对时间指从GMT 1970-1-1 00:00:00 到某个时刻经过的秒数
  61. /// \return 相对时间
  62. uint64_t makeTime() const;
  63. /// 分解相对时间
  64. /// \param time 相对时间
  65. void breakTime(uint64_t time);
  66. /// 时间调节
  67. /// \param seconds 调节的秒数
  68. /// \return 调节后新的时间对象
  69. CTime operator +( int64_t seconds ) const;
  70. /// 时间调节
  71. /// \param seconds 调节的秒数
  72. /// \return 调节后新的时间对象
  73. CTime operator -( int64_t seconds ) const;
  74. /// 时间差运算
  75. /// \param time 相比较的时间
  76. /// \return 比较的结果,为(*this - time)得到的秒数
  77. int64_t operator -( const CTime& time ) const;
  78. /// 时间调节
  79. /// \param seconds 调节的秒数
  80. /// \return 对象本身
  81. CTime& operator +=( int64_t seconds );
  82. /// 时间调节
  83. /// \param seconds 调节的秒数
  84. /// \return 对象本身
  85. CTime& operator -=( int64_t seconds );
  86. /// 时间比较
  87. /// \param time 相比较的时间
  88. /// \return 相等返回true,否则返回false
  89. bool operator == (const CTime& time) const;
  90. /// 时间比较
  91. /// \param time 相比较的时间
  92. /// \return 不等返回true,否则返回false
  93. bool operator != (const CTime& time) const;
  94. /// 时间比较
  95. /// \param time 相比较的时间
  96. /// \return <返回true,否则返回false
  97. bool operator < (const CTime& time) const;
  98. /// 时间比较
  99. /// \param time 相比较的时间
  100. /// \return <=返回true,否则返回false
  101. bool operator <= (const CTime& time) const;
  102. /// 时间比较
  103. /// \param time 相比较的时间
  104. /// \return >返回true,否则返回false
  105. bool operator > (const CTime& time) const;
  106. /// 时间比较
  107. /// \param time 相比较的时间
  108. /// \return >=返回true,否则返回false
  109. bool operator >= (const CTime& time) const;
  110. /// 时间格式化
  111. /// \param buf 字符串缓冲,一定要足够大
  112. /// \param format 格式化字符串,如"yyyy-MM-dd HH:mm:ss tt"
  113. /// yy = 年,不带世纪 yyyy = 年,带世纪
  114. /// M = 非0起始月 MM = 0起始月 MMMM = 月名称
  115. /// d = 非0起始日 dd = 0起始日
  116. /// H = 非0起始24小时 HH = 0起始24小时 h = 非0起始12小时 hh = 0起始12小时
  117. /// tt = 显示上午或下午
  118. /// \param mask 格式选项,指定日期分隔符,年月日顺序,小时制式是否由统一的格
  119. /// 式决定。相应位置0,表示使用统一格式,置1,表示使用format指定的格式
  120. void format(char* buf, const char *format = "yyyy-MM-dd HH:mm:ss", int mask = fmGeneral) const;
  121. /// 时间字符串解析
  122. /// \param buf 输入的字符串缓冲
  123. /// \param format 格式化字符串,暂时只支持默认的"yyyy-MM-dd HH:mm:ss"
  124. /// \param mask 格式选项,指定日期分隔符,年月日顺序,小时制式是否由统一的格
  125. /// 式决定。相应位置0,表示使用统一格式,置1,表示使用format指定的格式
  126. /// \return 是否成功
  127. bool parse(const char* buf, const char *format = "yyyy-MM-dd HH:mm:ss", int mask = fmGeneral);
  128. /// 会立刻实际的去取本地当前系统时间,更精确和及时
  129. /// 但linux下多线程调用频繁可能会导致较高cpu,适用于时间秒跳变准确,且调用不频繁的场景
  130. static CTime getCurrentTime();
  131. /// 返回保存的当前的系统时间,内部会间隔去刷新保存的本地当前系统时间
  132. /// 用于多线程频繁打印,与实际时间精度控制在1s偏差范围内,但是cpu占用率低
  133. static CTime getCurTimeForPrint();
  134. /// 设置本地当前系统时间
  135. /// \param time 新的时间
  136. /// \param toleranceSeconds 容差,表示容许设置时间和当前差多少秒内不做修改
  137. static void setCurrentTime(const CTime& time, int toleranceSeconds = 0);
  138. /// 设置本地当前系统时间并返回设置成功与否
  139. /// \param time 新的时间
  140. /// \param toleranceSeconds 容差,表示容许设置时间和当前差多少秒内不做修改
  141. static bool setCurrentTimeEx(const CTime& time, int toleranceSeconds = 0);
  142. /// 得到从系统启动到现在的毫秒数
  143. static uint64_t getCurrentMilliSecond();
  144. /// 得到从系统启动到现在的微秒数
  145. static uint64_t getCurrentMicroSecond();
  146. /// 设置时间格式,会影响Format的输出的字符串格式,如"yyyy-MM-dd HH:mm:ss"
  147. static void setFormatString(const char* format);
  148. /// 获取时间格式
  149. static const CString getFormatString();
  150. /// 获取日期格式
  151. static DateFormat getDateFormat();
  152. /// 获取当前是否是12小时制
  153. static bool get12Hour();
  154. /// 获取日期分割符
  155. static char getSeparator();
  156. /// 注册修改时间回调函数,这个在上层调用了setCurrentTime后会被同步触发
  157. /// \param proc 时间修改回调函数
  158. /// \return 注册时间修改回调函数是否成功
  159. static bool attachModifyProc(ModifyProc proc);
  160. /// 设置时间格式,会影响Format的输出的字符串格式,如"yyyy-MM-dd HH:mm:ss"
  161. static void setFormat(const CString2& format);
  162. /// 获取时间格式
  163. static const CString2 getFormat();
  164. /// 获取UTC标准时间
  165. static uint64_t getCurrentUTCtime();
  166. /// 禁止时区,优化性能。CTime中不进行时区转换,也不获取时区
  167. static int disableTimezone();
  168. public:
  169. static const CTime minTime; ///< 有效的最小时间
  170. static const CTime maxTime; ///< 有效的最大时间
  171. };
  172. } // namespace Infra
  173. } // namespace Dahua
  174. #endif // __INFRA_TIME_WRAPPER_H__