Skip to content

Commit

Permalink
update time.jl example to where syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
musm committed May 13, 2017
1 parent b1f668d commit 50e9d56
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions examples/time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const PREFIXES = [
const ZERO_INDEX = 9
const MAX_INDEX = 17

function show{p}(io::IO, x::TimeDelta{p})
function show(io::IO, x::TimeDelta{p}) where p
k = max(1,min(MAX_INDEX,fld(p,3)+ZERO_INDEX))
r = p-3(k-ZERO_INDEX)
prefix = PREFIXES[k]
Expand All @@ -30,16 +30,16 @@ function show{p}(io::IO, x::TimeDelta{p})
end
end

convert{p,q}(::Type{TimeDelta{p}}, x::TimeDelta{q}) =
convert(::Type{TimeDelta{p}}, x::TimeDelta{q}) where {p,q} =
TimeDelta{p}(p <= q ? x.v*10^(q-p) : div(x.v,10^(p-q)))

promote_rule{p,q}(::Type{TimeDelta{p}}, ::Type{TimeDelta{q}}) = TimeDelta{min(p,q)}
promote_rule(::Type{TimeDelta{p}}, ::Type{TimeDelta{q}}) where {p,q} = TimeDelta{min(p,q)}

-{p}(x::TimeDelta{p}) = TimeDelta{p}(-x.v)
+{p}(x::TimeDelta{p}, y::TimeDelta{p}) = TimeDelta{p}(x.v+y.v)
-{p}(x::TimeDelta{p}, y::TimeDelta{p}) = TimeDelta{p}(x.v-y.v)
(-)(x::TimeDelta{p}) where {p} = TimeDelta{p}(-x.v)
(+)(x::TimeDelta{p}, y::TimeDelta{p}) where {p} = TimeDelta{p}(x.v+y.v)
(-)(x::TimeDelta{p}, y::TimeDelta{p}) where {p} = TimeDelta{p}(x.v-y.v)

+(x::TimeDelta, y::TimeDelta) = +(promote(x,y)...)
-(x::TimeDelta, y::TimeDelta) = -(promote(x,y)...)
(+)(x::TimeDelta, y::TimeDelta) = +(promote(x,y)...)
(-)(x::TimeDelta, y::TimeDelta) = -(promote(x,y)...)

end # module

0 comments on commit 50e9d56

Please sign in to comment.