Empty period problem - an exceptional case

I have discovered a problem with the implementation of the Dates CompoundPeriod type.

Here’s a simple REPL demo.

julia> using Dates

julia> ms0 = Millisecond(0)
0 milliseconds

julia> canonicalize(ms0)
empty period

As you can see, the canonicalize function call returns an empty period object rather than a CompoundPeriod containing Milliseconds(0).

Why is this a problem?

  • It creates an exceptional case which has to be dealt with using control flow

For example, the following will crash if a CompoundPeriod is the empty period:

Dates.Time(canonicalize(compound_period).periods...)

which produces this error

ERROR: LoadError: MethodError: no method matching Time()

This is caused by the argument to Time() being the empty period object.

  • Aside: Is there any easy way to test for the empty period? The only way I can see to make this work at the moment would be to perform a check to see if a CompoundPeriod is empty or not.

I don’t expect this exceptional case to apply just to the creation of a Time object as in this example. I would imagine this is generally a problem rather than being a niche use case in this particular instance.

It does seem to make sense that a period of zero should not have any particular unit associated with it. I chose Millisecond for this example because it is the smallest unit. (If it isn’t - then the smallest unit should be used instead of Millisecond.)

So - possibly the “bug” is not with a CompoundPeriod but with the Time function/constructor.

That may actually make more logical sense? Perhaps others can share their thoughts?

This doesn’t seem to be a bug.

According to the docs:
canonicalize(::CompoundPeriod) -> CompoundPeriod

ms0 = Dates.CompoundPeriod(Dates.Millisecond(0))  #  empty period
ms0.periods       # Period[]

See Github’s source code here.

Thinking about it a bit more, probably it should be a Time() object which can take an empty Vector. I think that would make more sense