从时间戳开始
概念
时间戳(timestamp),通常是一个(long数字转化的)字符序列,唯一地标识某一刻的时间。
具体的说, 时间戳对应的数字, 是 从 0时区 ( 英国格林威治天文台对应的经线 ) 的 1970年1月1日 0 点 0 分 0 秒 开始, 到产生时间戳的时刻的总秒数。
重要的事情说 3 遍 : 地球上任何时区任何地点在同一个时刻取到的时间戳是相同的。
重要的事情说 3 遍 : 地球上任何时区任何地点在同一个时刻取到的时间戳是相同的。
重要的事情说 3 遍 : 地球上任何时区任何地点在同一个时刻取到的时间戳是相同的。
小测试
问题 :
一小时有3600 秒。 那么在我穿越到北京时间什么时候可以取到时间戳为 3600 ? 什么时候能取到时间戳 0 ?
答案 :
北京时间 1970/1/1 9:0:0 取到时间戳 3600
北京时间 1970/1/1 8:0:0 取到时间戳 0
Boost Data & Time
boost基于公历(儒略历)和 POSIX 时间规范编写了boost::gregorian 和boost::posix_time 两部分代码。
天 以及天以上的时间概念( 周 、 月 、 年 ) 属于Data* , 之下的概念(小时 、 分 、 秒、 毫秒 。。。 )属于Time*
良心工具站 :
http://tool.chinaz.com/Tools/unixtime.aspx
对时间的更精确描述方法
这里请先把相对论的知识忘记,想象时间的长河是x轴,我们所在的时间线是一条沿着x轴匀速延长的贪吃蛇, 蛇头所在就是此刻,蛇的身子都是历史。
另,此处只讨论如何在地球上描述时间, 如果您知道半人马星系的时间描述方式,请通知我。
时刻
某时某刻。
时间长河(x轴)上的一个点。具有唯一性。 没有长度值 。
比如 : 1970/1/1 0:0:0 。 生活中我经常这样表示唯一时刻,但是那是因为我从未离开东八区的掌控。想要精确的表示时刻,需要再加上时区 :
1970/1/1 0:0:0 UTC == (timestamp) 0
> boost::gregorian::date // 到某天
> boost::posix_time::ptime // 时刻
时间段
一段时间。
时间长河(x轴)上的 … 啥也不是 。 是个平行于时间长河(x轴)的向量, 不具有唯一性, 有长度。
比如一天, 比如1秒。
> boost::gregorian::data_duration
> boost::gregorian::months
> boost::gregorian::years
> boost::gregorian::weeks
>
> boost::poxis_time::hours
> boost::poxis_time::minutes
> boost::poxis_time::seconds
> boost::poxis_time::microseconds
> boost::poxis_time::nanoseconds
时期
从某时刻到某时刻。
时间长河(x轴)上的一段线段, 具有唯一性, 有长度。
比如 1970/1/1 0:0:0 UTC 到 1970/1/2 0:0:0 UTC 。 长度是1天。
> boost::gregorian::data_period
> boost::poxis_time::time_period
时间迭代器
给定一个时刻和步长, 则按照步长向前(++)/后(–) 遍历时刻。
> boost::gregorian::date_iterator //纯虚
> boost::gregorian::day_iterator
> boost::gregorian::week_iterator
> boost::gregorian::month_iterator
> boost::gregorian::year_iterator
>
> boost::poxis_time::time_iterator
功能简述
与字符串的交互
- 时刻
- 时刻支持由字符串构造
- 2002/1/25
- 20020125
- 2002-01-20 23:59:59.000
- 20020131T235959
- 时刻可以转换为字符串
- 时刻支持由字符串构造
to_simple_string //2002-Jan-01 10:00:01.123456789
to_iso_string //20020131T100001,123456789
to_iso_extended_string //2002-01-31T10:00:01,123456789
- 时间段 ( 仅time_duration)
to_simple_string //2002-Jan-01 10:00:01.123456789
to_iso_string //20020131T100001,123456789
- 时期
to_iso_string //[2002-Jan-01/2002-Jan-31]
// [2002-Jan-01 01:25:10.000000001/ 2002-Jan-31 01:25:10.123456789]
和C的交互
tm to_tm(date)
date date_from_tm(tm datetm)
tm to_tm(ptime)
ptime ptime_from_tm(tm timetm)
tm to_tm(time_duration)
ptime from_time_t(std::time_t)
基本运算 :
- 时刻 +- 时间段 = 时刻
时刻 +- 时刻 = 时间段
时刻之间可以依照时刻先后比较 ( “` < 、> 、 <= 、 >= 、 == 、 !=)
- 时间段之间可以依照时间长短比较 ( 。。。 )
时期功能
- 可以查询时刻与时期的关系 ( 之前 , 之后 , 位于 。。。 )
- 时期之间可以合并,串联, 。。。。
时间查询
boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); // 精确到秒
boost::gregorian::date d(boost::gregorian::day_clock::local_day()); // 今天
typedef boost::date_time::c_local_adjustor<ptime> local_adj;
boost::posix_time::ptime utc_now = local_adj::utc_to_local(now); // UTC 时区
boost::posix::ptime zero= boost::posix_time::from_time_t(time(0)); // UTC 1970/1/1 0:0:0
// 如果使用 东八区的ptime - UTC的ptime获得的秒数是时间戳 + 3600×8
long timestamp = (utc_now - zero).total_seconds() ;// 时间戳
特色接口
提供比如 是否闰年 , 是否闰秒 , 这个月有几天之类的各种有规范可依据的接口。
http://www.boost.org/doc/libs/1_58_0/doc/html/date_time/gregorian.html
http://www.boost.org/doc/libs/1_58_0/doc/html/date_time/posix_time.html