Skip to content

Commit

Permalink
Time cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Jan 6, 2017
1 parent d28b1d4 commit 4ce172b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
12 changes: 6 additions & 6 deletions base/dates/accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ hour(dt::DateTime) = mod(fld(value(dt),3600000),24)
minute(dt::DateTime) = mod(fld(value(dt),60000),60)
second(dt::DateTime) = mod(fld(value(dt),1000),60)
millisecond(dt::DateTime) = mod(value(dt),1000)
hour(t::Time) = mod(fld(value(t),3600000000000),24)
minute(t::Time) = mod(fld(value(t),60000000000),60)
second(t::Time) = mod(fld(value(t),1000000000),60)
millisecond(t::Time) = mod(fld(value(t),1000000),1000)
microsecond(t::Time) = mod(fld(value(t),1000),1000)
nanosecond(t::Time) = mod(value(t),1000)
hour(t::Time) = mod(fld(value(t),3600000000000),Int64(24))
minute(t::Time) = mod(fld(value(t),60000000000),Int64(60))
second(t::Time) = mod(fld(value(t),1000000000),Int64(60))
millisecond(t::Time) = mod(fld(value(t),Int64(1000000)),Int64(1000))
microsecond(t::Time) = mod(fld(value(t),Int64(1000)),Int64(1000))
nanosecond(t::Time) = mod(value(t),Int64(1000))

dayofmonth(dt::TimeType) = day(dt)

Expand Down
8 changes: 4 additions & 4 deletions base/dates/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ end
(-)(x::Date, y::Week) = return Date(UTD(value(x) - 7*value(y)))
(+)(x::Date, y::Day) = return Date(UTD(value(x) + value(y)))
(-)(x::Date, y::Day) = return Date(UTD(value(x) - value(y)))
(+)(x::DateTime, y::Period) = return DateTime(UTM(value(x) + toms(y)))
(-)(x::DateTime, y::Period) = return DateTime(UTM(value(x) - toms(y)))
(+)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) + tons(y)))
(-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y)))
(+)(x::DateTime, y::Period) = return DateTime(UTM(value(x) + toms(y)))
(-)(x::DateTime, y::Period) = return DateTime(UTM(value(x) - toms(y)))
(+)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) + tons(y)))
(-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y)))
(+)(y::Period, x::TimeType) = x + y
(-)(y::Period, x::TimeType) = x - y

Expand Down
25 changes: 14 additions & 11 deletions base/dates/periods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond
# AbstractString parsing (mainly for IO code)
@eval $period(x::AbstractString) = $period(Base.parse(Int64,x))
# Period accessors
typs = period in (:Microsecond, :Nanosecond) ? ["Time"] : (period in (:Hour, :Minute, :Second, :Millisecond) ? ["DateTime","Time"] : ["TimeType"])
description = typs == ["TimeType"] ? "`Date` or `DateTime`" : (typs == ["Time"] ? "`Time`" : "`Time` or `DateTime`")
reference = period == :Week ? " For details see [`$accessor_str(::TimeType)`](:func:`$accessor_str`)." : ""
typs = period in (:Microsecond, :Nanosecond) ? ["Time"] :
period in (:Hour, :Minute, :Second, :Millisecond) ? ["Time", "DateTime"] : ["Date","DateTime"]
reference = period == :Week ? " For details see [`$accessor_str(::Union{Date,DateTime})`](:func:`$accessor_str`)." : ""
for typ_str in typs
@eval begin
@doc """
$($period_str)(dt::$($typ_str)) -> $($period_str)
The $($accessor_str) part of a $($description) as a `$($period_str)`.$($reference)
The $($accessor_str) part of a $($typ_str) as a `$($period_str)`.$($reference)
""" $period(dt::$(Symbol(typ_str))) = $period($(Symbol(accessor_str))(dt))

@doc """
$($period_str)(v)
Construct a `$($period_str)` object with the given `v` value. Input must be
losslessly convertible to an `Int64`.
""" $period(v)
end
end
@eval begin
@doc """
$($period_str)(v)
Construct a `$($period_str)` object with the given `v` value. Input must be
losslessly convertible to an `Int64`.
""" $period(v)
end
end
# Now we're safe to define Period-Number conversions
# Anything an Int64 can convert to, a Period can convert to
Expand Down Expand Up @@ -449,6 +450,8 @@ Base.promote_rule(::Type{Year}, ::Type{Month}) = Month
Base.isless{T<:OtherPeriod,S<:OtherPeriod}(x::T,y::S) = isless(promote(x,y)...)

# truncating conversions to milliseconds and days:
toms(c::Nanosecond) = div(value(c), 1000000)
toms(c::Microsecond) = div(value(c), 1000)
toms(c::Millisecond) = value(c)
toms(c::Second) = 1000*value(c)
toms(c::Minute) = 60000*value(c)
Expand Down

0 comments on commit 4ce172b

Please sign in to comment.