Skip to content

Commit 52cf20c

Browse files
committed
Make time types/funcs a bit more flexible
1 parent cc98c4a commit 52cf20c

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/time.jl

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,38 @@ abstract TVal
99
type Time <: TVal
1010
secs::Int32
1111
nsecs::Int32
12-
Time(s,n) = begin
12+
function Time(s::Real,n::Real)
1313
cs, cns = _canonical_time(s,n)
1414
new(cs, cns)
1515
end
1616
end
1717
Time() = Time(0,0)
18-
Time(t::FloatingPoint) =
19-
Time(floor(Int32, t), round(Int32, mod(t,1)*1e9))
18+
Time(t::Real) = Time(t,0)
2019

2120
type Duration <: TVal
2221
secs::Int32
2322
nsecs::Int32
24-
Duration(s,n) = begin
23+
function Duration(s::Real,n::Real)
2524
cs, cns = _canonical_time(s,n)
2625
new(cs, cns)
2726
end
2827
end
2928
Duration() = Duration(0,0)
30-
Duration(t::FloatingPoint) =
31-
Duration(floor(Int32,t), round(Int32, mod(t,1)*1e9))
29+
Duration(t::Real) = Duration(t,0)
3230

3331
#Enforce 0 <= nsecs < 1e9
34-
function _canonical_time(secs::Integer, nsecs::Integer)
35-
nsec_conv = int32(1_000_000_000)
36-
dsecs = convert(Int32, div(nsecs, nsec_conv))
37-
rnsecs = convert(Int32, rem(nsecs, nsec_conv))
38-
if rnsecs < 0
39-
dsecs = dsecs - one(Int32)
40-
rnsecs = rnsecs + nsec_conv
32+
function _canonical_time(secs, nsecs)
33+
nsec_conv = convert(Int32, 1_000_000_000)
34+
secs32 = floor(Int32, secs)
35+
nsecs32 = floor(Int32, mod(secs,1)*1e9 + nsecs)
36+
37+
addsecs = div(nsecs32, nsec_conv)
38+
crnsecs = rem(nsecs32, nsec_conv)
39+
if crnsecs < 0
40+
addsecs -= one(Int32)
41+
crnsecs += nsec_conv
4142
end
42-
(secs + dsecs, rnsecs)
43+
(secs32 + addsecs, crnsecs)
4344
end
4445

4546
#Temporal arithmetic
@@ -57,7 +58,7 @@ convert(::Type{PyObject}, t::Time) = __rospy__.Time (t.secs,t.nsecs)
5758
convert(::Type{PyObject}, t::Duration) = __rospy__.Duration(t.secs,t.nsecs)
5859

5960
#Real number conversions
60-
to_sec{T<:TVal}(t::T) = float64(t.secs) + 1e-9*float64(t.nsecs)
61+
to_sec{T<:TVal}(t::T) = t.secs + 1e-9*t.nsecs
6162
to_nsec{T<:TVal}(t::T) = 1_000_000_000*t.secs + t.nsecs
6263
convert{T<:TVal}(::Type{Float64}, t::T) = to_sec(t)
6364

@@ -72,7 +73,7 @@ isless{T<:TVal}(t1::T, t2::T) = to_nsec(t1) < to_nsec(t2)
7273
type Rate
7374
o::PyObject
7475
end
75-
Rate(hz::FloatingPoint) = Rate(__rospy__.Rate(hz))
76+
Rate(hz::Real) = Rate(__rospy__.Rate(hz))
7677
Rate(d::Duration) = Rate(1.0/to_sec(d))
7778

7879
type Timer

0 commit comments

Comments
 (0)