Ale*_*xej 15 c++ year2038 c++11 c++-chrono
我想打印或提取年/月/日值.
我不想用time_t,因为2038年的问题,但我在网上找到的所有例子都使用它来转换time_point到tm.
有没有一种简单的方法可以从time_point转换为tm(最好没有boost)?
就像一个实现timesub从libc的将是我的最后一招: http://www.opensource.apple.com/source/Libc/Libc-262/stdtime/localtime.c
编辑:在阅读建议的链接并进行更多研究后,我得出以下结论.
值得注意的是,Boost.Date_Time可以是仅限标头的库.资料来源:http: //www.boost.org/doc/libs/1_53_0/more/getting_started/unix-variants.html#header-only-libraries
How*_*ant 24
使用更好的算法更新答案,链接到算法的详细描述,并完成转换std::tm.
我想打印或提取年/月/日值.有没有一种简单的方法可以从time_point转换为tm(最好没有boost)?
首先要注意的是,std::chrono::time_point它不仅仅是在duration时钟上,而且在时钟上.时钟意味着一个时代.不同的时钟可以有不同的时代.
例如,在我的系统上,std::chrono::high_resolution_clock并std::chrono::steady_clock有一个时代:每当计算机启动时.如果您不知道计算机启动的时间,则无法将其转换time_point为任何日历系统.
话虽这么说,你可能正在谈论std::chrono::system_clock::time_point,因为这个time_point,只有这个time_point,需要与民间(格里高利)日历有一个确定的关系.
事实证明,std::chrono::system_clock我所知道的每一个实现 都是使用unix时间.这有一个1970年的新纪元,忽视了闰秒.
标准不保证这一点.但是,如果您希望使用以下公式,则可以利用此事实:
首先,警告,我正在使用最新的C++ 1y草案,其中包括很棒的新constexpr工具.如果您需要constexpr为编译器取消某些属性,请执行此操作.
鉴于在上面的链接中找到的算法,您可以将a转换std::chrono::time_point<std::chrono::system_clock, Duration>为a std::tm,而无需使用time_t以下函数:
template <class Duration>
std::tm
make_utc_tm(std::chrono::time_point<std::chrono::system_clock, Duration> tp)
{
using namespace std;
using namespace std::chrono;
typedef duration<int, ratio_multiply<hours::period, ratio<24>>> days;
// t is time duration since 1970-01-01
Duration t = tp.time_since_epoch();
// d is days since 1970-01-01
days d = round_down<days>(t);
// t is now time duration since midnight of day d
t -= d;
// break d down into year/month/day
int year;
unsigned month;
unsigned day;
std::tie(year, month, day) = civil_from_days(d.count());
// start filling in the tm with calendar info
std::tm tm = {0};
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_wday = weekday_from_days(d.count());
tm.tm_yday = d.count() - days_from_civil(year, 1, 1);
// Fill in the time
tm.tm_hour = duration_cast<hours>(t).count();
t -= hours(tm.tm_hour);
tm.tm_min = duration_cast<minutes>(t).count();
t -= minutes(tm.tm_min);
tm.tm_sec = duration_cast<seconds>(t).count();
return tm;
}
Run Code Online (Sandbox Code Playgroud)
另请注意,std::chrono::system_clock::time_point所有现有实现都是UTC(忽略闰秒)时区的持续时间.如果要转换time_point使用其他时区,则需要将时区的持续时间偏移量加/减到std::chrono::system_clock::time_point之前将其转换为精度days.如果你还想考虑闰秒,那么在截断之前days使用适当的秒数进行调整以使用此表,以及unix时间现在与UTC对齐的知识.
可以通过以下方式验证此功能:
#include <iostream>
#include <iomanip>
void
print_tm(const std::tm& tm)
{
using namespace std;
cout << tm.tm_year+1900;
char fill = cout.fill();
cout << setfill('0');
cout << '-' << setw(2) << tm.tm_mon+1;
cout << '-' << setw(2) << tm.tm_mday;
cout << ' ';
switch (tm.tm_wday)
{
case 0:
cout << "Sun";
break;
case 1:
cout << "Mon";
break;
case 2:
cout << "Tue";
break;
case 3:
cout << "Wed";
break;
case 4:
cout << "Thu";
break;
case 5:
cout << "Fri";
break;
case 6:
cout << "Sat";
break;
}
cout << ' ';
cout << ' ' << setw(2) << tm.tm_hour;
cout << ':' << setw(2) << tm.tm_min;
cout << ':' << setw(2) << tm.tm_sec << " UTC.";
cout << setfill(fill);
cout << " This is " << tm.tm_yday << " days since Jan 1\n";
}
int
main()
{
print_tm(make_utc_tm(std::chrono::system_clock::now()));
}
Run Code Online (Sandbox Code Playgroud)
我目前打印出来的是:
2013-09-15太阳18:16:50 UTC 这是自1月1日以来的257天
如果时间兼容的低级别日期算法脱机或被移动,以下是使用的算法make_utc_tm.在上面的链接中有对这些算法的深入解释.它们经过了充分测试,具有非常大的有效性.
// Returns number of days since civil 1970-01-01. Negative values indicate
// days prior to 1970-01-01.
// Preconditions: y-m-d represents a date in the civil (Gregorian) calendar
// m is in [1, 12]
// d is in [1, last_day_of_month(y, m)]
// y is "approximately" in
// [numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
// Exact range of validity is:
// [civil_from_days(numeric_limits<Int>::min()),
// civil_from_days(numeric_limits<Int>::max()-719468)]
template <class Int>
constexpr
Int
days_from_civil(Int y, unsigned m, unsigned d) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
y -= m <= 2;
const Int era = (y >= 0 ? y : y-399) / 400;
const unsigned yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
const unsigned doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1; // [0, 365]
const unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096]
return era * 146097 + static_cast<Int>(doe) - 719468;
}
// Returns year/month/day triple in civil calendar
// Preconditions: z is number of days since 1970-01-01 and is in the range:
// [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
template <class Int>
constexpr
std::tuple<Int, unsigned, unsigned>
civil_from_days(Int z) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
z += 719468;
const Int era = (z >= 0 ? z : z - 146096) / 146097;
const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
const Int y = static_cast<Int>(yoe) + era * 400;
const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
const unsigned mp = (5*doy + 2)/153; // [0, 11]
const unsigned d = doy - (153*mp+2)/5 + 1; // [1, 31]
const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12]
return std::tuple<Int, unsigned, unsigned>(y + (m <= 2), m, d);
}
template <class Int>
constexpr
unsigned
weekday_from_days(Int z) noexcept
{
return static_cast<unsigned>(z >= -4 ? (z+4) % 7 : (z+5) % 7 + 6);
}
template <class To, class Rep, class Period>
To
round_down(const std::chrono::duration<Rep, Period>& d)
{
To t = std::chrono::duration_cast<To>(d);
if (t > d)
--t;
return t;
}
Run Code Online (Sandbox Code Playgroud)
更新
最近,我将上述算法包装到一个免费提供的日期/时间库中,并在此处提供.这个库可以很容易地提取一年/月/日std::system_clock::time_point,甚至几小时:分钟:秒:小数秒.一切都没有经过time_t.
这是一个简单的程序,使用上面的仅标题库来打印UTC时区中的当前日期和时间,以及任何商品的精度system_clock::time_point(在这种情况下为微秒):
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std;
using namespace std::chrono;
auto const now = system_clock::now();
auto const dp = time_point_cast<days>(now);
auto const date = year_month_day(dp);
auto const time = make_time(now-dp);
cout << date << ' ' << time << " UTC\n";
}
Run Code Online (Sandbox Code Playgroud)
这只是输出给我:
2015-05-19 15:03:47.754002 UTC
Run Code Online (Sandbox Code Playgroud)
该库有效地变成std::chrono::system_clock::time_point了易于使用的日期时间类型.
除了基于time_t.
选项是,按照我的喜好顺序:
gmtime()