ZetCode

Qt5 中的日期和时间

最后修改于 2023 年 10 月 18 日

在本 Qt5 C++ 编程教程中,我们将讨论时间和日期。

Qt5 拥有 QDateQTimeQDateTime 类来处理日期和时间。QDate 是一个用于处理公历日期的类。它具有用于确定日期、比较或操作日期的方法。QTime 类用于处理时钟时间。它提供了用于比较时间、确定时间和各种其他时间操作方法的方法。QDateTime 是一个将 QDateQTime 对象组合成一个对象的类。

在本章中,我们创建了命令行程序;因此,我们不需要 Qt GUI 模块。我们可以将 QT -= gui 声明添加到项目文件中。

Qt5 初始化日期和时间对象

日期和时间对象可以用两种基本方式初始化。我们在对象构造函数中初始化它们,或者我们可以创建空对象并在以后用数据填充它们。

init.cpp
#include <QTextStream>
#include <QDate>
#include <QTime>

int main(void) {

   QTextStream out(stdout);

   QDate dt1 { 2020, 4, 12 };
   out << "The date is " << dt1.toString() << endl;

   QDate dt2;
   dt2.setDate(2020, 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;
}

我们以两种方式初始化日期和时间对象。

QDate dt1 { 2020, 4, 12 };

QDate 对象构造函数接受三个参数:年、月和日。

out << "The date is " << dt1.toString() << endl;

日期被打印到控制台。我们使用 toString 方法将日期对象转换为字符串。

QTime tm2;
tm2.setHMS(13, 52, 45, 155);

创建一个空的 QTime 对象。我们使用 setHMS 方法用数据填充该对象。参数是小时、分钟、秒和毫秒。

out << "The time is " << tm2.toString("hh:mm:ss.zzz") << endl;

我们将 QTime 对象打印到控制台。我们使用一个特定的格式,该格式也包括毫秒,默认情况下会省略毫秒。

$ ./init
The date is Sun Apr 12 2020
The date is Tue Mar 3 2020
The time is 17:30:12.055
The time is 13:52:45.155

Qt5 当前日期和时间

在下面的示例中,我们将当前本地时间和日期打印到控制台。

current_datetime.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

QDate cd = QDate::currentDate();

QDate::currentDate 静态函数返回当前日期。

QTime ct = QTime::currentTime();

QTime::currentTime 静态函数返回当前时间。

out << "Current date is: " << cd.toString() << endl;
out << "Current time is: " << ct.toString() << endl;

我们使用 toString 方法将日期和时间对象转换为字符串。

$ ./current 
Current date is: Thu Dec 3 2020
Current time is: 10:21:48

Qt5 比较日期

关系运算符可用于比较日期。我们可以比较它们在日历中的位置。

compare_dates.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

   QTextStream out(stdout);

   QDate dt1 { 2020, 4, 5 };
   QDate dt2 { 2019, 4, 5 };

   if (dt1 < dt2) {

       out << dt1.toString() << " comes before "
            << dt2.toString() << endl;
   } else {

       out << dt1.toString() << " comes after "
            << dt2.toString() << endl;
   }
}

该示例比较了两个日期。

QDate dt1 { 2020, 4, 5 };
QDate dt2 { 2019, 4, 5 };

我们有两个不同的日期。

if (dt1 < dt2) {

    out << dt1.toString() << " comes before "
            << dt2.toString() << endl;
} else {

    out << dt1.toString() << " comes after "
            << dt2.toString() << endl;
}

我们使用小于(<)比较运算符比较日期,并确定它们中哪个位于日历中较早的位置。

$ ./compare 
Sun Apr 5 2020 comes after Fri Apr 5 2019

比较运算符也可以轻松地用于 QTimeQDateTime 对象。

Qt5 确定闰年

一个闰年是一个包含额外一天的年份。日历中增加额外一天的原因是天文年和日历年之间的差异。日历年正好有 365 天,而天文年(地球绕太阳公转一周的时间)是 365.25 天。

差异是 6 小时,这意味着在四年内我们会错过一天。因为我们希望我们的日历与季节同步,所以每四年我们在二月增加一天。(也有例外情况。)在公历中,闰年的二月有 29 天,而不是通常的 28 天。并且这一年持续 366 天,而不是通常的 365 天。

QDate::isLeapYear 静态方法确定某一年是否为闰年。

leapyear.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

   QTextStream out(stdout);

   QList<int> years({2010, 2011, 2012, 2013, 2014, 2015, 2016, 2020, 2024});

   for (int year: years) {

       if (QDate::isLeapYear(year)) {

           out << year << " is a leap year" << endl;
       } else {

           out << year << " is not a leap year" << endl;
       }
   }
}

