Skip to content

Commit 3227b0b

Browse files
FIX: Error with time.py (#93)
* FIX: Error with time.py * FIX: Fixed error with time.py, added tests --------- Co-authored-by: dborodin836 <dborodin836@gmail.com>
1 parent 4266d51 commit 3227b0b

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

modules/utils/time.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ def get_minutes_from_str(time_str: str) -> int:
4242
try:
4343
struct_time = time.strptime(time_str, "%H:%M:%S")
4444
tm = struct_time.tm_hour * 60 + struct_time.tm_min
45+
return tm
4546
except ValueError:
47+
pass
48+
49+
try:
4650
struct_time = time.strptime(time_str, "%M:%S")
4751
tm = struct_time.tm_min
48-
except Exception as e:
49-
main_logger.warning(f"Unhandled error while parsing time happened. ({e})")
50-
tm = 0
51-
52-
return tm
52+
return tm
53+
except ValueError as e:
54+
main_logger.warning(f"Invalid time format. ({e})")
55+
return 0

tests/test_tf_stats.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from modules.utils.time import get_date
1+
from modules.utils.time import get_date, get_minutes_from_str
22

33

44
def test_epoch_time():
@@ -8,3 +8,18 @@ def test_epoch_time():
88
assert get_date(1497448943, 1685541046) == "5 years 11 months 21 days"
99
assert get_date(1331474543, 1685541046) == "11 years 2 months 21 days"
1010
assert get_date(1685541046, 1685541046) == "0 years 0 months 0 days"
11+
12+
13+
def test_get_minutes_from_str():
14+
# Minutes
15+
assert get_minutes_from_str("12:20") == 12
16+
assert get_minutes_from_str("12:60") == 12
17+
18+
# Hours
19+
assert get_minutes_from_str("1:00:00") == 60
20+
assert get_minutes_from_str("1:25:00") == 85
21+
22+
# Fails
23+
assert get_minutes_from_str("TEST") == 0
24+
assert get_minutes_from_str("2:") == 0
25+
assert get_minutes_from_str("12:62") == 0

0 commit comments

Comments
 (0)