从c ++中的std :: chrono :: time_point中提取年/月/日等 [英] Extract year/month/day etc. from std::chrono::time_point in C++

查看:2866
本文介绍了从c ++中的std :: chrono :: time_point中提取年/月/日等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 std :: chrono :: time_point 中提取年,月,日,小时,分钟,秒和毫秒<对象?



我只看到了如何提取eg的总量的例子

解决方案

您只能从 a system_clock :: time_point 。这是与民用日历有关系的唯一系统提供的时钟。下面是如何使用这个时钟获取当前的time_point:

  system_clock :: time_point now = system_clock :: now 

然后可以将其转换为 time_t 与:

  time_t tt = system_clock :: to_time_t 

使用C库,您可以转换 time_t tm ,但您必须选择是希望该转换发生在UTC时区还是您的本地时区:

  tm utc_tm = * gmtime(& tt); 
tm local_tm = * localtime(& tt);然后你可以打印出tm的组件,例如:







$ b b

  std :: cout<< local_tm.tm_year + 1900<< '\\\
';
std :: cout<< local_tm.tm_mon + 1<< '\\\
';
std :: cout<< local_tm.tm_mday< '\\\
';

另外



如果愿意,您可以利用此无保证的信息:



每个实现 system_clock I知道是基于 unix时间。也就是说从1970年UTC开始的秒数,忽略闰秒。这个计数的精度通常比秒更精细。这是一个提取所有这些信息的完整程序:

  #include< chrono> 
#include< ctime>
#include< iostream>

int
main()
{
using namespace std;
using namespace std :: chrono;
typedef duration< int,ratio_multiply< hours :: period,ratio< 24> > :: type>天;
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;
std :: cout<< d.count()<< d< h.count()< ':'
<< m.count()< ':'<< s.count();
std :: cout<< < tp.count() [
<< system_clock :: duration :: period :: num<< '/'
<< system_clock :: duration :: period :: den< ] \\\
;

time_t tt = system_clock :: to_time_t(now);
tm utc_tm = * gmtime(& tt);
tm local_tm = * localtime(& tt);
std :: cout<< utc_tm.tm_year + 1900<< ' - ';
std :: cout<< utc_tm.tm_mon + 1<< ' - ';
std :: cout<< utc_tm.tm_mday< '';
std :: cout<< utc_tm.tm_hour<< ':';
std :: cout<< utc_tm.tm_min<< ':';
std :: cout<< utc_tm.tm_sec<< '\\\
';
}

创建自定义 code>到模型日期:

  typedef duration   

现在,您可以获取自纪元以来的时间,精确到可以管理的精度:

  system_clock :: duration tp = now.time_since_epoch 

然后将其截断为天,并将其减去。


$ b



继续,直到减去秒。



剩下的就是一秒钟的小部分,单位为 system_clock :: duration 。因此,输出该值的运行时间值和该值的编译时间单位。



对于我,该程序打印出:

  15806d 20:31:14 598155 [1/1000000] 
2013-4-11 20:31:14

我的输出表明 system_clock :: duration 精度是微秒。如果需要,可以截断到毫秒:

 毫秒ms = duration_cast< milliseconds>(tp); 

更新



此标题专用的C ++ 11/14库封装了上述工作,将客户端工作降低到:

  #includedate.h
#include< iostream>

int
main()
{
//减少冗长度,但让你知道什么是命名空间
命名空间C = std :: chrono;
namespace D = date;
namespace S = std;

auto tp = C :: system_clock :: now(); // tp is a C :: system_clock :: time_point
{
//需要达到这个流运算符的命名空间日期
使用命名空间date;
S :: cout<< tp<< '\\\
';
}
auto dp = D :: floor< D :: days>(tp); // dp is a sys_days,which is a
// type alias for a C :: time_point
auto ymd = D :: year_month_day {dp};
auto time = D :: make_time(C :: duration_cast< C :: milliseconds>(tp-dp));
S :: cout<< year =<< ymd.year()<< '\\\
';
S :: cout<< month =<< ymd.month()<< '\\\
';
S :: cout<< day =<< ymd.day()<< '\\\
';
S :: cout<< hour =<< time.hours()。count()< h\\\
;
S :: cout<< minute =<< time.minutes()。count()< min\\\
;
S :: cout<< second =<< time.seconds()。count()<< s\\\
;
S :: cout<< millisecond =< time.subseconds()。count()<< ms\\\
;
}

