设为首页 加入收藏

TOP

POCO C++库学习和分析 -- 日期与时间 (三)
2014-11-24 03:20:57 】 浏览:1135
Tags:POCO 学习 分析 --日期 时间
(UtcTimeva l val);
/// Creates a timestamp from a UTC time value.

static Timeva l resolution();
/// Returns the resolution in units per second.
/// Since the timestamp has microsecond resolution,
/// the returned value is always 1000000.

private:
Timeva l _ts;
};
Timestamp内部定义了一个Int64的变量_ts。存储了一个基于utc时间的64位int值,理论上可以提供微秒级的精度(实际精度依赖于操作系统)。由于Poco::Timestamp是基于UTC(世界标准时间或世界 )的,所以它是独立于时区设置的。Poco::Timestamp实现了值语义,比较和简单的算术操作。
1. UTC (Coordinated Universal Time)是从1582年10月15日深夜开始计时的. Poco库中精度为100纳秒。
2. epoch time指是从1970年1月1日深夜开始计时的(指unix诞生元年)。Poco库中精度为1秒。

数据类型:
Poco::Timestamp内部定义了下列数据类型:
1. Timeva l
一个64位的int整数值,保存utc时间,精度微秒
2. UtcTimeva l
一个64位的int整数值,保存utc时间,精度100纳秒(真实精度仍然是微秒)
3. TimeDiff
一个64位的int整数值,保存两个Timestamp的差值,精度微秒


构造函数:
1. 默认构造函数会以当前时间初始化一个Timestamp值,基于UTC时间(从1582年10月15日开始计时,精度为100纳秒)
2. 提供了两个静态函数用于创建Timestamp对象,
a) Timestamp fromEpochTime(time_t time)。这个函数从time_t构建,内部会把EpochTime(从1970年1月1日深夜开始计时的,精度为1秒)的时间转换成为UTC时间。
b) Timestamp fromUtcTime(UtcTimeva l val)。这个函数从一个UtcTimeva l构建。

Timestamp的成员函数:
1. time_t epochTime() const
返回一个以epoch time计算的日历时间(精度秒)。(函数内部会把基于UTC时间的值转为基于epoch time的值)
2. UtcTimeva l utcTime() const
返回一个以UTC时间计算的日历时间(精度100纳秒)。
3. Timeva l epochMicroseconds() const
返回一个以epoch time计算的日历时间(精度微秒)
4. void update()
取当前的时间更新
5. TimeDiff elapsed() const
返回当前时间与Timestamp内部时间_ts的一个时间差值(精度微秒)
6. bool isElapsed(TimeDiff interval) const
如果当前时间与Timestamp内部时间_ts的一个时间差值大于interval时间,返回true。(精度微秒)

Timestamp算术计算:
1. Timestamp operator + (TimeDiff diff) const
增加一个时间偏移,并返回值。(精度微秒)
2. Timestamp operator - (TimeDiff diff) const
减掉一个时间偏移,并返回值。(精度微秒)
3. TimeDiff operator - (const Timestamp& ts) const
返回两个Timestamp对象的时间偏移。(精度微秒)
4. Timestamp& operator += (TimeDiff d)
Timestamp& operator -= (TimeDiff d)
增加或减小一个时间偏移值

下面来看一个例子:


[cpp]
#include "Poco/Timestamp.h"
#include
using Poco::Timestamp;
int main(int argc, char** argv)
{
Timestamp now; // the current date and time
std::time_t t1 = now.epochTime(); // convert to time_t ...
Timestamp ts1(Timestamp::fromEpochTime(t1)); // ... and back again
for (int i = 0; i < 100000; ++i) ; // wait a bit
Timestamp::TimeDiff diff = now.elapsed(); // how long did it take
Timestamp start(now); // save start time
now.update(); // update with current
time diff = now - start; // again, how long
return 0;
}

#include "Poco/Timestamp.h"
#include
using Poco::Timestamp;
int main(int argc, char** argv)
{
Timestamp now; // the current date and time
std::time_t t1 = now.epochTime(); // convert to time_t ...
Timestamp ts1(Timestamp::fromEpochTime(t1)); // ... and back again
for (int i = 0; i < 100000; ++i) ; // wait a bit
Timestamp::TimeDiff diff = now.elapsed(); // how long did it take
Timestamp start(now); // save start time
now.update(); // update with current
time diff = now - start; // again, how long
return 0;
}

3. DateTime类
Poco中提供了DateTime类,作用和tm类似。下面是它的定义:

[cpp]
class Foundation_API DateTime
{
public:
enum Months
/// Symbolic names for month numbers (1 to 12).
{
JANUARY = 1,

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 3/12/12
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇fzu 2035 Axial symmetry(几何) 下一篇BZOJ 2565 最长双回文串

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目