Formating Dates.CompoundPeriod

I have variable of type Dates.CompoundPeriod
I need to change it to String in format HH:MM:SS (value will never exceed 1 hour)

julia> x = Dates.CompoundPeriod(Dates.Hour(12), Dates.Minute(13))
12 hours, 13 minutes

julia> Dates.format(x, dateformat"HH:MM:SS")
ERROR: MethodError: no method matching format(::Dates.CompoundPeriod, ::DateFormat{Symbol("HH:MM:SS"),Tuple{Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.Delim{Char,1},Dates.DatePart{'S'}}})
Closest candidates are:
  format(::TimeType, ::DateFormat) at julia\stdlib\v1.5\Dates\src\io.jl:535
  format(::TimeType, ::DateFormat, ::Any) at julia\stdlib\v1.5\Dates\src\io.jl:535
  format(::Any, ::Dates.DatePart{'s'}, ::Any) at julia\stdlib\v1.5\Dates\src\io.jl:203

Is there any standard way to do this? Or do I have to parse string as
n1 hours, n2 minutes, n3 seconds
?

Thank you.

1 Like

You should parse/convert your CompoundPeriod into a Dates.Time type object. It has support for formatting.

So far, in Julia converting a CompoundPeriod type object to any other Period type or TimeType is not straightforward. @Laco_Kovac, you can notice another topic here.

@quinnj, I think you are trying to say something like this:

julia> using Dates

julia> x = Dates.CompoundPeriod(Dates.Hour(12), Dates.Minute(13))
12 hours, 13 minutes

julia> Dates.format(Dates.Time(x.periods[1], x.periods[2]), dateformat"HH:MM:SS")
"12:13:00"

Thank you!

Dates.format(Dates.Time(x.periods…), dateformat"HH:MM:SS")

That is the best solution for my issue, because x could contain any of the 3 (hours, minutes, seconds).

1 Like