473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Date and time conversion


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! =-----
Jul 22 '05 #1
1 9196
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;
}

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: praba kar | last post by:
Dear All, I have doubt regarding date string to time conversion function. In Python I cannot find flexible date string conversion function like php strtotime. I try to use following type...
1
by: Riley DeWiley | last post by:
I have an UPDATE query that is always setting 0 records. When I cut and paste the SQL into Access and use it, it fails in the same way unless I coerce the date fields to be '=now()', in which case...
4
by: Bob Day | last post by:
Using VS 2003, VB.NET, MSDE... This is incredible misleading that the following produces a results of #2/13/04# if you hover your mouse over the Date_Time_Begin, although I do think its value...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
2
by: Bob Altman | last post by:
Hi all, I need to simulate current date and time and provide integer values for the current year, month, day, hour, minute, second, and millisec. I need to do this in unmanaged C++. My first...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
6
by: vdicarlo | last post by:
I am a programming amateur and a Python newbie who needs to convert about 100,000,000 strings of the form "1999-12-30" into ordinal dates for sorting, comparison, and calculations. Though my script...
7
by: bruce.dodds | last post by:
Access seems to be handling a date string conversion inconsistently in an append query. The query converts a YYYYMM string into a date, using the following function: CDate(Right(,2) & "/1/" &...
2
by: RN1 | last post by:
A TextBox displays the current date (in dd/mm/yyyy format) & time when a user comes to a page (e.g. 15/10/2008 1:36:39 PM). To convert the date into international format so that the remote server...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.