Skip to content

Latest commit

 

History

History
1071 lines (703 loc) · 25.5 KB

142.md

File metadata and controls

1071 lines (703 loc) · 25.5 KB

Qt4 中的日期和时间

原文: http://zetcode.com/gui/qt4/datetime/

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

Qt4 具有QDateQTimeQDateTime类以与日期和时间一起使用。 QDate是用于使用公历中的日历日期的类。 它具有确定日期,比较或操纵日期的方法。 QTime类使用时钟时间。 它提供了比较时间,确定时间的方法以及其他各种时间操纵方法。 QDateTime是将QDateQTime对象结合为一个对象的类。

初始化日期&时间对象

日期和时间对象可以通过两种基本方式进行初始化。 我们可以在对象构造器中对其进行初始化,也可以创建空对象并在以后用数据填充它们。

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

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

QDate dt1(2015, 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对象打印到控制台。 我们使用的特定格式还包括毫秒,默认情况下会省略毫秒。

Output

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

#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()方法将日期和时间对象转换为字符串。

Output

$ ./curdatetime 
Current date is: Wed Oct 14 2015
Current time is: 13:40:04

比较日期

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

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

该示例比较两个日期。

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

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

Output

$ ./comparedates 
Sun Apr 5 2015 comes after Sat Apr 5 2014

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

确定闰年

闰年是包含另一天的年份。 日历中额外一天的原因是天文日历年与日历年之间的差异。 日历年正好是 365 天,而天文学年(地球绕太阳公转的时间)是 365.25 天。 相差 6 个小时,这意味着在四年的时间里,我们一天中都没有。 因为我们希望日历与季节同步,所以每四年将 2 月增加一天。 (有例外。)在公历中,闰年的 2 月有 29 天,而不是通常的 28 天。该年持续 366 天,而不是通常的 365 天。

QDate::isLeapYear()静态方法确定年份是否为闰年。

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

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

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

我们初始化一个年份列表。 这是 C++ 11 构造,因此,我们需要启用 C++ 11。 我们需要将CONFIG += c++11QMAKE_CXXFLAGS += -std=c++11QMAKE_CXXFLAGS += -std=c++0x添加到.pro文件。

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。

Output

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

预定义的日期格式

Qt4 具有一些内置的日期格式。 QDate对象的toString()方法采用日期格式作为参数。 Qt4 使用的默认日期格式为Qt::TextDate

dateformats.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格式打印当前日期,这是用于显示日期的国际标准。

Output

$ ./dateformats 
Today is Wed Oct 14 2015
Today is 2015-10-14
Today is 10/14/15
Today is Wednesday, October 14, 2015
Today is 10/14/15
Today is Wednesday, October 14, 2015
Today is 10/14/15
Today is 10/14/15

自定义日期格式

日期可以用多种其他格式表示。 在 Qt4 中,我们也可以创建自定义日期格式。 toString()方法的另一个版本采用格式字符串,我们可以在其中使用各种格式说明符。 例如,d指示符代表一天,而不是前导零。 dd指示符代表一天,前导零。 下表列出了可用的日期格式表达式:

表达式 输出
d 不带前导零(1 到 31)的日期
dd 带前导零(01 到 31)的日期
ddd 本地化日期的缩写(例如,MonSun)。 使用QDate::shortDayName()
dddd 本地化的长名称(例如,MondaySunday)。 使用QDate::longDayName()
M 不带前导零(1 到 12)的月份
MM 带前导零(01 到 12)的月份
MMM 本地化月份的缩写名称(例如,"Jan""Dec")。 使用QDate::shortMonthName()
MMMM 本地化的长月份名称(例如,JanuaryDecember)。 使用QDate::longMonthName()
y 两位数年份(00 到 99)
yyyy 四位数年份。 如果年份为负数,则还会附加一个减号。

Table: Date format specifiers

customdateformats.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;

此日期格式在斯洛伐克使用。 零件由点字符分隔。 日和月没有前导零。 首先是日期,然后是月份,最后是年份。

Output

$ ./customdateformats 
Today is 2015-10-14
Today is 15/10/14
Today is 14.10.2015
Today is 14-October-2015

预定义的时间格式

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

timeformats.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格式打印当前时间,这是用于显示时间的国际标准。

Output

$ ./timeformats 
The time is 16:14:08
The time is 16:14:08
The time is 4:14 PM
The time is 4:14:08 PM CEST
The time is 4:14 PM
The time is 4:14:08 PM CEST
The time is 4:14 PM
The time is 4:14 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)
APA 使用 AM/PM 显示。 AP将被"AM""PM"替换。
apa 使用 am/pm 显示。 ap将被"am""pm"替换。
t 时区(例如"CEST"

Table: Time format specifiers

customtimeformats.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 Qt4 is " << qVersion() << 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 周期标识符。

Output

$ ./customtimeformats 
The time is 16:15:07.690
The time is 4:15:7 pm
The time is 16:15:7 PM t
The time is 4:15 PM

检索工作日

dayOfWeek()方法返回一个代表星期几的数字,其中 1 是星期一,7 是星期日。

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

在示例中,我们打印当前工作日的简称和长名称。

QDate cd = QDate::currentDate();

我们得到当前日期。

int wd = cd.dayOfWeek();

从当前日期开始,我们得到星期几。

out << "Today is " << QDate::shortDayName(wd) << endl;

使用QDate::shortDayName()静态方法,我们得到工作日的简称。

out << "Today is " << QDate::longDayName(wd) << endl;    

使用QDate::longDayName()静态方法,我们获得了工作日的长名称。

Output

$ ./weekday 
Today is Wed
Today is Wednesday

天数

我们可以使用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(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;         
}

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

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对象。 每个代表不同的日期。

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()方法获得一年中的天数。

Output

$ ./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()方法可以检查日期是否有效。

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

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

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

前两天有效。 第三个无效。 2 月有 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()方法的结果,我们将有关日期有效性的消息打印到控制台。

Output

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

到...的天数

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

daystofrom.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 天后获得日期。我们还将获得圣诞节前的天数。

QDate dt(2015, 5, 11);  
QDate nd = dt.addDays(55);

addDays()方法返回给定日期之后 55 天的QDate

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

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

Output

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

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()方法检索日期时间对象的日期部分。

Output

$ ./datetime 
The current datetime is Wed Oct 14 17:02:52 2015
The current date is Wed Oct 14 2015
The current time is 17:02:52

朱利安日

儒略日是指儒略时期开始以来的连续天数。 它主要由天文学家使用。 不应将其与朱利安历法相混淆。 朱利安纪元开始于公元前 4713 年。 朱利安天数 0 被指定为从公元前 4713 年 1 月 1 日中午开始的日期。 朱利安天数(JDN)是自此时间段开始以来经过的天数。 任意时刻的儒略日期(JD)是前一个正午的儒略日编号加上该时刻以来的那一天的分数。 (Qt4 不会计算此分数。)除天文学外,军事和大型机程序经常使用儒略日期。

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()方法返回儒略日。

Output

$ ./julianday
Gregorian date for today: 2015-10-14
Julian day for today: 2457310

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

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; 

我们计算了两次战斗以来经过的天数。

Output

$ date
Wed Oct 14 17:35:40 CEST 2015
$ ./battles 
Days since Slavkov battle: 76652
Days since Borodino battle: 74181

自 2015 年 10 月 14 日起,斯拉夫科夫战役已经过去了 76652 天,而波罗底诺战役已经过去了 74181 天。

UTC 时间

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

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

utclocal.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()用于获取本地日期时间。

Output

$ ./localutc 
Universal datetime: Wed Oct 14 15:50:30 2015
Local datetime: Wed Oct 14 17:50:30 2015

该示例在布拉迪斯拉发运行,该站点具有中欧夏令时(CEST)-UTC + 2 小时。

Unix 纪元

纪元是选择作为特定纪元起源的时间瞬间。 例如,在西方基督教国家,时间从耶稣出生的第 0 天开始。 另一个例子是法国共和党日历,使用了十二年。 这个时期是 1792 年 9 月 22 日宣布的共和纪元的开始,即宣布成立第一共和国并废除君主制的那一天。 电脑也有自己的纪元。 最受欢迎的时间之一是 Unix 时间。 Unix 纪元是 1970 年 1 月 1 日 UTC 时间 00:00:00(或1970-01-01T00:00:00Z ISO8601)。 计算机中的日期和时间是根据自该计算机或平台的定义时期以来经过的秒数或时钟滴答数确定的。

$ date +%s
1444838231

Unix date 命令可用于获取 Unix 时间。 在这个特定时刻,自 Unix 纪元以来已经过去了 1444838231 秒。

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

在示例中,我们使用两个 Qt4 函数获取 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 时间转换为日期时间,并将其格式化为易于阅读的格式。

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

Qt4 的toTime_t()方法也可以用来获取 Unix 时间。

Output

$ ./unixepoch 
1444838314
Wed Oct 14 17:58:34 2015
1444838314

在本章中,我们处理了时间和日期。