用C++封装了个时间类,大家帮忙完善一下

华时纳 2016-10-08 11:20:48
C++封装的时间和日期类,方便的操作时间。包括时间段、日期类、时间类。支持 时间/日期 加/减/比较,时间戳/字符串 相互转换。


一个包含三个类:Duration、Date、Time。

Duration 表示一个时间段

Date 表示一个日期,精确到秒,比如2016-01-01 12:00:00

Time 表示一个时间,精确到微秒,可以Date相互转换。


项目地址:https://github.com/havesnag/date

文档地址:http://www.baiyy.com/public/project/ecdate/index.html

使用教程:http://www.baiyy.com/article/index/id/18/cid/2

基本示例
#include <iostream>
#include "date.h"

int main(int argc, char *argv[])
{
// The current time
ec::Time now;

// 10 hours
ec::Duration d(10, ec::Duration::Hour);

// After 10 hours
now += d;

// output like 2016-01-01 12:00:00
std::cout << now.toString() << std::endl;
return 0;
}
...全文
636 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
华时纳 2016-10-10
  • 打赏
  • 举报
回复
2016-10-10 12:00:00更新: 1、修正了计算闰年的问题; 2、修正根据日期构建Date时时间有偏差的问题; 3、Date增加获取时区的接口; 4、Date增加对UTC的支持; 5、Time可直接转换为UTC基准时间的Date对象;
jiqiang01234 2016-10-09
  • 打赏
  • 举报
回复
考虑夏令时了没?欧洲很多国家还是有夏令时的
paschen 版主 2016-10-09
  • 打赏
  • 举报
回复
可以参考MFC、ATL等库中对时间处理的封装
华时纳 2016-10-09
  • 打赏
  • 举报
回复
引用 1 楼 lianshaohua 的回复:
参照std::chrono库来完善吧。
o
引用 8 楼 xuzzzhen123 的回复:
[quote=引用 4 楼 havesnag 的回复:] [quote=引用 2 楼 zjq9931 的回复:] 不错,支持原创. 还没有看源码,问几个问题: 闰年,闰秒问题,是使用系统库C++库处理的,还是库中自己处理的? 还有这个库的极限是多少? 比如是否存在2038年问题等等之类的极限问题。
希望做到极简单,没有引入第三库,主要是对C基础时间API的封装。 闰年自己算的,闰秒还未处理。 Windows下Date类的极限是1970-01-01,Linux本身支持支持。 多谢支持![/quote] Linux本身支持支持什么?[/quote] Linux下本身支持公元前,时间戳小到负数也可以,不过尚未深度测试
华时纳 2016-10-09
  • 打赏
  • 举报
回复
引用 5 楼 paschen 的回复:
闰年的判断不够准确: bool Date::isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0); } 四年一闰,百年不闰,四百年再闰 否则2000年判断时将会得到不是闰年
多谢,欢迎提交BUG和需要完善的功能,
赵4老师 2016-10-09
  • 打赏
  • 举报
回复
The COleDateTime class handles dates from 1 January 100 – 31 December 9999.
赵4老师 2016-10-09
  • 打赏
  • 举报
