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 8036839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:45:08+00:00 2026-06-05T02:45:08+00:00

I have looked at many examples seem to address this simple case. The string

  • 0

I have looked at many examples seem to address this simple case. The string I want to parse is:

“2012-06-01 16:45:34 EDT”

I have tried to create a local_time_input_facet with the folloiwng:
"%Y-%m-%d %H:%M:%S %Z"

The zone pointer of the local_date_time object is always not set. Reading the documentation is confusing:

%Z *!
Full time zone name (output only). This flag is ignored when using the time_facet with a ptime.

“EDT” // Eastern Daylight Time

Has anyone done this before?

UPDATE: I have updated the code to illustrate the problem a little better:

using namespace std;
using namespace boost::local_time;

int main()
{
    stringstream ss;

    // Set up the input datetime format.
    local_time_input_facet *input_facet 
    = new local_time_input_facet("%Y-%m-%d %H:%M:%S %ZP");
    ss.imbue(std::locale(ss.getloc(), input_facet));

    local_date_time ldt(not_a_date_time),ldt1(not_a_date_time);

    // Read a time into ldt
    ss.str("2012-06-01 17:45:34 EDT");
    ss >> ldt;

    ss.str("2012-06-01 17:45:34 CDT");
        ss >> ldt1;
        std::cerr << (ldt - ldt1).total_seconds() << std::endl;

    // Write the time to stdout.
    cout << "Full Time:\t"   << ldt.to_string() << endl;
    cout << "Local time:\t"  << ldt.local_time() << endl;
    cout << "Time zone:\t"   << ldt.zone_as_posix_string() << endl;
    cout << "Zone abbrev:\t" << ldt.zone_abbrev() << endl;
    cout << "Zone offset:\t" << ldt.zone_abbrev(true) << endl;

    cout << "Full Time:\t"   << ldt1.to_string() << endl;
    cout << "Local time:\t"  << ldt1.local_time() << endl;
    cout << "Time zone:\t"   << ldt1.zone_as_posix_string() << endl;
    cout << "Zone abbrev:\t" << ldt1.zone_abbrev() << endl;
    cout << "Zone offset:\t" << ldt1.zone_abbrev(true) << endl;

    return 0;
}

OUTPUT:

