tags:

views:

354

answers:

2

I am sure that this kind of questions must have been asked before, but failed to find anything by searching this site. My apologies in advance if I missed any similar questions.

  1. Is there anything in C++ that just does date manipulation at all? I am aware of SYSTEMTIME structure (it is the structure returned when you do GetSystemTime I think) but it does not seem to contain any manipulation functions. For example, I am looking at something which can do "give me the day of the second Tuesday in July 2010". Also, fitting a time not sourced from the system clock into a SYSTEMTIME structure just seems, well, wrong.

  2. Is there any library routine to validate a date at all? I am not thinking about the basic "check day is between 1 and 28". The routine must be able to say that 29-02-2009 does not exist, for example.

Thank you.

+6  A: 

You should check out Boost::DateTime.

But to answer your question there is no date time 'handling' as such in C++

Chris Huang-Leaver
+2  A: 

The SYSTEMTIME structure and GetSystemTime() function are Windows API functions and aren't really part of the core language or standard library. I know this is a little picky but this might save some embarrassment later on if someone who isn't on Windows reads this.

I'd certainly second the recommendation for the Boost::DateTime library that Chris made. That aside if you want to write standard C++ you've really only got the old C date and time functions that are also in ctime. But unless you're really stuck in a shop where you can't use boost for whatever reason, I'd go with Boost::DateTime.

Timo Geusch
There are a million and five (well almost) bolt on libraries which handle time, The Boost::DateTime is the one I would recommend. ctime, is strictly speaking a UNIX thing, and of course it's C not C++!
Chris Huang-Leaver
I agree regarding the number of libraries and I've amended my answer to make it a bit clearer. Just to make things even clearer, yes, ctime is based on the Unix time functions but it is part of the C++ standard as well.
Timo Geusch