Skip to content

Commit 1c09289

Browse files
fix time parser for missing centiseconds
1 parent e9ccedf commit 1c09289

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
jc changelog
22

3-
20250330 v1.25.5
3+
20250331 v1.25.5
44
- Add `amixer` command parser
5+
- Fix `time` command parser for output that does not contain centiseconds
56
- Fix typing for upcoming python v3.14
67
- Fix timezone setting for tests to support minimal chrooted builds
78

jc/parsers/time.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132

133133
class info():
134134
"""Provides parser metadata (version, author, etc.)"""
135-
version = '1.4'
135+
version = '1.5'
136136
description = '`/usr/bin/time` command parser'
137137
author = 'Kelly Brazil'
138138
author_email = 'kellyjonbrazil@gmail.com'
@@ -171,13 +171,18 @@ def _process(proc_data):
171171
proc_data['command_being_timed'] = proc_data['command_being_timed'][1:-1]
172172

173173
if 'elapsed_time' in proc_data:
174-
proc_data['elapsed_time'] = proc_data['elapsed_time'].replace('.', ':')
175-
*hours, minutes, seconds, centiseconds = proc_data['elapsed_time'].split(':')
176-
proc_data['elapsed_time'] = proc_data['elapsed_time'][::-1].replace(':', '.', 1)[::-1]
174+
*hours, minutes, ss = proc_data['elapsed_time'].split(':')
175+
if '.' in ss:
176+
seconds, centiseconds = ss.split('.')
177+
else:
178+
seconds = ss
179+
centiseconds = '0'
180+
177181
if hours:
178182
proc_data['elapsed_time_hours'] = jc.utils.convert_to_int(hours[0])
179183
else:
180184
proc_data['elapsed_time_hours'] = 0
185+
181186
proc_data['elapsed_time_minutes'] = jc.utils.convert_to_int(minutes)
182187
proc_data['elapsed_time_seconds'] = jc.utils.convert_to_int(seconds)
183188
proc_data['elapsed_time_centiseconds'] = jc.utils.convert_to_int(centiseconds)

tests/test_time.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,37 @@ def test_time_lp_osx_10_14_6(self):
113113
"""
114114
self.assertEqual(jc.parsers.time.parse(self.osx_10_14_6_time_lp, quiet=True), self.osx_10_14_6_time_lp_json)
115115

116+
def test_time_no_centiseconds(self):
117+
"""
118+
Test 'time' output with no centiseconds data
119+
"""
120+
data = ''' Command being timed: "echo"
121+
User time (seconds): 5156.20
122+
System time (seconds): 0.05
123+
Percent of CPU this job got: 99%
124+
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:25:56
125+
Average shared text size (kbytes): 0
126+
Average unshared data size (kbytes): 0
127+
Average stack size (kbytes): 0
128+
Average total size (kbytes): 0
129+
Maximum resident set size (kbytes): 21760
130+
Average resident set size (kbytes): 0
131+
Major (requiring I/O) page faults: 0
132+
Minor (reclaiming a frame) page faults: 4975
133+
Voluntary context switches: 1
134+
Involuntary context switches: 8159
135+
Swaps: 0
136+
File system inputs: 0
137+
File system outputs: 6272
138+
Socket messages sent: 0
139+
Socket messages received: 0
140+
Signals delivered: 0
141+
Page size (bytes): 4096
142+
Exit status: 0'''
143+
144+
expected = {"command_being_timed":"echo","user_time":5156.2,"system_time":0.05,"cpu_percent":99,"elapsed_time":"1:25:56","average_shared_text_size":0,"average_unshared_data_size":0,"average_stack_size":0,"average_total_size":0,"maximum_resident_set_size":21760,"average_resident_set_size":0,"major_pagefaults":0,"minor_pagefaults":4975,"voluntary_context_switches":1,"involuntary_context_switches":8159,"swaps":0,"block_input_operations":0,"block_output_operations":6272,"messages_sent":0,"messages_received":0,"signals_delivered":0,"page_size":4096,"exit_status":0,"elapsed_time_hours":1,"elapsed_time_minutes":25,"elapsed_time_seconds":56,"elapsed_time_centiseconds":0,"elapsed_time_total_seconds":5156.0}
145+
self.assertEqual(jc.parsers.time.parse(data, quiet=True), expected)
146+
116147

117148
if __name__ == '__main__':
118149
unittest.main()

0 commit comments

Comments
 (0)