POCO C++库学习和分析-- 日期与时间
POCO C++库学习和分析 -- 日期与时间
在Poco库中,与时间和日期相关的一些类,其内部实现是非常简单的。看相关文档时,比较有意思的倒是历史上的不同时间表示法。
1. 系统时间函数
在编程时,时间函数不可避免的会被使用。linux系统下相关时间的数据结构有time_t,timeval,timespec,tm,clock_t; windows下time_t,tm,SYSTEMTIME,clock_t。其中clock_t、timeval、timespec用于表示时间跨度,time_t、tm、SYSTEMTIME用于表示绝对时间。不同的数据结构之间,多少也有些差异。 首先 这些时间结构体的精度不同,Second(time_t/tm), microsecond(timeval/SYSTEMTIME), nanoSeconds(timespec)。
起始时间不同,time_t起始于1970年1月1日0时0分0秒,tm表示起始于1900年,SYSTEMTIME起始于1601年,clock起始于机器开机。
同这些数据结构相关联,C语言为tm,time_t提供了一组函数用于时间运算和数据结构转换:
[cpp] view plaincopy
1. // 日历时间(一个用time_t表示的整数) 2.
3. // 比较日历时间
4. double difftime(time_t time1, time_t time0); 5. // 获取日历时间
6. time_t time(time_t * timer); 7. // 转换日历时间为字符串
8. char * ctime(const time_t *timer);
9. // 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(GMT timezone) 10. struct tm * gmtime(const time_t *timer);
11. // 转换日历时间为我们平时看到的把年月日时分秒分开显示的时间格式tm(本地 timezone) 12. struct tm * localtime(const time_t * timer); 13. // 关于本地时间的计算公式:
14. localtime = utctime[Gmt time] + utcOffset()[时区偏移] + dst()[夏令时偏移] 15. 16.
17. // 把tm转换为字符串
18. char * asctime(const struct tm * timeptr); 19. // 把tm转换为日历时间
20. time_t mktime(struct tm * timeptr); 21. 22.
23. // 获取开机以来的微秒数 24. clock_t clock (void);
我们回想一下程序中的时间数据结构和函数的用法,可以发现主要是2个目的:
1. 获取绝对时间
2. 获取两个时间点的相对时间
2. Timestamp类
同C语言中函数类似,Poco中定义了自己的时间类。Timestamp类似于time_t,用于获取比较日历时间。Timestamp定义如下:
[cpp] view plaincopy
1. class Foundation_API Timestamp 2. { 3. public:
4. typedef Int64 TimeVal; /// monotonic UTC time value in microsecond resolution 5. typedef Int64 UtcTimeVal; /// monotonic UTC time value in 100 nanosecond resolution 6. typedef Int64 TimeDiff; /// difference between two timestamps in microseconds 7.
8. Timestamp();
9. /// Creates a timestamp with the current time. 10.
11. Timestamp(TimeVal tv);
12. /// Creates a timestamp from the given time value. 13.
14. Timestamp(const Timestamp& other); 15. /// Copy constructor. 16.
17. ~Timestamp();
18. /// Destroys the timestamp 19.
20. Timestamp& operator = (const Timestamp& other); 21. Timestamp& operator = (TimeVal tv); 22.
23. void swap(Timestamp& timestamp);
24. /// Swaps the Timestamp with another one. 25.
26. void update();
27. /// Updates the Timestamp with the current time. 28. 29.
30. bool operator == (const Timestamp& ts) const; 31. bool operator != (const Timestamp& ts) const; 32. bool operator > (const Timestamp& ts) const; 33. bool operator >= (const Timestamp& ts) const; 34. bool operator < (const Timestamp& ts) const; 35. bool operator <= (const Timestamp& ts) const; 36.
37. Timestamp operator + (TimeDiff d) const;
38. Timestamp operator - (TimeDiff d) const;
39. TimeDiff operator - (const Timestamp& ts) const; 40. Timestamp& operator += (TimeDiff d); 41. Timestamp& operator -= (TimeDiff d); 42.
43. std::time_t epochTime() const;
44. /// Returns the timestamp expressed in time_t. 45. /// time_t base time is midnight, January 1, 1970. 46. /// Resolution is one second. 47.
48. UtcTimeVal utcTime() const;
49. /// Returns the timestamp expressed in UTC-based 50. /// time. UTC base time is midnight, October 15, 1582. 51. /// Resolution is 100 nanoseconds. 52.
53. TimeVal epochMicroseconds() const;
54. /// Returns the timestamp expressed in microseconds 55. /// since the Unix epoch, midnight, January 1, 1970. 56.
57. TimeDiff elapsed() const;
58. /// Returns the time elapsed since the time denoted by 59. /// the timestamp. Equivalent to Timestamp() - *this. 60.
61. bool isElapsed(TimeDiff interval) const;
62. /// Returns true iff the given interval has passed 63. /// since the time denoted by the timestamp. 64.
65. static Timestamp fromEpochTime(std::time_t t); 66. /// Creates a timestamp from a std::time_t. 67.
68. static Timestamp fromUtcTime(UtcTimeVal val); 69. /// Creates a timestamp from a UTC time value. 70.
71. static TimeVal resolution();
72. /// Returns the resolution in units per second. 73. /// Since the timestamp has microsecond resolution, 74. /// the returned value is always 1000000. 75.
76. private:
77. TimeVal _ts; 78. };
Timestamp内部定义了一个Int64的变量_ts。存储了一个基于utc时间的64位int值,理论上可以提供微秒级的精度(实际精度依赖于操作系统)。由于Poco::Timestamp是基于UTC(世界标准时间或世界協調時間)的,所以它是独立于时区设置的。Poco::Timestamp实现了值语义,比较和简单的算术操作。
1. UTC (Coordinated Universal Time …… 此处隐藏:4061字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [综合文档]应答器设备技术规范(征求意见稿)A1
- [综合文档]教师 2012年高考政治试题按考点分类汇
- [综合文档]保险公司的总经理助理竞职演说
- [综合文档]卫生应急大练兵大比武活动考试--题库(
- [综合文档]徐州经济技术开发区总体规划环境影响报
- [综合文档]汉语拼音表(带声调)
- [综合文档]二年级 上 思维训练( 1~18)
- [综合文档]特色学校五年发展规划
- [综合文档]机床经常出现报警“X1轴定位监控”
- [综合文档]《电子技术基础》21.§5—2、3、4 习题
- [综合文档]浙江省深化普通高中课程改革
- [综合文档]CRISP原理 - 图文
- [综合文档]2017年电大社会调查研究与方法形考答案
- [综合文档]浅析建筑施工安全毕业论文
- [综合文档]《回忆我的母亲》名师教案
- [综合文档]装饰装修工程监理规划
- [综合文档]三下乡心得体会-文艺
- [综合文档]柱计算长度系数 - 图文
- [综合文档]全流程思考,提高燃电系统热电转换率--
- [综合文档]2018年嘉定区中考物理一模含答案
- 433M车库门滚动码遥控器
- 8、架空线路施工规范
- 大学四年声乐学习的体会
- 新北师大版五年级数学上册《轴对称再认
- 部编版五年级上册语文第六单元小结复习
- 小学六年级英语形容词用法
- 第2课 抗美援朝保家卫国 课件01(岳麓版
- 2015年天津大学运筹学基础考研真题,考
- 微机计算机控制技术课后于海生(第2版)
- 安全教育实践活动
- Delphi程序设计教程_第1章_Delphi概述
- 第八讲 工业革命与启蒙运动
- 《中华人民共和国药典》2005年版二部勘
- 科粤版九年级化学2.3构成物质的微粒(1)
- 西师大版数学三年级下册《长方形、正方
- ch6_冒泡排序演示
- 第4章 冲裁模具设计
- 浙江中小民营企业员工流失论文[终稿]
- 再议有线数字电视市场营运模式
- 昆明供水工程监理大纲




