国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
從 std::chrono::time_point 中提取日期和時間組件
首頁 后端開發(fā) C++ 如何從 C 中的'std::chrono::time_point”對象中提取特定的日期和時間組件,例如年、月、日、小時、分鐘、秒和毫秒?

如何從 C 中的'std::chrono::time_point”對象中提取特定的日期和時間組件,例如年、月、日、小時、分鐘、秒和毫秒?

Oct 30, 2024 am 07:59 AM

How do you extract specific date and time components, like year, month, day, hours, minutes, seconds, and milliseconds, from a `std::chrono::time_point` object in C  ?

從 std::chrono::time_point 中提取日期和時間組件

問題:

我們?nèi)绾螜z索特定的來自 std::chrono::time_point 對象的日期和時間組件,例如年、月、日、小時、分鐘、秒和毫秒?

解決方案:

要提取此信息,我們必須利用 system_clock::time_point,因為它是系統(tǒng)提供的唯一與民用日歷連接的時鐘。

<code class="cpp">system_clock::time_point now = system_clock::now();</code>

轉(zhuǎn)換為時間戳:

接下來,將 time_point 轉(zhuǎn)換為 time_t:

<code class="cpp">time_t tt = system_clock::to_time_t(now);</code>

提取組件:

使用 C 庫,將time_t 到 tm,指定是否要以 UTC 或本地時區(qū)進行轉(zhuǎn)換:

<code class="cpp">tm utc_tm = *gmtime(&tt);
tm local_tm = *localtime(&tt);</code>

現(xiàn)在,您可以打印 tm 的組成部分:

<code class="cpp">std::cout << local_tm.tm_year + 1900 << '\n';
std::cout << local_tm.tm_mon + 1 << '\n';
std::cout << local_tm.tm_mday << '\n';

擴展提?。?/strong>

雖然 system_clock 通常基于 Unix 時間,但其精度通常大于秒。要訪問高分辨率組件,請使用以下代碼:

<code class="cpp">using namespace std;
using namespace std::chrono;

int main()
{
    typedef duration<int, ratio_multiply<hours::period, ratio<24> >>::type days;
    system_clock::time_point now = system_clock::now();
    system_clock::duration tp = now.time_since_epoch();
    days d = duration_cast<days>(tp);
    tp -= d;
    hours h = duration_cast<hours>(tp);
    tp -= h;
    minutes m = duration_cast<minutes>(tp);
    tp -= m;
    seconds s = duration_cast<seconds>(tp);
    tp -= s;

    cout << d.count() << "d " << h.count() << ':'
         << m.count() << ':' << s.count();
    cout << " " << tp.count() << "["
         << system_clock::duration::period::num << '/'
         << system_clock::duration::period::den << "]\n";
}

通過將 time_point 轉(zhuǎn)換為天、小時、分鐘、秒,并提取秒的小數(shù)部分,此代碼輸出:

15806d 20:31:14 598155[1/1000000]

庫解決方案:

僅標頭日期庫簡化了此過程,允許使用以下代碼進行提?。?/p>

<code class="cpp">#include "date.h"

int main()
{
    auto tp = std::chrono::system_clock::now();
    auto dp = date::floor<date::days>(tp);
    auto ymd = date::year_month_day{dp};
    auto time = date::make_time(std::chrono::duration_cast<std::chrono::milliseconds>(tp - dp));

    std::cout << "year        = " << ymd.year() << '\n';
    std::cout << "month       = " << ymd.month() << '\n';
    std::cout << "day         = " << ymd.day() << '\n';
    std::cout << "hour        = " << time.hours().count() << "h\n";
    std::cout << "minute      = " << time.minutes().count() << "min\n";
    std::cout << "second      = " << time.seconds().count() << "s\n";
    std::cout << "millisecond = " << time.subseconds().count() << "ms\n";
}

C 20 集成:

在 C 20 中,包含建議的語法,用于使用 system_clock::time_point 提取這些字段:

<code class="cpp">#include <chrono>

int main()
{
    using namespace std::chrono;
    auto tp = system_clock::now();
    auto dp = floor<days>(tp);
    year_month_day ymd{dp};
    hh_mm_ss time{floor<milliseconds>(tp - dp)};
    auto y = ymd.year();
    auto m = ymd.month();
    auto d = ymd.day();
    auto h = time.hours();
    auto M = time.minutes();
    auto s = time.seconds();
    auto ms = time.subseconds();
}</code>

此外,您可以指定首選時間提取區(qū)。

