亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++通過boost.date_time進(jìn)行時(shí)間運(yùn)算

 更新時(shí)間:2022年06月20日 09:28:00   作者:天方  
這篇文章介紹了C++通過boost.date_time進(jìn)行時(shí)間運(yùn)算的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

標(biāo)準(zhǔn)C函數(shù)的時(shí)間運(yùn)算是非常不好用的,boost提供了一個(gè)跨平臺(tái)的日期庫(kù)boost.date_time,通過它可以快速實(shí)現(xiàn)各種時(shí)間運(yùn)算。

boost.date_time中的時(shí)間對(duì)象為boost::posix_time::ptime,在<boost/date_time.hpp>中定義,它的常用操作如下。

獲取現(xiàn)在時(shí)間:

ptime now = second_clock::local_time();
cout << now << endl;

獲取日期信息:

cout << now.date().day_of_week();

通過偏移獲取新時(shí)間:

ptime today = now - now.time_of_day();
ptime next_time = now + years(1) + months(1) + days(1) - hours(1);

計(jì)算時(shí)間差:

time_duration?timespan = now - today;

時(shí)間比較:

bool?result = next_day > now;

字符串轉(zhuǎn)換為時(shí)間

標(biāo)準(zhǔn)格式的字符串可以通過time_from_string轉(zhuǎn)換。

cout << time_from_string("2010-09-10 10:01:01");

但對(duì)于非標(biāo)準(zhǔn)的格式的時(shí)間,則需要自定義解析函數(shù)。這里我簡(jiǎn)單的寫了一個(gè):

ptime time_parse_exact(const string& time_str, const string& format)
{
    ptime output;
    time_input_facet facet1(format, 1);

    std::stringstream ss1(time_str);
    ss1.imbue(std::locale(ss1.getloc(), &facet1));
    ss1 >> output;

    return output;
}
cout << time_parse_exact("2010/09/10-10:01:01", "%Y/%m/%d-%H:%M:%S");

精準(zhǔn)計(jì)時(shí)

對(duì)于一般的計(jì)時(shí)操作,可以通過前面的時(shí)間差運(yùn)算獲取。但是,有的時(shí)候需要高精度計(jì)時(shí)操作,這個(gè)時(shí)候需要用到boost的另外一個(gè)庫(kù)cpu_timer。

#include <boost/timer/timer.hpp>
int main(void)
{
    boost::timer::cpu_timer timer;
    for (long i = 0; i < 100000000; ++i)
        std::sqrt(123.456L);

    cout << timer.format() << endl;
    //std::cout << timer.format(5, "%ws wall time,%ts totle time\n");

    return 0;
}

關(guān)于cpu_timer更多信息,請(qǐng)參看boost官方文檔。

到此這篇關(guān)于C++通過boost.date_time進(jìn)行時(shí)間運(yùn)算的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論