Re: Date and time conversion
From: Cube (dadob051_at_hotmail.com)
Date: 04/26/04
- Next message: David: "Re: HELP!"
- Previous message: bex: "HELP!"
- In reply to: Roger Leigh: "Date and time conversion"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 26 Apr 2004 11:23:05 +0200
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:
> 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;
> }
>
>
- Next message: David: "Re: HELP!"
- Previous message: bex: "HELP!"
- In reply to: Roger Leigh: "Date and time conversion"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|