R
Roger Leigh
I'd like to use standard C++ to process dates, converting to and from
std::string and std::tm representations. The dates are going to/from
a database, as ISO-8601 or SQL date-formatted strings. They will also
be presented to the user in the correct format for their locale.
I can't see a way to use ISO-8601 dates without resorting to
non-standard GNU extensions (I'm using GNU libstdc++ and libc). Is
this possible? I don't want to tie the code to non-portable GNU
extensions (example below).
It looks like std::time_get<> and std::time_put<> were intended for
this sort of thing, but I'm confused as to how I actually use them.
(I've got Jossuttis' "The Standard C++ Library", but it doesn't
include any examples of this). Could anyone provide any pointers
to examples of their usage? Does anyone actually use them? (I
really just want a default localised strftime/strptime format
specification).
Thanks,
Roger
#include <iostream>
#include <ctime>
int main()
{
// ISO-8601 from a PostgreSQL database query.
std::string stime("2004-04-20 20:13:20.962245+01");
std::tm brokentime;
// Parse with strptime(3) using GNU ISO-8601 %z (timezone offset)
// extension.
char *unparsed =
strptime(stime.c_str(), "%Y-%m-%d %H:%M:%S+%z", &brokentime);
std::string leftover = (unparsed) ? unparsed : "none";
std::cout << "Unparsed: " << leftover << '\n';
// Normalise.
mktime(&brokentime);
// Print with strftime(3) and GNU ISO-8601 %z (timezone offset)
// extension.
char buffer[40];
strftime(&buffer[0], 40, "%d/%m/%Y %H:%M%z", &brokentime);
std::cout << "Time: " << &buffer[0] << '\n';
// Try again, but use proper locale facets...
std::use_facet<std::time_get<char> >(std::locale::classic());
// err...
return 0;
}
std::string and std::tm representations. The dates are going to/from
a database, as ISO-8601 or SQL date-formatted strings. They will also
be presented to the user in the correct format for their locale.
I can't see a way to use ISO-8601 dates without resorting to
non-standard GNU extensions (I'm using GNU libstdc++ and libc). Is
this possible? I don't want to tie the code to non-portable GNU
extensions (example below).
It looks like std::time_get<> and std::time_put<> were intended for
this sort of thing, but I'm confused as to how I actually use them.
(I've got Jossuttis' "The Standard C++ Library", but it doesn't
include any examples of this). Could anyone provide any pointers
to examples of their usage? Does anyone actually use them? (I
really just want a default localised strftime/strptime format
specification).
Thanks,
Roger
#include <iostream>
#include <ctime>
int main()
{
// ISO-8601 from a PostgreSQL database query.
std::string stime("2004-04-20 20:13:20.962245+01");
std::tm brokentime;
// Parse with strptime(3) using GNU ISO-8601 %z (timezone offset)
// extension.
char *unparsed =
strptime(stime.c_str(), "%Y-%m-%d %H:%M:%S+%z", &brokentime);
std::string leftover = (unparsed) ? unparsed : "none";
std::cout << "Unparsed: " << leftover << '\n';
// Normalise.
mktime(&brokentime);
// Print with strftime(3) and GNU ISO-8601 %z (timezone offset)
// extension.
char buffer[40];
strftime(&buffer[0], 40, "%d/%m/%Y %H:%M%z", &brokentime);
std::cout << "Time: " << &buffer[0] << '\n';
// Try again, but use proper locale facets...
std::use_facet<std::time_get<char> >(std::locale::classic());
// err...
return 0;
}