Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cctz::parse error #79

Closed
urykhy opened this issue Jun 19, 2018 · 1 comment
Closed

cctz::parse error #79

urykhy opened this issue Jun 19, 2018 · 1 comment

Comments

@urykhy
Copy link

urykhy commented Jun 19, 2018

i'm trying to use cctz::parse but it seems have a problem with some formats:

    std::cout << "scanTime : " << scanTime("1970 02 02","%Y %m %d") << std::endl;
    std::cout << "scanTime : " << scanTime("19700202","%Y%m%d") << std::endl;

first line produce 2764800.
second - error.

full code:

#include <iostream>
#include <cctz/civil_time.h>
#include <cctz/time_zone.h>
#include <chrono>
using SC = std::chrono::system_clock;
time_t scanTime(const char* str, const char* format)
{
    std::chrono::system_clock::time_point tp;
    if (!cctz::parse(format, str, cctz::utc_time_zone(), &tp))
        return 0;
    return SC::to_time_t(tp);
}
int main(void)
{
    std::cout << "scanTime : " << scanTime("1970 02 02","%Y %m %d") << std::endl;
    std::cout << "scanTime : " << scanTime("19700202","%Y%m%d") << std::endl;
    return 0;
}
@devbww
Copy link
Contributor

devbww commented Jun 19, 2018

In the string "19700202" there is a year, 19700202, and then no month or day, so the parse fails. There is no backtracking on failed matches looking for alternatives.

If you want to limit yourself to 4-digit years, use the %E4Y format specifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants