Simplest way to get current time in current timezone using boost::date_time?

24,572

Solution 1

This does what I want:

  namespace pt = boost::posix_time;
  std::ostringstream msg;
  const pt::ptime now = pt::second_clock::local_time();
  pt::time_facet*const f = new pt::time_facet("%H-%M-%S");
  msg.imbue(std::locale(msg.getloc(),f));
  msg << now;

Solution 2

While this is not using boost::date_time it's relatively easy with boost::locale, which is quite more adapted for this task. As your need is simply getting a formatted time from the current locale.

IMHO boost::date_time should be used when you deal with softwares like gantt/planning computations, were you have alot of date_time arithmetic. But simply for using time and doing some arithmetic on it, you will faster success with boost::locale.

#include <iostream>
#include <boost/locale.hpp>

using namespace boost;

int main(int argc, char **argv) {
   locale::generator gen;
   std::locale::global(gen(""));

   locale::date_time now;
   std::cout.imbue(std::locale());       
   std::cout << locale::as::ftime("%H-%M-%S") << now << std::endl;

   return 0;
}

Right now it should output : 15-45-48. :)

Share:
24,572
timday
Author by

timday

"The most amazing achievement of the computer software industry is its continuing cancellation of the steady and staggering gains made by the computer hardware industry." - Henry Petroski "What if we didn't take it to our limit...wouldn't we be forever dissatisfied ?" - Doug Scott "Problems are inevitable. Problems are soluble." - David Deutsch "The incremental increase in systemic complexity is rarely if ever recognized as a problem that additional complexity can't solve." - Charles Hugh Smith (OfTwoMinds blog) "If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake." - Frank Wilczek "Only those that risk going too far can possibly find out how far one can go." – T.S. Eliot "Engineers turn dreams into reality" - Giovanni Caproni (in Hayao Miyazaki's The Wind Rises) "When an Oxford man walks into the room, he walks in like he owns it. When a Cambridge man walks into the room, he walks in like he doesn't care who owns it." - my grandmother "The greatest scientific discovery was the discovery of ignorance" - Yuval Noah Harari. "Always train your doubt most strongly on those ideas that you really want to be true." - Sean Carroll "The first principle is that you must not fool yourself — and you are the easiest person to fool" - Richard Feynman "On the plains of hesitation lie the blackened bones of countless millions who at the dawn of victory lay down to rest, and in resting died." - Adlai E. Stevenson "Therefore Simplicio, come either with arguments and demonstrations and bring us no more Texts and authorities, for our disputes are about the Sensible World, and not one of Paper." - Salviati to Simplicio in Galileo's Dialogue On Two World Systems (1632) "The larger the island of knowledge, the longer the shoreline of wonder." - Ralph W. Sockman "I never enlighten anyone who has not been driven to distraction by trying to understand a difficulty or who has not got into a frenzy trying to put his ideas into words. When I have pointed out one corner of a square to anyone and he does not come back with the other three, I will not point it out to him a second time." - Confucius "The way to bring about the new age of peace and enlightenment is to assume it has already started" - ?

Updated on July 05, 2022

Comments

  • timday
    timday almost 2 years

    If I do date +%H-%M-%S on the commandline (Debian/Lenny), I get a user-friendly (not UTC, not DST-less, the time a normal person has on their wristwatch) time printed.

    What's the simplest way to obtain the same thing with boost::date_time ?

    If I do this:

    std::ostringstream msg;
    
    boost::local_time::local_date_time t = 
      boost::local_time::local_sec_clock::local_time(
        boost::local_time::time_zone_ptr()
      );
    
    boost::local_time::local_time_facet* lf(
      new boost::local_time::local_time_facet("%H-%M-%S")
    );
    
    msg.imbue(std::locale(msg.getloc(),lf));
    msg << t;
    

    Then msg.str() is an hour earlier than the time I want to see. I'm not sure whether this is because it's showing UTC or local timezone time without a DST correction (I'm in the UK).

    What's the simplest way to modify the above to yield the DST corrected local timezone time ? I have an idea it involves boost::date_time:: c_local_adjustor but can't figure it out from the examples.

  • std''OrgnlDave
    std''OrgnlDave about 12 years
    He said a simple way. There is no simple way with Boost date/time. They abstracted and generalized too much.
  • timday
    timday about 12 years
    I think that's a little harsh (although not completely unjustified). The above is simple enough (although verbose with all the namespacing). The problem is often more the documentation is telling you how to get to Alpha Centauri when you just wanted to walk around the block. On the other hand, I wanted to do the same thing in Python a while ago, and that didn't need a post on StackOverflow to work out.
  • std''OrgnlDave
    std''OrgnlDave about 12 years
    don't get me started on my first experience with boost's 'posix time' and how easy I thought it would be to get a POSIX time stamp out of it and how wrong I was! with that said we'd all be lost without Boost
  • CashCow
    CashCow over 11 years
    What you have done will also be very inefficient if you are going to printing a lot of times. And it's a very common error I see. Create the facet and locale once, store it as a static and then start from the imbue() when you have something to print with it.