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

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

来源:网络收集 时间:2026-08-09
导读: 儒略日和儒略日日期 儒略日的起点订在公元前4713年(天文学上记为 -4712年)1月1日格林威治时间平午(世界时12:00),即JD 0指定为UT时间B.C.4713年1月1日12:00到UC时间B.C.4713年1月2日12:00的24小时。注意这一天

儒略日和儒略日日期

儒略日的起点订在公元前4713年(天文学上记为 -4712年)1月1日格林威治时间平午(世界时12:00),即JD 0指定为UT时间B.C.4713年1月1日12:00到UC时间B.C.4713年1月2日12:00的24小时。注意这一天是礼拜一。每一天赋予了一个唯一的数字,顺数而下,如:1996年1月1日12:00:00的儒略日是2450084。这个日期是考虑了太阳、月亮的轨道运行周期,以及当时收税的间隔而订出来的。Joseph Scliger定义儒略周期为7980年,是因28、19、15的最小公倍数为28×19×15=7980。

日期的注意事项:

1. 0是一个合法数字(根据ISO 8691和天文年编号) 2. 0年是一个闰年。

3. 负数是不支持的。比如说公元前1年。

4. 格里高利历同历史上的日期可能不同,由于它们采用的日历方式不同。 5. 最好只是用DateTime用来计算当前的时间。对于历史上的或者天文日历的时间计算,还是使用其他的特定软件。

构造DateTime:

1. 可以从一个已有的DateTime构造 2. 当前的时间和日期 3. 一个Timestamp对象

4. 用年、月、日、时、分、秒、微秒、毫秒构造 5. 使用一个儒略日日期构造(用double形式保存)

成员函数:

1. int year() const 返回年

2. int month() const 返回月(1-12)

3. int week(int firstDayOfWeek = DateTime::MONDAY) const

返回所在的周,根据ISO 8601标准(第一周是1月4日所在的周),一周的第一天是礼拜一或者礼拜天。

4. int day() const

返回月中的所在天(1 - 31) 5. int dayOfWeek() const

返回周中的所在天 0为周日,1为周一,..... 6. int dayOfYear() const

返回年中的所在天(1 - 366) 7. int hour() const

返回天中所在小时(0 - 23) 8. int hourAMPM() const

返回上下午所在小时(0 - 12) 9. bool isAM() const 如果上午返回真 10. bool isPM() const 如果下午返回真 11. int minute() const 返回分钟数(0 - 59) 12. int second() const 返回秒数(0 - 59)

13. int millisecond() const 返回毫秒数(0 - 999)

14. int microsecond() const 返回微秒数(0 - 999)

15. Timestamp timestamp() const

返回用Timestamp保存的日历时间(精度微秒) 16. Timestamp::UtcTimeVal utcTime() const

返回用Timestamp保存的日历时间(精度100纳秒) 17. DateTime支持关系运算符(==, !=, >, >=, <, <=). 18. DateTime支持算术操作(+, -, +=, -=)

静态函数:

1. bool isLeapYear(int year) 所给年是否闰年

2. int daysOfMonth(int year, int month) 所给年和月的天数

3. bool isValid(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond)

判断所给年月日是否合法

下面是DateTime的一个例子:

[cpp] view plaincopy

1. #include \ 2. using Poco::DateTime;

3. int main(int argc, char** argv) 4. {

5. DateTime now; // the current date and time in UTC 6. int year = now.year(); 7. int month = now.month(); 8. int day = now.day(); 9. int dow = now.dayOfWeek(); 10. int doy = now.dayOfYear(); 11. int hour = now.hour(); 12. int hour12 = now.hourAMPM(); 13. int min = now.minute(); 14. int sec = now.second(); 15. int ms = now.millisecond(); 16. int us = now.microsecond(); 17. double jd = now.julianDay();

18. Poco::Timestamp ts = now.timestamp();

19. DateTime xmas(2006, 12, 25); // 2006-12-25 00:00:00 20. Poco::Timespan timeToXmas = xmas - now; 21. 22.

23. DateTime dt(1973, 9, 12, 2, 30, 45); // 1973-09-12 02:30:45

24. dt.assign(2006, 10, 13, 13, 45, 12, 345); // 2006-10-13 12:45:12.345 25. bool isAM = dt.isAM(); // false

26. bool isPM = dt.isPM(); // true

27. bool isLeap = DateTime::isLeapYear(2006); // false 28. int days = DateTime::daysOfMonth(2006, 2); // 28

29. bool isValid = DateTime::isValid(2006, 02, 29); // false 30. dt.assign(2006, DateTime::OCTOBER, 22); // 2006-10-22 00:00:00 31. if (dt.dayOfWeek() == DateTime::SUNDAY) 32. {

33. // ... 34. }

35. return 0; 36. }

4. LocalDateTime类

Poco::LocalDateTime同Poco::DateTime类似,不同的是Poco::LocalDateTime存储一个本地时间。关于本地时间和UTC时间有如下计算公式: (UTC时间 = 本地时间 - 时区差).

构造函数:

1. 通过当前时间构造 2. 通过Timestamp对象

3. 通过年、月、日、时、分、秒、微秒、毫秒构造 4. 通过儒略日时间构造(儒略日时间用double存储)

5. 作为可选项。时区可作为构造时第一个参数被指定。(如果没有指定的话,会使用系统当前的时区)

成员函数:

1. LocalDateTime支持所有的DateTime的函数。

2. 在进行比较之前,所有的关系操作符函数会把时间都换算为UTC时间 3. int tzd() const 返回时区差

4. DateTime utc() const 转换本地时间到utc时间

下面是一个例子:

[cpp] view plaincopy

1. #include \ 2. using Poco::LocalDateTime; 3. int main(int argc, char** argv) 4. {

5. LocalDateTime now; // the current date and local time 6. int year = now.year(); 7. int month = now.month(); 8. int day = now.day(); 9. int dow = now.dayOfWeek(); 10. int doy = now.dayOfYear();

11. int hour = now.hour(); 12. int hour12 = now.hourAMPM(); 13. int min = now.minute(); 14. int sec = now.second(); 15. int ms = now.millisecond(); 16. int us = now.microsecond(); 17. int tzd = now.tzd();

18. double jd = now.julianDay();

19. Poco::Timestamp ts = now.timestamp(); 20. 21.

22. Loc …… 此处隐藏:4031字,全部文档内容请下载后查看。喜欢就下载吧 ……

POCO C++库学习和分析-- 日期与时间(3).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)