Here's 50 lines that implement the bulk of the functionality. Might be
worth just throwing this in Base since it's so simple. (this is 0.4
compatible, you'll want to do just `using Dates` for 0.3)
using Base.Dates
immutable Time
value::Millisecond
end
MS(x) = Millisecond(x)
value(x::Time) = x.value.value
function Time(h::Int32=0,m::Int32=0,s::Int32=0,ms::Int32=0)
-1 < h < 24 || throw(ArgumentError("Hour: $h out of range (0:23)"))
-1 < m < 60 || throw(ArgumentError("Minute: $m out of range (0:59)"))
-1 < s < 60 || throw(ArgumentError("Second: $s out of range (0:59)"))
-1 < ms < 1000 || throw(ArgumentError("Millisecond: $ms out of range
(0:999)"))
return Time(MS(ms + Int32(1000)*s + Int32(60000)*m + Int32(3600000)*h))
end
Time(h::Hour,m::Minute=Minute(0),s::Second=Second(0),ms::Millisecond=Millisecond(0))
= Time(Int32(h),Int32(m),Int32(s),Int32(ms))
_c(c) = convert(Int32,c)
Time(h,m=0,s=0,ms=0) = Time(_c(h),_c(m),_c(s),_c(ms))
Base.isfinite{T<:Time}(::Union(Type{T},T)) = true
Base.eps(t::Time) = Millisecond(1)
Base.typemax(::Union(Time,Type{Time})) = Time(23,59,59,999)
Base.typemin(::Union(Time,Type{Time})) = Time(0,0,0,0)
Base.isless(x::Time,y::Time) = isless(value(x),value(y))
==(x::Time,y::Time) = ===(value(x),value(y))
hour(t::Time) = mod(fld(value(t),3600000),24)
minute(t::Time) = mod(fld(value(t),60000),60)
second(t::Time) = mod(fld(value(t),1000),60)
millisecond(t::Time) = mod(value(t),1000)
(+)(x::Time,y::Dates.TimePeriod) = return Time(MS(value(x)+Dates.toms(y)))
(-)(x::Time,y::Dates.TimePeriod) = return Time(MS(value(x)-Dates.toms(y)))
(+)(y::Dates.TimePeriod,x::Time) = x + y
(-)(y::Dates.TimePeriod,x::Time) = x - y
function Base.string(t::Time)
h,mi,s = hour(t),minute(t),second(t)
hh = lpad(h,2,"0")
mii = lpad(mi,2,"0")
ss = lpad(s,2,"0")
ms = millisecond(t) == 0 ? "" : string(millisecond(t)/1000.0)[2:end]
return "$hh:$mii:$ss$(ms)"
end
Base.show(io::IO,x::Time) = print(io,string(x))
On Wed, Mar 18, 2015 at 9:43 AM, Seth <[email protected]> wrote:
> Apropos? https://www.youtube.com/watch?v=-5wpm-gesOY
>
> On Saturday, March 14, 2015 at 7:28:23 AM UTC-7, Stefan Karpinski wrote:
>>
>> There are no leap seconds in universal time. There are leap seconds in
>> UTC which bridges terrestrial time (SI seconds) with universal time (day =
>> 1 earth rotation; second = 1/86400 of an earth rotation) by using SI
>> seconds as the basic unit but introducing leap seconds here and there to
>> ensure that UTC days stay in sync with terrestrial days (but not
>> terrestrial seconds). This is why Date x Time ≅ DateTime for UT.
>>
>>