Thread: boost date/time and timezones

  1. #1
    Registered User
    Join Date
    May 2006

    boost date/time and timezones


    I have to output current date/time on my computer including timezone in this format:
    Date: Tue, 15 May 2007 09:21:48 -0400

    -0400 is a timezone

    Im not really familiar with timezones and I could not understand the documentation how to get local timezone on my computer..

    If i'm in gmt + 1, my program should know this and output 0100

    Anyone has an idea how to do this?

    The code I have so far:

    #include <boost/date_time/posix_time/posix_time.hpp>
    #include <boost/date_time/local_time/local_time.hpp>
    #include <boost/date_time/local_time/posix_time_zone.hpp>
    #include <boost/date_time/date_facet.hpp>
    #include <ctime>
    #include <locale>
    #include <memory>
    #include <sstream>
    #include <boost/shared_ptr.hpp>
    std::string mystrftime(const std::string format, time_t time,
    					   const std::locale& locale = std::locale::classic()) {
    	boost::shared_ptr<boost::local_time::time_zone> tz(new boost::local_time::posix_time_zone("M1.0.0"));
    	boost::local_time::local_date_time dt(boost::posix_time::from_time_t(time), tz);
    	boost::local_time::local_time_facet *ff = new boost::local_time::local_time_facet();
    	std::stringstream s;
    	s.imbue(std::locale(std::locale::classic(), ff));
    	s << dt;
    	return s.str();
    int main () {
    	std::cout << mystrftime("%A, %B %d, %Y %q", std::time(0)) << '\n';

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Chch, NZ
    try tz.base_utc_offset()
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed