Qt4 中的日期和时间
最后修改于 2023 年 10 月 18 日
在本 Qt4 C++ 编程教程中,我们将讨论时间和日期。
Qt4 具有 QDate
、QTime
和 QDateTime
类来处理日期和时间。 QDate
是一个用于处理公历日期的类。它有用于确定日期、比较或操作日期的方法。 QTime
类用于处理时钟时间。它提供了用于比较时间、确定时间和各种其他时间操作方法的方法。 QDateTime
是一个将 QDate
和 QTime
对象组合成一个对象的类。
初始化日期和时间对象
日期和时间对象可以通过两种基本方式初始化。 我们在对象构造函数中初始化它们,或者我们可以创建空对象并在稍后用数据填充它们。
#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
对象打印到控制台。 我们使用一个特定的格式,其中也包括毫秒,默认情况下省略了毫秒。
$ ./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
当前日期和时间
在下面的例子中,我们将当前本地时间和日期打印到控制台。
#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
方法将日期和时间对象转换为字符串。
$ ./curdatetime Current date is: Wed Oct 14 2015 Current time is: 13:40:04
比较日期
关系运算符可用于比较日期。 我们可以比较它们在日历中的位置。
#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; }
我们使用小于 (<) 比较运算符比较日期,并确定它们中哪一个位于日历中较早的位置。
$ ./comparedates Sun Apr 5 2015 comes after Sat Apr 5 2014
比较运算符也可以很容易地用于 QTime
和 QDateTime
对象。
确定闰年
闰年是包含额外一天的年份。日历中增加一天的原因是天文年和日历年之间的差异。日历年正好有 365 天,而天文年(地球绕太阳公转一周的时间)有 365.25 天。 差异是 6 小时,这意味着在四年内,我们缺少一天。因为我们希望我们的日历与季节同步,所以我们每四年在二月增加一天。(也有例外情况。)在公历中,闰年的二月有 29 天,而不是通常的 28 天。 而且这一年持续 366 天,而不是通常的 365 天。
QDate::isLeapYear
静态方法确定某年是否为闰年。
#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++11
、QMAKE_CXXFLAGS += -std=c++11
或 QMAKE_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。
$ ./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
。
#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
格式打印当前日期,这是显示日期的国际标准。
$ ./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
说明符表示没有前导零的日(1 到 31)。 dd
说明符表示带有前导零的日(01 到 31)。 下表列出了可用的日期格式表达式
表达式 | 输出 |
---|---|
d | 日,数字,无前导零 (1 to 31) |
dd | 日,数字,带前导零 (01 to 31) |
ddd | 本地化的日缩写 (例如 'Mon' to 'Sun')。 使用 QDate::shortDayName()。 |
dddd | 本地化的日全名 (例如 'Monday' to 'Sunday')。 使用 QDate::longDayName()。 |
M | 月,数字,无前导零 (1 to 12) |
MM | 月,数字,带前导零 (01 to 12) |
MMM | 本地化的月缩写 (例如 'Jan' to 'Dec')。 使用 QDate::shortMonthName()。 |
MMMM | 本地化的月全名 (例如 'January' to 'December')。 使用 QDate::longMonthName()。 |
yy | 年,两位数字 (00 to 99) |
yyyy | 年,四位数字。 如果年份为负数,则在前面加上一个负号。 |
#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 2015-10-14 Today is 15/10/14 Today is 14.10.2015 Today is 14-October-2015
预定义的时间格式
时间有一些预定义的格式。 标准格式说明符与日期格式中使用的说明符相同。 Qt4 使用的默认时间格式是 Qt::TextDate
。
#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
格式打印当前时间,这是显示时间的国际标准。
$ ./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 | 小时,无前导零 (0 to 23 或 1 to 12 如果是 AM/PM 显示) |
hh | 小时,带前导零 (00 to 23 或 01 to 12 如果是 AM/PM 显示) |
H | 小时,无前导零 (0 to 23,即使是 AM/PM 显示) |
HH | 小时,带前导零 (00 to 23,即使是 AM/PM 显示) |
m | 分钟,无前导零 (0 to 59) |
mm | 分钟,带前导零 (00 to 59) |
s | 秒,无前导零 (0 to 59) |
ss | 秒,带前导零 (00 to 59) |
z | 毫秒,无前导零 (0 to 999) |
zzz | 毫秒,带前导零 (000 to 999) |
AP 或 A | 使用 AM/PM 显示。 AP 将被替换为 "AM" 或 "PM"。 |
ap 或 a | 使用 am/pm 显示。 ap 将被替换为 "am" 或 "pm"。 |
t | 时区 (例如 "CEST") |
#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 周期标识符。
$ ./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 是星期日。
#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
静态方法,我们获取星期几的长名。
$ ./weekday Today is Wed Today is Wednesday
天数
我们可以使用 daysInMonth
方法计算特定月份的天数,并使用 daysInYear
方法计算一年中的天数。
#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
方法获取一年中的天数。
$ ./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
方法,用于检查日期是否有效。
#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)});
前两天是有效的。 第三个是无效的。 二月有 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
方法的结果,我们将关于日期有效性的消息打印到控制台。
$ ./isvalid Date 1 is a valid date Date 2 is a valid date Date 3 is not a valid date
天数到、天数从
我们可以很容易地计算出特定日期之后 n 天的日期。 我们使用 addDays
方法。 daysTo
方法返回到所选日期的天数。
#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
方法返回一个 QDate
,该日期是给定日期后的 55 天。
QDate xmas(2015, 12, 24); ... out << "There are " << QDate::currentDate().daysTo(xmas) << " days till Christmas" << endl;
我们使用 daysTo
方法来计算到圣诞节的天数。
$ ./daystofrom 55 days from Mon May 11 2015 is Sun Jul 5 2015 There are 71 days till Christmas
QDateTime 类
QDateTime
对象包含日历日期和时钟时间。 它是 QDate
和 QTime
类的组合。 它有许多类似的方法,并且用法与这两个类相同。
#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 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 不计算这个分数。) 除了天文学之外,儒略日期也经常被军事和大型机程序使用。
#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
方法返回儒略日。
$ ./julianday Gregorian date for today: 2015-10-14 Julian day for today: 2457310
使用儒略日期,很容易进行计算。
#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 Wed Oct 14 17:35:40 CEST 2015 $ ./battles Days since Slavkov battle: 76652 Days since Borodino battle: 74181
2015 年 10 月 14 日,自 Slavkov 战役以来已经过去了 76652 天,自 Borodino 战役以来已经过去了 74181 天。
UTC 时间
我们的星球是一个球体。 它绕其轴旋转。 地球向东旋转。 因此,太阳在不同的地方在不同的时间升起。 地球大约每 24 小时旋转一周。 因此,世界被分成 24 个时区。 在每个时区,都有不同的当地时间。 这种当地时间通常会因夏令时而进一步修改。
对一个全球时间有实际的需求。 一个全球时间有助于避免关于时区和夏令时的混淆。 UTC(协调世界时)被选为主要的时间标准。 UTC 用于航空、天气预报、飞行计划、空中交通管制许可和地图。 与当地时间不同,UTC 不会随季节的变化而变化。
#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
用于获取本地日期时间。
$ ./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 日 00:00:00 UTC (或 ISO 8601 的 1970-01-01T00:00:00Z) 的时间。 计算机中的日期和时间是根据自该计算机或平台的定义的纪元以来经过的秒数或时钟滴答数来确定的。
$ date +%s 1444838231
Unix 日期命令可用于获取 Unix 时间。 在这一刻,自 Unix 纪元以来已经过去了 1444838231 秒。
#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 时间。
$ ./unixepoch 1444838314 Wed Oct 14 17:58:34 2015 1444838314
在本章中,我们使用了时间和日期。