# Why do time quantities have to be integers?

**URL:** <https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [September 13, 2017, 8:52am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864 "2017-09-13T08:52:01Z")\
**Posts on this page:** 17\
**Page:** 3

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [September 22, 2017, 11:37pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/41 "2017-09-22T23:37:07Z")

</div>

Obviously Dates is a mess. What ever the bike shedding is, and it is, can we put someone on it? time representation seems crucial for a v1.0, does it not?

---

<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:** [September 23, 2017, 6:41am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/42 "2017-09-23T06:41:16Z")

</div>

> [@yakir12](#):
>
> can we put someone on it?

It will probably be done by someone who needs this behavior. Since you raised the issue, you could try submitting a PR.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 23, 2017, 12:34pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/43 "2017-09-23T12:34:43Z")

</div>

> [@yakir12](#):
>
> I have to admit that I can appreciate the accuracy we have thanks to the integer based time in Julia.

What makes you think that using `Float64` would be less accurate? Floating-point computations with integers \< 2^53 are exact.

---

<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:** [September 23, 2017, 1:09pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/44 "2017-09-23T13:09:05Z")

</div>

This [blog post by Bruce Dawson](https://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/) (actually [mentioned in the Julia manual!)](https://docs.julialang.org/en/latest/manual/integers-and-floating-point-numbers/#Background-and-References-1) is an interesting in-depth analysis of the related issues. TL;DR:

> Elapsed game time should never be stored in a float. Use a double instead.

so a `Float64` (what C calls a `double`) should be OK.

---

<div class="post-metadata">

**Author:** ![morningkyle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/morningkyle/32/1684_2.png) [@morningkyle](https://discourse.julialang.org/u/morningkyle)\
**Post date:** [September 23, 2017, 1:11pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/45 "2017-09-23T13:11:46Z")

</div>

0.3 years in nanosecond \> 2^53 nanoseconds

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 23, 2017, 1:18pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/46 "2017-09-23T13:18:10Z")

</div>

> [@morningkyle](#):
>
> 0.3 years in nanosecond \> 2^53 nanoseconds

Two points:

- 300 years in nanoseconds \> 2^63 nanoseconds. If you are measuring years in nanoseconds, pretty soon you are going to overflow Int64 too.

- If you are measuring years in nanoseconds, I’m willing to bet that you don’t actually have nanosecond accuracy. Float64 will still keep 15 significant digits for numbers \> 2^53, but Int64 will give complete nonsense if you exceed 2^63.

---

<div class="post-metadata">

**Author:** ![morningkyle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/morningkyle/32/1684_2.png) [@morningkyle](https://discourse.julialang.org/u/morningkyle)\
**Post date:** [September 23, 2017, 1:51pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/47 "2017-09-23T13:51:34Z")

</div>

> Blockquote  
> If you are measuring years in nanoseconds

That’s not my point, although nanosecond is usually the minimal time unit in designs. In my previous post, I said usually multiple integers(Int64) have be used with **different scale** of time units in the design.

If DateTime API is designed with Float64 format numbers, a single time unit is likely to be used. (Of course we may still use one Float64 for day and another Float64 for second if we choose to. This option is out of my consideration)

So this is a ONE Float64 vs. multiple Int64 comparison, and ONE time unit system vs. multiple time units design comparison.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 23, 2017, 5:49pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/48 "2017-09-23T17:49:02Z")

</div>

No one is advocating a single Float64 time unit. The question is, why can’t the current design (multiple time units) wrap Float64 values instead of Int64? Float64 seems a lot more flexible, with no apparent loss in accuracy.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [September 24, 2017, 11:41pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/49 "2017-09-24T23:41:10Z")

</div>

Split this topic on September 24, 2017, 11:41pm UTC

3 posts were split to a new topic: [Was: Why do time quantities have to be integers?](https://discourse.julialang.org/t/was-why-do-time-quantities-have-to-be-integers/6054)

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [September 24, 2017, 3:45am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/50 "2017-09-24T03:45:16Z")

</div>

The `Dates` module in Base was designed specifically with a few goals in mind:

- Simplicity: straightforward code with intuitive apis
- Efficiency: both in memory footprint & various calculations (compared to any other language basic datetime library)
- Analytics: in the “data analyst” world, timezones, leap seconds, and sub-nanosecond time precisions can often be ignored in favor of the two points already listed

This leads to the things that were specifically “punted” on (i.e. not designed for):

- Sub-nanosecond precision: requires larger memory footprint and native sub-nanosecond OS APIs
- Timezones: This avoids having to deal w/ various political shifts around the world, having to update the timezone database regularly, and having all the “timezone data compilation” have to live in Base
- Non-gregorian calendars

Now, just because these things weren’t implemented in Base, doesn’t mean they weren’t duly considered in the design and implementation: indeed, at one point, the `Dates` module _did_ include timezone support. The solution was to include design decisions that would allow packages to easily extend and implement these “date extensions” without needing to clutter Base w/ less-used features.

This has been very successfully shown to work in the [TimeZones.jl](https://github.com/JuliaTime/TimeZones.jl) package and the `ZonedDateTime` type.

On the subject of using `Float64` as the internal storage, I’ve said it and I’ll say it again: anyone is free to re-implement `Dates` using floats, I just think it’s going to be a harder time to achieve the same level of simplicity and test coverage that we currently have. I’m definitely open to it, but there was a reason I stuck with `Int64` when implementing as it made my life a lot easier. Should we support things like `Dates.Second(1.5) => Dates.Millisecond(1500)`? Sure, that seems cute.

At the end of the day, I think the entire community would be better served by having a separate package (ScientificTimes.jl or something) with a `NanoTimestamp` type (or whatever you want to call it) that could truly support the extended precision and play nice with floats.

For the data analyst world, I still think that `Dates` is and will continue to be one of the best-all-around implementations for simplicity and efficiency out there.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [October 20, 2017, 9:24am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/51 "2017-10-20T09:24:42Z")

</div>

Please forgive my oh-so-tainted presence, but… I just thought of something that could be great or moot, I’d appreciate your opinions:

Images.jl encodes all pixel intensities as a float between 0 and 1, even though the real value behind the scene is any of the `UInt`s, as is the case in all image formats. They use [FixedPointNumbers.jl](https://github.com/JuliaMath/FixedPointNumbers.jl) to accomplish this. Could we use the same trick here? i.e. allow for float like behavior and appearance but work with actual `Int`s for all the above-listed and indisputable benefits?

Just a thought.

---

<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:** [October 20, 2017, 11:05am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/52 "2017-10-20T11:05:32Z")

</div>

Related: I wrote a little package to represent dates using subtypes of `Integer` other than `Int64`, counting from a given epoch. This saved tens of Gb for me on a large dataset, and the abstraction is near-zero cost when using the same epoch.  
[https://github.com/tpapp/FlexDates.jl](https://github.com/tpapp/FlexDates.jl)

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [October 20, 2017, 11:10am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/53 "2017-10-20T11:10:25Z")

</div>

Nice! But post this on its own ANN thread. Otherwise it might get missed?

---

<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:** [October 20, 2017, 11:13am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/54 "2017-10-20T11:13:49Z")

</div>

It is WIP, I will announce it after some practical testing.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [February 4, 2018, 6:09pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/55 "2018-02-04T18:09:00Z")

</div>

Could it be extended to use a type such as `Dec128` (from `DecFP.jl`)?

---

<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:** [February 4, 2018, 6:50pm UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/56 "2018-02-04T18:50:39Z")

</div>

In theory yes, in practice I am not sure dates and floating point mix well. If you want timeunits with more precision than a day, a `Date` (or analogue, which is what this package provides) is not ideal.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 5, 2018, 1:26am UTC](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864/57 "2018-02-05T01:26:13Z")

</div>

[https://github.com/JuliaLang/julia/issues/9393](https://github.com/JuliaLang/julia/issues/9393)

[Previous page](https://discourse.julialang.org/t/why-do-time-quantities-have-to-be-integers/5864.md?page=2)
