gnucash maint: Fix test failure for timezones west of the prime meridian.

John Ralls jralls at code.gnucash.org
Sat May 2 18:35:23 EDT 2020


Updated	 via  https://github.com/Gnucash/gnucash/commit/6a3fabc3 (commit)
	from  https://github.com/Gnucash/gnucash/commit/ca9d58b2 (commit)



commit 6a3fabc30d8e0f551c8918703f07119d5f5bb065
Author: John Ralls <jralls at ceridwen.us>
Date:   Sat May 2 15:29:02 2020 -0700

    Fix test failure for timezones west of the prime meridian.
    
    The previous fix didn't calculate the comparison time correctly and
    ended up in the wrong day because the GMT time is before the local time.
    
    In the course of checking the corner-case timezones (Midway and
    Kiritimati) I found an error in the GncDateTime calculation of the
    neutral time, so fixed that too.

diff --git a/bindings/python/tests/test_business.py b/bindings/python/tests/test_business.py
index 97528ec71..4baea45a9 100644
--- a/bindings/python/tests/test_business.py
+++ b/bindings/python/tests/test_business.py
@@ -1,6 +1,6 @@
 from unittest import main
 
-from datetime import datetime, timezone
+from datetime import datetime, timezone, timedelta
 
 from gnucash import Account, \
     ACCT_TYPE_RECEIVABLE, ACCT_TYPE_INCOME, ACCT_TYPE_BANK, \
@@ -56,8 +56,15 @@ class TestBusiness(BusinessSession):
         self.assertEqual( NAME, self.employee.GetUsername() )
 
     def test_post(self):
-        self.assertEqual(datetime.now(timezone.utc).replace(hour=10, minute=59, second=0, microsecond=0).astimezone(),
-                         self.invoice.GetDatePosted().astimezone())
+        utc_offset = datetime.now().astimezone().utcoffset()
+        now = datetime.now().astimezone()
+        neutral_time = (now + utc_offset).astimezone(timezone.utc).replace(hour=10, minute=59, second=0, microsecond=0)
+        if utc_offset > timedelta(hours=13):
+            neutral_time -= utc_offset - timedelta(hours=13);
+        if utc_offset < timedelta(hours=-10):
+            neutral_time += timedelta(hours=-10) - utc_offset
+        self.assertEqual(neutral_time,
+                         self.invoice.GetDatePosted().astimezone(timezone.utc))
         self.assertTrue( self.invoice.IsPosted() )
 
     def test_owner(self):
diff --git a/libgnucash/engine/gnc-datetime.cpp b/libgnucash/engine/gnc-datetime.cpp
index 2192ffd3f..bd9a8cd31 100644
--- a/libgnucash/engine/gnc-datetime.cpp
+++ b/libgnucash/engine/gnc-datetime.cpp
@@ -314,7 +314,7 @@ GncDateTimeImpl::GncDateTimeImpl(const GncDateImpl& date, DayPart part) :
             if (offset < hours(-10))
                 m_time -= hours(offset.hours() + 10);
             if (offset > hours(13))
-                m_time -= hours(offset.hours() - 11);
+                m_time += hours(13 - offset.hours());
         }
         catch(boost::gregorian::bad_year&)
         {



Summary of changes:
 bindings/python/tests/test_business.py | 13 ++++++++++---
 libgnucash/engine/gnc-datetime.cpp     |  2 +-
 2 files changed, 11 insertions(+), 4 deletions(-)



More information about the gnucash-changes mailing list