Skip to content

Commit

Permalink
Merge branch master to orm
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Apr 14, 2020
2 parents a9bdcd7 + 6fb0fc3 commit 6bd8c55
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pony/converting.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,14 @@ def _extract_time_parts(groupdict):
return int(hh), int(mm or 0), int(ss or 0), int(mcs)

def str2timedelta(s):
negative = s.startswith('-')
if '.' in s:
s, fractional = s.split('.')
microseconds = int((fractional + '000000')[:6])
else: microseconds = 0
h, m, s = imap(int, s.split(':'))
td = timedelta(hours=abs(h), minutes=m, seconds=s, microseconds=microseconds)
return -td if h < 0 else td
return -td if negative else td

def timedelta2str(td):
total_seconds = td.days * (24 * 60 * 60) + td.seconds
Expand Down
67 changes: 67 additions & 0 deletions pony/orm/tests/test_conversions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import unittest
from datetime import timedelta

from pony import converting

class Test(unittest.TestCase):
def test_timestamps_1(self):
s = '1:2:3'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(hours=1, minutes=2, seconds=3))
s = '01:02:03'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(hours=1, minutes=2, seconds=3))
s = '1:2:3.456'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(hours=1, minutes=2, seconds=3, milliseconds=456))
s = '1:2:3.45678'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(hours=1, minutes=2, seconds=3, microseconds=456780))
s = '12:34:56'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(hours=12, minutes=34, seconds=56))
s = '12:34:56.789'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(hours=12, minutes=34, seconds=56, milliseconds=789))
s = '12:34:56.789123'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(hours=12, minutes=34, seconds=56, microseconds=789123))

def test_timestamps_2(self):
s = '-1:2:3'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(hours=1, minutes=2, seconds=3))
s = '-01:02:03'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(hours=1, minutes=2, seconds=3))
s = '-1:2:3.456'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(hours=1, minutes=2, seconds=3, milliseconds=456))
s = '-1:2:3.45678'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(hours=1, minutes=2, seconds=3, microseconds=456780))
s = '-12:34:56'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(hours=12, minutes=34, seconds=56))
s = '-12:34:56.789'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(hours=12, minutes=34, seconds=56, milliseconds=789))
s = '-12:34:56.789123'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(hours=12, minutes=34, seconds=56, microseconds=789123))

def test_timestamps_3(self):
s = '0:1:2'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(minutes=1, seconds=2))
s = '0:1:2.3456'
td = converting.str2timedelta(s)
self.assertEqual(td, timedelta(minutes=1, seconds=2, microseconds=345600))

def test_timestamps_4(self):
s = '-0:1:2'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(minutes=1, seconds=2))
s = '-0:1:2.3456'
td = converting.str2timedelta(s)
self.assertEqual(td, -timedelta(minutes=1, seconds=2, microseconds=345600))

0 comments on commit 6bd8c55

Please sign in to comment.