Re: Date and time conversion

From: Cube (dadob051_at_hotmail.com)
Date: 04/26/04


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



Relevant Pages

  • Re: Generic DBI question about backups
    ... our only Oracle database will be gone by the time one is selected. ... Which was the reason for the need of a database independent solution. ... I would recommend using a consistent text backup format. ... newlines, pipe symbols and escapes (eg, backslash-backslash for a single ...
    (perl.dbi.users)
  • Re: Date and time conversion
    ... Insted of using template classes for manipulating dates I sugest using ... standard libc routines for manipulating dates form database. ... it with Oracle and is quite simple. ... time manipulation routines and format). ...
    (comp.os.linux.development.apps)
  • Re: Express Interchange File Format
    ... The EIF file format is a binary export file for transferring data between Express Server and Personal Express databases, and with Oracle 9i or 10g Analytic Workspaces. ...
    (comp.databases.olap)
  • Re: Database Independance
    ... > independance with SQL database. ... with different databases using the SAME SQL. ... using Oracle vs. other databases, where the parser would use the Oracle ... JOIN format rather than the ANSI SQL one. ...
    (borland.public.delphi.non-technical)
  • Re: Protel 2004 answer to Brad Velander.
    ... Sorry, you misunderstand me, I mean to say that jump from 99SE to 2004 XP ... you don't have the same format with one database will all the documents. ... Well you said that you can export file format more recent than AUTOCAD 14 ... key/mouse click you press because I don't know how to do that with Protel ...
    (sci.electronics.cad)