Connecting Tech Pros Worldwide Forums | Help | Site Map

Date and time conversion

Roger Leigh
Guest
 
Posts: n/a
#1: Jul 22 '05

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;
}


--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----

Cube
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Date and time conversion


Insted of using template classes for manipulating dates I sugest using
standard libc routines for manipulating dates form database. I'we done
it with Oracle and is quite simple. This work for reading and writing to
databas. You can use function like mktime (man -k time -for list of all
time manipulation routines and format). The idea is to format datetime
value read from database to internal string representation (in format
you want, using T0_CHAR() function in Oracle) and then from string to
time_t struct. The mechanisam works also in oposite direction.

Cube

Roger Leigh wrote:
[color=blue]
> 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;
> }
>
>[/color]
Closed Thread


Similar C / C++ bytes