教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 综合文档 >

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

来源:网络收集 时间:2026-08-09
导读: 11. int days = age.days(); // in days 12. int hours = age.totalHours(); // in hours 13. int secs = age.totalSeconds(); // in seconds 14. // when was I 10000 days old? 15. Timespan span(10000*Timespan

11. int days = age.days(); // in days

12. int hours = age.totalHours(); // in hours 13. int secs = age.totalSeconds(); // in seconds 14. // when was I 10000 days old? 15. Timespan span(10000*Timespan::DAYS); 16. DateTime dt = birthdate + span; 17. return 0; 18. }

6. Timezone类

Poco::Timezone提供静态函数用于获取时区信息和夏令时信息。 1. 时区差

2. 是否采用夏时制(daylight saving time (DST)) 3. 时区名

成员函数:

1. int utcOffset()

返回本地时间相对于UTC时间的差值(精度秒)。不包括夏令时偏移: (local time = UTC + utcOffset()) 2. int dst()

返回夏令时偏移。通常是固定值3600秒。0的话表示无夏令时。 3. bool isDst(const Timestamp& timestamp)

对于给定的timestamp时间测试,是否使用夏令时 4. int tzd()

返回本地时间相对于UTC时间的差值(精度秒)。包括夏令时偏移: (tzd = utcOffset() + dst()) 5. std::string name() 返回当前的时区名

6. std::string standardName()

返回当前的标准时区名(如果不采用夏令时) 7. std::string dstName()

返回当前的时区名(如果采用夏令时)

8. 时区名的返回依赖于操作系统,并且名称不具有移植性,仅作显示用。

下面是一个使用的例子:

[cpp] view plaincopy

1. #include \ 2. #include \ 3. using Poco::Timezone; 4. using Poco::Timestamp;

5. int main(int argc, char** argv) 6. {

7. int utcOffset = Timezone::utcOffset(); 8. int dst = Timezone::dst();

9. bool isDst = Timezone::isDst(Timestamp()); 10. int tzd = Timezone::tzd();

11. std::string name = Timezone::name();

12. std::string stdName = Timezone::standardName(); 13. std::string dstName = Timezone::dstName(); 14. return 0; 15. }

6. Poco::DateTimeFormatter类

Poco::DateTimeFormatter用来定义当Timestamp, DateTime, LocalDateTime and Timespan转换为字符串时所需的日期和事件格式。Poco::DateTimeFormatter的作用和strftime()是类似的。Poco::DateTimeFormat内部定义了一些约定的格式。

1. ISO8601_FORMAT (2005-01-01T12:00:00+01:00) 2. RFC1123_FORMAT (Sat, 1 Jan 2005 12:00:00 +0100) 3. SORTABLE_FORMAT (2005-01-01 12:00:00)

4. For more information, please see the reference documentation.

成员函数:

所有的DateTimeFormatter函数都是静态的。

下面是一个使用的例子:

[cpp] view plaincopy

1. #include \ 2. #include \

3. #include \ 4. #include \ 5. using Poco::DateTimeFormatter; 6. using Poco::DateTimeFormat; 7. int main(int argc, char** argv) 8. {

9. Poco::DateTime dt(2006, 10, 22, 15, 22, 34);

10. std::string s(DateTimeFormatter::format(dt, \)); 11. // \ 12. Poco::Timestamp now;

13. s = DateTimeFormatter::format(now, DateTimeFormat::SORTABLE_FORMAT); 14. // \

15. Poco::Timespan span(5, 11, 33, 0, 0);

16. s = DateTimeFormatter::format(span, \); 17. // \ 18. return 0; 19. }

7. Poco::DateTimeParser类

Poco::DateTimeParser用来从字符串中解析时间和日期。下面是其一个例子:

[cpp] view plaincopy

1. #include \ 2. #include \ 3. #include \ 4. #include \ 5. #include \ 6. using Poco::DateTimeParser; 7. using Poco::DateTimeFormat; 8. using Poco::DateTime;

9. int main(int argc, char** argv) 10. {

11. std::string s(\); 12. int tzd; 13. DateTime dt;

14. DateTimeParser::parse(DateTimeFormat::RFC1123_FORMAT, s, dt, tzd); 15. Poco::Timestamp ts = dt.timestamp(); 16. Poco::LocalDateTime ldt(tzd, dt);

17. bool ok = DateTimeParser::tryParse(\, dt, tzd); 18. ok = DateTimeParser::tryParse(\, \, dt, tzd); 19. return 0; 20. }

8. Stopwatch类

Stopwatch用来测量时间差值,精度为微秒.下面是其定义:

[cpp] view plaincopy

1. class Foundation_API Stopwatch

2. /// A simple facility to measure time intervals 3. /// with microsecond resolution. 4. ///

5. /// Note that Stopwatch is based on the Timestamp 6. /// class. Therefore, if during a Stopwatch run, 7. /// the system time is changed, the measured time 8. /// will not be correct. 9. { 10. public:

11. Stopwatch(); 12. ~Stopwatch();

13. 14.

15. void start();

16. /// Starts (or restarts) the stopwatch. 17.

18. void stop();

19. /// Stops or pauses the stopwatch. 20.

21. void reset();

22. /// Resets the stopwatch. 23.

24. void restart();

25. /// Resets and starts the stopwatch. 26.

27. Timestamp::TimeDiff elapsed() const;

28. /// Returns the elapsed time in microseconds 29. /// since the stopwatch started. 30.

31. int elapsedSeconds() const;

32. /// Returns the number of seconds elapsed 33. /// since the stopwatch started. 34. 35.

36. static Timestamp::TimeVal resolution();

37. /// Returns the resolution of the stopwatch. 38. 39.

40. private:

41. Stopwatch(const Stopwatch&);

42. Stopwatch& operator = (const Stopwatch&); 43. 44.

45. Timestamp _start; 46. Timestamp::TimeDiff _elapsed; 47. bool _running; 48. };

附录:

1. The Gregorian Calendar: http://en.wikipedia.org/wiki/Gregorian_calend …… 此处隐藏:2116字,全部文档内容请下载后查看。喜欢就下载吧 ……

POCO C++库学习和分析-- 日期与时间(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/403843.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)