boost date/time and timezones

This is a discussion on boost date/time and timezones within the C++ Programming forums, part of the General Programming Boards category; Hello I have to output current date/time on my computer including timezone in this format: Date: Tue, 15 May 2007 ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    boost date/time and timezones

    Hello

    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:

    Code:
    #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();
    	ff->format(format.c_str());
    
    	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
    Location
    Chch, NZ
    Posts
    597
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21