以上是如何從 C 中的'std::chrono::time_point”對象中提取特定的日期和時間組件,例如年、月、日、小時、分鐘、秒和毫秒?的詳細內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

c多態(tài)性:功能是否超載一種多態(tài)性? c多態(tài)性:功能是否超載一種多態(tài)性? Jun 20, 2025 am 12:05 AM

是的,函數(shù)重載是C 中的一種多態(tài)形式,具體來說是編譯時多態(tài)。1.函數(shù)重載允許使用相同名稱但不同參數(shù)列表的多個函數(shù)。2.編譯器根據(jù)提供的參數(shù)在編譯時決定調(diào)用哪個函數(shù)。3.與運行時多態(tài)不同,函數(shù)重載在運行時沒有額外開銷,實現(xiàn)簡單,但靈活性較低。

C中有哪種多態(tài)性的多態(tài)性?解釋了 C中有哪種多態(tài)性的多態(tài)性?解釋了 Jun 20, 2025 am 12:08 AM

C 有兩種主要的多態(tài)類型:編譯時多態(tài)和運行時多態(tài)。1.編譯時多態(tài)通過函數(shù)重載和模板實現(xiàn),提供高效但可能導(dǎo)致代碼膨脹。2.運行時多態(tài)通過虛函數(shù)和繼承實現(xiàn),提供靈活性但有性能開銷。

C:多態(tài)性真的有用嗎? C:多態(tài)性真的有用嗎? Jun 20, 2025 am 12:01 AM

是的,C 中的多態(tài)性非常有用。 1)它提供了靈活性,允許輕松添加新類型;2)促進代碼重用,減少重復(fù);3)簡化維護,使代碼更易擴展和適應(yīng)變化。盡管存在性能和內(nèi)存管理的挑戰(zhàn),但其優(yōu)勢在復(fù)雜系統(tǒng)中尤為顯著。

C驅(qū)動器:常見錯誤 C驅(qū)動器:常見錯誤 Jun 20, 2025 am 12:12 AM

C destructorscanleadtoseveralcommonerrors.Toavoidthem:1)Preventdoubledeletionbysettingpointerstonullptrorusingsmartpointers.2)Handleexceptionsindestructorsbycatchingandloggingthem.3)Usevirtualdestructorsinbaseclassesforproperpolymorphicdestruction.4

C中的多態(tài)性:綜合指南 C中的多態(tài)性:綜合指南 Jun 21, 2025 am 12:11 AM

C 中的多態(tài)性分為運行時多態(tài)性和編譯時多態(tài)性。1.運行時多態(tài)性通過虛函數(shù)實現(xiàn),允許在運行時動態(tài)調(diào)用正確的方法。2.編譯時多態(tài)性通過函數(shù)重載和模板實現(xiàn),提供更高的性能和靈活性。

c認識python的人的教程 c認識python的人的教程 Jul 01, 2025 am 01:11 AM

學(xué)Python的人轉(zhuǎn)學(xué)C 最直接的困惑是:為什么不能像Python那樣寫?因為C 雖然語法更復(fù)雜,但提供了底層控制能力和性能優(yōu)勢。1.語法結(jié)構(gòu)上,C 使用花括號{}而非縮進組織代碼塊,且變量類型必須顯式聲明;2.類型系統(tǒng)與內(nèi)存管理方面,C 沒有自動垃圾回收機制,需手動管理內(nèi)存并注意釋放資源,使用RAII技術(shù)可輔助資源管理;3.函數(shù)與類定義中,C 需要明確訪問修飾符、構(gòu)造函數(shù)和析構(gòu)函數(shù),并支持如運算符重載等高級功能;4.標準庫方面,STL提供了強大的容器和算法,但需要適應(yīng)泛型編程思想;5

C中的多態(tài)性的各種形式是什么? C中的多態(tài)性的各種形式是什么? Jun 20, 2025 am 12:21 AM

C polymorphismincludescompile-time,runtime,andtemplatepolymorphism.1)Compile-timepolymorphismusesfunctionandoperatoroverloadingforefficiency.2)Runtimepolymorphismemploysvirtualfunctionsforflexibility.3)Templatepolymorphismenablesgenericprogrammingfo

C多態(tài)性:編碼樣式 C多態(tài)性:編碼樣式 Jun 19, 2025 am 12:25 AM

C polymorphismisuniqueduetoitscombinationofcompile-timeandruntimepolymorphism,allowingforbothefficiencyandflexibility.Toharnessitspowerstylishly:1)Usesmartpointerslikestd::unique_ptrformemorymanagement,2)Ensurebaseclasseshavevirtualdestructors,3)Emp

See all articles