jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354174?usp=email )

Change subject: time: Keep the sign of negative POSIX timestamps above -1
......................................................................

time: Keep the sign of negative POSIX timestamps above -1

Timestamp._from_posix took the sign from the integer seconds part, so
'-0.5' parsed as 0 seconds plus 0.5 and produced +0.5 seconds. Apply
the sign of the seconds string to the fractional part and let timedelta
normalize the result, which also covers the existing negative cases.

The test helper that computes the expected value had the same flaw;
make it apply the sign to the whole value and add '-0.5' and
'-0.000001' to the POSIX round-trip cases.

Change-Id: Id4e4cf925b27fa761059945aa1b2c55503a936ce
---
M pywikibot/time.py
M tests/time_tests.py
2 files changed, 9 insertions(+), 10 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/time.py b/pywikibot/time.py
index 067d7e1..4b3497e 100644
--- a/pywikibot/time.py
+++ b/pywikibot/time.py
@@ -199,9 +199,9 @@
         sec = int(m['S'])
         usec = m['u']
         usec = int(usec.ljust(6, '0')) if usec else 0
-        if sec < 0 < usec:
-            sec -= 1
-            usec = 1_000_000 - usec
+        # the sign applies to the fraction too, even if seconds are -0
+        if m['S'].startswith('-'):
+            usec = -usec

         return cls(1970, 1, 1) + datetime.timedelta(seconds=sec,
                                                     microseconds=usec)
diff --git a/tests/time_tests.py b/tests/time_tests.py
index a1e3c53..af53b25 100755
--- a/tests/time_tests.py
+++ b/tests/time_tests.py
@@ -57,6 +57,8 @@
             ['-1234567890.123456', '-1234567890.123456'],
             ['1234567890.000001', '1234567890.000001'],
             ['-1234567890.000001', '-1234567890.000001'],
+            ['-0.5', '-0.500000'],
+            ['-0.000001', '-0.000001'],
         ],
         'INVALID': [
             ['200902132331309999', None],
@@ -95,13 +97,10 @@
     @staticmethod
     def _compute_posix(timestr):
         """Compute POSIX timestamp with independent method."""
-        sec, usec = map(int, timestr.split('.'))
-
-        if sec < 0 < usec:
-            sec -= 1
-            usec = 1000000 - usec
-
-        return datetime(1970, 1, 1) + timedelta(seconds=sec, microseconds=usec)
+        sign = -1 if timestr.startswith('-') else 1
+        sec, usec = map(int, timestr.lstrip('-').split('.'))
+        delta = timedelta(seconds=sec, microseconds=usec)
+        return datetime(1970, 1, 1) + sign * delta

     def _test_set_from_string_fmt(self, fmt) -> None:
         """Test creating instance from <FMT> string."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354174?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id4e4cf925b27fa761059945aa1b2c55503a936ce
Gerrit-Change-Number: 1354174
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to