Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 841461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:47:56+00:00 2026-05-15T05:47:56+00:00

At the moment I am trying to read in a timestring formatted and create

  • 0

At the moment I am trying to read in a timestring formatted and create a duration from that. I am currently trying to use the boost date_time time_duration class to read and store the value.

boost date_time provides a method time_duration duration_from_string(std::string) that allows a time_duration to be created from a time string and it accepts strings formatted appropriately ("[-]h[h][:mm][:ss][.fff]".).

Now this method works fine if you use a correctly formatted time string. However if you submit something invalid like “ham_sandwich” or “100” then you will instead be returned a time_duration that is not valid. Specifically if you try to pass it to a standard output stream then an assertion will occur.

My question is: Does anyone know how to test the validity of the boost time_duration? and failing that can you suggest another method of reading a timestring and getting a duration from it?

Note: I have tried the obvious testing methods that time_duration provides; is_not_a_date_time(), is_special() etc and they don’t pick up that there is an issue.

Using boost 1.38.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:47:57+00:00Added an answer on May 15, 2026 at 5:47 am

    From the documentation, it looks like you may want to try using the stream operators (operator<<, operator>>); error conditions are described at Date Time Input/Output.

    Alternately, I suppose you could validate the string before passing it in. Right offhand, it doesn’t look like that particular method has any error handling.

    Edit:
    I’m not sure I would have thought to check the return value like this if it weren’t for Brian’s answer, but for completeness here’s a full example that takes a string as input. You can either check the return value or have it throw an exception (I believe you’d want to catch std::ios_base_failure):

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <boost/date_time/posix_time/posix_time.hpp>
    
    using namespace std;
    using namespace boost::posix_time;
    
    int main(int argc, char **argv) {
        if (argc < 2) {
            cout << "Usage: " << argv[0] << " TIME_DURATION" << endl;
            return 2;
        }
    
        // No exception
        stringstream ss_noexcept(argv[1]);
        time_duration td1;
        if (ss_noexcept >> td1) {
            cout << "Valid time duration: " << td1 << endl;
        } else {
            cout << "Invalid time duration." << endl;
        }
    
        // Throws exception
        stringstream ss2;
        time_duration td2;
        ss2.exceptions(ios_base::failbit);
        ss2.str(argv[1]);
        try {
            ss2 >> td2;
            cout << "Time duration: " << td2 << endl;
        } catch (ios_base::failure e) {
            cout << "Invalid time duration (exception caught). what():\n"
                    << e.what() << endl;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read data from multiple Kinect sensors (3 at the moment)
Background Hi All, I'm trying to use Boost::MPI, at the moment I'm just trying
I am currently trying to build a small system that read's in a bunch
I'm trying to read data into my calendar visualisation using JSON. At the moment
At the moment I'm trying to figure out how use default and custom settings
At the moment I'm trying to create a kind of model in vb.net which
At the moment I am trying to create a setup where page content is
I am trying to use Python to read in an XML file containing some
I'm trying to use DateTimeOffset to convey a specific moment in time across any
I'm trying to use recode in R (from the car package) and it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.