(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,