.h file

  1. #ifndef LIBFRAME_DATETIME_H_
  2. #define LIBFRAME_DATETIME_H_
  3.  
  4. #include <stdint.h>
  5. #include "platform/platform.h"class CDateTime
  6. {
  7. public:
  8. struct Time
  9. {
  10. int16_t year, month, day, hour, minite, second, milliseconds, dayOfWeek;
  11. };
  12. enum
  13. {
  14. enmDateTime_MsPerSec = ,
  15. enmDateTime_SecPerMin = ,
  16. enmDateTime_MinPerHour = ,
  17. enmDateTime_SecPerHour = enmDateTime_SecPerMin * enmDateTime_MinPerHour,
  18. enmDateTime_HourPerDay = ,
  19. enmDateTime_SecPerDay = enmDateTime_SecPerHour * enmDateTime_HourPerDay,
  20. enmDateTime_DayPerWeek = ,
  21. enmDateTime_SecPerWeek = enmDateTime_SecPerDay * enmDateTime_DayPerWeek,
  22. };
  23. enum
  24. {
  25. // 2017-1-1 0:0:0
  26. enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss = ,
  27. // 2017-1-1 0:0:0:0
  28. enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms = ,
  29. // 2017-1-1 0:0:0 6
  30. enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_wday = ,
  31. // 2017-1-1 0:0:0:0 6
  32. enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms_wday = ,
  33. };
  34. public:
  35. CDateTime(uint16_t year = , uint16_t month = , uint16_t day = , uint16_t hour = , uint16_t minite = , uint16_t second = , uint16_t millisecond = );
  36. CDateTime(int64_t seconds);
  37. ~CDateTime();
  38. static CDateTime CurrentLocalTime();
  39. static CDateTime CurrentSystemTime();
  40. static bool IsLeapYear(int16_t year);
  41. static int16_t GetMonthDays(int16_t year, int16_t month);
  42. const CDateTime::Time &GetTime() const;
  43. int64_t Seconds() const;
  44. int64_t MilliSeconds() const;
  45.  
  46. int32_t ToString(char buf[], int32_t maxBufLen, int32_t format = enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss);
  47.  
  48. CDateTime DayBeginTime() const;
  49. CDateTime WeekBeginTime() const;
  50. CDateTime MonthBeginTime() const;
  51. CDateTime LastMonthBeginTime() const;
  52. CDateTime NextMonthBeginTime() const;
  53. CDateTime YearBeginTime() const;
  54.  
  55. bool InSameMonth(int64_t timestamp, int32_t offset = );
  56. bool InSameWeek(int64_t timestamp, int32_t offset = );
  57. bool InSameDay(int64_t timestamp, int32_t offset = );
  58.  
  59. private:
  60. void FetchLoaclTime();
  61. void FetchSystemTime();
  62. void SetTime(const CDateTime::Time &time);
  63. void SetSeconds(int64_t seconds);
  64. void SetMilliSeconds(int64_t milliseconds);
  65. int64_t MakeTime(uint16_t year = , uint16_t month = , uint16_t day = , uint16_t hour = , uint16_t minite = , uint16_t second = );
  66. private:
  67. Time m_time;
  68. int64_t m_seconds; // seconds
  69. int64_t m_milliseconds; // milliseconds
  70. };
  71.  
  72. #endif

