# Empty period problem - an exceptional case

**URL:** <https://discourse.julialang.org/t/empty-period-problem-an-exceptional-case/121655>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [October 23, 2024, 3:11pm UTC](https://discourse.julialang.org/t/empty-period-problem-an-exceptional-case/121655 "2024-10-23T15:11:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 3:11pm UTC](https://discourse.julialang.org/t/empty-period-problem-an-exceptional-case/121655/1 "2024-10-23T15:11:45Z")

</div>

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

Here’s a simple REPL demo.

```julia
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:

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

```

which produces this error

```julia
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?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 23, 2024, 3:59pm UTC](https://discourse.julialang.org/t/empty-period-problem-an-exceptional-case/121655/2 "2024-10-23T15:59:31Z")

</div>

This doesn’t seem to be a bug.

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

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

```

See Github’s [source code here](https://github.com/JuliaLang/julia/blob/6c70bf784999b478bc176fe594738f9746a1dcfd/stdlib/Dates/src/periods.jl#L314C1-L325C4).

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 4:58pm UTC](https://discourse.julialang.org/t/empty-period-problem-an-exceptional-case/121655/3 "2024-10-23T16:58:10Z")

</div>

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