回复
参考COleDateTime和COleDateTimeSpan: COleDateTime Class Members Construction COleDateTime Constructs a COleDateTime object. GetCurrentTime Creates a COleDateTime object that represents the current time (static member function). Attributes GetStatus Gets the status (validity) of this COleDateTime object. SetStatus Sets the status (validity) of this COleDateTime object. GetAsSystemTime Converts the time in the COleDateTime object to be represented as aSYSTEMTIME data structure. GetYear Returns the year this COleDateTime object represents. GetMonth Returns the month this COleDateTime object represents (1 – 12). GetDay Returns the day this COleDateTime object represents (1 – 31). GetHour Returns the hour this COleDateTime object represents (0 – 23). GetMinute Returns the minute this COleDateTime object represents (0 – 59). GetSecond Returns the second this COleDateTime object represents (0 – 59). GetDayOfWeek Returns the day of the week this COleDateTime object represents (Sunday = 1). GetDayOfYear Returns the day of the year this COleDateTime object represents (Jan 1 = 1). Operations SetDateTime Sets the value of this COleDateTime object to the specified date/time value. SetDate Sets the value of this COleDateTime object to the specified date-only value. SetTime Sets the value of this COleDateTime object to the specified time-only value. Format Generates a formatted string representation of a COleDateTime object. ParseDateTime Reads a date/time value from a string and sets the value of COleDateTime. Operators operator DATE Converts a ColeDateTime value into a DATE. operator DATE* Converts a ColeDateTime value into a DATE*. operator = Copies a COleDateTime value. operator +, - Add and subtract ColeDateTime values. operator +=, -= Add and subtract a ColeDateTime value from this COleDateTime object. operator ==, <, <=, etc. Compare two ColeDateTime values. Data Members m_dt Contains the underlying DATE for this COleDateTime object. m_status Contains the status of this COleDateTime object. Archive/Dump operator << Outputs a COleDateTime value to Carchive or CDumpContext. operator >> Inputs a ColeDateTime object from CArchive. COleDateTime Overview | Hierarchy Chart COleDateTimeSpan Class Members Constructor COleDateTimeSpan Constructs a COleDateTimeSpan object. Attributes GetStatus Gets the status (validity) of this COleDateTimeSpan object. SetStatus Sets the status (validity) of this COleDateTimeSpan object. GetDays Returns the day portion of the span this COleDateTimeSpan object represents. GetHours Returns the hour portion of the span this COleDateTimeSpan object represents. GetMinutes Returns the minute portion of the span this COleDateTimeSpan object represents. GetSeconds Returns the second portion of the span this COleDateTimeSpan object represents. GetTotalDays Returns the number of days this COleDateTimeSpan object represents. GetTotalHours Returns the number of hours this COleDateTimeSpan object represents. GetTotalMinutes Returns the number of minutes this COleDateTimeSpan object represents. GetTotalSeconds Returns the number of seconds this COleDateTimeSpan object represents. Operations SetDateTimeSpan Sets the value of this COleDateTimeSpan object. Format Generates a formatted string representation of a COleDateTimeSpan object. Operators operator double Converts this COleDateTimeSpan value to a double. operator = Copies a COleDateTimeSpan value. operator +, - Add, subtract, and change sign for COleDateTimeSpan values. operator +=, -= Add and subtract a COleDateTimeSpan value from this COleDateTimeSpan value. operator ==, <, <= Compare two COleDateTimeSpan values. Data Members m_span Contains the underlying double for this COleDateTimeSpan object. m_status Contains the status of this COleDateTimeSpan object. Dump/Archive operator << Outputs a COleDateTimeSpan value to CArchive or CDumpContext. operator >> Inputs a COleDateTimeSpan object from CArchive. COleDateTimeSpan Overview | Hierarchy Chart
天外怪魔 2016-10-09
  • 打赏
  • 举报
回复
引用 4 楼 havesnag 的回复:
[quote=引用 2 楼 zjq9931 的回复:] 不错,支持原创. 还没有看源码,问几个问题: 闰年,闰秒问题,是使用系统库C++库处理的,还是库中自己处理的? 还有这个库的极限是多少? 比如是否存在2038年问题等等之类的极限问题。
希望做到极简单,没有引入第三库,主要是对C基础时间API的封装。 闰年自己算的,闰秒还未处理。 Windows下Date类的极限是1970-01-01,Linux本身支持支持。 多谢支持![/quote] Linux本身支持支持什么?
yshuise 2016-10-09
  • 打赏
  • 举报
回复
时间还是比较复杂的。
paschen 版主 2016-10-09
  • 打赏
  • 举报
回复
如果要更精确,能被100整除而不能被400整除不是闰年,能被3200整除的不是闰年
paschen 版主 2016-10-09
  • 打赏
  • 举报
回复
闰年的判断不够准确: bool Date::isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0); } 四年一闰,百年不闰,四百年再闰 否则2000年判断时将会得到不是闰年
华时纳 2016-10-09
  • 打赏
  • 举报
回复
引用 2 楼 zjq9931 的回复:
不错,支持原创. 还没有看源码,问几个问题: 闰年,闰秒问题,是使用系统库C++库处理的,还是库中自己处理的? 还有这个库的极限是多少? 比如是否存在2038年问题等等之类的极限问题。
希望做到极简单,没有引入第三库,主要是对C基础时间API的封装。 闰年自己算的,闰秒还未处理。 Windows下Date类的极限是1970-01-01,Linux本身支持支持。 多谢支持!
paschen 版主 2016-10-09
  • 打赏
  • 举报
回复
不错
  • 打赏
  • 举报
回复
不错,支持原创. 还没有看源码,问几个问题: 闰年,闰秒问题,是使用系统库C++库处理的,还是库中自己处理的? 还有这个库的极限是多少? 比如是否存在2038年问题等等之类的极限问题。
ztenv 版主 2016-10-09
  • 打赏
  • 举报
回复
参照std::chrono库来完善吧。
百亿云科技 2016-10-09
  • 打赏
  • 举报
回复
写得不错,希望保持更新!
华时纳 2016-10-09
  • 打赏
  • 举报
回复
引用 14 楼 jiqiang01234 的回复:
考虑夏令时了没?欧洲很多国家还是有夏令时的
多谢,的确有很多我没想到的地方

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