Skip to content

Commit b278707

Browse files
authored
fix get_time for small values (#358)
* fix get_time for small values based on #314 * fix pycodestyle
1 parent b60d80b commit b278707

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

snap7/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,8 @@ def get_time(bytearray_: bytearray, byte_index: int) -> str:
749749
hours = minutes // 60
750750
days = hours // 24
751751

752-
time_str = f"{days * sign!s}:{hours % 24!s}:{minutes % 60}:{seconds % 60!s}.{milli_seconds!s}"
752+
sign_str = '' if sign >= 0 else '-'
753+
time_str = f"{sign_str}{days!s}:{hours % 24!s}:{minutes % 60!s}:{seconds % 60!s}.{milli_seconds!s}"
753754

754755
return time_str
755756

test/test_util.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,27 @@ def test_get_dt(self):
116116
self.assertEqual(row['testdateandtime'], '2020-07-12T17:32:02.854000')
117117

118118
def test_get_time(self):
119-
"""
120-
TIME extraction from bytearray
121-
"""
122-
test_array = bytearray(_bytearray)
123-
124-
row = util.DB_Row(test_array, test_spec, layout_offset=4)
125-
126-
self.assertEqual(row['testTime'], '-21:17:57:28.193')
119+
test_values = [
120+
(0, '0:0:0:0.0'),
121+
(1, '0:0:0:0.1'), # T#1MS
122+
(1000, '0:0:0:1.0'), # T#1S
123+
(60000, '0:0:1:0.0'), # T#1M
124+
(3600000, '0:1:0:0.0'), # T#1H
125+
(86400000, '1:0:0:0.0'), # T#1D
126+
(2147483647, '24:20:31:23.647'), # max range
127+
(-0, '0:0:0:0.0'),
128+
(-1, '-0:0:0:0.1'), # T#-1MS
129+
(-1000, '-0:0:0:1.0'), # T#-1S
130+
(-60000, '-0:0:1:0.0'), # T#-1M
131+
(-3600000, '-0:1:0:0.0'), # T#-1H
132+
(-86400000, '-1:0:0:0.0'), # T#-1D
133+
(-2147483647, '-24:20:31:23.647'), # min range
134+
]
135+
136+
data = bytearray(4)
137+
for value_to_test, expected_value in test_values:
138+
data[:] = struct.pack(">i", value_to_test)
139+
self.assertEqual(util.get_time(data, 0), expected_value)
127140

128141
def test_set_time(self):
129142
test_array = bytearray(_new_bytearray)

0 commit comments

Comments
 (0)