.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++ 简易时间类的更多相关文章

  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. POJ1135 Domino Effect(SPFA)

    题目大概是,普通骨牌连接两张关键骨牌,一旦一张关键骨牌倒下与其相邻的普通骨牌也倒下,普通骨牌倒下与其相邻的骨牌也倒下.给出所有有普通骨牌相连的两个关键骨牌之间普通骨牌倒下所需时间,问1号关键骨牌开始倒 ...

  2. BZOJ3734 : [Ontak2013]Miny

    将所有炸弹按坐标排序 x<-y连边表示x爆炸了y也会爆炸 如果是DAG则直接拓扑排序+DP求出每个点出发能走到的最左端和最右端的点 有环则SCC缩点后再拓扑 用线段树优化建图的过程 边数$O(n ...

  3. Mono for Android 篇二 使用Spinner 实现下拉列表读取Browser.BookmarksUri

    http://www.cnblogs.com/ivanyb/archive/2013/03/05/2944818.html 1.首先在VS2010里面创建一个Android Application 简 ...

  4. 【Vijos】1792 摆花

    题目链接:https://vijos.org/p/1792 算法:DP 看到这题真的一点不会...只能爆搜一下..但太太慢了..看了题解后,听说是分组背包??不知道.. 好吧,,还是百度了下题解,渐渐 ...

  5. X.509证书_生成X.509协议的证书

    用法:1. 用NOTE打开,修改按实际情况脚本中的(1)~ (6)处参数2. 找一台含JVM环境的WIN机器3. 双击执行后,会生成一对密钥4. 请确保当前使用的JDK版本为6.0!!! @echo ...

  6. 关于HTML条件注释你可能不知道的一些事儿

    最近经常看到类似这样的HTML代码片段,很多前端开发人员应该都熟悉: 1 <!--[if lt IE 7]>      <html class="ie6"> ...

  7. IIS权限设置

    Check in the IIS Manager to see what authentication type is enabled on the directories that are part ...

  8. HOWTO:制作 Windows 7 加速部署映像(作者:苏繁)

    加速部署映像 - 也就是我们通常说的系统模板,通常我们为了提高 Windows 的安装速度,会事先制作一套包含驱动.应用软件.补丁程序以及自定义设置的标准化系统.这样我们在使用该加速部署映像完成安装后 ...

  9. unity3d 基于物理渲染的问题解决

    最近1个月做了unity 次世代开发的一些程序方面的支持工作,当然也是基于物理渲染相关的,主要还是skyshop marmoset的使用吧,他算是unity4.x版本 PBR的优秀方案之一了但在使用以 ...

  10. [源码]随机获取虾米音乐song_id API文件

    [源码]随机获取虾米音乐song_id API文件 January 11, 2015 注意:此API请放置于国内主机使用,如香港.北京等等,否则会提示:虾米音乐在您所处的国家或地区暂时无法使用 < ...