Opened 6 years ago

Closed 5 years ago

#12449 closed Feature Requests (wontfix)

date_time from string throws exception if time not set

Reported by: peter.kirchweger@… Owned by: az_sw_dude
Milestone: To Be Determined Component: date_time
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:

Description

There was a bug in parse_iso_time of date_time\time_parsing.hpp in earlier versions of boost. This has been fixed, but the fix does not work if just a date is given (no time set). This is not what I would expect, I would like to get a time set to midnight. My suggestion is to update the fix in split() from

  //! Utility function to split appart string
  inline bool 
  split(const std::string& s,
        char sep,
        std::string& first,
        std::string& second)
  {
    std::string::size_type sep_pos = s.find(sep);
    first = s.substr(0,sep_pos);
    if (sep_pos!=std::string::npos)
        second = s.substr(sep_pos+1);
    return true;
  }

to

  //! Utility function to split appart string
  inline bool 
  split(const std::string& s,
        char sep,
        std::string& first,
        std::string& second)
  {
    std::string::size_type sep_pos = s.find(sep);
    first = s.substr(0,sep_pos);
    if (sep_pos!=std::string::npos)
        second = s.substr(sep_pos+1);
    else
        // no time set, so we want to get midnight!
        second = ::std::string("00:00:00");
    return true;
  }

Change History (1)

comment:1 by James E. King, III, 5 years ago

Resolution: wontfix
Status: newclosed

The method you are looking for is date_from_iso_string, which only takes YYYYMMDD. parse_iso_time is documented as requiring the full string:

  //! Parse time string of form YYYYMMDDThhmmss where T is delimeter between date and time
  template<class time_type>
  inline
  time_type
  parse_iso_time(const std::string& s, char sep)

The code path you describe currently throws std::out_of_range because the code is trying to look at the first character after the "T" and failing, using std::string::at. An exception here is appropriate as the input is invalid; std::invalid_argument or a runtime_error might be more reasonable than a flavor of logic_error, but I don't think it's worth the effort in this case.

Note: See TracTickets for help on using tickets.