# Simple conversion from Time to Period?

**URL:** <https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781>\
**Category:** General Usage\
**Tags:** dates, timeperiod\
**Created:** [May 27, 2022, 12:50pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781 "2022-05-27T12:50:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [May 27, 2022, 12:50pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/1 "2022-05-27T12:50:42Z")

</div>

I could do Time(12).instant, but that is probably not good practice. Doing Time(12) - Time(0) seems odd. Is there some proper way to convert Time to Period?

One thing that motivates this question is that I want to calculate the Period from a Time to the end of day. However, I cannot do Time(24) - Time(x) because the Time constructor will not allow 24 (out of range).

---

<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:** [May 27, 2022, 1:59pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/2 "2022-05-27T13:59:03Z")

</div>

> [@taotree](#):
>
> I cannot do Time(24) - Time(x) because the Time constructor will not allow 24 (out of range)

Perhaps you could do: `typemax(Time) - Time(x)`  
or maybe:  
`(typemax(Time) - Time(x)) + Nanosecond(1)`  
if `1 ns` matters.

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [May 27, 2022, 2:31pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/3 "2022-05-27T14:31:27Z")

</div>

Thanks! I didn’t know about typemax. That’s interesting.  
That solution is interesting, but isn’t much better than `Time(x).instant` because it relies on (or reveals) the implementation details of Time (that it is stored in nanoseconds).

The more I think about it now, the more I think I just need to write my own Base.convert(Period, Time) and use Time.instant. If that ever changes, there’s only a single place to fix.

I was just asking if that conversion already existed somewhere and I just couldn’t find it.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [May 27, 2022, 6:27pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/4 "2022-05-27T18:27:06Z")

</div>

It looks like something has been done for DateTime

```julia

t=(1,1,1)
julia> canonicalize(DateTime(t...,24, 0, 0)- DateTime(t...,17, 55, 30))
6 hours, 4 minutes, 30 seconds

julia> canonicalize(Hour(24)-(Hour(17)+Minute(44)+Second(30)))
6 hours, 15 minutes, 30 seconds

```

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [May 27, 2022, 7:05pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/5 "2022-05-27T19:05:34Z")

</div>

That’s curious. That only works if you have all zeros after the hour. `DateTime(1,1,1,24,0,0,1)` gives error. Putting that 24 in hours rolls it over to 0 time the next day. Interesting, but probably not helpful to what I’m trying to do.

I think I’ll just do something like:

```julia
Base.convert(::Type{Nanosecond}, t::Time) = t.instant
(Base.convert(::Type{T}, t::Time)::T) where T<:TimePeriod = convert(T, convert(Nanosecond, t))

```

Then calculating until end of day is something like: `Day(1) - convert(Second, Time(12))`

---

<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:** [May 27, 2022, 7:19pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/6 "2022-05-27T19:19:19Z")

</div>

What about:

```julia
using Dates

dt = (typemax(Time) - Time(12,10,15)) + Nanosecond(1)
period = canonicalize(dt)

11 hours, 49 minutes, 45 seconds

```

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [May 27, 2022, 7:28pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/7 "2022-05-27T19:28:00Z")

</div>

I think that’s the same as the first suggestion above. It leaks the details of Time implementation. If I’m ok leaking that, I might as well just do Time(x).instant because that would also be the highest performing (which this may be used in some big loops so may matter).

---

<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:** [May 27, 2022, 7:46pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/8 "2022-05-27T19:46:14Z")

</div>

It doesn’t leak anything if hermetically closed in a function.

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [May 27, 2022, 7:56pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/9 "2022-05-27T19:56:02Z")

</div>

Right, which is why I’ll just wrap Time(x).instant in a function.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 27, 2022, 9:48pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/10 "2022-05-27T21:48:49Z")

</div>

> [@taotree](#):
>
> I think I’ll just do something like:
> 
> ```julia
> Base.convert(::Type{Nanosecond}, t::Time) = t.instant
> (Base.convert(::Type{T}, t::Time)::T) where T<:TimePeriod = convert(T, convert(Nanosecond, t))
> 
> ```

That’s type piracy, isn’t it? Also, it doesn’t seem to make sense, converting time to period. It’s unsound.

Isn’t it better to create a custom function instead?

---

<div class="post-metadata">

**Author:** ![taotree](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taotree/32/31982_2.png) [@taotree](https://discourse.julialang.org/u/taotree)\
**Post date:** [May 27, 2022, 10:01pm UTC](https://discourse.julialang.org/t/simple-conversion-from-time-to-period/81781/11 "2022-05-27T22:01:23Z")

</div>

> [@DNF](#):
>
> That’s type piracy, isn’t it? Also, it doesn’t seem to make sense, converting time to period. It’s unsound.
> 
> Isn’t it better to create a custom function instead?

I thought the same thing after typing it but didn’t edit the post. I agree that doing a custom function is more appropriate.