.cpp file

  1. #include <ctime>
  2. #include "datetime.h"
  3.  
  4. CDateTime::CDateTime(uint16_t year /*= 0*/, uint16_t month /*= 0*/, uint16_t day /*= 0*/, uint16_t hour /*= 0*/, uint16_t minite /*= 0*/, uint16_t second /*= 0*/, uint16_t millisecond /*= 0*/)
  5. {
  6. m_time.year = year;
  7. m_time.month = month;
  8. m_time.day = day;
  9. m_time.hour = hour;
  10. m_time.minite = minite;
  11. m_time.second = second;
  12. m_time.milliseconds = millisecond;
  13. m_seconds = MakeTime(m_time.year, m_time.month, m_time.day, m_time.hour, m_time.minite, m_time.second);
  14. struct tm *ptm = localtime(&m_seconds);
  15. m_time.dayOfWeek = ptm->tm_wday;
  16. m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
  17. }
  18.  
  19. CDateTime::CDateTime(int64_t seconds)
  20. {
  21. struct tm *ptm = localtime(&seconds);
  22. m_time.year = ptm->tm_year + ;
  23. m_time.month = ptm->tm_mon + ;
  24. m_time.day = ptm->tm_mday;
  25. m_time.hour = ptm->tm_hour;
  26. m_time.minite = ptm->tm_min;
  27. m_time.second = ptm->tm_sec;
  28. m_time.milliseconds = ;
  29. m_time.dayOfWeek = ptm->tm_wday;
  30. m_seconds = seconds;
  31. m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
  32. }
  33.  
  34. CDateTime::~CDateTime()
  35. {
  36. }
  37.  
  38. CDateTime CDateTime::CurrentLocalTime()
  39. {
  40. static CDateTime localTime;
  41. localTime.FetchLoaclTime();
  42. return localTime;
  43. }
  44.  
  45. CDateTime CDateTime::CurrentSystemTime()
  46. {
  47. static CDateTime systemTime;
  48. systemTime.FetchSystemTime();
  49. return systemTime;
  50. }
  51.  
  52. bool CDateTime::IsLeapYear(int16_t year)
  53. {
  54. return (year % == ) || (year % != && year % == );
  55. }
  56.  
  57. int16_t CDateTime::GetMonthDays(int16_t year, int16_t month)
  58. {
  59. static int16_t common_year_month_days[] = { , , , , , , , , , , , , };
  60. static int16_t leap_year_month_days[] = { , , , , , , , , , , , , };
  61. if (month < || month > )
  62. {
  63. month = ;
  64. }
  65. if (IsLeapYear(year))
  66. {
  67. return leap_year_month_days[month];
  68. }
  69. else
  70. {
  71. return common_year_month_days[month];
  72. }
  73. }
  74.  
  75. int64_t CDateTime::MakeTime(uint16_t year /*= 0*/, uint16_t month /*= 0*/, uint16_t day /*= 0*/, uint16_t hour /*= 0*/, uint16_t minite /*= 0*/, uint16_t second /*= 0*/)
  76. {
  77. struct tm st = { };
  78. if (year < )
  79. {
  80. year = ;
  81. }
  82. st.tm_year = year - ;
  83. st.tm_mon = month - ;
  84. st.tm_mday = day;
  85. st.tm_hour = hour;
  86. st.tm_min = minite;
  87. st.tm_sec = second;
  88. int64_t seconds = mktime(&st);
  89. if (seconds < )
  90. {
  91. seconds = ;
  92. }
  93. return seconds;
  94. }
  95.  
  96. void CDateTime::FetchLoaclTime()
  97. {
  98. #ifdef OS_WINDOWS
  99. static SYSTEMTIME sysTime;
  100. GetLocalTime(&sysTime);
  101. m_time.year = sysTime.wYear;
  102. m_time.month = sysTime.wMonth;
  103. m_time.day = sysTime.wDay;
  104. m_time.hour = sysTime.wHour;
  105. m_time.minite = sysTime.wMinute;
  106. m_time.second = sysTime.wSecond;
  107. m_time.milliseconds = sysTime.wMilliseconds;
  108. m_time.dayOfWeek = sysTime.wDayOfWeek;
  109. m_seconds = MakeTime(m_time.year, m_time.month, m_time.day, m_time.hour, m_time.minite, m_time.second);
  110. m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
  111. #endif // OS_WINDOWS
  112. #ifdef OS_LINUX
  113. static struct timeb tp;
  114. ftime(&tp);
  115. struct tm *ptm = localtime(&tp.time);
  116. m_time.year = ptm->tm_year + ;
  117. m_time.month = ptm->tm_mon + ;
  118. m_time.day = ptm->tm_mday;
  119. m_time.hour = ptm->tm_hour;
  120. m_time.minite = ptm->tm_min;
  121. m_time.second = ptm->tm_sec;
  122. m_time.milliseconds = tp.millitm;
  123. m_time.dayOfWeek = ptm->tm_wday;
  124. m_seconds = tp.time;
  125. m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
  126. #endif // OS_LINUX
  127. }
  128.  
  129. void CDateTime::FetchSystemTime()
  130. {
  131. #ifdef OS_WINDOWS
  132. static SYSTEMTIME sysTime;
  133. GetSystemTime(&sysTime);
  134. m_time.year = sysTime.wYear;
  135. m_time.month = sysTime.wMonth;
  136. m_time.day = sysTime.wDay;
  137. m_time.hour = sysTime.wHour;
  138. m_time.minite = sysTime.wMinute;
  139. m_time.second = sysTime.wSecond;
  140. m_time.milliseconds = sysTime.wMilliseconds;
  141. m_time.dayOfWeek = sysTime.wDayOfWeek;
  142. m_seconds = MakeTime(m_time.year, m_time.month, m_time.day, m_time.hour, m_time.minite, m_time.second);
  143. m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
  144. #endif // OS_WINDOWS
  145. #ifdef OS_LINUX
  146. static struct timeb tp;
  147. ftime(&tp);
  148. struct tm *ptm = gmtime(&tp.time);
  149. m_time.year = ptm->tm_year + ;
  150. m_time.month = ptm->tm_mon + ;
  151. m_time.day = ptm->tm_mday;
  152. m_time.hour = ptm->tm_hour;
  153. m_time.minite = ptm->tm_min;
  154. m_time.second = ptm->tm_sec;
  155. m_time.milliseconds = tp.millitm;
  156. m_time.dayOfWeek = ptm->tm_wday;
  157. m_seconds = tp.time;
  158. m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
  159. #endif // OS_LINUX
  160. }
  161.  
  162. void CDateTime::SetTime(const CDateTime::Time &time)
  163. {
  164. m_time = time;
  165. }
  166.  
  167. void CDateTime::SetSeconds(int64_t seconds)
  168. {
  169. m_seconds = seconds;
  170. }
  171.  
  172. void CDateTime::SetMilliSeconds(int64_t milliseconds)
  173. {
  174. m_milliseconds = milliseconds;
  175. }
  176.  
  177. const CDateTime::Time & CDateTime::GetTime() const
  178. {
  179. return m_time;
  180. }
  181.  
  182. int64_t CDateTime::Seconds() const
  183. {
  184. return m_seconds;
  185. }
  186.  
  187. int64_t CDateTime::MilliSeconds() const
  188. {
  189. return m_milliseconds;
  190. }
  191.  
  192. int32_t CDateTime::ToString(char buf[], int32_t maxBufLen, int32_t format /*= enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss*/)
  193. {
  194. const CDateTime::Time &time = GetTime();
  195. switch (format)
  196. {
  197. case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss:
  198. return sprintf(buf, maxBufLen, "%04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day, time.hour, time.minite, time.second);
  199. case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms:
  200. return sprintf(buf, maxBufLen, "%04d-%02d-%02d %02d:%02d:%02d:%03d", time.year, time.month, time.day, time.hour, time.minite, time.second, time.milliseconds);
  201. case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_wday:
  202. return sprintf(buf, maxBufLen, "%04d-%02d-%02d %02d:%02d:%02d %d", time.year, time.month, time.day, time.hour, time.minite, time.second, time.dayOfWeek);
  203. case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms_wday:
  204. return sprintf(buf, maxBufLen, "%04d-%02d-%02d %02d:%02d:%02d:%03d %d", time.year, time.month, time.day, time.hour, time.minite, time.second, time.milliseconds, time.dayOfWeek);
  205. default:
  206. return sprintf(buf, maxBufLen, "%04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day, time.hour, time.minite, time.second);
  207. }
  208. }
  209.  
  210. CDateTime CDateTime::DayBeginTime() const
  211. {
  212. CDateTime dayBeginTime;
  213. Time time = GetTime();
  214. time.hour = ;
  215. time.minite = ;
  216. time.second = ;
  217. time.milliseconds = ;
  218. dayBeginTime.SetTime(time);
  219. dayBeginTime.SetSeconds(m_seconds - GetTime().hour * enmDateTime_SecPerHour - GetTime().minite * enmDateTime_SecPerMin - GetTime().second);
  220. dayBeginTime.SetMilliSeconds(dayBeginTime.Seconds() * enmDateTime_MsPerSec);
  221. return dayBeginTime;
  222. }
  223.  
  224. CDateTime CDateTime::WeekBeginTime() const
  225. {
  226. CDateTime weekBeginTime;
  227. Time time = GetTime();
  228. time.dayOfWeek = ;
  229. time.day -= (GetTime().dayOfWeek - );
  230. time.hour = ;
  231. time.minite = ;
  232. time.second = ;
  233. time.milliseconds = ;
  234. weekBeginTime.SetTime(time);
  235. weekBeginTime.SetSeconds(m_seconds - (GetTime().dayOfWeek - ) * enmDateTime_SecPerDay - GetTime().hour * enmDateTime_SecPerHour - GetTime().minite * enmDateTime_SecPerMin - GetTime().second);
  236. weekBeginTime.SetMilliSeconds(weekBeginTime.Seconds() * enmDateTime_MsPerSec);
  237. return weekBeginTime;
  238. }
  239.  
  240. CDateTime CDateTime::MonthBeginTime() const
  241. {
  242. CDateTime monthBeginTime;
  243. Time time = GetTime();
  244. int16_t wdayOffset = (GetTime().day - ) % enmDateTime_DayPerWeek;
  245. time.dayOfWeek = time.dayOfWeek > wdayOffset ? time.dayOfWeek - wdayOffset : time.dayOfWeek + enmDateTime_DayPerWeek - wdayOffset;
  246. time.day -= (GetTime().day - );
  247. time.hour = ;
  248. time.minite = ;
  249. time.second = ;
  250. time.milliseconds = ;
  251. monthBeginTime.SetTime(time);
  252. monthBeginTime.SetSeconds(m_seconds - (GetTime().day - ) * enmDateTime_SecPerDay - GetTime().hour * enmDateTime_SecPerMin * enmDateTime_MinPerHour - GetTime().minite * enmDateTime_SecPerMin - GetTime().second);
  253. monthBeginTime.SetMilliSeconds(monthBeginTime.Seconds() * enmDateTime_MsPerSec);
  254. return monthBeginTime;
  255. }
  256.  
  257. LibFrame::CDateTime CDateTime::LastMonthBeginTime() const
  258. {
  259. return CDateTime(MonthBeginTime().Seconds() - ).MonthBeginTime();
  260. }
  261.  
  262. LibFrame::CDateTime CDateTime::NextMonthBeginTime() const
  263. {
  264. return CDateTime(MonthBeginTime().Seconds() + GetMonthDays(GetTime().year, GetTime().month) * enmDateTime_SecPerDay + ).MonthBeginTime();
  265. }
  266.  
  267. LibFrame::CDateTime CDateTime::YearBeginTime() const
  268. {
  269. return CDateTime(GetTime().year, , );
  270. }
  271.  
  272. bool CDateTime::InSameMonth(int64_t timestamp, int32_t offset /*= 0*/)
  273. {
  274. int64_t thisMonthDemarcationPoint = MonthBeginTime().Seconds() + offset;
  275. return (timestamp < thisMonthDemarcationPoint + offset && thisMonthDemarcationPoint + offset < Seconds())
  276. || (timestamp < LastMonthBeginTime().Seconds() + offset)
  277. || (timestamp > NextMonthBeginTime().Seconds() + offset);
  278. }
  279.  
  280. bool CDateTime::InSameWeek(int64_t timestamp, int32_t offset /*= 0*/)
  281. {
  282. int64_t thisWeekDemarcationPoint = WeekBeginTime().Seconds() + offset;
  283. return (timestamp < thisWeekDemarcationPoint && thisWeekDemarcationPoint < Seconds())
  284. || (timestamp < thisWeekDemarcationPoint - enmDateTime_SecPerWeek)
  285. || (timestamp > thisWeekDemarcationPoint + enmDateTime_SecPerWeek);
  286. }
  287.  
  288. bool CDateTime::InSameDay(int64_t timestamp, int32_t offset /*= 0*/)
  289. {
  290. int64_t todayDemarcationPoint = Seconds() + offset;
  291. return (timestamp < todayDemarcationPoint && todayDemarcationPoint < Seconds())
  292. || (timestamp < todayDemarcationPoint - enmDateTime_SecPerDay)
  293. || (timestamp > todayDemarcationPoint + enmDateTime_SecPerDay);
  294. }

C++ 简易时间类的更多相关文章

  1. WPF Timeline简易时间轴控件的实现

    原文:WPF Timeline简易时间轴控件的实现 效果图: 由于整个控件是实现之后才写的教程,因此这里记录的代码是最终实现后的,前后会引用到其他的一些依赖属性或者代码,需要阅读整篇文章. 1.确定T ...

  2. Java中六大时间类的使用和区别

    关于java中六个时间类的使用和区别 java.util.Date java.sql.Date  java.sql.Time  java.sql.Timestamp java.text.SimpleD ...

  3. C#可用的日出日落时间类

    一个现成代码的公共类库,复制下来作为一个类文件就可以调用了.一般不需要了解实现过程,各种数学公式太麻烦. 调用方法: SunTimeResult result = SunTimes.GetSunTim ...

  4. iOS--时间类date详解

    NSDate定义时间的类 NSDate是一个时间类,在编写程序时,我们很少遇到.今天我突然碰到,感觉很生疏. 给大家发个博客,让大家也都温习一下,哈哈! 兄弟用的时候突然发现竟然有一些bug,大家用时 ...

  5. java时间类简单总结

    java时间类(Data类) 1.Data类(没有考虑到国际化,好多方法已过时java.util.Data包中) 父类(是类不是接口含有直接子类3个):  日期格式为:年月日时分秒(不包含毫秒部分) ...

  6. 3 EventTime 事件时间类和TimeNow函数——Live555源码阅读(一)基本组件类

    这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 这里是时间相关类的第三个部分,也是最后一个部分. EventTime 事件时间类 这个类和Dela ...

  7. Python学习笔记(4):自定义时间类

    Python的时间我实在无法接受,太难用了.我觉得C#的时间就非常完美,简单.好用.所以,自定义了自己的时间类: 用法: 一个小小的应用,我需要取出每天股市交易的分钟段,开始是这样的: 稍微改进一下, ...

  8. YTU 2617: B C++时间类的运算符重载

    2617: B C++时间类的运算符重载 时间限制: 1 Sec  内存限制: 128 MB 提交: 284  解决: 108 题目描述 C++时间类的运算符重载 定义一个时间类Time,其数据成员为 ...

  9. js(jQuery)获取时间的方法及常用时间类搜集

    获取时间的方法及常用时间类都是大家经常使用的,在本文为大家整理了一些,个人感觉还比较全,感兴趣的朋友可以收集下   复制代码代码如下: $(function(){ var mydate = new D ...

随机推荐

  1. 分享Kali Linux 2016.2最新镜像201609

    分享Kali Linux 2016.2最新镜像20160919   Kali Linux官方继9月9日发布Kali Linux 2016.2的第36周镜像W36后,9月19日发布了Kali Linux ...

  2. HDU4812 D Tree(树的点分治)

    题目大概说给一棵有点权的树,输出字典序最小的点对,使这两点间路径上点权的乘积模1000003的结果为k. 树的点分治搞了.因为是点权过根的两条路径的LCA会被重复统计,而注意到1000003是质数,所 ...

  3. 【Linux下进程机制】从一道面试题谈linux下fork的运行机制

    今天一位朋友去一个不错的外企面试linux开发职位,面试官出了一个如下的题目: 给出如下C程序,在linux下使用gcc编译: #include "stdio.h" #includ ...

  4. Period[HDU1358]

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  5. Useful Qt Examples

    Canvas API Basic Layouts Camera Example Video Widget Example Image Viewer Example Part 6 - Loading a ...

  6. 诅咒JavaScript之----ArcGIS JavaScript 点聚合 ClusterLayer

    对一个之前一直做winForm的 菜鸟来说,突然接触这么神奇的语言,基本上每天都会诅咒一下这门神奇的语言. 最近做了一个小网站,底图用的是天地图的服务,用ArcGIS JavaScript提供的一些G ...

  7. [转]ASP.NET会话(Session)保存模式

    本文转自:http://blog.csdn.net/cityhunter172/article/details/727743 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦 ...

  8. java抛出异常是怎么回事?

    一.Java异常的基础知识 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的.比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如 ...

  9. Symantec Antivirus (SAV) for Linux Installation Checklist

    https://support.symantec.com/en_US/article.TECH134163.html

  10. Lamda表达式

    Lamda表达式 函数式编程的产生(匿名内部类) interface Mes{ public void speak(); } public class Lam { public static void ...