在这个例子中,我们有一个年份列表。我们检查每个年份是否为闰年。

QList<int> years({2010, 2011, 2012, 2013, 2014, 2015, 2016, 2020, 2024});

我们初始化一个年份列表。

for (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 
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
2020 is a leap year
2024 is a leap year

Qt5 预定义的日期格式

Qt5 有一些内置的日期格式。QDate 对象的 toString 方法接受日期格式作为参数。Qt5 使用的默认日期格式是 Qt::TextDate

predefined_date_formats.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;
}

在本例中,我们展示了当前日期的八种不同日期格式。

out << "Today is " << cd.toString(Qt::ISODate) << endl;

这里,我们以 Qt::ISODate 格式打印当前日期,这是一种用于显示日期的国际标准。

$ ./predefineddateformats 
Today is Thu Dec 3 2020
Today is 2020-12-03
Today is 12/3/20
Today is Thursday, December 3, 2020
Today is 12/3/20
Today is Thursday, December 3, 2020
Today is 12/3/20
Today is 12/3/20

Qt5 自定义日期格式

日期可以用各种其他格式表示。在 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。
yy年,作为两位数字(00 到 99)
yyyy年,作为四位数字。如果年份为负数,则会添加一个负号。
表:日期格式说明符
custom_date_formats.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;
}

我们有四种自定义日期格式。

out << "Today is " << cd.toString("yyyy-MM-dd") << endl;

这是国际日期格式。日期的各个部分用短划线分隔。yyyy 是一个四位数的年份。MM 是月,作为带前导零的数字(01 到 12)。而 dd 是日,作为带前导零的数字(01 到 31)。

out << "Today is " << cd.toString("yy/M/dd") << endl;

这是另一种常见的日期格式。各个部分用斜杠 (/) 分隔。M 说明符代表月,作为没有前导零的数字(1 到 12)。

out << "Today is " << cd.toString("d. M. yyyy") << endl;

这种日期格式用于斯洛伐克。各个部分用点分隔。日和月没有前导零。首先是日,然后是月,最后是年。

$ ./customdateformats 
Today is 2020-12-03
Today is 20/12/03
Today is 3. 12. 2020
Today is 3-December-2020

Qt5 预定义的时间格式

时间有一些预定义的格式。标准格式说明符与日期格式中使用的相同。Qt5 使用的默认时间格式是 Qt::TextDate

predefined_time_formats.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;
}

在这个例子中,我们展示了当前时间的八种不同时间格式。

out << "The time is " << ct.toString(Qt::ISODate) << endl;

这里,我们以 Qt::ISODate 格式打印当前时间,这是一种用于显示时间的国际标准。

$ ./predefinedtimeformats 
The time is 10:59:05
The time is 10:59:05
The time is 10:59 AM
The time is 10:59:05 AM CET
The time is 10:59 AM
The time is 10:59:05 AM CET
The time is 10:59 AM
The time is 10:59 AM

Qt5 自定义时间格式

我们可以创建其他时间格式。我们构建了一个自定义时间格式,在其中使用时间格式说明符。下表给出了可用格式表达式的列表。

表达式输出
h小时,没有前导零(0 到 23 或 1 到 12,如果显示 AM/PM)
hh小时,带前导零(00 到 23 或 01 到 12,如果显示 AM/PM)
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)
zzz毫秒,带前导零(000 到 999)
AP 或 A 使用 AM/PM 显示。AP 将替换为“AM”或“PM”。
ap 或 a使用 am/pm 显示。ap 将替换为“am”或“pm”。
t时区(例如“CEST”)
表:时间格式说明符
custom_time_formats.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 time is " << ct.toString("hh:mm:ss.zzz") << endl;

在这种格式中,我们的小时、分钟和秒都有前导零。我们还添加了带有前导零的毫秒。

out << "The time is " << ct.toString("h:m:s a") << endl;

此时间格式说明符使用没有前导零的小时、分钟和秒,并添加了 am/pm 时段标识符。

$ ./customtimeformats 
The time is 11:03:16.007
The time is 11:3:16 am
The time is 11:3:16 AM
The time is 11:3 AM

Qt5 获取星期几

dayOfWeek 方法返回一个数字,该数字代表一周中的某一天,其中 1 是星期一,7 是星期日。

