Fix tm_to_string to use local timezone on all platforms. Some comments.
authorCarl Hetherington <cth@carlh.net>
Thu, 15 May 2014 23:01:08 +0000 (18:01 -0500)
committerCarl Hetherington <cth@carlh.net>
Thu, 15 May 2014 23:01:08 +0000 (18:01 -0500)
src/kdm.cc
src/util.cc
src/util.h
test/utc_offset_to_string_test.cc

index 4132b242a99b9d56ca2a7d9c8d13306237143084..70109936ac0f0a59b9abb640e3c3f8ed6f9ceaa8 100644 (file)
@@ -86,6 +86,9 @@ KDM::KDM (boost::filesystem::path kdm, boost::filesystem::path private_key)
        RSA_free (rsa);
 }
 
+/** @param not_valid_before KDM not-valid-before time in local time.
+ *  @param not_valid_after KDM not-valid-after time in local time.
+ */
 KDM::KDM (
        shared_ptr<const CPL> cpl, shared_ptr<const Signer> signer, shared_ptr<const Certificate> recipient_cert,
        boost::posix_time::ptime not_valid_before, boost::posix_time::ptime not_valid_after,
index a668b7fc9a5304d587b0ebeedb6094d8e9cc2e8c..4ac3685c4cd9ab9c5087ce20ac5c45d6cc85d89c 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 #include <iomanip>
 #include <boost/filesystem.hpp>
 #include <boost/lexical_cast.hpp>
+#include <boost/date_time/c_local_time_adjustor.hpp>
 #include <openssl/sha.h>
 #include <libxml++/nodes/element.h>
 #include <libxml++/document.h>
@@ -297,36 +298,33 @@ libdcp::base64_decode (string const & in, unsigned char* out, int out_length)
        return N;
 }
 
+/** @param tm Local time.
+ *  @return String of the form 2014-04-02T18:05:23+04:00, where the UTC offset is derived
+ *  from the current system time zone.
+ */
 string
 libdcp::tm_to_string (struct tm* tm)
 {
        char buffer[64];
        strftime (buffer, 64, "%Y-%m-%dT%H:%M:%S", tm);
 
-       int offset = 0;
+       /* Compute current UTC offset */
+       boost::posix_time::ptime const utc_now = boost::posix_time::second_clock::universal_time ();
+       boost::posix_time::ptime const now = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local (utc_now);
 
-#ifdef LIBDCP_POSIX
-       offset = tm->tm_gmtoff / 60;
-#else
-       TIME_ZONE_INFORMATION tz;
-       GetTimeZoneInformation (&tz);
-       offset = tz.Bias;
-#endif
-       
-       return string (buffer) + utc_offset_to_string (offset);
+       return string (buffer) + utc_offset_to_string (now - utc_now);
 }
 
-/** @param b Offset from UTC to local time in minutes.
+/** @param b Offset from UTC to local time.
  *  @return string of the form e.g. -01:00.
  */
 string
-libdcp::utc_offset_to_string (int b)
+libdcp::utc_offset_to_string (boost::posix_time::time_duration b)
 {
-       bool const negative = (b < 0);
-       b = negative ? -b : b;
-
-       int const hours = b / 60;
-       int const minutes = b % 60;
+       bool const negative = b.is_negative ();
+       if (negative) {
+               b = boost::posix_time::time_duration (-b.hours(), b.minutes(), 0, 0);
+       }
 
        stringstream o;
        if (negative) {
@@ -335,7 +333,7 @@ libdcp::utc_offset_to_string (int b)
                o << "+";
        }
 
-       o << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes;
+       o << setw(2) << setfill('0') << b.hours() << ":" << setw(2) << setfill('0') << b.minutes();
        return o.str ();
 }
 
index 2a6aae1b004de20443ab631e34ad8c4fa2c415ad..94ee136a2aac206b3700c36944d39d6fb65cf872 100644 (file)
@@ -82,7 +82,7 @@ extern void add_signer (xmlpp::Element* parent, CertificateChain const & certifi
 extern int base64_decode (std::string const & in, unsigned char* out, int out_length);
 
 extern std::string tm_to_string (struct tm *);
-extern std::string utc_offset_to_string (int);
+extern std::string utc_offset_to_string (boost::posix_time::time_duration);
 extern std::string ptime_to_string (boost::posix_time::ptime);
 extern FILE * fopen_boost (boost::filesystem::path, std::string);
        
index af9c65335e99df1192a74135917936064a866554..01b16f47661ed051921985d261f4f199575f6c98 100644 (file)
@@ -24,9 +24,9 @@
 /** Test libdcp::utc_offset_to_string */
 BOOST_AUTO_TEST_CASE (utc_offset_to_string_test)
 {
-       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (30), "+00:30");
-       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (60), "+01:00");
-       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (61), "+01:01");
-       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (7 * 60), "+07:00");
-       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (-11 * 60), "-11:00");
+       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (boost::posix_time::time_duration (0, 30, 0, 0)), "+00:30");
+       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (boost::posix_time::time_duration (0, 60, 0, 0)), "+01:00");
+       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (boost::posix_time::time_duration (0, 61, 0, 0)), "+01:01");
+       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (boost::posix_time::time_duration (7, 0, 0, 0)), "+07:00");
+       BOOST_CHECK_EQUAL (libdcp::utc_offset_to_string (boost::posix_time::time_duration (-11, 0, 0, 0)), "-11:00");
 }