How to convert period in milisecond to minutes / seconds , hour etc

How to convert period in milisecond to minutes / seconds , hour etc
julia 0.5.0
julia> delta=events[1,2].-events[1:1]
75631000 (miliseconds)

julia> convert(Dates.Minute,delta)
75631000 minutes ??? WRONG
Paul

1 Like

Do you need the result to be in a time type? If it’s of some numeric type but represents milliseconds then just do the conversion. seconds = delta/1000

If you really need it in a time type then you can extend your other question.

julia> t1 = map((x) -> convert(DateTime,x),events[:,1])
julia> t2 = map((x) -> convert(DateTime,x),events[:,2])
julia> delta = t2 - t1
10-element Array{Base.Dates.Millisecond,1}:
 75631000 milliseconds 
 6031000 milliseconds  
 13531000 milliseconds 
 12685000 milliseconds 
 632000 milliseconds   
 21631000 milliseconds 
 212430000 milliseconds
 30000 milliseconds    
 30000 milliseconds    
 30000 milliseconds 

or

julia> time = map((x) -> convert(DateTime,x),events)
10Ɨ2 Array{DateTime,2}:
 2016-04-01T22:00:44  2016-04-02T19:01:15
 2016-04-01T12:00:27  2016-04-01T13:40:58
 2016-04-01T14:43:50  2016-04-01T18:29:21
 2016-04-01T08:29:33  2016-04-01T12:00:58
 2016-04-01T22:53:45  2016-04-01T23:04:17
 2016-04-01T22:53:44  2016-04-02T04:54:15
 2016-04-01T02:59:52  2016-04-03T14:00:22
 2016-04-01T17:26:38  2016-04-01T17:27:08
 2016-04-01T17:32:34  2016-04-01T17:33:04
 2016-04-01T17:24:58  2016-04-01T17:25:28

julia> time[:,2] - time[:,1]
10-element Array{Base.Dates.Millisecond,1}:
 75631000 milliseconds 
 6031000 milliseconds  
 13531000 milliseconds 
 12685000 milliseconds 
 632000 milliseconds   
 21631000 milliseconds 
 212430000 milliseconds
 30000 milliseconds    
 30000 milliseconds    
 30000 milliseconds

I’m not sure what code you’re running or if it’s indeed 0.5, because I get

julia> convert(Dates.Minute, Dates.Millisecond(75631000))
ERROR: InexactError()
 in divexact(::Int64, ::Int64) at ./dates/periods.jl:376
 in convert(::Type{Base.Dates.Minute}, ::Base.Dates.Millisecond) at ./dates/periods.jl:398

julia> convert(Dates.Millisecond, Dates.Minute(2))
120000 milliseconds

julia> convert(Dates.Minute, convert(Dates.Millisecond, Dates.Minute(2)))
2 minutes

Nice, :
julia> convert(Dates.Minute, Dates.Millisecond(12000*60))
12 minutes

bat is unposible :
julia> convert(Dates.Minute, Dates.Millisecond(12001*60))
ERROR: InexactError()
in divexact(::Int64, ::Int64) at .\dates\periods.jl:376
in convert(::Type{Base.Dates.Minute}, ::Base.Dates.Millisecond) at .\dates\periods.jl:398

What with 12001 milliscond ?

1 Like

You can also do:

julia> Dates.CompoundPeriod(Dates.Millisecond(756312342))
1 week, 1 day, 18 hours, 5 minutes, 12 seconds, 342 milliseconds
1 Like

Sadly this appears to have disappeared in v0.6:

julia> Dates.CompoundPeriod(Dates.Millisecond(756312342))
756312342 milliseconds
2 Likes

In 0.6 you can do

julia> Dates.canonicalize(Dates.CompoundPeriod(Dates.Millisecond(756312342)))
1 week, 1 day, 18 hours, 5 minutes, 12 seconds, 342 milliseconds
9 Likes

Cool. That’s a nice function. With a cool name that provides some typing practice… :slight_smile:

5 Likes

#milliseconds to seconds:
delta_sec = Dates.Second(delta)

#milliseconds to minutes:
delta_min = Dates.Minute(delta)

For my own future reference (and anyone else who might stuble across this when googling).

In 1.0 it is written type last.

julia> round(Millisecond(12_001*60), Minute)
12 minutes

The logic behind this is that your are also allowed to specify things like

julia> round(Millisecond(12_001*60), Minute(5))
10 minutes

See discussion on

20 Likes

Do you know a canonical way of converting a compound period to (e.g.) minutes (with digits)

julia> t=Minute(3)+Second(26)
3 minutes, 26 seconds

julia> want=3+26/60
3.433333333333333

I suppose I can always (??) convert to something small (say Nanosecond or Millisecond) and then divide, but there must be a more elegant way (or rather, an existing function) for this.

I’d do:

DateTime(0) + t - DateTime(0)

but I’m also curious to see if there’s a better way…

ok, I guess this works (as for elegance…)

julia> t=Minute(3)+Second(26)
3 minutes, 26 seconds

julia> (DateTime(0) + t - DateTime(0))/convert(Millisecond,Minute(1))
3.433333333333333

You can do

Dates.toms(t) / Dates.toms(Minute(1))

using the undocumented Dates.toms function to convert time periods to floating-point milliseconds.

7 Likes

This seems to be undocumented:

Should it perhaps be documented under " Conversion Functions"?

Also see (my) package DateFormats.jl. It converts between different date/time formats, including regular decimal numbers.

That is, instead of

do period_decimal(Minute, t).
For example,

using DateFormats

julia> period_decimal(Minute, Second(123))
2.05

# and inverse conversion
julia> period_decimal(Minute, 2.05) == Second(123)
true

Note that, unlike Dates.toms, it doesn’t truncate everything below 1 ms to zero.