Date conversions in C or C++

P

Peter Nolan

Hi All,
I promise I did my homework and looked around, but I can't find a
simple date/datetime converter in C or C++. I think there might be one
in YACL and things like that but it's pretty huge and I was hoping to
find just a simple snippet somewhere.

Situation is I have some files with varying date/date time formats in
them. They are coming in as strings. I am setting some parameters to
my code to tell it what format the date/datetime is in for this file.
(only one format each per file) And from the definition of the format I
want to convert each date/datetime field to it's ISO format YYYY-MM-DD
and YYYY-MM-DD HH:MM:SS.MMM.

I am guessing that such a snippet exists as this is an extremely common
activity. I am surprised I didn't find one searching.

I'm looking for C or C++. C would be nice and easy as my code is C++
but where I want to put this is inside another class where I already
have the format defintion and the date field to convert as C strings.

Any pointer would be most appreciated..

Best Regards
Peter Nolan
www.peternolan.com
 
K

Karl Heinz Buchegger

Peter said:
Hi All,
I promise I did my homework and looked around, but I can't find a
simple date/datetime converter in C or C++. I think there might be one
in YACL and things like that but it's pretty huge and I was hoping to
find just a simple snippet somewhere.

Situation is I have some files with varying date/date time formats in
them. They are coming in as strings. I am setting some parameters to
my code to tell it what format the date/datetime is in for this file.
(only one format each per file) And from the definition of the format I
want to convert each date/datetime field to it's ISO format YYYY-MM-DD
and YYYY-MM-DD HH:MM:SS.MMM.

I am guessing that such a snippet exists as this is an extremely common
activity. I am surprised I didn't find one searching.

Maybe it is not *that* common :)

Anyway:
What you need is some code which does the parsing of existing date/datetime
strings (if the format is fixed and you are sure that no high sophisticated
error handling needs to be done eg. a simple sscanf() can do the job).
When you have that you can easily fill in the values in a 'struct tm'
and use function strftime() to format it according to your needs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top