1

I'm looking for a C++ library that has equivalent functionality to Python's dateutil. Specifically, I want:

dateutil.parser.parse: Given a string that represents a date, convert it to some kind of date object from which I can get seconds since the epoch. dateutil can handle diverse strings like "January 1st, 2017", " "2017-01-01 18:00" and "Jan 1 17 7am" - if there's a C++ library that can automatically handle a variety of strings as well, that would be ideal.

dateutil.rrule - Generate a list of dates and times from a rule. For instance, in Python I can get 7am every Saturday and Sunday between two dates by writing:

dateutil.rrule.rrule(
            dateutil.rrule.DAILY,
            byweekday=(dateutil.rrule.SA, dateutil.rrule.SU),
            dtstart=datetime.datetime.combine(start_date_day, datetime.time(7, 00)),
            until=datetime.datetime.combine(end_date_day, datetime.time(23, 59)))

I'm looking for the same ability to generate dates/times between dates with rules like the one above.

2
  • 1. Parsing is one thing, and "date rules" are another. I'm not even sure you need a library for the the sequence of dates/date-times if you use something like std::iota and std::transform. 2. Can you explain what about the standard library's parsing capabilities is insufficient for you? I recall (although I'm not sure) that it does do some parsing.
    – einpoklum
    Commented Jan 10, 2017 at 16:01
  • I don't really see how std::iota helps. How would you write the rrule above with iota? It would involve checking the days of week, the the start times, end times, etc., which is what I was hoping an existing library would handle for me Commented Jan 10, 2017 at 16:21

2 Answers 2

1

Beginning with the C++11 standard, the standard library includes date and time facilities, accessible with via the <chrono> header file; these should satisfy your parsing needs:

std::tm tm;
std::stringstream ss("Jan 9 2014 12:35:34");
ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));

this answer is based on this other answer (on StackOverflow.com).

1
  • 1
    Yeah, so the nice thing about Python dateutil is that I don't have to specify the format string. Parsing with a format string is obviously trivial Commented Jan 10, 2017 at 16:19
1

Take a look at boost date time library which is very comprehensive, open source, mature and is well documented.

It has a very comprehensive Date time parsing and good Date Calculation support. I am sure you can get it to do your calculation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.