快速转换 std::chrono::time_point to/from std::string
Fast converting std::chrono::time_point to/from std::string
如何无语言环境转换std::chrono::time_point
to/fromstd::string
预定义日期-时间格式 YYYY-mm-dd HH:MM:SS.zzzzzzzzz
?
For me it turned out 唯一的方法是将 std::get_time
和 std::put_time
与 std::itringstream
和 std::ostringstream
一起使用,但是:
- 前两个有
std::tm
和 time_t
的 C 风格界面(可以忍受)
- 最后两个很慢并且
std::locale
依赖(关键)。
还有其他选择吗?
也可以考虑第三方库。我查看了 boost::posix_time::ptime
但接缝它也依赖于语言环境,而且它比 std::chrono::time_point
.
更慢且更不通用
不知道它对你来说是否足够快,但是:
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
std::cout << std::chrono::system_clock::now() << '\n';
}
只为我输出:
2017-03-02 17:04:26.813110
这是我编写的库。它是免费的,open-source,"date.h"
部分是header-only。有full documentation, and even video tutorials, all linked off of the GitHub page.
有相当广泛的格式和解析选项,以及许多其他功能,可以安全、轻松地进行时间和日历计算。
此外,如果这种格式化对您来说不够快,该库可以灵活分层,以便您可以 lower-level 与其交互并轻松编写您自己的 I/O。例如,这里是如何将 all 字段类型作为 int
从 system_clock::time_point
到微秒精度:
#include "date.h"
int
main()
{
using namespace date;
using namespace std::chrono;
auto t = system_clock::now();
// Convert system_clock::time_point to days-precision time_point
auto sd = floor<days>(t);
// Create time_of_day
auto tod = make_time(t - sd);
// Create year_month_day
year_month_day ymd = sd;
// Extract field types as int
int y = int{ymd.year()}; // Note 1
int m = unsigned{ymd.month()};
int d = unsigned{ymd.day()};
int h = tod.hours().count();
int M = tod.minutes().count();
int s = tod.seconds().count();
int us = duration_cast<microseconds>(tod.subseconds()).count();
}
现在您可以根据需要格式化这些 int
。
注意 1: 在 gcc-6.1 之前有一个关于 {}
的编译器错误。使用 ()
来解决编译器错误。
反向(从 int
s 到 system_clock::time_point
)更容易:
#include "date.h"
int
main()
{
using namespace date;
using namespace std::chrono;
int y = 2017;
int m = 3;
int d = 2;
int h = 15;
int M = 33;
int s = 55;
int us = 123456;
// Convert field values to system_clock::time_point
system_clock::time_point t = sys_days(year{y}/m/d) + hours{h} + minutes{M} +
seconds{s} + microseconds{us};
}
根据需要解析 int
,然后您可以在单个语句中形成 system_clock::time_point
。
图书馆有一个活跃的用户社区,有助于在多个平台上维护它。
如何无语言环境转换std::chrono::time_point
to/fromstd::string
预定义日期-时间格式 YYYY-mm-dd HH:MM:SS.zzzzzzzzz
?
For me it turned out 唯一的方法是将 std::get_time
和 std::put_time
与 std::itringstream
和 std::ostringstream
一起使用,但是:
- 前两个有
std::tm
和time_t
的 C 风格界面(可以忍受) - 最后两个很慢并且
std::locale
依赖(关键)。
还有其他选择吗?
也可以考虑第三方库。我查看了 boost::posix_time::ptime
但接缝它也依赖于语言环境,而且它比 std::chrono::time_point
.
不知道它对你来说是否足够快,但是:
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
std::cout << std::chrono::system_clock::now() << '\n';
}
只为我输出:
2017-03-02 17:04:26.813110
这是我编写的库。它是免费的,open-source,"date.h"
部分是header-only。有full documentation, and even video tutorials, all linked off of the GitHub page.
有相当广泛的格式和解析选项,以及许多其他功能,可以安全、轻松地进行时间和日历计算。
此外,如果这种格式化对您来说不够快,该库可以灵活分层,以便您可以 lower-level 与其交互并轻松编写您自己的 I/O。例如,这里是如何将 all 字段类型作为 int
从 system_clock::time_point
到微秒精度:
#include "date.h"
int
main()
{
using namespace date;
using namespace std::chrono;
auto t = system_clock::now();
// Convert system_clock::time_point to days-precision time_point
auto sd = floor<days>(t);
// Create time_of_day
auto tod = make_time(t - sd);
// Create year_month_day
year_month_day ymd = sd;
// Extract field types as int
int y = int{ymd.year()}; // Note 1
int m = unsigned{ymd.month()};
int d = unsigned{ymd.day()};
int h = tod.hours().count();
int M = tod.minutes().count();
int s = tod.seconds().count();
int us = duration_cast<microseconds>(tod.subseconds()).count();
}
现在您可以根据需要格式化这些 int
。
注意 1: 在 gcc-6.1 之前有一个关于 {}
的编译器错误。使用 ()
来解决编译器错误。
反向(从 int
s 到 system_clock::time_point
)更容易:
#include "date.h"
int
main()
{
using namespace date;
using namespace std::chrono;
int y = 2017;
int m = 3;
int d = 2;
int h = 15;
int M = 33;
int s = 55;
int us = 123456;
// Convert field values to system_clock::time_point
system_clock::time_point t = sys_days(year{y}/m/d) + hours{h} + minutes{M} +
seconds{s} + microseconds{us};
}
根据需要解析 int
,然后您可以在单个语句中形成 system_clock::time_point
。
图书馆有一个活跃的用户社区,有助于在多个平台上维护它。