0
Full Time:  2012-Jun-01 17:45:34 EDT
Local time: 2012-Jun-01 17:45:34
Time zone:  EDT+00
Zone abbrev:    EDT
Zone offset:    +0000
Full Time:  2012-Jun-01 17:45:34 CDT
Local time: 2012-Jun-01 17:45:34
Time zone:  CDT+00
Zone abbrev:    CDT
Zone offset:    +0000
  • 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-06-05T02:45:10+00:00Added an answer on June 5, 2026 at 2:45 am

    The Bug

    According to boost’s documentation here: http://www.boost.org/doc/libs/1_57_0/doc/html/date_time/date_time_io.html#date_time.format_flags

    %Z is:

    Full time zone name (output only).

    It also says to %ZP:

    Posix time zone string (available to both input and output).

    So you need to change %Z to %ZP. Now your timestamps will parse. However, you’ll notice that the zone offset will not be set.

    Posix Time Zone Strings

    For a Posix time zone string, you’ll need to specify at least the zone abbreviation and the offset from UTC, e.g. EST-5.

    A full Posix time zone string is formatted as:

    "std offset dst [offset],start[/time],end[/time]"
    

    with no spaces, according to http://www.boost.org/doc/libs/1_57_0/doc/html/date_time/local_time.html#date_time.local_time.posix_time_zone

    Here are some examples of full Posix time zone strings for EST and PST:

    EST-5EDT,M3.2.0,M11.1.0
    PST-8PDT,M4.1.0,M10.1.0
    

    This contains the information on when Daylight Savings Time is in effect.

    However, you may be able to get away with EDT-4 in your case, depending on what you’re doing with it.

    It should be noted that as simple as Posix time zones are, they are limited in that they don’t account for historical changes in time zone rules. I think it’s best to just avoid working with time zones in the first place.

    What if I can’t control the input format?

    As the OP noted in the comments, the offsets are not listed in his input timestamps. One solution is to read the zone abbreviation from the end of the input timestamp (e.g. "EDT"), and check it against a map of known zone abbreviations:

    std::map<std::string, std::string> zone_map;
    zone_map["EST"] = "EST-5EDT,M4.1.0,M10.5.0";  // Eastern Standard Time
    zone_map["EDT"] = zone_map["EST"];            // Eastern Daylight Time
    zone_map["PST"] = "PST-8PDT,M4.1.0,M10.1.0";  // Pacific Standard Time
    zone_map["PDT"] = zone_map["PST"];            // Pacific Daylight Time
    // ...
    

    (Note the DST zones above should be the same as the standard time zones.)

    You might also get away with with just storing simple UTC offsets:

    zone_map["EST"] = "EST-5";  // Eastern Standard Time
    zone_map["EDT"] = "EDT-4";  // Eastern Daylight Time
    // ...
    

    Working Example

    Here’s an example that uses a built-in timezone database:

    #include <map>
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <boost/date_time/local_time/local_time.hpp>
    
    using namespace boost::local_time;
    
    int main()
    {
        // A little database of time zones.
        std::map<std::string, std::string> zone_map;
        zone_map["EST"] = "EST-5EDT,M4.1.0,M10.5.0";  // Eastern Standard Time
        zone_map["EDT"] = zone_map["EST"];            // Eastern Daylight Time
        zone_map["PST"] = "PST-8PDT,M4.1.0,M10.1.0";  // Pacific Standard Time
        zone_map["PDT"] = zone_map["PST"];            // Pacific Daylight Time
        // ...
    
        // This is our input timestamp.
        std::string timestamp = "2012-06-01 16:45:34 EDT";
    
        // Replace time zone abbrev with full Posix time zone.
        const size_t abbrev_pos = timestamp.find_last_of(' ') + 1;
        const std::string abbrev = timestamp.substr(abbrev_pos);
        timestamp.replace(abbrev_pos, std::string::npos, zone_map[abbrev]);
    
        std::cout << "Time stamp with full timezone: " << timestamp << std::endl;
    
        // Set up the input datetime format.
        local_time_input_facet *input_facet = new local_time_input_facet("%Y-%m-%d %H:%M:%S %ZP");
        std::stringstream ss;
        ss.imbue(std::locale(ss.getloc(), input_facet));
    
        // This is our output date time.
        local_date_time ldt(not_a_date_time);
    
        // Read the timestamp into ldt.
        ss.str(timestamp);
        ss >> ldt;
    
        // Write the time to stdout.
        std::cout << "Full Time:\t"   << ldt.to_string() << std::endl
                  << "Local time:\t"  << ldt.local_time() << std::endl
                  << "Time zone:\t"   << ldt.zone_as_posix_string() << std::endl
                  << "Zone abbrev:\t" << ldt.zone_abbrev() << std::endl
                  << "Zone offset:\t" << ldt.zone_abbrev(true) << std::endl;
    
        return 0;
    }
    

    This outputs:

    Time stamp with full timezone: 2012-06-01 16:45:34 EST-5EDT,M4.1.0,M10.5.0
    Full Time:      2012-Jun-01 16:45:34 EDT
    Local time:     2012-Jun-01 16:45:34
    Time zone:      EST-05EDT+01,M4.1.0/02:00,M10.5.0/02:00
    Zone abbrev:    EDT
    Zone offset:    -0400
    

    While the solution here may not be ideal, it’s the only way I can see of doing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked at many examples for validating an XML file against a DTD,
I have looked into all the suggested articles and they seem to address the
Have looked quite hard for this answer but having no luck. I have 3
I have looked and tried but don't see where I can stop some being
I have looked at the usual suspects...Spark, NHaml, etc. They all seem to be
I have looked on the web and the discussions/examples appear to be for traditional
Situation: I have many gzipped input files that I want to process in a
I looked and everywhere are many examples how to do property name resolution, but
I have looked at few examples here Calling a Method from an Expression and
Preface: I have looked all over stackoverflow.com and google for this. I have found

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.