设为首页 加入收藏

TOP

POCO C++库学习和分析 -- 日期与时间 (一)
2014-11-24 03:20:57 】 浏览:1127
Tags:POCO 学习 分析 --日期 时间

POCO C++库学习和分析 -- 日期与时间


在Poco库中,与时间和日期相关的一些类,其内部实现是非常简单的。看相关文档时,比较有意思的倒是历史上的不同时间表示法。


1. 系统时间函数
编程时,时间函数不可避免的会被使用。linux系统下相关时间的数据结构有time_t,timeva l,timespec,tm,clock_t; windows下time_t,tm,SYSTEMTIME,clock_t。其中clock_t、timeva l、timespec用于表示时间跨度,time_t、tm、SYSTEMTIME用于表示绝对时间。不同的数据结构之间,多少也有些差异。

首先 这些时间结构体的精度不同,Second(time_t/tm), microsecond(timeva l/SYSTEMTIME), nanoSeconds(timespec)。

起始时间不同,time_t起始于1970年1月1日0时0分0秒,tm表示起始于1900年,SYSTEMTIME起始于1601年,clock起始于机器开机。

同这些数据结构相关联,C语言为tm,time_t提供了一组函数用于时间运算和数据结构转换:


[cpp]
// 日历时间(一个用time_t表示的整数)

// 比较日历时间
double difftime(time_t time1, time_t time0);
// 获取日历时间
time_t time(time_t * timer);
// 转换日历时间为字符串
char * ctime(const time_t *timer);
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(GMT timezone)
struct tm * gmtime(const time_t *timer);
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(本地 timezone)
struct tm * localtime(const time_t * timer);
// 关于本地时间的计算公式:
localtime = utctime[Gmt time] + utcOffset()[时区偏移] + dst()[夏令时偏移]


// 把tm转换为字符串
char * asctime(const struct tm * timeptr);
// 把tm转换为日历时间
time_t mktime(struct tm * timeptr);


// 获取开机以来的微秒数
clock_t clock (void);

// 日历时间(一个用time_t表示的整数)

// 比较日历时间
double difftime(time_t time1, time_t time0);
// 获取日历时间
time_t time(time_t * timer);
// 转换日历时间为字符串
char * ctime(const time_t *timer);
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(GMT timezone)
struct tm * gmtime(const time_t *timer);
// 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(本地 timezone)
struct tm * localtime(const time_t * timer);
// 关于本地时间的计算公式:
localtime = utctime[Gmt time] + utcOffset()[时区偏移] + dst()[夏令时偏移]


// 把tm转换为字符串
char * asctime(const struct tm * timeptr);
// 把tm转换为日历时间
time_t mktime(struct tm * timeptr);


// 获取开机以来的微秒数
clock_t clock (void);
我们回想一下程序中的时间数据结构和函数的用法,可以发现主要是2个目的:
1. 获取绝对时间
2. 获取两个时间点的相对时间


2. Timestamp类
C语言中函数类似,Poco中定义了自己的时间类。Timestamp类似于time_t,用于获取比较日历时间。Timestamp定义如下:


[cpp]
class Foundation_API Timestamp
{
public:
typedef Int64 Timeva l; /// monotonic UTC time value in microsecond resolution
typedef Int64 UtcTimeva l; /// monotonic UTC time value in 100 nanosecond resolution
typedef Int64 TimeDiff; /// difference between two timestamps in microseconds

Timestamp();
/// Creates a timestamp with the current time.

Timestamp(Timeva l tv);
/// Creates a timestamp from the given time value.

Timestamp(const Timestamp& other);
/// Copy constructor.

~Timestamp();
/// Destroys the timestamp

Timestamp& operator = (const Timestamp& other);
Timestamp& operator = (Timeva l tv);

void swap(Timestamp& timestamp);
/// Swaps the Timestamp with another one.

void update();
/// Updates the Timestamp with the current time.


bool operator == (const Timestamp& ts) const;
bool operator != (const Timestamp& ts) const;
bool operator > (const Timestamp& ts) const;
bool operator >= (const Timestamp& ts) const;
bool operator < (const Timestamp& ts) const;
bool operator <= (const Timestamp& ts) const;

Timestamp operator + (TimeDiff d) const;
Timestamp operator - (TimeDiff d) const;
TimeDiff operator - (const Timestamp& ts) const;
Timestamp& operator += (TimeDiff d);
Timestamp& operator -= (TimeDiff d);

std::time_t epochTim

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

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目