这只是输出给我:

  2015-07-10 20:10:3​​6.023017 
year = 2015
month = Jul
day = 10
小时= 20h
minute = 10min
second = 36s
millisecond = 23ms


How can I extract the year, month, day, hour, minute, second and millisecond from an std::chrono::time_point object?

I only saw examples on how to extract the total amount of e.g. seconds from a duration.

解决方案

You can only extract this information from a system_clock::time_point. This is the only system-supplied clock that has a relationship with the civil calendar. Here is how to get the current time_point using this clock:

 system_clock::time_point now = system_clock::now();

You can then convert this to a time_t with:

time_t tt = system_clock::to_time_t(now);

Using the C library you can then convert a time_t to a tm, but you must choose whether you want that conversion to happen in the UTC timezone, or you local timezone:

tm utc_tm = *gmtime(&tt);
tm local_tm = *localtime(&tt);

Then you can print out the components of the tm, for example:

std::cout << local_tm.tm_year + 1900 << '\n';
std::cout << local_tm.tm_mon + 1 << '\n';
std::cout << local_tm.tm_mday << '\n';

Additionally

If you want, you can take advantage of this non-guaranteed information:

Every implementation of system_clock I'm aware of is based on unix time. I.e. the number of seconds since New Years 1970 UTC, neglecting leap seconds. And the precision of this count is usually finer than seconds. Here is a complete program which extracts all of this information:

#include <chrono>
#include <ctime>
#include <iostream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    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;
    std::cout << d.count() << "d " << h.count() << ':'
              << m.count() << ':' << s.count();
    std::cout << " " << tp.count() << "["
              << system_clock::duration::period::num << '/'
              << system_clock::duration::period::den << "]\n";

    time_t tt = system_clock::to_time_t(now);
    tm utc_tm = *gmtime(&tt);
    tm local_tm = *localtime(&tt);
    std::cout << utc_tm.tm_year + 1900 << '-';
    std::cout << utc_tm.tm_mon + 1 << '-';
    std::cout << utc_tm.tm_mday << ' ';
    std::cout << utc_tm.tm_hour << ':';
    std::cout << utc_tm.tm_min << ':';
    std::cout << utc_tm.tm_sec << '\n';
}

It is handy to create a custom duration to model days:

typedef duration<int, ratio_multiply<hours::period, ratio<24> >::type> days;

Now you can get the time since the epoch, to as fine a precision as it can manage, with:

system_clock::duration tp = now.time_since_epoch();

Then truncate it to days, and subtract that off.

Then truncate it to hours, and subtract that off.

Continue until you've subtracted off the seconds.

What you're left with is the fraction of a second with the units of system_clock::duration. So print out that run time value and the compile time units of that value as shown.

For me this program prints out:

15806d 20:31:14 598155[1/1000000]
2013-4-11 20:31:14

My output indicates the system_clock::duration precision is microseconds. If desired, that can be truncated to milliseconds with:

milliseconds ms = duration_cast<milliseconds>(tp);

Update

This header-only C++11/14 library encapsulates the work above, reducing client work down to:

#include "date.h"
#include <iostream>

int
main()
{
    // Reduce verbosity but let you know what is in what namespace
    namespace C = std::chrono;
    namespace D = date;
    namespace S = std;

    auto tp = C::system_clock::now(); // tp is a C::system_clock::time_point
    {
        // Need to reach into namespace date for this streaming operator
        using namespace date;
        S::cout << tp << '\n';
    }
    auto dp = D::floor<D::days>(tp);  // dp is a sys_days, which is a
                                      // type alias for a C::time_point
    auto ymd = D::year_month_day{dp};
    auto time = D::make_time(C::duration_cast<C::milliseconds>(tp-dp));
    S::cout << "year        = " << ymd.year() << '\n';
    S::cout << "month       = " << ymd.month() << '\n';
    S::cout << "day         = " << ymd.day() << '\n';
    S::cout << "hour        = " << time.hours().count() << "h\n";
    S::cout << "minute      = " << time.minutes().count() << "min\n";
    S::cout << "second      = " << time.seconds().count() << "s\n";
    S::cout << "millisecond = " << time.subseconds().count() << "ms\n";
}

Which just output for me:

2015-07-10 20:10:36.023017
year        = 2015
month       = Jul
day         = 10
hour        = 20h
minute      = 10min
second      = 36s
millisecond = 23ms

这篇关于从c ++中的std :: chrono :: time_point中提取年/月/日等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