[date_time] local_time_period between two different time zones
I want to calculate the time difference between local_date_time objects that I initialized using two different time zones: #include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/local_time/local_time.hpp> // TZ database int main(int argc, char* argv[]) { using namespace boost::posix_time; using namespace boost::local_time; tz_database tz_db; try { tz_db.load_from_file("tz.csv"); } catch( data_not_accessible& e ) { std::cerr << "Error with time zone data file: " << e.what() << std::endl; exit(EXIT_FAILURE); } catch( bad_field_count& e ) { std::cerr << "Error with time zone data file: " << e.what() << std::endl; exit(EXIT_FAILURE); } time_zone_ptr tzp_nyc = tz_db.time_zone_from_region("America/New_York"); time_zone_ptr tzp_rome = tz_db.time_zone_from_region("Europe/Rome"); local_date_time nyc_time(second_clock::universal_time(), tzp_nyc); std::cout << "Current time in NY is " << to_simple_string(nyc_time.local_time()) << std::endl; local_date_time rome_time(second_clock::universal_time(), tzp_rome); std::cout << "Current time in Rome is " << to_simple_string(rome_time.local_time()) << std::endl; local_time_period tdiff(nyc_time, rome_time); std::cout << "Difference between time zones: " << tdiff.length() << std::endl; return 0; } tdiff.lenth() returns 00:00:00. Why the function can't return the difference between the two local times? ____________________________________________________________________________________ Sponsored Link $420k for $1,399/mo. Think You Pay Too Much For Your Mortgage? Find Out! www.LowerMyBills.com/lre
Alberto Giannetti escribió:
I want to calculate the time difference between local_date_time objects that I initialized using two different time zones:
...
time_zone_ptr tzp_nyc = tz_db.time_zone_from_region("America/New_York"); time_zone_ptr tzp_rome = tz_db.time_zone_from_region("Europe/Rome");
local_date_time nyc_time(second_clock::universal_time(), tzp_nyc); // #1 ... local_date_time rome_time(second_clock::universal_time(), tzp_rome); // #2 ...
tdiff.lenth() returns 00:00:00. Why the function can't return the difference between the two local times?
I don't even read the Boost.Date_Time documentation, so maybe my response is going to be a nonsense, but nobody is answering, so I'll give it a try... I think that second_clock::universal_time() is returning the same time in lines #| and #2. Your computer clock is the same, whether you are in New York or in Rome. It is when you call to_simple_string(...local_time()) when this same time gets represented as NewYork time or Rome time. Now in Roma it is 21:00 h, but in New York it is 15:00. The universal time it's the same, but gets "changed" when you call to_simple_string. So if you try to diff the two times, you get 00:00:00. It seems logical to me... ;) Hope it helps (and excuse my poor English), Raul.
participants (2)
-
Alberto Giannetti
-
Raúl Huertas