Conversion and formatting of Dates.TimePeriod

Let’s say I have a TimePeriod object x = Minute(62).

What I’m looking for is basically

julia> Dates.format(x, "HH:MM") # not actually working!
01:02

However, this is currently not working.

1) Why can’t I format x similar to a DateTime?

julia> Dates.format(x, "HH:MM")
ERROR: MethodError: no method matching format(::Minute, ::String)
Closest candidates are:
  format(::TimeType, ::AbstractString; locale) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Dates\src\io.jl:527
  format(::Any, ::Dates.DatePart{'Y'}, ::Any) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Dates\src\io.jl:130
  format(::Any, ::Dates.DatePart{'m'}, ::Any) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Dates\src\io.jl:130
  ...
Stacktrace:
 [1] top-level scope at none:0

2) Why doesn’t canonicalize work on TimePeriod but only on Dates.CompoundPeriod?

julia> c = canonicalize(x)
ERROR: MethodError: no method matching canonicalize(::Minute)
Closest candidates are:
  canonicalize(::Dates.CompoundPeriod) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Dates\src\periods.jl:251
Stacktrace:
 [1] top-level scope at none:0

julia> c = canonicalize(Dates.CompoundPeriod(x))
1 hour, 2 minutes

3) Now that we already have the compound representation, why can’t I format it similar to DateTime?

julia> Dates.format(c, "HH:MM")
ERROR: MethodError: no method matching format(::Dates.CompoundPeriod, ::String)
Closest candidates are:
  format(::TimeType, ::AbstractString; locale) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Dates\src\io.jl:527
  format(::Any, ::Dates.DatePart{'Y'}, ::Any) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Dates\src\io.jl:130
  format(::Any, ::Dates.DatePart{'m'}, ::Any) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Dates\src\io.jl:130
  ...
Stacktrace:
 [1] top-level scope at none:0

I feel we should add these options.

1 Like

Those all seem like reasonable requests/options to me; mind filing an issue or better yet, a PR?

PR sounds good. Don’t know when I will find the time for this though. So if someone else is willing to give it a shot, go ahead.

https://github.com/JuliaLang/julia/issues/29241