<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Qt4 中的日期和時間 > 原文: [http://zetcode.com/gui/qt4/datetime/](http://zetcode.com/gui/qt4/datetime/) 在 Qt4 C++ 編程教程的這一部分中,我們將討論時間和日期。 Qt4 具有`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`對象打印到控制臺。 我們使用的特定格式還包括毫秒,默認情況下會省略毫秒。 `Output` ```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()`方法將日期和時間對象轉換為字符串。 `Output` ```cpp $ ./curdatetime Current date is: Wed Oct 14 2015 Current time is: 13:40:04 ``` ## 比較日期 關系運算符可用于比較日期。 我們可以比較他們在日歷中的位置。 `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; } ``` 我們使用小于(`<`)的比較運算符比較日期,并確定其中哪個位于日歷的更早位置。 `Output` ```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。 `Output` ```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 ``` ## 預定義的日期格式 Qt4 具有一些內置的日期格式。 `QDate`對象的`toString()`方法采用日期格式作為參數。 Qt4 使用的默認日期格式為`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`格式打印當前日期,這是用于顯示日期的國際標準。 `Output` ```cpp $ ./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` | 本地化日期的縮寫(例如,`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; ``` 此日期格式在斯洛伐克使用。 零件由點字符分隔。 日和月沒有前導零。 首先是日期,然后是月份,最后是年份。 `Output` ```cpp $ ./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` ```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`格式打印當前時間,這是用于顯示時間的國際標準。 `Output` ```cpp $ ./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) | | `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 Qt4 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 周期標識符。 `Output` ```cpp $ ./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` ```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()`靜態方法,我們獲得了工作日的長名稱。 `Output` ```cpp $ ./weekday Today is Wed Today is Wednesday ``` ## 天數 我們可以使用`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()`方法獲得一年中的天數。 `Output` ```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()`方法可以檢查日期是否有效。 `isvalid.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()`方法的結果,我們將有關日期有效性的消息打印到控制臺。 `Output` ```cpp $ ./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` ```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()`方法來計算直到圣誕節的天數。 `Output` ```cpp $ ./daystofrom 55 days from Mon May 11 2015 is Sun Jul 5 2015 There are 71 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()`方法檢索日期時間對象的日期部分。 `Output` ```cpp $ ./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` ```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()`方法返回儒略日。 `Output` ```cpp $ ./julianday Gregorian date for today: 2015-10-14 Julian day for today: 2457310 ``` 使用儒略日期,很容易進行計算。 `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; ``` 我們計算了兩次戰斗以來經過的天數。 `Output` ```cpp $ 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` ```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()`用于獲取本地日期時間。 `Output` ```cpp $ ./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`)。 計算機中的日期和時間是根據自該計算機或平臺的定義時期以來經過的秒數或時鐘滴答數確定的。 ```cpp $ date +%s 1444838231 ``` Unix date 命令可用于獲取 Unix 時間。 在這個特定時刻,自 Unix 紀元以來已經過去了 1444838231 秒。 `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; } ``` 在示例中,我們使用兩個 Qt4 函數獲取 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; ``` Qt4 的`toTime_t()`方法也可以用來獲取 Unix 時間。 `Output` ```cpp $ ./unixepoch 1444838314 Wed Oct 14 17:58:34 2015 1444838314 ``` 在本章中,我們處理了時間和日期。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看