6

I have a string "2011-10-20T09:30:10-05:00"

Does someone know how I can parse it with boost::date_time library?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Alek86
  • 1,489
  • 3
  • 17
  • 26

2 Answers2

7

ok, I've found the answer

the code (for VS)

it converts the string to local_date_time, but for me it's acceptable:

#pragma warning(push)
#pragma warning(disable:4244)
#pragma warning(disable:4245)
#include <boost/date_time/local_time/local_time.hpp>
#pragma warning(pop)

#include <iostream>
#include <string>

int main() 
{
    using namespace std;
    using namespace boost::local_time;

    istringstream ss("2011-10-20T09:30:10-05:00");
    ss.exceptions(ios_base::failbit);
    local_time_input_facet* facet = new local_time_input_facet("%Y-%m-%dT%H:%M:%S%ZP");
    ss.imbue(locale(ss.getloc(), facet));

    local_date_time ldt(not_a_date_time);
    ss >> ldt; // do the parse

    std::cout <<
        ldt.to_string() <<
        "\noffset is: " <<
        to_simple_string(ldt.zone()->base_utc_offset()) <<
        std::endl;
}

maybe someone will need it

Alek86
  • 1,489
  • 3
  • 17
  • 26
  • Are you sure there is a leak? I don't remember details, but other examples also do not delete the pointer - maybe it's deleted inside some other class? – Alek86 Mar 23 '15 at 13:15
  • 2
    @flyx _"the locale is responsible for calling the matching delete from its own destructor."_ - from http://en.cppreference.com/w/cpp/locale/locale/locale .. – ALittleDiff Nov 22 '16 at 13:36
  • @flyx lol this is not a leak, the imbue method takes ownership of the facet. You should learn about the standard C++ library before denigrating others? – fabspro Oct 28 '20 at 12:51
2
const char *s = "2011-10-20T09:30:10-05:00";

boost::posix_time::ptime t_local(
    boost::gregorian::from_string(std::string(s, s + 10)),
    boost::posix_time::duration_from_string(std::string(s + 11, s + 19))
);

boost::posix_time::ptime t(
    t_local - boost::posix_time::duration_from_string(std::string(s + 19, s + 25))
);

Now t is the UTC time and t_local is the local time.

James Brock
  • 3,236
  • 1
  • 28
  • 33
  • @Alek86 Yes I do suggest that. Those magic numbers are invariant indices into your [ISO-8601](http://en.wikipedia.org/wiki/ISO_8601) formatted date string, they won't change. Of course, you should add error checking for string length, etc. in production code. This code is a proof-of-concept example. – James Brock Dec 14 '11 at 17:54
  • 1
    Should the duration for t_local be _subtracted_ from 't'? – J.Churchill Feb 14 '14 at 17:18
  • @J.Churchill I don't think so... consider: US Eastern Time is UTC−05:00. If it's noon in Greenwich, then add -05:00 to that, so it's 07:00 am in New York. So, sanity check pass. – James Brock Apr 04 '14 at 06:18
  • 1
    If the goal is to get UTC time it should be subtracted. In above 2011-10-20T09:30:10-05:00 example, the specified time _is_ 9:30:10am _Eastern Time_, so `t`already is local time. And `t_local` would then be five time zones further west. – Leon Feb 29 '16 at 16:02