# Cannot compare some time periods

**URL:** https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417
**Category:** General Usage
**Created:** [December 7, 2018, 1:17pm UTC](https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417 "2018-12-07T13:17:13Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [December 7, 2018, 1:17pm UTC](https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417/1 "2018-12-07T13:17:13Z")

</div>

Some periods of time, e.g. Millisecond vs Month, behave as incomparable:

```julia
using Dates
Millisecond(2) < Week(2) # true
Millisecond(2) < Month(2) # error

```

The reason for this error is unclear for me. Even the error trace (see below) says that there exists a corresponding `isless` method:

```julia
julia> Millisecond(2) < Month(2)
ERROR: MethodError: no method matching isless(::Millisecond, ::Month)
Closest candidates are:
  isless(::Union{Day, Hour, Microsecond, Millisecond, Minute, Nanosecond, Second, Week}, ::Union{Month, Year}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Dates/src/periods.jl:464
  isless(::Period, ::Period) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Dates/src/periods.jl:71
  isless(::Missing, ::Any) at missing.jl:66
  ...
Stacktrace:
 [1] isless(::Millisecond, ::Month) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.0/Dates/src/periods.jl:464
 [2] <(::Millisecond, ::Month) at ./operators.jl:260
 [3] top-level scope at none:0

```

`versioninfo()`:

```julia

Julia Version 1.0.2
Commit d789231e99 (2018-11-08 20:11 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-1650 v4 @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, broadwell)
Environment:
  JULIA_VERSION = 1.0.2
  JULIA_NUM_THREADS = 12

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 7, 2018, 1:24pm UTC](https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417/2 "2018-12-07T13:24:39Z")

</div>

AFAIK `<:FixedPeriod`s are only comparable to other fixed periods, see

[https://github.com/JuliaLang/julia/pull/21378](https://github.com/JuliaLang/julia/pull/21378)

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [December 7, 2018, 2:16pm UTC](https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417/3 "2018-12-07T14:16:22Z")

</div>

Okay, and does this mean that the only way to see if a millisecond-based period is larger than e.g. half a year is to compare `period > Week(26)` (or using smaller units like days)?

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [December 7, 2018, 2:34pm UTC](https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417/4 "2018-12-07T14:34:44Z")

</div>

Yes, the point is that you have to use a time period which actually has a fixed definition. A month is not a fixed period, nor is a year (it’s a calendar year not an astronomical year). It’s a terrible system, but some parts of it go back about 5000 years so I guess we’re stuck with it.

Depending on what you’re doing you might define some custom functions. In light of the above, it would probably make more sense to talk about periods like, for example 182 days rather than half a year.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [December 7, 2018, 3:14pm UTC](https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417/5 "2018-12-07T15:14:33Z")

</div>

I see, it’s actually the same in python. Then the only thing still bothering me is that the error message is completely unintuitive - it says that there is no method `isless(::Millisecond, ::Month)`, while this very traceback comes from `isless(::Millisecond, ::Month) at ...`. It made me think that it’s a bug in method resolution or something like that.

---

<div class="post-metadata">

### Author: ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)
#### Post date: [December 7, 2018, 3:21pm UTC](https://discourse.julialang.org/t/cannot-compare-some-time-periods/18417/6 "2018-12-07T15:21:42Z")

</div>

The `MethodError` is indeed very confusing. Maybe a `DomainError` would be more useful.
