
Format a timedelta_type as a readable string. When days == 0 and compact is present and .true., sub-second/sub-minute durations are formatted compactly (e.g. "5ms", "1.500s", "05:30[.mmm]"); the "0 days, " prefix is omitted. When compact is absent or .false., or when days /= 0, the original verbose format is preserved.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(timedelta_type), | intent(in) | :: | td | |||
| logical, | intent(in), | optional | :: | compact |
pure function format_timedelta(td, compact) result(str) !! version: experimental !! !! Format a timedelta_type as a readable string. !! When days == 0 and compact is present and .true., sub-second/sub-minute durations are !! formatted compactly (e.g. "5ms", "1.500s", "05:30[.mmm]"); the "0 days, " prefix is omitted. !! When compact is absent or .false., or when days /= 0, the original verbose format is preserved. type(timedelta_type), intent(in) :: td logical, intent(in), optional :: compact character(:), allocatable :: str integer :: h, m, s, ms logical :: show_compact h = td%seconds / 3600 m = mod(td%seconds, 3600) / 60 s = mod(td%seconds, 60) ms = td%milliseconds show_compact = .false. ! .false. by default if (present(compact)) show_compact = compact if (td%days == 0 .and. show_compact) then ! Compact formatting for zero-day durations if (h == 0 .and. m == 0 .and. s == 0) then if (ms == 0) then str = "0s" else str = to_string(ms, '(I0)') // "ms" end if return end if if (h == 0 .and. m == 0) then if (ms == 0) then str = to_string(s, '(I0)') // "s" else str = to_string(s, '(I0)') // "." // to_string(ms, '(I3.3)') // "s" end if return end if if (h > 0) then str = to_string(h, '(I2.2)') // ":" // & to_string(m, '(I2.2)') // ":" // & to_string(s, '(I2.2)') else str = to_string(m, '(I2.2)') // ":" // & to_string(s, '(I2.2)') end if if (ms /= 0) str = str // "." // to_string(ms, '(I3.3)') else ! Original format for durations with days str = to_string(td%days, '(I0)') // ' days, ' // & to_string(h, '(I2.2)') // ':' // & to_string(m, '(I2.2)') // ':' // & to_string(s, '(I2.2)') if (ms /= 0) then str = str // '.' // to_string(ms, '(I3.3)') end if end if end function format_timedelta