Skip to content

Commit c863617

Browse files
committed
refactor(report): Clean _extract_timestamp method code
1 parent 663648b commit c863617

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/powerapi/report/report.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,23 @@ def to_json(report: Report) -> Dict:
109109

110110
@staticmethod
111111
def _extract_timestamp(ts):
112+
# Unix timestamp format (in milliseconds)
113+
if isinstance(ts, int):
114+
return datetime.fromtimestamp(ts / 1000)
115+
116+
# datetime object
117+
if isinstance(ts, datetime):
118+
return ts
119+
112120
if isinstance(ts, str):
113121
try:
122+
# ISO 8601 date format
114123
return datetime.strptime(ts, "%Y-%m-%dT%H:%M:%S.%f")
115124
except ValueError:
125+
# Unix timestamp format (in milliseconds)
116126
return datetime.fromtimestamp(int(ts) / 1000)
117-
if isinstance(ts, datetime):
118-
return ts
119127

120-
raise ValueError('timestamp must be a datetime.datetime or a string')
128+
raise ValueError('Invalid timestamp format')
121129

122130
@staticmethod
123131
def create_empty_report():

0 commit comments

Comments
 (0)