c++ - Parse failure using Boost datetime library with time zone string -
i'm trying parse date/time string in custom format using boost's date time library. format i'm trying use rather unusual because includes posix time zone description string. docs library clearly state there flag (%zp
) usable both input , output handles posix time zone string. value i'm trying parse coming web browser, , rather write js perform transformation specified in zone string , send server in utc, i'd rather server-side (since boost should easily). wouldn't posting here if worked. code throws boost::bad_lexical_cast
value of "source type value not interpreted target".
using namespace boost::posix_time; using namespace boost::local_time; using namespace boost::gregorian; std::istringstream ss("1989-11-09t15:30:42.005;pst-8pdt,m3.2.0,m11.1.0"); ss.exceptions(std::ios_base::failbit); local_time_input_facet* facet = new local_time_input_facet("%y-%m-%dt%h:%m:%s%f;%zp"); ss.imbue(std::locale(ss.getloc(), facet)); local_date_time ldt(not_a_date_time); ss >> ldt; // parse std::cout << ldt.to_string();
however, if replace format string "%y-%m-%dt%h:%m:%s%f;"
, parse succeeds fine (of course outputs value in wrong time zone).
any idea i'm doing wrong? docs %zp
flag don't have example, i'm not sure how it's supposed used.
i think format string should this: %y-%m-%dt%h:%m:%s *;%zp
%s *
match seconds , fractional seconds. change, above code runs, bizarely though, output is:
1989-nov-09 15:30:42.005000 st
not sure why says st
rather pst
, timezone information parsed correctly, if change date example nov-01, report pdt
.
edit: description of time zone object here.
okay, messing around this, appears parsing error caused presence of ;
, removing original string such becomes:
std::istringstream ss("1989-10-16t15:30:42.005 pst-8pdt,m3.2.0,m10.2.0");
and changing format string to:
local_time_input_facet* facet = new local_time_input_facet("%y-%m-%d %h:%m:%s %zp");
correctly reports:
1989-oct-16 15:30:42.005000 pst
if change input string again to:
std::istringstream ss("1989-10-16t15:30:42.005 pst-8pdt,m3.2.0,m11.1.0");
the output again correct:
1989-oct-16 15:30:42.005000 pdt
this tells me is indeed honouring timezone information embedded in string... interesting question why barfing if there ;
?
further update, appears input string can contain alphanumerics, characters set .:-,
, spaces - after fractional bits - i.e. cannot separate timezone information stuff character other i've listed above (it's not exhaustive, don't have time test of them!)
Comments
Post a Comment