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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                #### 18. 日期 & 時間 C++ 標準庫沒有提供所謂的日期類型。C++ 繼承了 C 語言用于日期和時間操作的結構和函數。為了使用日期和時間相關的函數和結構,需要在 C++ 程序中引用 頭文件。 有四個與時間相關的類型:**clock\_t、time\_t、size\_t** 和 **tm**。類型 clock\_t、size\_t 和 time\_t 能夠把系統時間和日期表示為某種整數。 結構類型 **tm** 把日期和時間以 C 結構的形式保存,tm 結構的定義如下: ~~~ struct tm { int tm_sec; // 秒,正常范圍從 0 到 59,但允許至 61 int tm_min; // 分,范圍從 0 到 59 int tm_hour; // 小時,范圍從 0 到 23 int tm_mday; // 一月中的第幾天,范圍從 1 到 31 int tm_mon; // 月,范圍從 0 到 11 int tm_year; // 自 1900 年起的年數 int tm_wday; // 一周中的第幾天,范圍從 0 到 6,從星期日算起 int tm_yday; // 一年中的第幾天,范圍從 0 到 365,從 1 月 1 日算起 int tm_isdst; // 夏令時 } ~~~ 下面是 C/C++ 中關于日期和時間的重要函數。所有這些函數都是 C/C++ 標準庫的組成部分,您可以在 C++ 標準庫中查看一下各個函數的細節。 | 序號 | 函數 & 描述 | | --- | --- | | 1 | [**time\_t time(time\_t \*time);**](https://www.runoob.com/cplusplus/c-function-time.html) 該函數返回系統的當前日歷時間,自 1970 年 1 月 1 日以來經過的秒數。如果系統沒有時間,則返回 .1。 | | 2 | [**char \*ctime(const time\_t \*time);**](https://www.runoob.com/cplusplus/c-function-ctime.html) 該返回一個表示當地時間的字符串指針,字符串形式 *day month year hours:minutes:seconds year\\n\\0*。 | | 3 | [**struct tm \*localtime(const time\_t \*time);**](https://www.runoob.com/cplusplus/c-function-localtime.html) 該函數返回一個指向表示本地時間的 **tm** 結構的指針。 | | 4 | [**clock\_t clock(void);**](https://www.runoob.com/cplusplus/c-function-clock.html) 該函數返回程序執行起(一般為程序的開頭),處理器時鐘所使用的時間。如果時間不可用,則返回 .1。 | | 5 | [**char \* asctime ( const struct tm \* time );**](https://www.runoob.com/cplusplus/c-function-asctime.html) 該函數返回一個指向字符串的指針,字符串包含了 time 所指向結構中存儲的信息,返回形式為:day month date hours:minutes:seconds year\\n\\0。 | | 6 | [**struct tm \*gmtime(const time\_t \*time);**](https://www.runoob.com/cplusplus/c-function-gmtime.html) 該函數返回一個指向 time 的指針,time 為 tm 結構,用協調世界時(UTC)也被稱為格林尼治標準時間(GMT)表示。 | | 7 | [**time\_t mktime(struct tm \*time);**](https://www.runoob.com/cplusplus/c-function-mktime.html) 該函數返回日歷時間,相當于 time 所指向結構中存儲的時間。 | | 8 | [**double difftime ( time\_t time2, time\_t time1 );**](https://www.runoob.com/cplusplus/c-function-difftime.html) 該函數返回 time1 和 time2 之間相差的秒數。 | | 9 | [**size\_t strftime();**](https://www.runoob.com/cplusplus/c-function-strftime.html) 該函數可用于格式化日期和時間為指定的格式。 | **當前日期:** ~~~ void test16() { //獲取系統的時間 time_t now = time(0); //把 now 轉為字符串 char *curTime = ctime(&now); //把 now 轉為 tm 結構 tm *gmtm = gmtime(&now); cout << "當前時間\t" << curTime << endl; curTime = asctime(gmtm); cout << "UTC 日期和時間\t" << curTime << endl; } 復制代碼 ~~~ > **輸出:** > > 當前時間 Sun Jan 5 22:11:15 2020 > > UTC 日期和時間 Sun Jan 5 14:11:15 2020 **使用結構 tm 格式化時間:** **tm** 結構在 C/C++ 中處理日期和時間相關的操作時,顯得尤為重要。tm 結構以 C 結構的形式保存日期和時間。大多數與時間相關的函數都使用了 tm 結構。下面的實例使用了 tm 結構和各種與日期和時間相關的函數。 在練習使用結構之前,需要對 C 結構有基本的了解,并懂得如何使用箭頭 -> 運算符來訪問結構成員。 ~~~ void test16() { //基于當前系統的日期/時間 cout << "1970 到目前經過的秒數:" << now << endl; tm *ltm = localtime(&now); //輸出 tm 結構的各個組成部分 cout << "年: " << 1990 + ltm->tm_year << endl; cout << "月: " << 1 + ltm->tm_mon << endl; cout << "日: " << ltm->tm_mday << endl; cout << "時: " << ltm->tm_hour << endl; cout << "分: " << ltm->tm_min << endl; cout << "秒: " << ltm->tm_sec << endl; } ~~~ > **輸出:** > > 1970 到目前經過的秒數:1578233475 年: 2110 月: 1 日: 5 時: 22 分: 11 秒: 15
                  <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>

                              哎呀哎呀视频在线观看