原文链接:http://www.baiyy.com/article/index/id/18/cid/2
C++封装的时间和日期类,方便的操作时间。包括时间段、日期类、时间类。支持 时间/日期 加/减/比较,时间戳/字符串 相互转换。
一个包含三个类:Duration、Date、Time。
Duration 表示一个时间段
Date 表示一个日期,精确到秒,比如2016-01-01 12:00:00
Time 表示一个时间,精确到微秒,可以Date相互转换。
直接下载源代码 ec_date_src.zip
项目地址:https://github.com/havesnag/date
文档地址:http://www.baiyy.com/public/project/ecdate/index.html
基本示例
#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;
}
Duration
表示日期一个时间段,支持以下几种类型:
MicroSecond | 微秒 1/1000000秒 |
MilliSecond | 毫秒 1/1000秒 |
Second | 秒 |
Minute | 分 60秒 |
Hour | 小时 3600秒 |
Day | 天 86400秒 |
Week | 周 604800秒 |
Month | 月 周与月相互转换比例为1:4 |
Year | 年 |
// 10 days ec::Duration d0(10, ec::Duration::Day); // 240 hours ec::Duration d1 = d0.down(); // 864000 seconds int64 seconds = d0.valueAs(ec::Duration::Second);
Date
// 2016-01-01 00:00:00 ec::Date d0(2016, 1, 1, 0, 0, 0); // 2016-01-02 00:00:00 d0.add(ec::Duration(1, ec::Duration::Hour); // 2015-01-02 00:00:00 d0.setYear(2015);
Date 成员函数
| Time (time_t stamp) |
| 以时间戳构造 |
| |
| Time (const Date &date) |
| 以Date对象构造 |
| |
| Time (const Time &time) |
| 以Time对象复制 |
| |
Time | clone () const |
| 克隆当前对象 |
| |
Date | toDate () const |
| 转换成Date对象 |
| |
time_t | stamp () const |
| 获取时间戳 |
| |
time_t | seconds () const |
| 获取秒数,等同于时间戳 |
| |
long | microSeconds () const |
| 获取微秒数, [0,1000000) 更多... |
| |
int64 | milliStamp () const |
| 获取毫秒时间戳 |
| |
int64 | microStamp () const |
| 获取微秒时间戳 |
| |
Time & | set (time_t seconds, long microSeconds=0) |
| 设置秒数和微秒数 |
| |
Time & | setSeconds (time_t seconds) |
| 获取微秒数, [0,1000) |
| |
Time & | setMicroSeconds (long microSeconds) |
| 获取微秒数, [0,1000000) |
| |
Time & | zeroSet (Duration::Period period) |
| 设置为某个时间的开始 更多... |
| |
Time & | add (int64 value, Duration::Period period) |
| 加/减 一段时间 |
| |
Time & | add (const Duration &duration) |
| 加/减 一段时间 |
| |
Time & | addWeek (int value) |
| 加/减 周 |
| |
Time & | addDay (int value) |
| 加/减 天 |
| |
Time & | addHour (int value) |
| 加/减 时 |
| |
Time & | addMinute (int value) |
| 加/减 分 |
| |
Time & | addSecond (int value) |
| 加/减 秒 |
| |
Time & | addMilliSecond (int value) |
| 加/减 毫秒 |
| |
Time & | addMicroSecond (int value) |
| 加/减 微秒 |
| |
int64 | diff (const Time &other, Duration::Period period=Duration::Second) |
| 比较两时间之间的差异 更多... |
| |
time_t | getUTCStamp () const |
| 获取UTC时间戳 |
| |
int64 | getUTCFullMicroSeconds () const |
| 距离1970-01-01 00:00:00的微秒数 |
| |
int64 | getUTCFullMilliSeconds () const |
| 距离1970-01-01 00:00:00的毫秒数 |
| |
time_t | getUTCFullSeconds () const |
| 距离1970-01-01 00:00:00的秒数 更多... |
| |
int | getUTCFullMinutes () const |
| 距离1970-01-01 00:00:00的分钟 |
| |
int | getUTCFullHours () const |
| 距离1970-01-01 00:00:00的小时 |
| |
int | getUTCFullDays () const |
| 距离1970-01-01 00:00:00的天数 |
| |
int | getUTCFullWeeks () const |
| 距离1970-01-01 00:00:00的周数 更多... |
| |
Time | operator+ (const Duration &duration) |
| |
Time | operator- (const Duration &duration) |
| |
Duration | operator- (const Time &other) |
| |
Time & | operator+= (const Duration &duration) |
| |
Time & | operator-= (const Duration &duration) |
| |
bool | operator< (const Time &other) |
| |
bool | operator= (const Time &other) |
Time
// The current time ec::Time t0; // 2016-01-01 00:00:00 ec::Time t1(ec::Date(2016, 1, 1)); // seconds
Time 成员函数
| Time (time_t stamp) |
| 以时间戳构造 |
| |
| Time (const Date &date) |
| 以Date对象构造 |
| |
| Time (const Time &time) |
| 以Time对象复制 |
| |
Time | clone () const |
| 克隆当前对象 |
| |
Date | toDate () const |
| 转换成Date对象 |
| |
time_t | stamp () const |
| 获取时间戳 |
| |
time_t | seconds () const |
| 获取秒数,等同于时间戳 |
| |
long | microSeconds () const |
| 获取微秒数, [0,1000000) 更多... |
| |
int64 | milliStamp () const |
| 获取毫秒时间戳 |
| |
int64 | microStamp () const |
| 获取微秒时间戳 |
| |
Time & | set (time_t seconds, long microSeconds=0) |
| 设置秒数和微秒数 |
| |
Time & | setSeconds (time_t seconds) |
| 获取微秒数, [0,1000) |
| |
Time & | setMicroSeconds (long microSeconds) |
| 获取微秒数, [0,1000000) |
| |
Time & | zeroSet (Duration::Period period) |
| 设置为某个时间的开始 更多... |
| |
Time & | add (int64 value, Duration::Period period) |
| 加/减 一段时间 |
| |
Time & | add (const Duration &duration) |
| 加/减 一段时间 |
| |
Time & | addWeek (int value) |
| 加/减 周 |
| |
Time & | addDay (int value) |
| 加/减 天 |
| |
Time & | addHour (int value) |
| 加/减 时 |
| |
Time & | addMinute (int value) |
| 加/减 分 |
| |
Time & | addSecond (int value) |
| 加/减 秒 |
| |
Time & | addMilliSecond (int value) |
| 加/减 毫秒 |
| |
Time & | addMicroSecond (int value) |
| 加/减 微秒 |
| |
int64 | diff (const Time &other, Duration::Period period=Duration::Second) |
| 比较两时间之间的差异 更多... |
| |
time_t | getUTCStamp () const |
| 获取UTC时间戳 |
| |
int64 | getUTCFullMicroSeconds () const |
| 距离1970-01-01 00:00:00的微秒数 |
| |
int64 | getUTCFullMilliSeconds () const |
| 距离1970-01-01 00:00:00的毫秒数 |
| |
time_t | getUTCFullSeconds () const |
| 距离1970-01-01 00:00:00的秒数 更多... |
| |
int | getUTCFullMinutes () const |
| 距离1970-01-01 00:00:00的分钟 |
| |
int | getUTCFullHours () const |
| 距离1970-01-01 00:00:00的小时 |
| |
int | getUTCFullDays () const |
| 距离1970-01-01 00:00:00的天数 |
| |
int | getUTCFullWeeks () const |
| 距离1970-01-01 00:00:00的周数 更多... |
| |
Time | operator+ (const Duration &duration) |
| |
Time | operator- (const Duration &duration) |
| |
Duration | operator- (const Time &other) |
| |
Time & | operator+= (const Duration &duration) |
| |
Time & | operator-= (const Duration &duration) |
| |
bool | operator< (const Time &other) |
| |
bool | operator= (const Time &other) |