多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Qt5 中的日期和时间 > 原文: [http://zetcode.com/gui/qt5/datetime/](http://zetcode.com/gui/qt5/datetime/) 在 Qt5 C++ 编程教程的这一部分中,我们将讨论时间和日期。 Qt5 具有`QDate`,`QTime`和`QDateTime`类以与日期和时间一起使用。 `QDate`是用于使用公历中的日历日期的类。 它具有确定日期,比较或操纵日期的方法。 `QTime`类使用时钟时间。 它提供了比较时间,确定时间的方法以及其他各种时间操纵方法。 `QDateTime`是将`QDate`和`QTime`对象结合为一个对象的类。 ## 初始化日期和时间对象 日期和时间对象可以通过两种基本方式进行初始化。 我们可以在对象构造器中对其进行初始化,也可以创建空对象并在以后用数据填充它们。 `init.cpp` ```cpp #include <QTextStream> #include <QDate> #include <QTime> int main(void) { QTextStream out(stdout); QDate dt1(2015, 4, 12); out << "The date is " << dt1.toString() << endl; QDate dt2; dt2.setDate(2015, 3, 3); out << "The date is " << dt2.toString() << endl; QTime tm1(17, 30, 12, 55); out << "The time is " << tm1.toString("hh:mm:ss.zzz") << endl; QTime tm2; tm2.setHMS(13, 52, 45, 155); out << "The time is " << tm2.toString("hh:mm:ss.zzz") << endl; } ``` 我们以两种方式初始化日期和时间对象。 ```cpp QDate dt1(2015, 4, 12); ``` `QDate`对象构造器采用三个参数:年,月和日。 ```cpp out << "The date is " << dt1.toString() << endl; ``` 日期将打印到控制台。 我们使用`toString()`方法将日期对象转换为字符串。 ```cpp QTime tm2; tm2.setHMS(13, 52, 45, 155); ``` 空的`QTime`对象已创建。 我们使用`setHMS()`方法用数据填充对象。 参数是小时,分钟,秒和毫秒。 ```cpp out << "The time is " << tm2.toString("hh:mm:ss.zzz") << endl; ``` 我们将`QTime`对象打印到控制台。 我们使用的特定格式还包括毫秒,默认情况下会省略毫秒。 输出: ```cpp $ ./init The date is Sun Apr 12 2015 The date is Tue Mar 3 2015 The time is 17:30:12.055 The time is 13:52:45.155 ``` ## 当前日期和时间 在以下示例中,我们将当前本地时间和日期打印到控制台。 `curdatetime.cpp` ```cpp #include <QTextStream> #include <QTime> #include <QDate> int main(void) { QTextStream out(stdout); QDate cd = QDate::currentDate(); QTime ct = QTime::currentTime(); out << "Current date is: " << cd.toString() << endl; out << "Current time is: " << ct.toString() << endl; } ``` 请注意,不得将文件命名为`time.cpp`。 ```cpp QDate cd = QDate::currentDate(); ``` `QDate::currentDate()`静态函数返回当前日期。 ```cpp QTime ct = QTime::currentTime(); ``` `QTime::currentTime()`静态函数返回当前时间。 ```cpp out << "Current date is: " << cd.toString() << endl; out << "Current time is: " << ct.toString() << endl; ``` 我们使用`toString()`方法将日期和时间对象转换为字符串。 输出: ```cpp $ ./curdatetime Current date is: Fri Oct 30 2015 Current time is: 20:55:27 ``` ## 比较日期 关系运算符可用于比较日期。 我们可以比较他们在日历中的位置。 `comparedates.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate dt1(2015, 4, 5); QDate dt2(2014, 4, 5); if (dt1 < dt2) { out << dt1.toString() << " comes before " << dt2.toString() << endl; } else { out << dt1.toString() << " comes after " << dt2.toString() << endl; } } ``` 该示例比较两个日期。 ```cpp QDate dt1(2015, 4, 5); QDate dt2(2014, 4, 5); ``` 我们有两个不同的日期。 ```cpp if (dt1 < dt2) { out << dt1.toString() << " comes before " << dt2.toString() << endl; } else { out << dt1.toString() << " comes after " << dt2.toString() << endl; } ``` 我们使用小于(`<`)的比较运算符比较日期,并确定其中哪个位于日历的更早位置。 输出: ```cpp $ ./comparedates Sun Apr 5 2015 comes after Sat Apr 5 2014 ``` 比较运算符也可以轻松用于`QTime`和`QDateTime`对象。 ## 确定闰年 闰年是包含另一天的年份。 日历中额外一天的原因是天文日历年与日历年之间的差异。 日历年正好是 365 天,而天文学年(地球绕太阳公转的时间)是 365.25 天。 相差 6 个小时,这意味着在四年的时间里,我们一天中都没有。 因为我们希望日历与季节同步,所以每四年将 2 月增加一天。 (有例外。)在公历中,闰年的 2 月有 29 天,而不是通常的 28 天。该年持续 366 天,而不是通常的 365 天。 `QDate::isLeapYear()`静态方法确定年份是否为闰年。 `leapyear.cpp` ```cpp #include <QTextStream> #include <QDate> int main() { QTextStream out(stdout); QList<int> years({2010, 2011, 2012, 2013, 2014, 2015, 2016}); foreach (int year, years) { if (QDate::isLeapYear(year)) { out << year << " is a leap year" << endl; } else { out << year << " is not a leap year" << endl; } } } ``` 在示例中,我们有一个年份列表。 我们每年检查是否是闰年。 ```cpp QList<int> years({2010, 2011, 2012, 2013, 2014, 2015, 2016}); ``` 我们初始化一个年份列表。 这是 C++ 11 构造,因此,我们需要启用 C++ 11。 我们需要将`CONFIG += c++11`,`QMAKE_CXXFLAGS += -std=c++11`或`QMAKE_CXXFLAGS += -std=c++0x`添加到`.pro`文件。 ```cpp foreach (int year, years) { if (QDate::isLeapYear(year)) { out << year << " is a leap year" << endl; } else { out << year << " is not a leap year" << endl; } } ``` 我们遍历列表,确定给定的年份是否为闰年。 `QDate::isLeapYear()`返回布尔值`true`或`false`。 `leapyear.pro` ```cpp ###################################################################### # Automatically generated by qmake (3.0) Fri Oct 30 21:01:31 2015 ###################################################################### TEMPLATE = app TARGET = leapyear INCLUDEPATH += . CONFIG += c++11 # Input SOURCES += leapyear.cpp QT -= gui ``` 这是项目文件。 我们添加`CONFIG += c++11`声明以启用 C++ 11。 我们用`QT -= gui`禁用了 Qt GUI 模块,因为命令行程序不需要它。 输出: ```cpp $ ./leapyear 2010 is not a leap year 2011 is not a leap year 2012 is a leap year 2013 is not a leap year 2014 is not a leap year 2015 is not a leap year 2016 is a leap year ``` ## 预定义的日期格式 Qt5 具有一些内置的日期格式。 `QDate`对象的`toString()`方法采用日期格式作为参数。 Qt5 使用的默认日期格式为`Qt::TextDate`。 `dateformats.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate cd = QDate::currentDate(); out << "Today is " << cd.toString(Qt::TextDate) << endl; out << "Today is " << cd.toString(Qt::ISODate) << endl; out << "Today is " << cd.toString(Qt::SystemLocaleShortDate) << endl; out << "Today is " << cd.toString(Qt::SystemLocaleLongDate) << endl; out << "Today is " << cd.toString(Qt::DefaultLocaleShortDate) << endl; out << "Today is " << cd.toString(Qt::DefaultLocaleLongDate) << endl; out << "Today is " << cd.toString(Qt::SystemLocaleDate) << endl; out << "Today is " << cd.toString(Qt::LocaleDate) << endl; } ``` 在示例中,我们显示了当前日期的八种不同日期格式。 ```cpp out << "Today is " << cd.toString(Qt::ISODate) << endl; ``` 在这里,我们以`Qt::ISODate`格式打印当前日期,这是用于显示日期的国际标准。 输出: ```cpp $ ./dateformats Today is Sat Oct 31 2015 Today is 2015-10-31 Today is 10/31/15 Today is Saturday, October 31, 2015 Today is 10/31/15 Today is Saturday, October 31, 2015 Today is 10/31/15 Today is 10/31/15 ``` ## 自定义日期格式 日期可以用多种其他格式表示。 在 Qt5 中,我们也可以创建自定义日期格式。 `toString()`方法的另一个版本采用格式字符串,我们可以在其中使用各种格式说明符。 例如,`d`指示符代表一天,而不是前导零。 `dd`指示符代表一天,前导零。 下表列出了可用的日期格式表达式: | 表达式 | 输出 | | --- | --- | | `d` | 不带前导零(1 到 31)的日期 | | `dd` | 带前导零(01 到 31)的日期 | | `ddd` | 本地化日期的缩写(例如,`Mon`到`Sun`)。 使用`QDate::shortDayName()`。 | | `dddd` | 本地化的长名称(例如,`Monday`到`Sunday`)。 使用`QDate::longDayName()`。 | | `M` | 不带前导零(1 到 12)的月份 | | `MM` | 前导零(01 到 12)的月份 | | `MMM` | 本地化月份的缩写名称(例如,`"Jan"`到`"Dec"`)。 使用`QDate::shortMonthName()`。 | | `MMMM` | 本地化的长月份名称(例如,`January`到`December`)。 使用`QDate::longMonthName()`。 | | `y` | 两位数(00 到 99)的年份 | | `yyyy` | 四位数的年份。 如果年份为负数,则还会附加一个减号。 | Table: Date format specifiers `customdateformats.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate cd = QDate::currentDate(); out << "Today is " << cd.toString("yyyy-MM-dd") << endl; out << "Today is " << cd.toString("yy/M/dd") << endl; out << "Today is " << cd.toString("d. M. yyyy") << endl; out << "Today is " << cd.toString("d-MMMM-yyyy") << endl; } ``` 我们有四种自定义日期格式。 ```cpp out << "Today is " << cd.toString("yyyy-MM-dd") << endl; ``` 这是国际日期格式。 日期的各部分用笔划线分隔。 `yyyy`是具有四个数字的年份。 `MM`是月份,前导零(01 到 12)。 `dd`是带前导零(01 到 31)的数字的日子。 ```cpp out << "Today is " << cd.toString("yy/M/dd") << endl; ``` 这是另一种常见的日期格式。 零件之间用斜杠(`/`)字符分隔。 `M`指示符代表一个月,不带前导零(1 到 12)。 ```cpp out << "Today is " << cd.toString("d. M. yyyy") << endl; ``` 此日期格式在斯洛伐克使用。 零件由点字符分隔。 日和月没有前导零。 首先是日期,然后是月份,最后是年份。 输出: ```cpp $ ./customdateformats Today is 2015-10-31 Today is 15/10/31 Today is 31\. 10\. 2015 Today is 31-October-2015 ``` ## 预定义的时间格式 时间具有一些预定义的格式。 标准格式说明符与日期格式中使用的说明符相同。 Qt5 使用的默认时间格式为`Qt::TextDate`。 `timeformats.cpp` ```cpp #include <QTextStream> #include <QTime> int main(void) { QTextStream out(stdout); QTime ct = QTime::currentTime(); out << "The time is " << ct.toString(Qt::TextDate) << endl; out << "The time is " << ct.toString(Qt::ISODate) << endl; out << "The time is " << ct.toString(Qt::SystemLocaleShortDate) << endl; out << "The time is " << ct.toString(Qt::SystemLocaleLongDate) << endl; out << "The time is " << ct.toString(Qt::DefaultLocaleShortDate) << endl; out << "The time is " << ct.toString(Qt::DefaultLocaleLongDate) << endl; out << "The time is " << ct.toString(Qt::SystemLocaleDate) << endl; out << "The time is " << ct.toString(Qt::LocaleDate) << endl; } ``` 在示例中,我们显示了当前时间的八种不同时间格式。 ```cpp out << "The time is " << ct.toString(Qt::ISODate) << endl; ``` 在这里,我们以`Qt::ISODate`格式打印当前时间,这是用于显示时间的国际标准。 输出: ```cpp $ ./timeformats The time is 15:58:26 The time is 15:58:26 The time is 3:58 PM The time is 3:58:26 PM CET The time is 3:58 PM The time is 3:58:26 PM CET The time is 3:58 PM The time is 3:58 PM ``` ## 自定义时间格式 我们可以创建其他时间格式。 我们在使用时间格式说明符的地方构建自定义时间格式。 下表列出了可用的格式表达式。 | 表达式 | 输出 | | --- | --- | | `h` | 没有前导零的小时(如果显示 AM/PM,则为 0 到 23 或 1 到 12) | | `hh` | 带前导零的小时(如果显示 AM/PM,则为 00 到 23 或 01 到 12) | | `H` | 没有前导零的小时(0 到 23,即使有 AM/PM 显示) | | `HH` | 带前导零的小时(00 到 23,即使有 AM/PM 显示) | | `m` | 没有前导零(0 到 59)的分钟 | | `mm` | 带前导零(00 到 59)的分钟 | | `s` | 不带前导零(0 到 59)的秒 | | `ss` | 带有前导零(00 到 59)的秒 | | `z` | 不带前导零的毫秒数(0 到 999) | | `zz` | 带有前导零的毫秒数(000 到 999) | | `AP` 或 `A` | 使用 AM/PM 显示。 `AP` 将被`"AM"`或`"PM"`替换。 | | `ap` 或 `a` | 使用 am/pm 显示。 `ap` 将被`"am"`或`"pm"`替换。 | | `t` | 时区(例如`"CEST"`) | Table: Time format specifiers `customtimeformats.cpp` ```cpp #include <QTextStream> #include <QTime> int main(void) { QTextStream out(stdout); QTime ct = QTime::currentTime(); out << "The time is " << ct.toString("hh:mm:ss.zzz") << endl; out << "The time is " << ct.toString("h:m:s a") << endl; out << "The time is " << ct.toString("H:m:s A") << endl; out << "The time is " << ct.toString("h:m AP") << endl; out << "The version of Qt5 is " << qVersion() << endl; } ``` 我们有四种自定义时间格式。 ```cpp out << "The time is " << ct.toString("hh:mm:ss.zzz") << endl; ``` 在这种格式下,我们具有小时,分钟和秒,前导零。 我们还以毫秒为单位加上前导零。 ```cpp out << "The time is " << ct.toString("h:m:s a") << endl; ``` 此时间格式说明符使用小时,分钟和秒(不带前导零),并添加 AM/PM 周期标识符。 输出: ```cpp $ ./customtimeformats The time is 16:23:43.542 The time is 4:23:43 pm The time is 16:23:43 PM The time is 4:23 PM ``` ## 检索工作日 `dayOfWeek()`方法返回一个代表星期几的数字,其中 1 是星期一,7 是星期日。 `weekday.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate cd = QDate::currentDate(); int wd = cd.dayOfWeek(); out << "Today is " << QDate::shortDayName(wd) << endl; out << "Today is " << QDate::longDayName(wd) << endl; } ``` 在示例中,我们打印当前工作日的简称和长名称。 ```cpp QDate cd = QDate::currentDate(); ``` 我们得到当前日期。 ```cpp int wd = cd.dayOfWeek(); ``` 从当前日期开始,我们得到星期几。 ```cpp out << "Today is " << QDate::shortDayName(wd) << endl; ``` 使用`QDate::shortDayName()`静态方法,我们得到工作日的简称。 ```cpp out << "Today is " << QDate::longDayName(wd) << endl; ``` 使用`QDate::longDayName()`静态方法,我们获得了工作日的长名称。 输出: ```cpp $ ./weekday Today is Sat Today is Saturday ``` ## 天数 我们可以使用`daysInMonth()`方法计算特定月份中的天数,并使用`daysInYear()`方法计算一年中的天数。 `nofdays.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QList<QString> months; months.append("January"); months.append("February"); months.append("March"); months.append("April"); months.append("May"); months.append("June"); months.append("July"); months.append("August"); months.append("September"); months.append("October"); months.append("November"); months.append("December"); QDate dt1(2015, 9, 18); QDate dt2(2015, 2, 11); QDate dt3(2015, 5, 1); QDate dt4(2015, 12, 11); QDate dt5(2015, 1, 21); out << "There are " << dt1.daysInMonth() << " days in " << months.at(dt1.month()-1) << endl; out << "There are " << dt2.daysInMonth() << " days in " << months.at(dt2.month()-1) << endl; out << "There are " << dt3.daysInMonth() << " days in " << months.at(dt3.month()-1) << endl; out << "There are " << dt4.daysInMonth() << " days in " << months.at(dt4.month()-1) << endl; out << "There are " << dt5.daysInMonth() << " days in " << months.at(dt5.month()-1) << endl; out << "There are " << dt1.daysInYear() << " days in year " << QString::number(dt1.year()) << endl; } ``` 创建了五个日期对象。 我们计算这些月份和特定年份的天数。 ```cpp QDate dt1(2015, 9, 18); QDate dt2(2015, 2, 11); QDate dt3(2015, 5, 1); QDate dt4(2015, 12, 11); QDate dt5(2015, 1, 21); ``` 创建了五个`QDate`对象。 每个代表不同的日期。 ```cpp out << "There are " << dt1.daysInMonth() << " days in " << months.at(dt1.month()-1) << endl; ``` 我们使用`daysInMonth()`方法获取日期对象中的天数。 ```cpp out << "There are " << dt1.daysInYear() << " days in year " << QString::number(dt1.year()) << endl; ``` 在这里,我们使用日期对象的`daysInYear()`方法获得一年中的天数。 输出: ```cpp $ ./nofdays There are 30 days in September There are 28 days in February There are 31 days in May There are 31 days in December There are 31 days in January There are 365 days in year 2015 ``` ## 检查日期的有效性 有`isValid()`方法可以检查日期是否有效。 `validity.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QList<QDate> dates({QDate(2015, 5, 11), QDate(2015, 8, 1), QDate(2015, 2, 30)}); for (int i=0; i < dates.size(); i++) { if (dates.at(i).isValid()) { out << "Date " << i+1 << " is a valid date" << endl; } else { out << "Date " << i+1 << " is not a valid date" << endl; } } } ``` 在示例中,我们检查了三天的有效性。 ```cpp QList<QDate> dates({QDate(2015, 5, 11), QDate(2015, 8, 1), QDate(2015, 2, 30)}); ``` 前两天有效。 第三个无效。 2 月有 28 或 29 天。 ```cpp if (dates.at(i).isValid()) { out << "Date " << i+1 << " is a valid date" << endl; } else { out << "Date " << i+1 << " is not a valid date" << endl; } ``` 根据`isValid()`方法的结果,我们将有关日期有效性的消息打印到控制台。 输出: ```cpp $ ./validity Date 1 is a valid date Date 2 is a valid date Date 3 is not a valid date ``` ## 到...的天数 我们可以轻松地从特定日期算起 n 天的日期。 我们使用`addDays()`方法。 `daysTo()`方法返回到选定日期的天数。 `daystofrom.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate dt(2015, 5, 11); QDate nd = dt.addDays(55); QDate xmas(2015, 12, 24); out << "55 days from " << dt.toString() << " is " << nd.toString() << endl; out << "There are " << QDate::currentDate().daysTo(xmas) << " days till Christmas" << endl; } ``` 从 2015 年 5 月 11 日起,我们将在 55 天后获得日期。我们还将获得圣诞节前的天数。 ```cpp QDate dt(2015, 5, 11); QDate nd = dt.addDays(55); ``` `addDays()`方法返回给定日期之后 55 天的`QDate`。 ```cpp QDate xmas(2015, 12, 24); ... out << "There are " << QDate::currentDate().daysTo(xmas) << " days till Christmas" << endl; ``` 我们使用`daysTo()`方法来计算直到圣诞节的天数。 输出: ```cpp $ ./daystofrom 55 days from Mon May 11 2015 is Sun Jul 5 2015 There are 54 days till Christmas ``` ## `QDateTime`类 `QDateTime`对象包含日历日期和时钟时间。 它是`QDate`和`QTime`类的组合。 它有许多相似的方法,用法与这两类相同。 `datetime.cpp` ```cpp #include <QTextStream> #include <QDateTime> int main(void) { QTextStream out(stdout); QDateTime cdt = QDateTime::currentDateTime(); out << "The current datetime is " << cdt.toString() << endl; out << "The current date is " << cdt.date().toString() << endl; out << "The current time is " << cdt.time().toString() << endl; } ``` 该示例检索当前日期时间。 ```cpp out << "The current datetime is " << cdt.toString() << endl; ``` 这行代码将当前日期时间打印到终端。 ```cpp out << "The current date is " << cdt.date().toString() << endl; ``` 此行使用`date()`方法检索日期时间对象的日期部分。 输出: ```cpp $ ./datetime The current datetime is Sat Oct 31 17:10:50 2015 The current date is Sat Oct 31 2015 The current time is 17:10:50 ``` ## 朱利安日 儒略日是指儒略时期开始以来的连续天数。 它主要由天文学家使用。 不应将其与朱利安历法相混淆。 朱利安纪元开始于公元前 4713 年。 朱利安天数 0 被指定为从公元前 4713 年 1 月 1 日正午开始的那一天。 朱利安天数(JDN)是自此时间段开始以来经过的天数。 任意时刻的儒略日期(JD)是前一个正午的儒略日编号加上该时刻以来的那一天的分数。 (Qt5 不会计算此分数。)除天文学外,军事和大型机程序经常使用儒略日期。 `julianday.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate cd = QDate::currentDate(); out << "Gregorian date for today: " << cd.toString(Qt::ISODate) << endl; out << "Julian day for today: " << cd.toJulianDay() << endl; } ``` 在示例中,我们计算了今天的公历日期和儒略日。 ```cpp out << "Julian day for today: " << cd.toJulianDay() << endl; ``` 使用`toJulianDay()`方法返回儒略日。 输出: ```cpp $ ./julianday Gregorian date for today: 2015-10-31 Julian day for today: 2457327 ``` 使用儒略日期,很容易进行计算。 `battles.cpp` ```cpp #include <QTextStream> #include <QDate> int main(void) { QTextStream out(stdout); QDate bordate(1812, 9, 7); QDate slavdate(1805, 12, 2); QDate cd = QDate::currentDate(); int j_today = cd.toJulianDay(); int j_borodino = bordate.toJulianDay(); int j_slavkov = slavdate.toJulianDay(); out << "Days since Slavkov battle: " << j_today - j_slavkov << endl; out << "Days since Borodino battle: " << j_today - j_borodino << endl; } ``` 该示例计算自两次历史事件以来经过的天数。 ```cpp QDate bordate(1812, 9, 7); QDate slavdate(1805, 12, 2); ``` 我们有拿破仑纪元的两次战斗。 ```cpp int j_today = cd.toJulianDay(); int j_borodino = bordate.toJulianDay(); int j_slavkov = slavdate.toJulianDay(); ``` 我们计算了今天以及斯拉夫科夫和波罗底诺战役的儒略日。 ```cpp out << "Days since Slavkov battle: " << j_today - j_slavkov << endl; out << "Days since Borodino battle: " << j_today - j_borodino << endl; ``` 我们计算了两次战斗以来经过的天数。 输出: ```cpp $ date Sat Oct 31 20:07:32 CET 2015 $ ./battles Days since Slavkov battle: 76669 Days since Borodino battle: 74198 ``` 自 2015 年 10 月 31 日起,斯拉夫科夫战役已经过去了 76669 天,而波罗底诺战役已经过去了 74198 天。 ## UTC 时间 我们的星球是一个球体。 它绕其轴旋转。 地球向东旋转。 因此,太阳在不同位置的不同时间升起。 地球大约每 24 小时旋转一次。 因此,世界被划分为 24 个时区。 在每个时区,都有一个不同的本地时间。 夏令时通常会进一步修改此本地时间。 实际需要一个全球时间。 全球时间可以避免时区和夏令时的混淆。 UTC(世界标准时间)被选为主要时间标准。 UTC 用于航空,天气预报,飞行计划,空中交通管制通关和地图。 与当地时间不同,UTC 不会随季节变化而变化。 `utclocal.cpp` ```cpp #include <QTextStream> #include <QDateTime> int main(void) { QTextStream out(stdout); QDateTime cdt = QDateTime::currentDateTime(); out << "Universal datetime: " << cdt.toUTC().toString() << endl; out << "Local datetime: " << cdt.toLocalTime().toString() << endl; } ``` 在示例中,我们计算当前日期时间。 我们用 UTC 日期时间和本地日期时间表示日期时间。 ```cpp out << "Universal datetime" << cdt.toUTC().toString() << endl; ``` `toUTC()`方法用于获取 UTC 日期时间。 ```cpp out << "Local datetime" << cdt.toLocalTime().toString() << endl; ``` `toLocalTime()`用于获取本地日期时间。 输出: ```cpp $ ./utclocal Universal datetime: Sat Oct 31 19:09:44 2015 GMT Local datetime: Sat Oct 31 20:09:44 2015 ``` 该示例在布拉迪斯拉发运行,该站点的中欧时间(CET)为 UTC + 1 小时。 ## Unix 纪元 纪元是选择作为特定纪元起源的时间瞬间。 例如,在西方基督教国家,时间从耶稣出生的第 0 天开始。 另一个例子是法国共和党日历,使用了十二年。 这个时期是 1792 年 9 月 22 日宣布的共和纪元的开始,即宣布成立第一共和国并废除君主制的那一天。 电脑也有自己的纪元。 最受欢迎的版本之一是 Unix 纪元。 Unix 纪元是 1970 年 1 月 1 日 UTC 时间 00:00:00(或`1970-01-01T00:00:00Z ISO8601`)。 计算机中的日期和时间是根据自该计算机或平台的定义时期以来经过的秒数或时钟滴答数确定的。 Unix 时间是自 Unix 纪元以来经过的秒数。 ```cpp $ date +%s 1446318984 ``` Unix date 命令可用于获取 Unix 时间。 在这个特定时刻,自 Unix 纪元以来已经过去了 1446318984 秒。 `unixepoch.cpp` ```cpp #include <QTextStream> #include <QDateTime> #include <ctime> int main(void) { QTextStream out(stdout); time_t t = time(0); out << t << endl; QDateTime dt; dt.setTime_t(t); out << dt.toString() << endl; QDateTime cd = QDateTime::currentDateTime(); out << cd.toTime_t() << endl; } ``` 在示例中,我们使用两个 Qt5 函数获取 Unix 时间并将其转换为人类可读的形式。 ```cpp #include <ctime> ``` 我们包括标准的 C++ 时间头文件。 ```cpp time_t t = time(0); out << t << endl; ``` 使用标准的 C++ `time()`命令,我们可以获得 Unix 时间。 ```cpp QDateTime dt; dt.setTime_t(t); out << dt.toString() << endl; ``` `setTime_t()`方法用于将 Unix 时间转换为日期时间,并将其格式化为易于阅读的格式。 ```cpp QDateTime cd = QDateTime::currentDateTime(); out << cd.toTime_t() << endl; ``` Qt5 的`toTime_t()`方法也可以用来获取 Unix 时间。 输出: ```cpp $ ./unixepoch 1446319003 Sat Oct 31 20:16:43 2015 1446319003 ``` 在本章中,我们处理了时间和日期。