更新时间:2023-11-04 23:22:34
使用此免费的,开源的C ++ 11/14库,这里是另一种处理解析小数秒的方法:
Using this free, open source C++11/14 library, here is another way to deal with parsing fractional seconds:
#include "tz.h"
#include <iostream>
#include <sstream>
int main()
{
using namespace date;
using namespace std::chrono;
std::istringstream str("20:48:01.469 UTC MAR 31 2016");
sys_time<milliseconds> tp;
parse(str, "%T %Z %b %d %Y", tp);
std::cout << tp << '\n';
}
输出:
2016-03-31 20:48:01.469
,使用此工具%S
和%T
只是工作。精度不是用标志控制的,而是用 std :: chrono :: time_point
的精度控制的。
I.e., with this tool %S
and %T
just work. The precision is controlled not with flags, but with the precision of the std::chrono::time_point
.
如果你想知道什么时区缩写你解析,也是可能的:
If you want to find out what timezone abbreviation you parsed, that is also possible:
std::istringstream str("20:48:01.469 UTC MAR 31 2016");
sys_time<milliseconds> tp;
std::string abbrev;
parse(str, "%T %Z %b %d %Y", tp, abbrev);
std::cout << tp << ' ' << abbrev << '\n';
输出:
2016-03-31 20:48:01.469 UTC
库建立在 std :: get_time
之上,因此具有与Jonathan的优秀(和upvoted)答案所提到的相同的可移植性问题:只有libc ++当前解析一个案例中的月份名称敏感方式。希望这会在不太遥远的未来发生变化。
This being said, this library is built on top of std::get_time
and thus has the same portability problem that Jonathan's excellent (and upvoted) answer alludes to: Only libc++ currently parses month names in a case-insensitive manner. Hopefully that will change in the not-too-distant future.
VSO#232129错误报告。
VSO#232129 bug report.
如果你必须处理UTC以外的时区,一般来说,没有确定的方法来做,因为在任何时候,多个时区可以使用相同的缩写。因此UTC偏移可能是模糊的。不过,这里是一篇关于如何使用此库的简短文章将缩写缩写为候选时区列表,您可以从这些候选时区中选择一个独特的时区。
If you have to deal with timezones other than UTC, in general, there is no sure-fire method to do that, because at any one time, more than one timezone can be using the same abbreviation. So the UTC offset can be ambiguous. However here is a short article on how to use this library to narrow down an abbreviation to a list of candidate timezones from which you might have some ad hoc logic for choosing a unique timezone.