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

  • Home
  • SEARCH
  • 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 6365221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:15:59+00:00 2026-05-25T00:15:59+00:00

I have a boost::posix_time::ptime instance and would like to convert (format) it to a

  • 0

I have a boost::posix_time::ptime instance and would like to convert (“format”) it to a string using a given boost::local_time::time_zone_ptr instance. Below is a test program showing what I currently have. It converts a ptime to a local_date_time which, from my understanding, expresses a time zone in addition to the time information.

When running this program at 2011-08-18 12:00:00 UTC, I expect the output 2011-08-18 14.00.00 UTC+02:00. Instead it prints 2011-08-18 12:00:00 UTC+00:00. i.e. Relative to the printed time zone, the printed time is correct, but it’s not in the time zone I used to create the boost::local_time::local_date_time instance.

I currently use the technique suggested in this question to use a custom format string.

#include <iostream>
#include <ctime>

#include <boost/date_time.hpp>

int main(int argc, char ** argv) {
    using namespace std;

    // Get current time, as an example
    boost::posix_time::ptime dt = boost::posix_time::microsec_clock::universal_time();

    // Create a time_zone_ptr for the desired time zone and use it to create a local_date_time
    boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("EST"));
    boost::local_time::local_date_time dt_with_zone(dt, zone);

    std::stringstream strm;

    // Set the formatting facet on the stringstream and print the local_date_time to it.
    // Ownership of the boost::local_time::local_time_facet object goes to the created std::locale object.
    strm.imbue(std::locale(std::cout.getloc(), new boost::local_time::local_time_facet("%Y-%m-%d %H:%M:%S UTC%Q")));
    strm << dt_with_zone;

    // Print the stream's content to the console
    cout << strm.str() << endl;

    return 0;
}

How should I convert the local_date_time instance to a string so the date in the string is represented using the time zone specified by the time_zone_ptr instance?

  • 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-25T00:15:59+00:00Added an answer on May 25, 2026 at 12:15 am

    I think boost does not know the time zone specifier. Replace

    new boost::local_time::posix_time_zone("EST")
    

    in your code by

    new boost::local_time::posix_time_zone("EST-05:00:00")
    

    and everything works fine. If you want to use the common standard names, you have to create a timezone database as described in the boost documentation.

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

Sidebar

Related Questions

I have a boost::posix_time::ptime that points to March 31st 2010 like this: ptime p(date(2010,
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
I have a certain boost::filesystem::path in hand and I'd like to append a string
I have following XML generated by serializing a boost::posix_time::ptime structure. I want to create
Currently I have to use stuff like ptime t = from_time_t(last_write_time(p)); std::string Created =
I am using boost::posix_time::ptime to measure my simulation run-time and for something else. assuimg
I have a class with some function like: void workerFunc(int ClassVariable) { boost::posix_time::seconds workTime(classVariableA);
I'm new in using boost and have a problem. I need shared_mutex function in
I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
So I have a function which takes const boost::posix_time::time_duration& t as an attribute and

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.