@@ -12,7 +12,7 @@ for T in (:Year,:Month,:Week,:Day)
1212        $ T (v:: Number ) =  new (v)
1313    end 
1414end 
15- for  T in  (:Hour ,:Minute ,:Second ,:Millisecond )
15+ for  T in  (:Hour ,:Minute ,:Second ,:Millisecond , :Microsecond , :Nanosecond )
1616    @eval  immutable $ T <:  TimePeriod 
1717        value:: Int64 
1818        $ T (v:: Number ) =  new (v)
@@ -58,10 +58,11 @@ immutable Date <: TimeType
5858    Date (instant:: UTInstant{Day} ) =  new (instant)
5959end 
6060
61- #  Fallback constructors
62- _c (x) =  convert (Int64,x)
63- DateTime (y,m= 1 ,d= 1 ,h= 0 ,mi= 0 ,s= 0 ,ms= 0 ) =  DateTime (_c (y),_c (m),_c (d),_c (h),_c (mi),_c (s),_c (ms))
64- Date (y,m= 1 ,d= 1 ) =  Date (_c (y),_c (m),_c (d))
61+ #  Time is a nanosecond precision representation of hours:minutes:seconds.fractional
62+ immutable Time <:  TimeType 
63+     instant:: Nanosecond 
64+     Time (instant:: Nanosecond ) =  new (instant)
65+ end 
6566
6667#  Convert y,m,d to # of Rata Die days
6768#  Works by shifting the beginning of the year to March 1,
@@ -112,6 +113,21 @@ function Date(y::Int64,m::Int64=1,d::Int64=1)
112113    return  Date (UTD (totaldays (y,m,d)))
113114end 
114115
116+ """ 
117+     Time(h, [mi, s, ms, us, ns]) -> Time 
118+ 
119+ Construct a `Time` type by parts. Arguments must be convertible to `Int64`. 
120+ """ 
121+ function  Time (h:: Int64 ,mi:: Int64 = 0 ,s:: Int64 = 0 ,ms:: Int64 = 0 ,us:: Int64 = 0 ,ns:: Int64 = 0 )
122+     - 1  <  h <  24  ||  throw (ArgumentError (" Hour: $h  out of range (0:23)" 
123+     - 1  <  mi <  60  ||  throw (ArgumentError (" Minute: $mi  out of range (0:59)" 
124+     - 1  <  s <  60  ||  throw (ArgumentError (" Second: $s  out of range (0:59)" 
125+     - 1  <  ms <  1000  ||  throw (ArgumentError (" Millisecond: $ms  out of range (0:999)" 
126+     - 1  <  us <  1000  ||  throw (ArgumentError (" Microsecond: $us  out of range (0:999)" 
127+     - 1  <  ns <  1000  ||  throw (ArgumentError (" Nanosecond: $ns  out of range (0:999)" 
128+     return  Time (Nanosecond (ns +  1000 us +  1000000 ms +  1000000000 s +  60000000000 mi +  3600000000000 h))
129+ end 
130+ 
115131#  Convenience constructors from Periods
116132function  DateTime (y:: Year ,m:: Month = Month (1 ),d:: Day = Day (1 ),
117133                  h:: Hour = Hour (0 ),mi:: Minute = Minute (0 ),
@@ -121,6 +137,11 @@ function DateTime(y::Year,m::Month=Month(1),d::Day=Day(1),
121137end 
122138
123139Date (y:: Year ,m:: Month = Month (1 ),d:: Day = Day (1 )) =  Date (value (y),value (m),value (d))
140+ function  Time (h:: Hour ,mi:: Minute = Minute (0 ),s:: Second = Second (0 ),
141+               ms:: Millisecond = Millisecond (0 ),
142+               us:: Microsecond = Microsecond (0 ),ns:: Nanosecond = Nanosecond (0 ))
143+     return  Time (value (h),value (mi),value (s),value (ms),value (us),value (ns))
144+ end 
124145
125146#  To allow any order/combination of Periods
126147
@@ -161,6 +182,31 @@ function Date(periods::Period...)
161182    return  Date (y,m,d)
162183end 
163184
185+ """ 
186+     Time(period::Period...) -> Time 
187+ 
188+ Construct a `Time` type by `Period` type parts. Arguments may be in any order. `Time` parts 
189+ not provided will default to the value of `Dates.default(period)`. 
190+ """ 
191+ function  Time (periods:: Period... )
192+     h =  Hour (0 ); mi =  Minute (0 ); s =  Second (0 )
193+     ms =  Millisecond (0 ); us =  Microsecond (0 ); ns =  Nanosecond (0 )
194+     for  p in  periods
195+         isa (p, Hour) &&  (h =  p:: Hour )
196+         isa (p, Minute) &&  (mi =  p:: Minute )
197+         isa (p, Second) &&  (s =  p:: Second )
198+         isa (p, Millisecond) &&  (ms =  p:: Millisecond )
199+         isa (p, Microsecond) &&  (us =  p:: Microsecond )
200+         isa (p, Nanosecond) &&  (ns =  p:: Nanosecond )
201+     end 
202+     return  Time (h,mi,s,ms,us,ns)
203+ end 
204+ 
205+ #  Fallback constructors
206+ DateTime (y,m= 1 ,d= 1 ,h= 0 ,mi= 0 ,s= 0 ,ms= 0 ) =  DateTime (Int64 (y),Int64 (m),Int64 (d),Int64 (h),Int64 (mi),Int64 (s),Int64 (ms))
207+ Date (y,m= 1 ,d= 1 ) =  Date (Int64 (y),Int64 (m),Int64 (d))
208+ Time (h,mi= 0 ,s= 0 ,ms= 0 ,us= 0 ,ns= 0 ) =  Time (Int64 (h),Int64 (mi),Int64 (s),Int64 (ms),Int64 (us),Int64 (ns))
209+ 
164210#  Traits, Equality
165211Base. isfinite {T<:TimeType} (:: Union{Type{T},T} ) =  true 
166212calendar (dt:: DateTime ) =  ISOCalendar
@@ -169,21 +215,32 @@ calendar(dt::Date) = ISOCalendar
169215""" 
170216    eps(::DateTime) -> Millisecond 
171217    eps(::Date) -> Day 
218+     eps(::Time) -> Nanosecond 
172219
173- Returns `Millisecond(1)` for `DateTime` values and  `Day(1)` for `Date` values. 
220+ Returns `Millisecond(1)` for `DateTime` values,  `Day(1)` for `Date` values, and `Nanosecond(1)` for `Time ` values. 
174221""" 
175222Base. eps
176223
177224Base. eps (dt:: DateTime ) =  Millisecond (1 )
178225Base. eps (dt:: Date ) =  Day (1 )
226+ Base. eps (t:: Time ) =  Nanosecond (1 )
179227
180228Base. typemax (:: Union{DateTime,Type{DateTime}} ) =  DateTime (146138512 ,12 ,31 ,23 ,59 ,59 )
181229Base. typemin (:: Union{DateTime,Type{DateTime}} ) =  DateTime (- 146138511 ,1 ,1 ,0 ,0 ,0 )
182230Base. typemax (:: Union{Date,Type{Date}} ) =  Date (252522163911149 ,12 ,31 )
183231Base. typemin (:: Union{Date,Type{Date}} ) =  Date (- 252522163911150 ,1 ,1 )
232+ Base. typemax (:: Union{Time,Type{Time}} ) =  Time (23 ,59 ,59 ,999 ,999 ,999 )
233+ Base. typemin (:: Union{Time,Type{Time}} ) =  Time (0 )
234+ 
184235#  Date-DateTime promotion, isless, ==
185236Base. promote_rule (:: Type{Date} ,x:: Type{DateTime} ) =  DateTime
186- Base. isless (x:: Date ,y:: Date ) =  isless (value (x),value (y))
187237Base. isless (x:: DateTime ,y:: DateTime ) =  isless (value (x),value (y))
238+ Base. isless (x:: Date ,y:: Date ) =  isless (value (x),value (y))
239+ Base. isless (x:: Time ,y:: Time ) =  isless (value (x),value (y))
188240Base. isless (x:: TimeType ,y:: TimeType ) =  isless (promote (x,y)... )
189241== (x:: TimeType ,y:: TimeType ) =  === (promote (x,y)... )
242+ function  == (a:: Time ,b:: Time )
243+     return  hour (a) ==  hour (b) &&  minute (a) ==  minute (b) && 
244+         second (a) ==  second (b) &&  millisecond (a) ==  millisecond (b) && 
245+         microsecond (a) ==  microsecond (b) &&  nanosecond (a) ==  nanosecond (b)
246+ end 
0 commit comments