Std :: time_get - century?

Is there a way to say std::time_get get_date

what this century is? We are dealing with dates before 1900. Is there a better C ++ date time library that would allow for this? We have our own solution that deals with multiple cultures, but get_date

seems to handle all cultures, so this is good as a last resort to catch everything ...

+3


source to share


2 answers


If you have a C ++ 11 environment (at least in the std :: lib implementation), you can use the new manipulator std::get_time

:

template <class charT>
unspecified
get_time(struct tm* tmb, const charT* fmt);

      

For example:

#include <iomanip>
#include <sstream>
#include <iostream>
#include <ctime>

int
main()
{
    std::istringstream infile("1799-03-03");
    std::tm tm = {0};
    infile >> std::get_time(&tm, "%Y-%m-%d");
    std::cout << tm.tm_year + 1900 << '\n';
}

      

This should output:

1799

      

The conversion %Y

specifier is specified as "year as a decimal number (eg 1997)". When stored in std::tm

, it will be the year 1900, but the value is held down int

to negative values.

The full set of conversion specifiers is specified by C ++ 11 as specified for an ISO / IEC 9945 function strptime

.



If you're looking for a full featured date library, the boost::datetime

one Rapptz mentioned is a good suggestion.

You can also use my personal date library which is the only title and only source that was suggested to the C ++ committee and rejected. I mention that since the source is still in the std :: chrono namespace (for the purposes of the claim), but if you use it, you must change the namespace. Here's a proposal that documents the library, and links to a single header and original implementation.

A translation of the above example would look like this:

#include "date"
#include <iostream>
#include <sstream>

int
main()
{
    std::istringstream infile("1799-03-03");
    std::chrono::date date;
    infile >> date;
    std::cout << date.year() << '\n';
}

      

which again outputs:

1799

      

As implemented, this library also depends on the C ++ 11 std::get_time

input manipulator and includes options for changing the I / O conversion specifier (specified in the linked clause).

+1


source


You can try Boost.Gregorian



+3


source







All Articles