Qt5 C++14教程--Qt5中的日期和时间

目录

Qt5初始化日期和时间对象

Qt5的当前日期和时间

Qt5比较日期

Qt5确定闰年

Qt5预定义的日期格式

Qt5自定义日期格式

Qt5预定义的时间格式

Qt5自定义时间格式

Qt5检索工作日

天数

Qt5检查日期的有效性

Qt5 addDays, daysTo

Qt5 QDateTime 类

 Julian日

Unix 时间戳


在Qt5 C++编程教程的这一部分,我们将谈论时间和日期。

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

在本章中,我们创建了命令行程序;因此,我们不需要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

 比较运算符也可以很容易地用于QTime和QDateTime对象。

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 本地化的长日期名称(例如,"星期一 "到 "星期日")。使用QDate::longDayName。
M 作为数字的月份,没有前导零(1到12)。

MM 以数字形式表示的月份,前导零(01至12)。
MMM 缩写的本地化月份名称(例如,'Jan'到'Dec')。使用QDate::shortMonthName。
MMMM 本地化的长月份名称(例如,'1月'到'12月')。使用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是以数字形式表示的月份,前导0(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;

 这个时间格式指定器使用小时、分钟和秒,没有前导零,并增加了上午/下午的时间标识。

$ ./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

天数

我们可以用dayInMonth方法计算某月的天数,用dayInYear方法计算某年的天数。

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;


在这里,我们使用日期对象的dayInYear方法得到一年中的天数。

$ ./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方法返回一个比给定日期晚55天的QDate。

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 对象包含日历日期和时钟时间。它是 QDate 和 QTime 类的组合。它具有许多类似的方法,使用方法与这两个类相同。

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

 Julian日

Julian日是指自Julian时期开始以来的连续天数。它主要由天文学家使用。它不应与Julian历混淆。Julian时期始于公元前4713年。Julian日数字0被分配给公元前4713年1月1日中午开始的一天。Julian日数(JDN)是从这一时期开始以来经过的天数。任何时刻的Julian日期(JD)是前午12点的Julian日数加上自那一时刻以来一天的小数。 (Qt5没有计算这个小数。)除了天文学之外,Julian日期通常被军队和大型机程序使用。

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

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

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

Julian日是用toJulianDay方法返回的。

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

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

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();

我们计算今天和斯拉夫科夫和博罗季诺战役的Julian日。

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天。

我们的星球是一个球体。它围绕其轴旋转。地球朝东旋转。因此,太阳在不同的地点升起的时间是不同的。地球大约在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 时间戳

Unix 时间戳(Unix timestamp)是从 Unix 纪元(即 Unix epoch)开始经过的秒数。 Unix 纪元是 1970 年 1 月 1 日(UTC 时间)00:00:00。

可以使用 Unix 的 date 命令来获取 Unix 时间戳。在当前时刻,距离 Unix 纪元已经过去了 1606995554 秒。

以下是一个使用 Qt5 库获取 Unix 时间戳并将其转换为可读的日期时间的 C++ 程序的代码示例。

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中处理了时间和日期。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值