weekday.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

   QTextStream out(stdout);

   QDate cd = QDate::currentDate();
   int wd = cd.dayOfWeek();

   QLocale locale(QLocale("en_US"));

   out << "Today is " << locale.dayName(wd) << endl;
   out << "Today is " << locale.dayName(wd, QLocale::ShortFormat) << endl;
}

在这个例子中,我们打印了当前星期几的短名称和长名称。

QDate cd = QDate::currentDate();

我们获取当前日期。

int wd = cd.dayOfWeek();

从当前日期,我们获取星期几。

out << "Today is " << locale.dayName(wd) << endl;

我们获取星期几的完整名称。

out << "Today is " << locale.dayName(wd, QLocale::ShortFormat) << endl;

我们获取星期几的完整名称。

$ ./weekday 
Today is Thursday
Today is Thu

天数

我们可以使用 daysInMonth 方法计算特定月份的天数,并使用 daysInYear 方法计算一年中的天数。

nofdays.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 { 2020, 9, 18 };
   QDate dt2 { 2020, 2, 11 };
   QDate dt3 { 2020, 5, 1 };
   QDate dt4 { 2020, 12, 11 };
   QDate dt5 { 2020, 2, 29 };

   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;
}

创建了五个日期对象。我们计算了这些月份和特定年份的天数。

QDate dt1 { 2020, 9, 18 };
QDate dt2 { 2020, 2, 11 };
QDate dt3 { 2020, 5, 1 };
QDate dt4 { 2020, 12, 11 };
QDate dt5 { 2020, 2, 29 };

创建了五个 QDate 对象。它们中的每一个都代表一个不同的日期。

out << "There are " << dt1.daysInMonth() << " days in "
    << months.at(dt1.month()-1) << endl;

我们使用 daysInMonth 方法获取日期对象中的天数。

out << "There are " << dt1.daysInYear() << " days in year "
    << QString::number(dt1.year()) << endl;

在这里,我们使用 daysInYear 方法获取日期对象中一年的天数。

$ ./nofdays 
There are 30 days in September
There are 29 days in February
There are 31 days in May
There are 31 days in December
There are 29 days in February
There are 366 days in year 2020

Qt5 检查日期有效性

有一个 isValid 方法,它检查日期是否有效。

validity.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

    QTextStream out(stdout);

    QList<QDate> dates { 
        QDate(2020, 5, 11), QDate(2020, 8, 1),
        QDate(2020, 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;
       }
    }
}

在这个例子中,我们检查了三天的有效性。

QList<QDate> dates { 
    QDate(2020, 5, 11), QDate(2020, 8, 1),
    QDate(2020, 2, 30)
};

前两天有效。第三个无效。二月有 28 或 29 天。

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 方法的结果,我们在控制台上打印一条关于日期有效性的消息。

$ ./validity
Date 1 is a valid date
Date 2 is a valid date
Date 3 is not a valid date

Qt5 addDays, daysTo

我们可以轻松地计算从特定日期开始 n 天的日期。我们使用 addDays 方法。daysTo 方法返回到所选日期的天数。

daystofrom.cpp
#include <QTextStream>
#include <QDate>

int main(void) {

    QTextStream out(stdout);

    QDate dt { 2020, 5, 11 };
    QDate nd = dt.addDays(55);

    QDate cd = QDate::currentDate();
    int year = cd.year();
    QDate xmas { year, 12, 24 };

    out << "55 days from " << dt.toString() << " is "
        << nd.toString() << endl;
    out << "There are " << QDate::currentDate().daysTo(xmas)
        << " days till Christmas" << endl;
}

我们获取了 2020 年 5 月 11 日之后 55 天的日期。我们还获取了到圣诞节的天数。

QDate dt { 2020, 5, 11 };
QDate nd = dt.addDays(55);

addDays 方法返回一个 QDate,该日期是给定日期后的 55 天。

QDate xmas { year, 12, 24 };
...
out << "There are " << QDate::currentDate().daysTo(xmas)
    << " days till Christmas" << endl;

我们使用 daysTo 方法计算到圣诞节的天数。

$ ./daystofrom 
55 days from Mon May 11 2020 is Sun Jul 5 2020
There are 21 days till Christmas

Qt5 QDateTime 类

QDateTime 对象包含一个日历日期和一个时钟时间。它是 QDateQTime 类的组合。它有许多类似的方法,并且用法与这两个类相同。

datetime.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;
}

这个例子检索当前日期时间。

out << "The current datetime is " << cdt.toString() << endl;

这行代码将当前日期时间打印到终端。

out << "The current date is " << cdt.date().toString() << endl;

这行使用 date 方法检索日期时间对象的日期部分。

