Skip to content

Commit d3e247c

Browse files
committed
ENH: make get_datetime accept microseconds kwarg to not provide microseconds
1 parent a1e5b33 commit d3e247c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

heudiconv/tests/test_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,4 @@ def test_get_datetime():
9494
"""
9595
assert get_datetime('20200512', '162130') == '2020-05-12T16:21:30'
9696
assert get_datetime('20200512', '162130.5') == '2020-05-12T16:21:30.500000'
97+
assert get_datetime('20200512', '162130.5', microseconds=False) == '2020-05-12T16:21:30'

heudiconv/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def get_typed_attr(obj, attr, _type, default=None):
508508
return val
509509

510510

511-
def get_datetime(date, time):
511+
def get_datetime(date, time, *, microseconds=True):
512512
"""
513513
Combine date and time from dicom to isoformat.
514514
@@ -518,16 +518,21 @@ def get_datetime(date, time):
518518
Date in YYYYMMDD format.
519519
time : str
520520
Time in either HHMMSS.ffffff format or HHMMSS format.
521+
microseconds: bool, optional
522+
Either to include microseconds in the output
521523
522524
Returns
523525
-------
524526
datetime_str : str
525-
Combined date and time in ISO format (with microseconds if
526-
they were available in provided time).
527+
Combined date and time in ISO format, with microseconds as
528+
if fraction was provided in 'time', and 'microseconds' was
529+
True.
527530
"""
528531
if '.' not in time:
529532
# add dummy microseconds if not available for strptime to parse
530533
time += '.000000'
531534
td = time + ':' + date
532535
datetime_str = datetime.strptime(td, '%H%M%S.%f:%Y%m%d').isoformat()
536+
if not microseconds:
537+
datetime_str = datetime_str.split('.', 1)[0]
533538
return datetime_str

0 commit comments

Comments
 (0)