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

Parsing dates with no separating marks. #859

Open
wiluite opened this issue Nov 26, 2024 · 1 comment
Open

Parsing dates with no separating marks. #859

wiluite opened this issue Nov 26, 2024 · 1 comment

Comments

@wiluite
Copy link

wiluite commented Nov 26, 2024

Hello,

Can one use the date library for parsing dates/datetimes (at least, dates) represented by such a non-standard format like "yyyymmdd", i.e without separating marks?

Thanks.

@HowardHinnant
Copy link
Owner

Yes:

#include "date/date.h"
#include <chrono>
#include <iostream>
#include <sstream>

int
main()
{
    using namespace date;
    using namespace std;
    using namespace chrono;

    istringstream in{"20241126"};
    sys_days tp;
    in >> parse("%Y%m%d", tp);
    cout << tp << '\n';
}

Output:

2024-11-26

Several of the flags have default widths, beyond which no parsing will take place. For example the default width for %Y is 4. There is also a way to modify the default width, for example %2Y will only read two digits for the year. But that year will become a year in the first century, not a recent year.

Here are the details: https://howardhinnant.github.io/date/date.html#from_stream_formatting

This library is also now part of C++20, but in namespace std::chrono and in header <chrono>. If you can use C++20 and your vendor implements it, I encourage the use of the <chrono> version over this library.

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