$ ./datetime 
The current datetime is Thu Dec 3 12:29:42 2020
The current date is Thu Dec 3 2020
The current time is 12:29:42

儒略日

儒略日是指自儒略周期开始以来连续计算的天数。它主要由天文学家使用。它不应与儒略历混淆。儒略周期始于公元前 4713 年。儒略日编号 0 被分配给公元前 4713 年 1 月 1 日中午开始的那一天。儒略日 (JDN) 是自该周期开始以来经过的天数。任何时刻的儒略日期 (JD) 都是前一天中午的儒略日编号加上自该时刻起的一天的小数部分。(Qt5 不计算这个小数部分。)除了天文学,儒略日期也经常被军方和大型机程序使用。

julianday.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;
}

在该示例中,我们计算今天的公历日期和儒略日。

out << "Julian day for today: " << cd.toJulianDay() << endl;

使用 toJulianDay 方法返回儒略日。

$ ./juliandate 
Gregorian date for today: 2020-12-03
Julian day for today: 2459187

使用儒略日期,很容易进行计算。

battles.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;
}

该示例计算自两个历史事件以来经过的天数。

QDate bordate(1812, 9, 7);
QDate slavdate(1805, 12, 2);

我们有两个拿破仑时代的战役日期。

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;

我们计算自这两场战役以来经过的天数。

$ date
Thu 03 Dec 2020 12:33:56 PM CET
$ ./battles 
Days since Slavkov battle: 78529
Days since Borodino battle: 76058

2020 年 12 月 3 日,距离斯瓦科夫战役已经过去了 78529 天,距离博罗迪诺战役过去了 76058 天。

UTC 时间

我们的星球是一个球体。它绕着它的轴旋转。地球向东旋转。因此,太阳在不同位置以不同的时间升起。地球大约 24 小时旋转一周。因此,世界被划分为 24 个时区。在每个时区,都有不同的本地时间。这种本地时间通常会受到夏令时的进一步修改。

全球时间存在务实的需求。一个全球时间有助于避免关于时区和夏令时的混淆。UTC(协调世界时)被选为主要的时间标准。UTC 用于航空、天气预报、飞行计划、空中交通管制许可和地图。与本地时间不同,UTC 不会随着季节的变化而变化。

utc_local.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 日期时间和本地日期时间来表达日期时间。

out << "Universal datetime" << cdt.toUTC().toString() << endl;

toUTC 方法用于获取 UTC 日期时间。

out << "Local datetime" << cdt.toLocalTime().toString() << endl;

toLocalTime 用于获取本地日期时间。

$ ./utclocal 
Universal datetime: Thu Dec 3 11:36:19 2020 GMT
Local datetime: Thu Dec 3 12:36:19 2020

该示例在布拉迪斯拉发运行,该地使用中欧时间 (CET) — UTC + 1 小时。

Unix 纪元

纪元是选定为特定时代起点的某个时间点。例如,在西方基督教国家,时间纪元从耶稣诞生的第 0 天开始。另一个例子是法国共和历,它使用了十二年。纪元是共和时代的开始,于 1792 年 9 月 22 日,即宣布第一共和国成立并废除君主制的当天,被宣布。

计算机也有它们的纪元。其中最受欢迎的是 Unix 纪元。Unix 纪元是 1970 年 1 月 1 日 00:00:00 UTC(或 ISO 8601 格式的 1970-01-01T00:00:00Z)。计算机中的日期和时间是根据自该计算机或平台的定义纪元以来经过的秒数或时钟滴答声来确定的。

Unix 时间是从 Unix 纪元开始经过的秒数。

$ date +%s
1606995554

可以使用 Unix date 命令获取 Unix 时间。此时此刻,自 Unix 纪元以来已经过去了 1606995554 秒。

unix_epoch.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 时间并将其转换为人类可读的格式。

#include <ctime>

我们包含标准 C++ 时间头文件。

time_t t = time(0);
out << t << endl;

使用标准 C++ 的 time 函数,我们获取 Unix 时间。

QDateTime dt;
dt.setTime_t(t);
out << dt.toString() << endl;

setTime_t 函数用于将 Unix 时间转换为 DateTime,后者被格式化为人类可读的格式。

QDateTime cd = QDateTime::currentDateTime();
out << cd.toTime_t() << endl;

Qt5 的 toTime_t 函数也可以用于获取 Unix 时间。

$ ./unixepoch 
1606995613
Thu Dec 3 12:40:13 2020
1606995613

在本章中,我们使用了 Qt5 中的时间和日期。