1

I'm looking for c++ type and functions to work with "days".

In particular I need to:

  • convert strings like "20140224" to datatype and vice versa (of course i can parse strings myself to int if necessary - 2014, 02, 24)
  • "iterate", for example from "20140101" to "20140224"

I don't need:

  • time of the day. current day is "smallest" pieace i'm working with

I can use c++11, what can you recommend?

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • have a look at boost date time library: www.boost.org. – Bathsheba Feb 24 '14 at 08:21
  • @Bathsheba does it mean that c++ and c++11 doesn't contain anything suitable? – Oleg Vazhnev Feb 24 '14 at 08:29
  • Why don't you turn your comment into an answer, @Bathsheba? I don't think much better advice can be given. – Nicola Musatti Feb 24 '14 at 08:30
  • 2
    @NicolaMusatti: I took your advice and risked an answer ;-) Questions of this sort are difficult to answer well. – Bathsheba Feb 24 '14 at 08:34
  • check out [](http://en.cppreference.com/w/cpp/chrono) for managing and computing, and [C or istream](http://stackoverflow.com/q/21021388/847349) parsing, and of course, [POCO](http://pocoproject.org/docs/Poco.DateTimeParser.html) have a parser – Dmitry Ledentsov Feb 24 '14 at 09:00
  • You could use mktime from , see this answer, http://stackoverflow.com/questions/12297007/issue-with-using-ctime-to-advance-a-given-date-to-next-calendar-date – amdn Feb 24 '14 at 09:30

1 Answers1

2

C++ has always stayed away from implementing a standard date library.

They are, in general, very hard to implement and standardise. ISO8601 provides some guidance but issues such as leap-seconds, and unusual calendars (e.g. Ethiopia) will always be tricky areas.

Boost (www.boost.org) certainly makes inroads. Refer to their date-time library. Unfortunately it's quite heavyweight and the build process takes a while to get used to, but worth attempting.

(As an aside, Java have attempted it but the date time library in Java is widely considered to be broken).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • as an addition: [BOOST Date Time Input/Output](http://www.boost.org/doc/libs/1_42_0/doc/html/date_time/date_time_io.html) and [POCO](http://pocoproject.org/slides/070-DateAndTime.pdf) – Dmitry Ledentsov Feb 24 '14 at 09:04
  • Another issue is that often, you'll have to skip weekends and holidays. – James Kanze Feb 24 '14 at 09:43
  • FYI… Yes the java.util.Date & .Calendar classes bundled with early Java are notoriously troublesome. But since then Java gained the best date-time library around: [Joda-Time](http://www.joda.org/joda-time/). That third-party library was so good that a re-architected version is now bundled in the forthcoming [Java 8](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8), the new [java.time package](http://download.java.net/jdk8/docs/api/java/time/package-summary.html). A port to .Net is known as [NodaTime](http://nodatime.org/). – Basil Bourque Feb 24 '14 at 11:58