C++ 简易时间类
.h file
- #ifndef LIBFRAME_DATETIME_H_
- #define LIBFRAME_DATETIME_H_
- #include <stdint.h>
- #include "platform/platform.h"class CDateTime
- {
- public:
- struct Time
- {
- int16_t year, month, day, hour, minite, second, milliseconds, dayOfWeek;
- };
- enum
- {
- enmDateTime_MsPerSec = ,
- enmDateTime_SecPerMin = ,
- enmDateTime_MinPerHour = ,
- enmDateTime_SecPerHour = enmDateTime_SecPerMin * enmDateTime_MinPerHour,
- enmDateTime_HourPerDay = ,
- enmDateTime_SecPerDay = enmDateTime_SecPerHour * enmDateTime_HourPerDay,
- enmDateTime_DayPerWeek = ,
- enmDateTime_SecPerWeek = enmDateTime_SecPerDay * enmDateTime_DayPerWeek,
- };
- enum
- {
- // 2017-1-1 0:0:0
- enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss = ,
- // 2017-1-1 0:0:0:0
- enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms = ,
- // 2017-1-1 0:0:0 6
- enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_wday = ,
- // 2017-1-1 0:0:0:0 6
- enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms_wday = ,
- };
- public:
- CDateTime(uint16_t year = , uint16_t month = , uint16_t day = , uint16_t hour = , uint16_t minite = , uint16_t second = , uint16_t millisecond = );
- CDateTime(int64_t seconds);
- ~CDateTime();
- static CDateTime CurrentLocalTime();
- static CDateTime CurrentSystemTime();
- static bool IsLeapYear(int16_t year);
- static int16_t GetMonthDays(int16_t year, int16_t month);
- const CDateTime::Time &GetTime() const;
- int64_t Seconds() const;
- int64_t MilliSeconds() const;
- int32_t ToString(char buf[], int32_t maxBufLen, int32_t format = enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss);
- CDateTime DayBeginTime() const;
- CDateTime WeekBeginTime() const;
- CDateTime MonthBeginTime() const;
- CDateTime LastMonthBeginTime() const;
- CDateTime NextMonthBeginTime() const;
- CDateTime YearBeginTime() const;
- bool InSameMonth(int64_t timestamp, int32_t offset = );
- bool InSameWeek(int64_t timestamp, int32_t offset = );
- bool InSameDay(int64_t timestamp, int32_t offset = );
- private:
- void FetchLoaclTime();
- void FetchSystemTime();
- void SetTime(const CDateTime::Time &time);
- void SetSeconds(int64_t seconds);
- void SetMilliSeconds(int64_t milliseconds);
- int64_t MakeTime(uint16_t year = , uint16_t month = , uint16_t day = , uint16_t hour = , uint16_t minite = , uint16_t second = );
- private:
- Time m_time;
- int64_t m_seconds; // seconds
- int64_t m_milliseconds; // milliseconds
- };
- #endif
.cpp file
- #include <ctime>
- #include "datetime.h"
- 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*/)
- {
- m_time.year = year;
- m_time.month = month;
- m_time.day = day;
- m_time.hour = hour;
- m_time.minite = minite;
- m_time.second = second;
- m_time.milliseconds = millisecond;
- m_seconds = MakeTime(m_time.year, m_time.month, m_time.day, m_time.hour, m_time.minite, m_time.second);
- struct tm *ptm = localtime(&m_seconds);
- m_time.dayOfWeek = ptm->tm_wday;
- m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
- }
- CDateTime::CDateTime(int64_t seconds)
- {
- struct tm *ptm = localtime(&seconds);
- m_time.year = ptm->tm_year + ;
- m_time.month = ptm->tm_mon + ;
- m_time.day = ptm->tm_mday;
- m_time.hour = ptm->tm_hour;
- m_time.minite = ptm->tm_min;
- m_time.second = ptm->tm_sec;
- m_time.milliseconds = ;
- m_time.dayOfWeek = ptm->tm_wday;
- m_seconds = seconds;
- m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
- }
- CDateTime::~CDateTime()
- {
- }
- CDateTime CDateTime::CurrentLocalTime()
- {
- static CDateTime localTime;
- localTime.FetchLoaclTime();
- return localTime;
- }
- CDateTime CDateTime::CurrentSystemTime()
- {
- static CDateTime systemTime;
- systemTime.FetchSystemTime();
- return systemTime;
- }
- bool CDateTime::IsLeapYear(int16_t year)
- {
- return (year % == ) || (year % != && year % == );
- }
- int16_t CDateTime::GetMonthDays(int16_t year, int16_t month)
- {
- static int16_t common_year_month_days[] = { , , , , , , , , , , , , };
- static int16_t leap_year_month_days[] = { , , , , , , , , , , , , };
- if (month < || month > )
- {
- month = ;
- }
- if (IsLeapYear(year))
- {
- return leap_year_month_days[month];
- }
- else
- {
- return common_year_month_days[month];
- }
- }
- 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*/)
- {
- struct tm st = { };
- if (year < )
- {
- year = ;
- }
- st.tm_year = year - ;
- st.tm_mon = month - ;
- st.tm_mday = day;
- st.tm_hour = hour;
- st.tm_min = minite;
- st.tm_sec = second;
- int64_t seconds = mktime(&st);
- if (seconds < )
- {
- seconds = ;
- }
- return seconds;
- }
- void CDateTime::FetchLoaclTime()
- {
- #ifdef OS_WINDOWS
- static SYSTEMTIME sysTime;
- GetLocalTime(&sysTime);
- m_time.year = sysTime.wYear;
- m_time.month = sysTime.wMonth;
- m_time.day = sysTime.wDay;
- m_time.hour = sysTime.wHour;
- m_time.minite = sysTime.wMinute;
- m_time.second = sysTime.wSecond;
- m_time.milliseconds = sysTime.wMilliseconds;
- m_time.dayOfWeek = sysTime.wDayOfWeek;
- m_seconds = MakeTime(m_time.year, m_time.month, m_time.day, m_time.hour, m_time.minite, m_time.second);
- m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
- #endif // OS_WINDOWS
- #ifdef OS_LINUX
- static struct timeb tp;
- ftime(&tp);
- struct tm *ptm = localtime(&tp.time);
- m_time.year = ptm->tm_year + ;
- m_time.month = ptm->tm_mon + ;
- m_time.day = ptm->tm_mday;
- m_time.hour = ptm->tm_hour;
- m_time.minite = ptm->tm_min;
- m_time.second = ptm->tm_sec;
- m_time.milliseconds = tp.millitm;
- m_time.dayOfWeek = ptm->tm_wday;
- m_seconds = tp.time;
- m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
- #endif // OS_LINUX
- }
- void CDateTime::FetchSystemTime()
- {
- #ifdef OS_WINDOWS
- static SYSTEMTIME sysTime;
- GetSystemTime(&sysTime);
- m_time.year = sysTime.wYear;
- m_time.month = sysTime.wMonth;
- m_time.day = sysTime.wDay;
- m_time.hour = sysTime.wHour;
- m_time.minite = sysTime.wMinute;
- m_time.second = sysTime.wSecond;
- m_time.milliseconds = sysTime.wMilliseconds;
- m_time.dayOfWeek = sysTime.wDayOfWeek;
- m_seconds = MakeTime(m_time.year, m_time.month, m_time.day, m_time.hour, m_time.minite, m_time.second);
- m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
- #endif // OS_WINDOWS
- #ifdef OS_LINUX
- static struct timeb tp;
- ftime(&tp);
- struct tm *ptm = gmtime(&tp.time);
- m_time.year = ptm->tm_year + ;
- m_time.month = ptm->tm_mon + ;
- m_time.day = ptm->tm_mday;
- m_time.hour = ptm->tm_hour;
- m_time.minite = ptm->tm_min;
- m_time.second = ptm->tm_sec;
- m_time.milliseconds = tp.millitm;
- m_time.dayOfWeek = ptm->tm_wday;
- m_seconds = tp.time;
- m_milliseconds = m_seconds * enmDateTime_MsPerSec + m_time.milliseconds;
- #endif // OS_LINUX
- }
- void CDateTime::SetTime(const CDateTime::Time &time)
- {
- m_time = time;
- }
- void CDateTime::SetSeconds(int64_t seconds)
- {
- m_seconds = seconds;
- }
- void CDateTime::SetMilliSeconds(int64_t milliseconds)
- {
- m_milliseconds = milliseconds;
- }
- const CDateTime::Time & CDateTime::GetTime() const
- {
- return m_time;
- }
- int64_t CDateTime::Seconds() const
- {
- return m_seconds;
- }
- int64_t CDateTime::MilliSeconds() const
- {
- return m_milliseconds;
- }
- int32_t CDateTime::ToString(char buf[], int32_t maxBufLen, int32_t format /*= enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss*/)
- {
- const CDateTime::Time &time = GetTime();
- switch (format)
- {
- case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss:
- return sprintf(buf, maxBufLen, "%04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day, time.hour, time.minite, time.second);
- case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms:
- 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);
- case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_wday:
- 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);
- case enmDateTimeToStr__yyyy_MM_dd_hh_mm_ss_ms_wday:
- 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);
- default:
- return sprintf(buf, maxBufLen, "%04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day, time.hour, time.minite, time.second);
- }
- }
- CDateTime CDateTime::DayBeginTime() const
- {
- CDateTime dayBeginTime;
- Time time = GetTime();
- time.hour = ;
- time.minite = ;
- time.second = ;
- time.milliseconds = ;
- dayBeginTime.SetTime(time);
- dayBeginTime.SetSeconds(m_seconds - GetTime().hour * enmDateTime_SecPerHour - GetTime().minite * enmDateTime_SecPerMin - GetTime().second);
- dayBeginTime.SetMilliSeconds(dayBeginTime.Seconds() * enmDateTime_MsPerSec);
- return dayBeginTime;
- }
- CDateTime CDateTime::WeekBeginTime() const
- {
- CDateTime weekBeginTime;
- Time time = GetTime();
- time.dayOfWeek = ;
- time.day -= (GetTime().dayOfWeek - );
- time.hour = ;
- time.minite = ;
- time.second = ;
- time.milliseconds = ;
- weekBeginTime.SetTime(time);
- weekBeginTime.SetSeconds(m_seconds - (GetTime().dayOfWeek - ) * enmDateTime_SecPerDay - GetTime().hour * enmDateTime_SecPerHour - GetTime().minite * enmDateTime_SecPerMin - GetTime().second);
- weekBeginTime.SetMilliSeconds(weekBeginTime.Seconds() * enmDateTime_MsPerSec);
- return weekBeginTime;
- }
- CDateTime CDateTime::MonthBeginTime() const
- {
- CDateTime monthBeginTime;
- Time time = GetTime();
- int16_t wdayOffset = (GetTime().day - ) % enmDateTime_DayPerWeek;
- time.dayOfWeek = time.dayOfWeek > wdayOffset ? time.dayOfWeek - wdayOffset : time.dayOfWeek + enmDateTime_DayPerWeek - wdayOffset;
- time.day -= (GetTime().day - );
- time.hour = ;
- time.minite = ;
- time.second = ;
- time.milliseconds = ;
- monthBeginTime.SetTime(time);
- monthBeginTime.SetSeconds(m_seconds - (GetTime().day - ) * enmDateTime_SecPerDay - GetTime().hour * enmDateTime_SecPerMin * enmDateTime_MinPerHour - GetTime().minite * enmDateTime_SecPerMin - GetTime().second);
- monthBeginTime.SetMilliSeconds(monthBeginTime.Seconds() * enmDateTime_MsPerSec);
- return monthBeginTime;
- }
- LibFrame::CDateTime CDateTime::LastMonthBeginTime() const
- {
- return CDateTime(MonthBeginTime().Seconds() - ).MonthBeginTime();
- }
- LibFrame::CDateTime CDateTime::NextMonthBeginTime() const
- {
- return CDateTime(MonthBeginTime().Seconds() + GetMonthDays(GetTime().year, GetTime().month) * enmDateTime_SecPerDay + ).MonthBeginTime();
- }
- LibFrame::CDateTime CDateTime::YearBeginTime() const
- {
- return CDateTime(GetTime().year, , );
- }
- bool CDateTime::InSameMonth(int64_t timestamp, int32_t offset /*= 0*/)
- {
- int64_t thisMonthDemarcationPoint = MonthBeginTime().Seconds() + offset;
- return (timestamp < thisMonthDemarcationPoint + offset && thisMonthDemarcationPoint + offset < Seconds())
- || (timestamp < LastMonthBeginTime().Seconds() + offset)
- || (timestamp > NextMonthBeginTime().Seconds() + offset);
- }
- bool CDateTime::InSameWeek(int64_t timestamp, int32_t offset /*= 0*/)
- {
- int64_t thisWeekDemarcationPoint = WeekBeginTime().Seconds() + offset;
- return (timestamp < thisWeekDemarcationPoint && thisWeekDemarcationPoint < Seconds())
- || (timestamp < thisWeekDemarcationPoint - enmDateTime_SecPerWeek)
- || (timestamp > thisWeekDemarcationPoint + enmDateTime_SecPerWeek);
- }
- bool CDateTime::InSameDay(int64_t timestamp, int32_t offset /*= 0*/)
- {
- int64_t todayDemarcationPoint = Seconds() + offset;
- return (timestamp < todayDemarcationPoint && todayDemarcationPoint < Seconds())
- || (timestamp < todayDemarcationPoint - enmDateTime_SecPerDay)
- || (timestamp > todayDemarcationPoint + enmDateTime_SecPerDay);
- }
C++ 简易时间类的更多相关文章
- WPF Timeline简易时间轴控件的实现
原文:WPF Timeline简易时间轴控件的实现 效果图: 由于整个控件是实现之后才写的教程,因此这里记录的代码是最终实现后的,前后会引用到其他的一些依赖属性或者代码,需要阅读整篇文章. 1.确定T ...
- Java中六大时间类的使用和区别
关于java中六个时间类的使用和区别 java.util.Date java.sql.Date java.sql.Time java.sql.Timestamp java.text.SimpleD ...
- C#可用的日出日落时间类
一个现成代码的公共类库,复制下来作为一个类文件就可以调用了.一般不需要了解实现过程,各种数学公式太麻烦. 调用方法: SunTimeResult result = SunTimes.GetSunTim ...
- iOS--时间类date详解
NSDate定义时间的类 NSDate是一个时间类,在编写程序时,我们很少遇到.今天我突然碰到,感觉很生疏. 给大家发个博客,让大家也都温习一下,哈哈! 兄弟用的时候突然发现竟然有一些bug,大家用时 ...
- java时间类简单总结
java时间类(Data类) 1.Data类(没有考虑到国际化,好多方法已过时java.util.Data包中) 父类(是类不是接口含有直接子类3个): 日期格式为:年月日时分秒(不包含毫秒部分) ...
- 3 EventTime 事件时间类和TimeNow函数——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 这里是时间相关类的第三个部分,也是最后一个部分. EventTime 事件时间类 这个类和Dela ...
- Python学习笔记(4):自定义时间类
Python的时间我实在无法接受,太难用了.我觉得C#的时间就非常完美,简单.好用.所以,自定义了自己的时间类: 用法: 一个小小的应用,我需要取出每天股市交易的分钟段,开始是这样的: 稍微改进一下, ...
- YTU 2617: B C++时间类的运算符重载
2617: B C++时间类的运算符重载 时间限制: 1 Sec 内存限制: 128 MB 提交: 284 解决: 108 题目描述 C++时间类的运算符重载 定义一个时间类Time,其数据成员为 ...
- js(jQuery)获取时间的方法及常用时间类搜集
获取时间的方法及常用时间类都是大家经常使用的,在本文为大家整理了一些,个人感觉还比较全,感兴趣的朋友可以收集下 复制代码代码如下: $(function(){ var mydate = new D ...
随机推荐
- 分享Kali Linux 2016.2最新镜像201609
分享Kali Linux 2016.2最新镜像20160919 Kali Linux官方继9月9日发布Kali Linux 2016.2的第36周镜像W36后,9月19日发布了Kali Linux ...
- HDU4812 D Tree(树的点分治)
题目大概说给一棵有点权的树,输出字典序最小的点对,使这两点间路径上点权的乘积模1000003的结果为k. 树的点分治搞了.因为是点权过根的两条路径的LCA会被重复统计,而注意到1000003是质数,所 ...
- 【Linux下进程机制】从一道面试题谈linux下fork的运行机制
今天一位朋友去一个不错的外企面试linux开发职位,面试官出了一个如下的题目: 给出如下C程序,在linux下使用gcc编译: #include "stdio.h" #includ ...
- Period[HDU1358]
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Useful Qt Examples
Canvas API Basic Layouts Camera Example Video Widget Example Image Viewer Example Part 6 - Loading a ...
- 诅咒JavaScript之----ArcGIS JavaScript 点聚合 ClusterLayer
对一个之前一直做winForm的 菜鸟来说,突然接触这么神奇的语言,基本上每天都会诅咒一下这门神奇的语言. 最近做了一个小网站,底图用的是天地图的服务,用ArcGIS JavaScript提供的一些G ...
- [转]ASP.NET会话(Session)保存模式
本文转自:http://blog.csdn.net/cityhunter172/article/details/727743 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦 ...
- java抛出异常是怎么回事?
一.Java异常的基础知识 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的.比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如 ...
- Symantec Antivirus (SAV) for Linux Installation Checklist
https://support.symantec.com/en_US/article.TECH134163.html
- Lamda表达式
Lamda表达式 函数式编程的产生(匿名内部类) interface Mes{ public void speak(); } public class Lam { public static void ...