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


#1

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


#2

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

#3

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


#4

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 ?


#5

You can also do:

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

#6

Sadly this appears to have disappeared in v0.6:

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

#7

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

#8

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