[Boost-bugs] [Boost C++ Libraries] #9584: date_time::difference produces incorrect values

Subject: [Boost-bugs] [Boost C++ Libraries] #9584: date_time::difference produces incorrect values
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-01-17 10:00:27


#9584: date_time::difference produces incorrect values
--------------------------------------------------+---------------------
 Reporter: Alexey Spiridonov <bstbg.20.lesha@…> | Owner: artyom
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: locale
  Version: Boost 1.54.0 | Severity: Problem
 Keywords: |
--------------------------------------------------+---------------------
 The program below attempts to compute the local timezone's GMT offset for
 a given timestamp. The idea is simple. First, get the year-month-day-hour-
 minute-second representation of the timestamp in the local timezone. Then,
 create a UTC date_time with exactly the same components. The difference
 between those times is exactly the UTC offset. The program gives this:

 {{{
 g++ -std=c++11 c.cpp -o c -l boost_locale ; ./c 1383469199 ; ./c 0;
 -25199
 -28799
 }}}

 The expected output is 28800 (8 hours) both times, since my computer's
 local timezone is America/Los_Angeles (or Pacific Time).

 {{{#!c++
 #include <boost/locale.hpp>
 #include <iostream>

 int compute_utc_offset_seconds(time_t time_since_utc_epoch) {
   using namespace boost::locale;
   std::locale loc = generator()("");

   // Get the local time coordinates for the given timestamp
   date_time local_dt{(double)time_since_utc_epoch, calendar(loc)};

   // Copy the date_time coordinates into a UTC value
   date_time utc_dt{calendar(loc, "UTC")};
   for (auto p : {
     period::year(), period::month(), period::day(),
     period::hour(), period::minute(), period::second()
   }) {
     utc_dt.set(p, local_dt.get(p));
   }

   return local_dt.difference(utc_dt, period::second());
 }

 int main(int argc, char **argv) {
   if (argc < 2) {
     return 1;
   }
   std::cout << compute_utc_offset_seconds(atoi(argv[1])) << std::endl;
   return 0;
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/9584>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:15 UTC