Observed behavior
consider the following:
#include <chrono>
#include <sstream>
#include <iostream>
#include <scn/scan.h>
#include <scn/chrono.h>
int main() {
const auto datetime = std::string_view("2025-02-05T16:00:01.010101");
std::chrono::sys_time<std::chrono::nanoseconds> tp;
auto is = std::istringstream(std::string(datetime));
is >> std::chrono::parse("%Y-%m-%dT%H:%M:%S", tp);
if (is.fail()) { return 1; }
std::cout << tp.time_since_epoch().count() << std::endl;
auto result = scn::scan<std::chrono::system_clock::time_point>(
datetime, "{:%Y-%m-%dT%H:%M:%S}");
if (!result) { return 2; }
std::cout << result->value().time_since_epoch().count() << std::endl;
return 0;
}
This program outputs:
1738771201010101000
1738771201000000000
https://godbolt.org/z/vPfxP9bvG
Here std::chrono::parse and scn::scan use the same chrono format specifier for the datetime, but scnlib parses up to the decimal point, and std::chrono::parse parses up to the end of the input string.
Otherwise scn::scan can parse datetimes fine, including the fractional seconds if they include some terminator, like "2025-02-05T16:00:01.010101Z" with the format string %Y-%m-%dT%H:%M:%SZ.
It looks like the difference is that std::chrono::parse eagerly parses, while scnlib lazily.
Expected behavior
std::chrono::parse and scnlib::scan to behave consistently when parsing date and time.
- Overall eager seems to be preferable in this case, regardless of what
std::chrono::parse is doing.
Notes
Changing this possibly breaks existing user code. As a compromise a distinct eager seconds format specifier could be added (if it doesn't exist already, I didn't look too hard), and a change in behavior could be considered for %S in a major version bump.
Observed behavior
consider the following:
This program outputs:
https://godbolt.org/z/vPfxP9bvG
Here
std::chrono::parseandscn::scanuse the same chrono format specifier for the datetime, but scnlib parses up to the decimal point, andstd::chrono::parseparses up to the end of the input string.Otherwise
scn::scancan parse datetimes fine, including the fractional seconds if they include some terminator, like"2025-02-05T16:00:01.010101Z"with the format string%Y-%m-%dT%H:%M:%SZ.It looks like the difference is that
std::chrono::parseeagerly parses, while scnlib lazily.Expected behavior
std::chrono::parseandscnlib::scanto behave consistently when parsing date and time.std::chrono::parseis doing.Notes
Changing this possibly breaks existing user code. As a compromise a distinct eager seconds format specifier could be added (if it doesn't exist already, I didn't look too hard), and a change in behavior could be considered for
%Sin a major version bump.