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 ...
随机推荐
- C#可以做什么
C#可以做什么 学以致用.这也是大家学习C#的目标.当我们充分了解C#广阔的应用,才会激发我们最大的学习热情.下面将介绍一下C#的常见应用场景本文选自明明白白学C#大学霸. 0.2.1 开发普通软件 ...
- Animator窗口视图Project视图PlayerIdleAnimation和PlayerWalkingAnimation
Animator窗口视图Project视图PlayerIdleAnimation和PlayerWalkingAnimation 通过上一小节的操作,我们新建了2个动画:PlayerIdleAnimat ...
- pda完美结合开销售单据haohantech-安卓打票扫描PDA智能POS 发展现状如何,会取代传统电脑 代替手工开单 么?
现场开单时指业务员每天到终端客户的现场进行谈单和接单的动作流程,现场开单一般主要是集中在BCD类门店和一些批发零售小店 现场开单的优势:现场开单可以及时掌控终端客户情况,促进客情关系,提高产品的铺市率 ...
- Codeforces Round #329 (Div. 2)
推迟了15分钟开始,B卡住不会,最后弃疗,rating只涨一分... 水(暴力枚举) A - 2Char /******************************************** ...
- c++ map删除元素
typedef std::map<std::string,float> StringFloatMap; StringFloatMap col1; StringFloatMap::itera ...
- sql中的视图
视图可分为:普通视图和索引视图视图的作用: 1.提高数据库的安全性,降低数据库被攻击的风险,使外界无法得知数据库的数据结构 2.使查询更方便,简化程序员的操作非只读视图可以增删改,但不建议.视图的试用 ...
- Mac or Centos 下如何编译objective-C
#import <Foundation/Foundation.h> int main(int argc,const char *argv[]){ @autoreleasepool{ NSL ...
- nginx源码编译安装
安装编译所需的包: [root@xaiofan ~]# yum install -y gcc gcc-c++ autoconf automake 安装nginx使用某些功能需要的包: [root@xa ...
- 决策树ID3算法示例
决策树代码如下: #include "MyID3.h" using namespace std; void ReadData() //读入数据 { ifstream fin(&qu ...
- Canvas - 气泡
Main.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...