From 665563bb5f26bd215eca71e5716e5469f51e626d Mon Sep 17 00:00:00 2001 From: quinnj Date: Tue, 24 Jan 2017 18:05:54 -0700 Subject: [PATCH 01/11] Add NEWS entry for the new Dates.Time type --- NEWS.md | 33 ++------------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/NEWS.md b/NEWS.md index a899202425e93..f27c5adbad181 100644 --- a/NEWS.md +++ b/NEWS.md @@ -46,10 +46,6 @@ Breaking changes This section lists changes that do not have deprecation warnings. - * `readline`, `readlines` and `eachline` return lines without line endings by default. - You *must* use `readline(s, chomp=false)`, etc. to get the old behavior where lines - returned include trailing end-of-line character(s). ([#19944]) - * `String`s no longer have a `.data` field (as part of a significant performance improvement). Use `Vector{UInt8}(str)` to access a string as a byte array. However, allocating the `Vector` object has overhead. You can also use @@ -133,9 +129,6 @@ This section lists changes that do not have deprecation warnings. Library improvements -------------------- - * `@views` macro to convert a whole expression or block of code to - use views for all slices ([#20164]). - * `max`, `min`, and related functions (`minmax`, `maximum`, `minimum`, `extrema`) now return `NaN` for `NaN` arguments ([#12563]). @@ -212,15 +205,11 @@ Library improvements * `notify` now returns a count of tasks woken up ([#19841]). + * A new `Dates.Time` type was added that supports representing the time of day with up to nanosecond resolution. + Compiler/Runtime improvements ----------------------------- -* `ccall` is now implemented as a macro, removing the need for special code-generator support for Intrinsics. - -* `ccall` gained limited support for a `llvmcall` calling-convention. This can replace many uses of `llvmcall` with a simpler, shorter declaration. - -* All Intrinsics are now Builtin functions instead and have proper error checking and fall-back static compilation support. - Deprecated or removed --------------------- @@ -756,12 +745,10 @@ Language tooling improvements [#6190]: https://github.com/JuliaLang/julia/issues/6190 [#6842]: https://github.com/JuliaLang/julia/issues/6842 [#7258]: https://github.com/JuliaLang/julia/issues/7258 -[#7669]: https://github.com/JuliaLang/julia/issues/7669 [#8036]: https://github.com/JuliaLang/julia/issues/8036 [#8599]: https://github.com/JuliaLang/julia/issues/8599 [#8814]: https://github.com/JuliaLang/julia/issues/8814 [#8846]: https://github.com/JuliaLang/julia/issues/8846 -[#8974]: https://github.com/JuliaLang/julia/issues/8974 [#9482]: https://github.com/JuliaLang/julia/issues/9482 [#9503]: https://github.com/JuliaLang/julia/issues/9503 [#9627]: https://github.com/JuliaLang/julia/issues/9627 @@ -837,7 +824,6 @@ Language tooling improvements [#16098]: https://github.com/JuliaLang/julia/issues/16098 [#16107]: https://github.com/JuliaLang/julia/issues/16107 [#16154]: https://github.com/JuliaLang/julia/issues/16154 -[#16213]: https://github.com/JuliaLang/julia/issues/16213 [#16219]: https://github.com/JuliaLang/julia/issues/16219 [#16260]: https://github.com/JuliaLang/julia/issues/16260 [#16285]: https://github.com/JuliaLang/julia/issues/16285 @@ -886,7 +872,6 @@ Language tooling improvements [#18330]: https://github.com/JuliaLang/julia/issues/18330 [#18339]: https://github.com/JuliaLang/julia/issues/18339 [#18346]: https://github.com/JuliaLang/julia/issues/18346 -[#18457]: https://github.com/JuliaLang/julia/issues/18457 [#18473]: https://github.com/JuliaLang/julia/issues/18473 [#18628]: https://github.com/JuliaLang/julia/issues/18628 [#18644]: https://github.com/JuliaLang/julia/issues/18644 @@ -899,24 +884,10 @@ Language tooling improvements [#19233]: https://github.com/JuliaLang/julia/issues/19233 [#19288]: https://github.com/JuliaLang/julia/issues/19288 [#19305]: https://github.com/JuliaLang/julia/issues/19305 -[#19331]: https://github.com/JuliaLang/julia/issues/19331 [#19449]: https://github.com/JuliaLang/julia/issues/19449 -[#19464]: https://github.com/JuliaLang/julia/issues/19464 [#19469]: https://github.com/JuliaLang/julia/issues/19469 [#19543]: https://github.com/JuliaLang/julia/issues/19543 [#19598]: https://github.com/JuliaLang/julia/issues/19598 [#19635]: https://github.com/JuliaLang/julia/issues/19635 -[#19670]: https://github.com/JuliaLang/julia/issues/19670 -[#19677]: https://github.com/JuliaLang/julia/issues/19677 [#19680]: https://github.com/JuliaLang/julia/issues/19680 -[#19692]: https://github.com/JuliaLang/julia/issues/19692 -[#19722]: https://github.com/JuliaLang/julia/issues/19722 -[#19741]: https://github.com/JuliaLang/julia/issues/19741 [#19787]: https://github.com/JuliaLang/julia/issues/19787 -[#19800]: https://github.com/JuliaLang/julia/issues/19800 -[#19841]: https://github.com/JuliaLang/julia/issues/19841 -[#19903]: https://github.com/JuliaLang/julia/issues/19903 -[#19919]: https://github.com/JuliaLang/julia/issues/19919 -[#19944]: https://github.com/JuliaLang/julia/issues/19944 -[#19950]: https://github.com/JuliaLang/julia/issues/19950 -[#20164]: https://github.com/JuliaLang/julia/issues/20164 From 94aa9019e18288fd4cda9820668066e16e0f36fe Mon Sep 17 00:00:00 2001 From: quinnj Date: Tue, 24 Jan 2017 18:24:30 -0700 Subject: [PATCH 02/11] Remove commutative subtraction methods. Fixes #20205 --- base/dates/arithmetic.jl | 78 ++--- base/dates/ranges.jl | 36 +- test/dates/arithmetic.jl | 713 +++++++++++++++++++-------------------- 3 files changed, 409 insertions(+), 418 deletions(-) diff --git a/base/dates/arithmetic.jl b/base/dates/arithmetic.jl index 845268f19e27a..09c8d162dcc24 100644 --- a/base/dates/arithmetic.jl +++ b/base/dates/arithmetic.jl @@ -2,11 +2,11 @@ # Instant arithmetic (+)(x::Instant) = x -(-){T<:Instant}(x::T,y::T) = x.periods - y.periods +(-){T<:Instant}(x::T, y::T) = x.periods - y.periods # TimeType arithmetic (+)(x::TimeType) = x -(-){T<:TimeType}(x::T,y::T) = x.instant - y.instant +(-){T<:TimeType}(x::T, y::T) = x.instant - y.instant # Date-Time arithmetic """ @@ -24,56 +24,56 @@ end (+)(t::Time, dt::Date) = dt + t # TimeType-Year arithmetic -function (+)(dt::DateTime,y::Year) - oy,m,d = yearmonthday(dt); ny = oy+value(y); ld = daysinmonth(ny,m) - return DateTime(ny,m,d <= ld ? d : ld,hour(dt),minute(dt),second(dt),millisecond(dt)) +function (+)(dt::DateTime, y::Year) + oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m) + return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt)) end function (+)(dt::Date,y::Year) - oy,m,d = yearmonthday(dt); ny = oy+value(y); ld = daysinmonth(ny,m) - return Date(ny,m,d <= ld ? d : ld) + oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m) + return Date(ny, m, d <= ld ? d : ld) end function (-)(dt::DateTime,y::Year) - oy,m,d = yearmonthday(dt); ny = oy-value(y); ld = daysinmonth(ny,m) - return DateTime(ny,m,d <= ld ? d : ld,hour(dt),minute(dt),second(dt),millisecond(dt)) + oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m) + return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt)) end function (-)(dt::Date,y::Year) - oy,m,d = yearmonthday(dt); ny = oy-value(y); ld = daysinmonth(ny,m) - return Date(ny,m,d <= ld ? d : ld) + oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m) + return Date(ny, m, d <= ld ? d : ld) end # TimeType-Month arithmetic # monthwrap adds two months with wraparound behavior (i.e. 12 + 1 == 1) -monthwrap(m1,m2) = (v = mod1(m1+m2,12); return v < 0 ? 12 + v : v) +monthwrap(m1, m2) = (v = mod1(m1 + m2, 12); return v < 0 ? 12 + v : v) # yearwrap takes a starting year/month and a month to add and returns # the resulting year with wraparound behavior (i.e. 2000-12 + 1 == 2001) -yearwrap(y,m1,m2) = y + fld(m1 + m2 - 1,12) +yearwrap(y, m1, m2) = y + fld(m1 + m2 - 1, 12) -function (+)(dt::DateTime,z::Month) +function (+)(dt::DateTime, z::Month) y,m,d = yearmonthday(dt) - ny = yearwrap(y,m,value(z)) - mm = monthwrap(m,value(z)); ld = daysinmonth(ny,mm) - return DateTime(ny,mm,d <= ld ? d : ld,hour(dt),minute(dt),second(dt),millisecond(dt)) + ny = yearwrap(y, m, value(z)) + mm = monthwrap(m, value(z)); ld = daysinmonth(ny, mm) + return DateTime(ny, mm, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt)) end -function (+)(dt::Date,z::Month) +function (+)(dt::Date, z::Month) y,m,d = yearmonthday(dt) - ny = yearwrap(y,m,value(z)) - mm = monthwrap(m,value(z)); ld = daysinmonth(ny,mm) - return Date(ny,mm,d <= ld ? d : ld) + ny = yearwrap(y, m, value(z)) + mm = monthwrap(m, value(z)); ld = daysinmonth(ny, mm) + return Date(ny, mm, d <= ld ? d : ld) end -function (-)(dt::DateTime,z::Month) +function (-)(dt::DateTime, z::Month) y,m,d = yearmonthday(dt) - ny = yearwrap(y,m,-value(z)) - mm = monthwrap(m,-value(z)); ld = daysinmonth(ny,mm) - return DateTime(ny,mm,d <= ld ? d : ld,hour(dt),minute(dt),second(dt),millisecond(dt)) + ny = yearwrap(y, m, -value(z)) + mm = monthwrap(m, -value(z)); ld = daysinmonth(ny, mm) + return DateTime(ny, mm, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt)) end -function (-)(dt::Date,z::Month) +function (-)(dt::Date, z::Month) y,m,d = yearmonthday(dt) - ny = yearwrap(y,m,-value(z)) - mm = monthwrap(m,-value(z)); ld = daysinmonth(ny,mm) - return Date(ny,mm,d <= ld ? d : ld) + ny = yearwrap(y, m, -value(z)) + mm = monthwrap(m, -value(z)); ld = daysinmonth(ny, mm) + return Date(ny, mm, d <= ld ? d : ld) end -(+)(x::Date, y::Week) = return Date(UTD(value(x) + 7*value(y))) -(-)(x::Date, y::Week) = return Date(UTD(value(x) - 7*value(y))) +(+)(x::Date, y::Week) = return Date(UTD(value(x) + 7 * value(y))) +(-)(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))) @@ -81,17 +81,17 @@ end (+)(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 for op in (:+, :-) @eval begin - # GeneralPeriod, AbstractArray{TimeType} - ($op){T<:TimeType}(x::AbstractArray{T}, y::GeneralPeriod) = broadcast($op,x,y) - ($op){T<:TimeType}(y::GeneralPeriod, x::AbstractArray{T}) = broadcast($op,x,y) - - # TimeType, StridedArray{GeneralPeriod} - ($op){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = broadcast($op,x,y) - ($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = broadcast($op,x,y) + ($op){T<:TimeType}(x::AbstractArray{T}, y::GeneralPeriod) = broadcast($op, x, y) + ($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = broadcast($op, x, y) + end + if op == :+ + @eval begin + ($op){T<:TimeType}(y::GeneralPeriod, x::AbstractArray{T}) = broadcast($op, x, y) + ($op){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = broadcast($op, x, y) + end end end diff --git a/base/dates/ranges.jl b/base/dates/ranges.jl index 92fb4868aaad3..60625055e2e49 100644 --- a/base/dates/ranges.jl +++ b/base/dates/ranges.jl @@ -14,35 +14,35 @@ Base.range(start::Date, len::Integer) = range(start, Day(1), len) throw(ArgumentError("must specify step as a Period when constructing Dates ranges")) # Given a start and end date, how many steps/periods are in between -guess(a::DateTime,b::DateTime,c) = floor(Int64,(Int128(value(b)) - Int128(value(a)))/toms(c)) -guess(a::Date,b::Date,c) = Int64(div(value(b - a),days(c))) -len(a::Time,b::Time,c) = Int64(div(value(b - a), tons(c))) -function len(a,b,c) - lo, hi, st = min(a,b), max(a,b), abs(c) - i = guess(a,b,c)-1 - while lo+st*i <= hi +guess(a::DateTime, b::DateTime, c) = floor(Int64, (Int128(value(b)) - Int128(value(a))) / toms(c)) +guess(a::Date, b::Date, c) = Int64(div(value(b - a), days(c))) +len(a::Time, b::Time, c) = Int64(div(value(b - a), tons(c))) +function len(a, b, c) + lo, hi, st = min(a, b), max(a, b), abs(c) + i = guess(a, b, c) - 1 + while lo + st * i <= hi i += 1 end - return i-1 + return i - 1 end -Base.length{T<:TimeType}(r::StepRange{T}) = isempty(r) ? 0 : len(r.start,r.stop,r.step) + 1 +Base.length{T<:TimeType}(r::StepRange{T}) = isempty(r) ? 0 : len(r.start, r.stop, r.step) + 1 # Period ranges hook into Int64 overflow detection -Base.length{P<:Period}(r::StepRange{P}) = length(StepRange(value(r.start),value(r.step),value(r.stop))) +Base.length{P<:Period}(r::StepRange{P}) = length(StepRange(value(r.start), value(r.step), value(r.stop))) # Used to calculate the last valid date in the range given the start, stop, and step -# last = stop - steprem(start,stop,step) -Base.steprem{T<:TimeType}(a::T,b::T,c) = b - (a + c*len(a,b,c)) +# last = stop - steprem(start, stop, step) +Base.steprem{T<:TimeType}(a::T, b::T, c) = b - (a + c * len(a, b, c)) import Base.in function in{T<:TimeType}(x::T, r::StepRange{T}) - n = len(first(r),x,step(r)) + 1 + n = len(first(r), x, step(r)) + 1 n >= 1 && n <= length(r) && r[n] == x end Base.start{T<:TimeType}(r::StepRange{T}) = 0 -Base.next{T<:TimeType}(r::StepRange{T}, i::Int) = (r.start+r.step*i,i+1) -Base.done{T<:TimeType,S<:Period}(r::StepRange{T,S}, i::Integer) = length(r) <= i +Base.next{T<:TimeType}(r::StepRange{T}, i::Int) = (r.start + r.step*i, i + 1) +Base.done{T<:TimeType, S<:Period}(r::StepRange{T, S}, i::Integer) = length(r) <= i -+{T<:TimeType}(x::Period, r::Range{T}) = (x+first(r)):step(r):(x+last(r)) -+{T<:TimeType}(r::Range{T},x::Period) = x + r --{T<:TimeType}(r::Range{T},x::Period) = (first(r)-x):step(r):(last(r)-x) ++{T<:TimeType}(x::Period, r::Range{T}) = (x + first(r)):step(r):(x + last(r)) ++{T<:TimeType}(r::Range{T}, x::Period) = x + r +-{T<:TimeType}(r::Range{T}, x::Period) = (first(r)-x):step(r):(last(r)-x) \ No newline at end of file diff --git a/test/dates/arithmetic.jl b/test/dates/arithmetic.jl index 961b2f031406f..61b39509c7e5b 100644 --- a/test/dates/arithmetic.jl +++ b/test/dates/arithmetic.jl @@ -1,372 +1,372 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license # Time arithmetic -a = Dates.Time(23,59,59) -b = Dates.Time(11,59,59) +a = Dates.Time(23, 59, 59) +b = Dates.Time(11, 59, 59) @test Dates.CompoundPeriod(a - b) == Dates.Hour(12) # Dates.DateTime arithmetic -a = Dates.DateTime(2013,1,1,0,0,0,1) -b = Dates.DateTime(2013,1,1,0,0,0,0) +a = Dates.DateTime(2013, 1, 1, 0, 0, 0, 1) +b = Dates.DateTime(2013, 1, 1, 0, 0, 0, 0) @test a - b == Dates.Millisecond(1) -@test Dates.DateTime(2013,1,2) - b == Dates.Millisecond(86400000) +@test Dates.DateTime(2013, 1, 2) - b == Dates.Millisecond(86400000) # Dates.DateTime-Year arithmetic -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Year(1) == Dates.DateTime(2000,12,27) -@test dt + Dates.Year(100) == Dates.DateTime(2099,12,27) -@test dt + Dates.Year(1000) == Dates.DateTime(2999,12,27) -@test dt - Dates.Year(1) == Dates.DateTime(1998,12,27) -@test dt - Dates.Year(100) == Dates.DateTime(1899,12,27) -@test dt - Dates.Year(1000) == Dates.DateTime(999,12,27) -dt = Dates.DateTime(2000,2,29) -@test dt + Dates.Year(1) == Dates.DateTime(2001,2,28) -@test dt - Dates.Year(1) == Dates.DateTime(1999,2,28) -@test dt + Dates.Year(4) == Dates.DateTime(2004,2,29) -@test dt - Dates.Year(4) == Dates.DateTime(1996,2,29) -dt = Dates.DateTime(1972,6,30,23,59,59) -@test dt + Dates.Year(1) == Dates.DateTime(1973,6,30,23,59,59) -@test dt - Dates.Year(1) == Dates.DateTime(1971,6,30,23,59,59) -@test dt + Dates.Year(-1) == Dates.DateTime(1971,6,30,23,59,59) -@test dt - Dates.Year(-1) == Dates.DateTime(1973,6,30,23,59,59) +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Year(1) == Dates.DateTime(2000, 12, 27) +@test dt + Dates.Year(100) == Dates.DateTime(2099, 12, 27) +@test dt + Dates.Year(1000) == Dates.DateTime(2999, 12, 27) +@test dt - Dates.Year(1) == Dates.DateTime(1998, 12, 27) +@test dt - Dates.Year(100) == Dates.DateTime(1899, 12, 27) +@test dt - Dates.Year(1000) == Dates.DateTime(999, 12, 27) +dt = Dates.DateTime(2000, 2, 29) +@test dt + Dates.Year(1) == Dates.DateTime(2001, 2, 28) +@test dt - Dates.Year(1) == Dates.DateTime(1999, 2, 28) +@test dt + Dates.Year(4) == Dates.DateTime(2004, 2, 29) +@test dt - Dates.Year(4) == Dates.DateTime(1996, 2, 29) +dt = Dates.DateTime(1972, 6, 30, 23, 59, 59) +@test dt + Dates.Year(1) == Dates.DateTime(1973, 6, 30, 23, 59, 59) +@test dt - Dates.Year(1) == Dates.DateTime(1971, 6, 30, 23, 59, 59) +@test dt + Dates.Year(-1) == Dates.DateTime(1971, 6, 30, 23, 59, 59) +@test dt - Dates.Year(-1) == Dates.DateTime(1973, 6, 30, 23, 59, 59) # Wrapping arithemtic for Months # This ends up being trickier than expected because # the user might do 2014-01-01 + Month(-14) # monthwrap figures out the resulting month # when adding/subtracting months from a date -@test Dates.monthwrap(1,-14) == 11 -@test Dates.monthwrap(1,-13) == 12 -@test Dates.monthwrap(1,-12) == 1 -@test Dates.monthwrap(1,-11) == 2 -@test Dates.monthwrap(1,-10) == 3 -@test Dates.monthwrap(1,-9) == 4 -@test Dates.monthwrap(1,-8) == 5 -@test Dates.monthwrap(1,-7) == 6 -@test Dates.monthwrap(1,-6) == 7 -@test Dates.monthwrap(1,-5) == 8 -@test Dates.monthwrap(1,-4) == 9 -@test Dates.monthwrap(1,-3) == 10 -@test Dates.monthwrap(1,-2) == 11 -@test Dates.monthwrap(1,-1) == 12 -@test Dates.monthwrap(1,0) == 1 -@test Dates.monthwrap(1,1) == 2 -@test Dates.monthwrap(1,2) == 3 -@test Dates.monthwrap(1,3) == 4 -@test Dates.monthwrap(1,4) == 5 -@test Dates.monthwrap(1,5) == 6 -@test Dates.monthwrap(1,6) == 7 -@test Dates.monthwrap(1,7) == 8 -@test Dates.monthwrap(1,8) == 9 -@test Dates.monthwrap(1,9) == 10 -@test Dates.monthwrap(1,10) == 11 -@test Dates.monthwrap(1,11) == 12 -@test Dates.monthwrap(1,12) == 1 -@test Dates.monthwrap(1,13) == 2 -@test Dates.monthwrap(1,24) == 1 -@test Dates.monthwrap(12,-14) == 10 -@test Dates.monthwrap(12,-13) == 11 -@test Dates.monthwrap(12,-12) == 12 -@test Dates.monthwrap(12,-11) == 1 -@test Dates.monthwrap(12,-2) == 10 -@test Dates.monthwrap(12,-1) == 11 -@test Dates.monthwrap(12,0) == 12 -@test Dates.monthwrap(12,1) == 1 -@test Dates.monthwrap(12,2) == 2 -@test Dates.monthwrap(12,11) == 11 -@test Dates.monthwrap(12,12) == 12 -@test Dates.monthwrap(12,13) == 1 +@test Dates.monthwrap(1, -14) == 11 +@test Dates.monthwrap(1, -13) == 12 +@test Dates.monthwrap(1, -12) == 1 +@test Dates.monthwrap(1, -11) == 2 +@test Dates.monthwrap(1, -10) == 3 +@test Dates.monthwrap(1, -9) == 4 +@test Dates.monthwrap(1, -8) == 5 +@test Dates.monthwrap(1, -7) == 6 +@test Dates.monthwrap(1, -6) == 7 +@test Dates.monthwrap(1, -5) == 8 +@test Dates.monthwrap(1, -4) == 9 +@test Dates.monthwrap(1, -3) == 10 +@test Dates.monthwrap(1, -2) == 11 +@test Dates.monthwrap(1, -1) == 12 +@test Dates.monthwrap(1, 0) == 1 +@test Dates.monthwrap(1, 1) == 2 +@test Dates.monthwrap(1, 2) == 3 +@test Dates.monthwrap(1, 3) == 4 +@test Dates.monthwrap(1, 4) == 5 +@test Dates.monthwrap(1, 5) == 6 +@test Dates.monthwrap(1, 6) == 7 +@test Dates.monthwrap(1, 7) == 8 +@test Dates.monthwrap(1, 8) == 9 +@test Dates.monthwrap(1, 9) == 10 +@test Dates.monthwrap(1, 10) == 11 +@test Dates.monthwrap(1, 11) == 12 +@test Dates.monthwrap(1, 12) == 1 +@test Dates.monthwrap(1, 13) == 2 +@test Dates.monthwrap(1, 24) == 1 +@test Dates.monthwrap(12, -14) == 10 +@test Dates.monthwrap(12, -13) == 11 +@test Dates.monthwrap(12, -12) == 12 +@test Dates.monthwrap(12, -11) == 1 +@test Dates.monthwrap(12, -2) == 10 +@test Dates.monthwrap(12, -1) == 11 +@test Dates.monthwrap(12, 0) == 12 +@test Dates.monthwrap(12, 1) == 1 +@test Dates.monthwrap(12, 2) == 2 +@test Dates.monthwrap(12, 11) == 11 +@test Dates.monthwrap(12, 12) == 12 +@test Dates.monthwrap(12, 13) == 1 # yearwrap figures out the resulting year # when adding/subtracting months from a date -@test Dates.yearwrap(2000,1,-3600) == 1700 -@test Dates.yearwrap(2000,1,-37) == 1996 -@test Dates.yearwrap(2000,1,-36) == 1997 -@test Dates.yearwrap(2000,1,-35) == 1997 -@test Dates.yearwrap(2000,1,-25) == 1997 -@test Dates.yearwrap(2000,1,-24) == 1998 -@test Dates.yearwrap(2000,1,-23) == 1998 -@test Dates.yearwrap(2000,1,-14) == 1998 -@test Dates.yearwrap(2000,1,-13) == 1998 -@test Dates.yearwrap(2000,1,-12) == 1999 -@test Dates.yearwrap(2000,1,-11) == 1999 -@test Dates.yearwrap(2000,1,-2) == 1999 -@test Dates.yearwrap(2000,1,-1) == 1999 -@test Dates.yearwrap(2000,1,0) == 2000 -@test Dates.yearwrap(2000,1,1) == 2000 -@test Dates.yearwrap(2000,1,11) == 2000 -@test Dates.yearwrap(2000,1,12) == 2001 -@test Dates.yearwrap(2000,1,13) == 2001 -@test Dates.yearwrap(2000,1,23) == 2001 -@test Dates.yearwrap(2000,1,24) == 2002 -@test Dates.yearwrap(2000,1,25) == 2002 -@test Dates.yearwrap(2000,1,36) == 2003 -@test Dates.yearwrap(2000,1,3600) == 2300 -@test Dates.yearwrap(2000,2,-2) == 1999 -@test Dates.yearwrap(2000,3,10) == 2001 -@test Dates.yearwrap(2000,4,-4) == 1999 -@test Dates.yearwrap(2000,5,8) == 2001 -@test Dates.yearwrap(2000,6,-18) == 1998 -@test Dates.yearwrap(2000,6,-6) == 1999 -@test Dates.yearwrap(2000,6,6) == 2000 -@test Dates.yearwrap(2000,6,7) == 2001 -@test Dates.yearwrap(2000,6,19) == 2002 -@test Dates.yearwrap(2000,12,-3600) == 1700 -@test Dates.yearwrap(2000,12,-36) == 1997 -@test Dates.yearwrap(2000,12,-35) == 1998 -@test Dates.yearwrap(2000,12,-24) == 1998 -@test Dates.yearwrap(2000,12,-23) == 1999 -@test Dates.yearwrap(2000,12,-14) == 1999 -@test Dates.yearwrap(2000,12,-13) == 1999 -@test Dates.yearwrap(2000,12,-12) == 1999 -@test Dates.yearwrap(2000,12,-11) == 2000 -@test Dates.yearwrap(2000,12,-2) == 2000 -@test Dates.yearwrap(2000,12,-1) == 2000 -@test Dates.yearwrap(2000,12,0) == 2000 -@test Dates.yearwrap(2000,12,1) == 2001 -@test Dates.yearwrap(2000,12,11) == 2001 -@test Dates.yearwrap(2000,12,12) == 2001 -@test Dates.yearwrap(2000,12,13) == 2002 -@test Dates.yearwrap(2000,12,24) == 2002 -@test Dates.yearwrap(2000,12,25) == 2003 -@test Dates.yearwrap(2000,12,36) == 2003 -@test Dates.yearwrap(2000,12,37) == 2004 -@test Dates.yearwrap(2000,12,3600) == 2300 - -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Month(1) == Dates.DateTime(2000,1,27) -@test dt + Dates.Month(-1) == Dates.DateTime(1999,11,27) -@test dt + Dates.Month(-11) == Dates.DateTime(1999,1,27) -@test dt + Dates.Month(11) == Dates.DateTime(2000,11,27) -@test dt + Dates.Month(-12) == Dates.DateTime(1998,12,27) -@test dt + Dates.Month(12) == Dates.DateTime(2000,12,27) -@test dt + Dates.Month(13) == Dates.DateTime(2001,1,27) -@test dt + Dates.Month(100) == Dates.DateTime(2008,4,27) -@test dt + Dates.Month(1000) == Dates.DateTime(2083,4,27) -@test dt - Dates.Month(1) == Dates.DateTime(1999,11,27) -@test dt - Dates.Month(-1) == Dates.DateTime(2000,1,27) -@test dt - Dates.Month(100) == Dates.DateTime(1991,8,27) -@test dt - Dates.Month(1000) == Dates.DateTime(1916,8,27) -dt = Dates.DateTime(2000,2,29) -@test dt + Dates.Month(1) == Dates.DateTime(2000,3,29) -@test dt - Dates.Month(1) == Dates.DateTime(2000,1,29) -dt = Dates.DateTime(1972,6,30,23,59,59) -@test dt + Dates.Month(1) == Dates.DateTime(1972,7,30,23,59,59) -@test dt - Dates.Month(1) == Dates.DateTime(1972,5,30,23,59,59) -@test dt + Dates.Month(-1) == Dates.DateTime(1972,5,30,23,59,59) - -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Week(1) == Dates.DateTime(2000,1,3) -@test dt + Dates.Week(100) == Dates.DateTime(2001,11,26) -@test dt + Dates.Week(1000) == Dates.DateTime(2019,2,25) -@test dt - Dates.Week(1) == Dates.DateTime(1999,12,20) -@test dt - Dates.Week(100) == Dates.DateTime(1998,1,26) -@test dt - Dates.Week(1000) == Dates.DateTime(1980,10,27) -dt = Dates.DateTime(2000,2,29) -@test dt + Dates.Week(1) == Dates.DateTime(2000,3,7) -@test dt - Dates.Week(1) == Dates.DateTime(2000,2,22) -dt = Dates.DateTime(1972,6,30,23,59,59) -@test dt + Dates.Week(1) == Dates.DateTime(1972,7,7,23,59,59) -@test dt - Dates.Week(1) == Dates.DateTime(1972,6,23,23,59,59) -@test dt + Dates.Week(-1) == Dates.DateTime(1972,6,23,23,59,59) - -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Day(1) == Dates.DateTime(1999,12,28) -@test dt + Dates.Day(100) == Dates.DateTime(2000,4,5) -@test dt + Dates.Day(1000) == Dates.DateTime(2002,9,22) -@test dt - Dates.Day(1) == Dates.DateTime(1999,12,26) -@test dt - Dates.Day(100) == Dates.DateTime(1999,9,18) -@test dt - Dates.Day(1000) == Dates.DateTime(1997,4,1) -dt = Dates.DateTime(1972,6,30,23,59,59) -@test dt + Dates.Day(1) == Dates.DateTime(1972,7,1,23,59,59) -@test dt - Dates.Day(1) == Dates.DateTime(1972,6,29,23,59,59) -@test dt + Dates.Day(-1) == Dates.DateTime(1972,6,29,23,59,59) - -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Hour(1) == Dates.DateTime(1999,12,27,1) -@test dt + Dates.Hour(100) == Dates.DateTime(1999,12,31,4) -@test dt + Dates.Hour(1000) == Dates.DateTime(2000,2,6,16) -@test dt - Dates.Hour(1) == Dates.DateTime(1999,12,26,23) -@test dt - Dates.Hour(100) == Dates.DateTime(1999,12,22,20) -@test dt - Dates.Hour(1000) == Dates.DateTime(1999,11,15,8) -dt = Dates.DateTime(1972,6,30,23,59,59) -@test dt + Dates.Hour(1) == Dates.DateTime(1972,7,1,0,59,59) -@test dt - Dates.Hour(1) == Dates.DateTime(1972,6,30,22,59,59) -@test dt + Dates.Hour(-1) == Dates.DateTime(1972,6,30,22,59,59) - -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Minute(1) == Dates.DateTime(1999,12,27,0,1) -@test dt + Dates.Minute(100) == Dates.DateTime(1999,12,27,1,40) -@test dt + Dates.Minute(1000) == Dates.DateTime(1999,12,27,16,40) -@test dt - Dates.Minute(1) == Dates.DateTime(1999,12,26,23,59) -@test dt - Dates.Minute(100) == Dates.DateTime(1999,12,26,22,20) -@test dt - Dates.Minute(1000) == Dates.DateTime(1999,12,26,7,20) -dt = Dates.DateTime(1972,6,30,23,59,59) -@test dt + Dates.Minute(1) == Dates.DateTime(1972,7,1,0,0,59) -@test dt - Dates.Minute(1) == Dates.DateTime(1972,6,30,23,58,59) -@test dt + Dates.Minute(-1) == Dates.DateTime(1972,6,30,23,58,59) - -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Second(1) == Dates.DateTime(1999,12,27,0,0,1) -@test dt + Dates.Second(100) == Dates.DateTime(1999,12,27,0,1,40) -@test dt + Dates.Second(1000) == Dates.DateTime(1999,12,27,0,16,40) -@test dt - Dates.Second(1) == Dates.DateTime(1999,12,26,23,59,59) -@test dt - Dates.Second(100) == Dates.DateTime(1999,12,26,23,58,20) -@test dt - Dates.Second(1000) == Dates.DateTime(1999,12,26,23,43,20) - -dt = Dates.DateTime(1999,12,27) -@test dt + Dates.Millisecond(1) == Dates.DateTime(1999,12,27,0,0,0,1) -@test dt + Dates.Millisecond(100) == Dates.DateTime(1999,12,27,0,0,0,100) -@test dt + Dates.Millisecond(1000) == Dates.DateTime(1999,12,27,0,0,1) -@test dt - Dates.Millisecond(1) == Dates.DateTime(1999,12,26,23,59,59,999) -@test dt - Dates.Millisecond(100) == Dates.DateTime(1999,12,26,23,59,59,900) -@test dt - Dates.Millisecond(1000) == Dates.DateTime(1999,12,26,23,59,59) -dt = Dates.DateTime(1972,6,30,23,59,59) -@test dt + Dates.Millisecond(1) == Dates.DateTime(1972,6,30,23,59,59,1) -@test dt - Dates.Millisecond(1) == Dates.DateTime(1972,6,30,23,59,58,999) -@test dt + Dates.Millisecond(-1) == Dates.DateTime(1972,6,30,23,59,58,999) - -dt = Dates.Date(1999,12,27) -@test dt + Dates.Year(1) == Dates.Date(2000,12,27) -@test dt + Dates.Year(100) == Dates.Date(2099,12,27) -@test dt + Dates.Year(1000) == Dates.Date(2999,12,27) -@test dt - Dates.Year(1) == Dates.Date(1998,12,27) -@test dt - Dates.Year(100) == Dates.Date(1899,12,27) -@test dt - Dates.Year(1000) == Dates.Date(999,12,27) -dt = Dates.Date(2000,2,29) -@test dt + Dates.Year(1) == Dates.Date(2001,2,28) -@test dt - Dates.Year(1) == Dates.Date(1999,2,28) -@test dt + Dates.Year(4) == Dates.Date(2004,2,29) -@test dt - Dates.Year(4) == Dates.Date(1996,2,29) - -dt = Dates.Date(1999,12,27) -@test dt + Dates.Month(1) == Dates.Date(2000,1,27) -@test dt + Dates.Month(100) == Dates.Date(2008,4,27) -@test dt + Dates.Month(1000) == Dates.Date(2083,4,27) -@test dt - Dates.Month(1) == Dates.Date(1999,11,27) -@test dt - Dates.Month(100) == Dates.Date(1991,8,27) -@test dt - Dates.Month(1000) == Dates.Date(1916,8,27) -dt = Dates.Date(2000,2,29) -@test dt + Dates.Month(1) == Dates.Date(2000,3,29) -@test dt - Dates.Month(1) == Dates.Date(2000,1,29) - -dt = Dates.Date(1999,12,27) -@test dt + Dates.Week(1) == Dates.Date(2000,1,3) -@test dt + Dates.Week(100) == Dates.Date(2001,11,26) -@test dt + Dates.Week(1000) == Dates.Date(2019,2,25) -@test dt - Dates.Week(1) == Dates.Date(1999,12,20) -@test dt - Dates.Week(100) == Dates.Date(1998,1,26) -@test dt - Dates.Week(1000) == Dates.Date(1980,10,27) -dt = Dates.Date(2000,2,29) -@test dt + Dates.Week(1) == Dates.Date(2000,3,7) -@test dt - Dates.Week(1) == Dates.Date(2000,2,22) - -dt = Dates.Date(1999,12,27) -@test dt + Dates.Day(1) == Dates.Date(1999,12,28) -@test dt + Dates.Day(100) == Dates.Date(2000,4,5) -@test dt + Dates.Day(1000) == Dates.Date(2002,9,22) -@test dt - Dates.Day(1) == Dates.Date(1999,12,26) -@test dt - Dates.Day(100) == Dates.Date(1999,9,18) -@test dt - Dates.Day(1000) == Dates.Date(1997,4,1) +@test Dates.yearwrap(2000, 1, -3600) == 1700 +@test Dates.yearwrap(2000, 1, -37) == 1996 +@test Dates.yearwrap(2000, 1, -36) == 1997 +@test Dates.yearwrap(2000, 1, -35) == 1997 +@test Dates.yearwrap(2000, 1, -25) == 1997 +@test Dates.yearwrap(2000, 1, -24) == 1998 +@test Dates.yearwrap(2000, 1, -23) == 1998 +@test Dates.yearwrap(2000, 1, -14) == 1998 +@test Dates.yearwrap(2000, 1, -13) == 1998 +@test Dates.yearwrap(2000, 1, -12) == 1999 +@test Dates.yearwrap(2000, 1, -11) == 1999 +@test Dates.yearwrap(2000, 1, -2) == 1999 +@test Dates.yearwrap(2000, 1, -1) == 1999 +@test Dates.yearwrap(2000, 1, 0) == 2000 +@test Dates.yearwrap(2000, 1, 1) == 2000 +@test Dates.yearwrap(2000, 1, 11) == 2000 +@test Dates.yearwrap(2000, 1, 12) == 2001 +@test Dates.yearwrap(2000, 1, 13) == 2001 +@test Dates.yearwrap(2000, 1, 23) == 2001 +@test Dates.yearwrap(2000, 1, 24) == 2002 +@test Dates.yearwrap(2000, 1, 25) == 2002 +@test Dates.yearwrap(2000, 1, 36) == 2003 +@test Dates.yearwrap(2000, 1, 3600) == 2300 +@test Dates.yearwrap(2000, 2, -2) == 1999 +@test Dates.yearwrap(2000, 3, 10) == 2001 +@test Dates.yearwrap(2000, 4, -4) == 1999 +@test Dates.yearwrap(2000, 5, 8) == 2001 +@test Dates.yearwrap(2000, 6, -18) == 1998 +@test Dates.yearwrap(2000, 6, -6) == 1999 +@test Dates.yearwrap(2000, 6, 6) == 2000 +@test Dates.yearwrap(2000, 6, 7) == 2001 +@test Dates.yearwrap(2000, 6, 19) == 2002 +@test Dates.yearwrap(2000, 12, -3600) == 1700 +@test Dates.yearwrap(2000, 12, -36) == 1997 +@test Dates.yearwrap(2000, 12, -35) == 1998 +@test Dates.yearwrap(2000, 12, -24) == 1998 +@test Dates.yearwrap(2000, 12, -23) == 1999 +@test Dates.yearwrap(2000, 12, -14) == 1999 +@test Dates.yearwrap(2000, 12, -13) == 1999 +@test Dates.yearwrap(2000, 12, -12) == 1999 +@test Dates.yearwrap(2000, 12, -11) == 2000 +@test Dates.yearwrap(2000, 12, -2) == 2000 +@test Dates.yearwrap(2000, 12, -1) == 2000 +@test Dates.yearwrap(2000, 12, 0) == 2000 +@test Dates.yearwrap(2000, 12, 1) == 2001 +@test Dates.yearwrap(2000, 12, 11) == 2001 +@test Dates.yearwrap(2000, 12, 12) == 2001 +@test Dates.yearwrap(2000, 12, 13) == 2002 +@test Dates.yearwrap(2000, 12, 24) == 2002 +@test Dates.yearwrap(2000, 12, 25) == 2003 +@test Dates.yearwrap(2000, 12, 36) == 2003 +@test Dates.yearwrap(2000, 12, 37) == 2004 +@test Dates.yearwrap(2000, 12, 3600) == 2300 + +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Month(1) == Dates.DateTime(2000, 1, 27) +@test dt + Dates.Month(-1) == Dates.DateTime(1999, 11, 27) +@test dt + Dates.Month(-11) == Dates.DateTime(1999, 1, 27) +@test dt + Dates.Month(11) == Dates.DateTime(2000, 11, 27) +@test dt + Dates.Month(-12) == Dates.DateTime(1998, 12, 27) +@test dt + Dates.Month(12) == Dates.DateTime(2000, 12, 27) +@test dt + Dates.Month(13) == Dates.DateTime(2001, 1, 27) +@test dt + Dates.Month(100) == Dates.DateTime(2008, 4, 27) +@test dt + Dates.Month(1000) == Dates.DateTime(2083, 4, 27) +@test dt - Dates.Month(1) == Dates.DateTime(1999, 11, 27) +@test dt - Dates.Month(-1) == Dates.DateTime(2000, 1, 27) +@test dt - Dates.Month(100) == Dates.DateTime(1991, 8, 27) +@test dt - Dates.Month(1000) == Dates.DateTime(1916, 8, 27) +dt = Dates.DateTime(2000, 2, 29) +@test dt + Dates.Month(1) == Dates.DateTime(2000, 3, 29) +@test dt - Dates.Month(1) == Dates.DateTime(2000, 1, 29) +dt = Dates.DateTime(1972, 6, 30, 23, 59, 59) +@test dt + Dates.Month(1) == Dates.DateTime(1972, 7, 30, 23, 59, 59) +@test dt - Dates.Month(1) == Dates.DateTime(1972, 5, 30, 23, 59, 59) +@test dt + Dates.Month(-1) == Dates.DateTime(1972, 5, 30, 23, 59, 59) + +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Week(1) == Dates.DateTime(2000, 1, 3) +@test dt + Dates.Week(100) == Dates.DateTime(2001, 11, 26) +@test dt + Dates.Week(1000) == Dates.DateTime(2019, 2, 25) +@test dt - Dates.Week(1) == Dates.DateTime(1999, 12, 20) +@test dt - Dates.Week(100) == Dates.DateTime(1998, 1, 26) +@test dt - Dates.Week(1000) == Dates.DateTime(1980, 10, 27) +dt = Dates.DateTime(2000, 2, 29) +@test dt + Dates.Week(1) == Dates.DateTime(2000, 3, 7) +@test dt - Dates.Week(1) == Dates.DateTime(2000, 2, 22) +dt = Dates.DateTime(1972, 6, 30, 23, 59, 59) +@test dt + Dates.Week(1) == Dates.DateTime(1972, 7, 7, 23, 59, 59) +@test dt - Dates.Week(1) == Dates.DateTime(1972, 6, 23, 23, 59, 59) +@test dt + Dates.Week(-1) == Dates.DateTime(1972, 6, 23, 23, 59, 59) + +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Day(1) == Dates.DateTime(1999, 12, 28) +@test dt + Dates.Day(100) == Dates.DateTime(2000, 4, 5) +@test dt + Dates.Day(1000) == Dates.DateTime(2002, 9, 22) +@test dt - Dates.Day(1) == Dates.DateTime(1999, 12, 26) +@test dt - Dates.Day(100) == Dates.DateTime(1999, 9, 18) +@test dt - Dates.Day(1000) == Dates.DateTime(1997, 4, 1) +dt = Dates.DateTime(1972, 6, 30, 23, 59, 59) +@test dt + Dates.Day(1) == Dates.DateTime(1972, 7, 1, 23, 59, 59) +@test dt - Dates.Day(1) == Dates.DateTime(1972, 6, 29, 23, 59, 59) +@test dt + Dates.Day(-1) == Dates.DateTime(1972, 6, 29, 23, 59, 59) + +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Hour(1) == Dates.DateTime(1999, 12, 27, 1) +@test dt + Dates.Hour(100) == Dates.DateTime(1999, 12, 31, 4) +@test dt + Dates.Hour(1000) == Dates.DateTime(2000, 2, 6, 16) +@test dt - Dates.Hour(1) == Dates.DateTime(1999, 12, 26, 23) +@test dt - Dates.Hour(100) == Dates.DateTime(1999, 12, 22, 20) +@test dt - Dates.Hour(1000) == Dates.DateTime(1999, 11, 15, 8) +dt = Dates.DateTime(1972, 6, 30, 23, 59, 59) +@test dt + Dates.Hour(1) == Dates.DateTime(1972, 7, 1, 0, 59, 59) +@test dt - Dates.Hour(1) == Dates.DateTime(1972, 6, 30, 22, 59, 59) +@test dt + Dates.Hour(-1) == Dates.DateTime(1972, 6, 30, 22, 59, 59) + +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Minute(1) == Dates.DateTime(1999, 12, 27, 0, 1) +@test dt + Dates.Minute(100) == Dates.DateTime(1999, 12, 27, 1, 40) +@test dt + Dates.Minute(1000) == Dates.DateTime(1999, 12, 27, 16, 40) +@test dt - Dates.Minute(1) == Dates.DateTime(1999, 12, 26, 23, 59) +@test dt - Dates.Minute(100) == Dates.DateTime(1999, 12, 26, 22, 20) +@test dt - Dates.Minute(1000) == Dates.DateTime(1999, 12, 26, 7, 20) +dt = Dates.DateTime(1972, 6, 30, 23, 59, 59) +@test dt + Dates.Minute(1) == Dates.DateTime(1972, 7, 1, 0, 0, 59) +@test dt - Dates.Minute(1) == Dates.DateTime(1972, 6, 30, 23, 58, 59) +@test dt + Dates.Minute(-1) == Dates.DateTime(1972, 6, 30, 23, 58, 59) + +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Second(1) == Dates.DateTime(1999, 12, 27, 0, 0, 1) +@test dt + Dates.Second(100) == Dates.DateTime(1999, 12, 27, 0, 1, 40) +@test dt + Dates.Second(1000) == Dates.DateTime(1999, 12, 27, 0, 16, 40) +@test dt - Dates.Second(1) == Dates.DateTime(1999, 12, 26, 23, 59, 59) +@test dt - Dates.Second(100) == Dates.DateTime(1999, 12, 26, 23, 58, 20) +@test dt - Dates.Second(1000) == Dates.DateTime(1999, 12, 26, 23, 43, 20) + +dt = Dates.DateTime(1999, 12, 27) +@test dt + Dates.Millisecond(1) == Dates.DateTime(1999, 12, 27, 0, 0, 0, 1) +@test dt + Dates.Millisecond(100) == Dates.DateTime(1999, 12, 27, 0, 0, 0, 100) +@test dt + Dates.Millisecond(1000) == Dates.DateTime(1999, 12, 27, 0, 0, 1) +@test dt - Dates.Millisecond(1) == Dates.DateTime(1999, 12, 26, 23, 59, 59, 999) +@test dt - Dates.Millisecond(100) == Dates.DateTime(1999, 12, 26, 23, 59, 59, 900) +@test dt - Dates.Millisecond(1000) == Dates.DateTime(1999, 12, 26, 23, 59, 59) +dt = Dates.DateTime(1972, 6, 30, 23, 59, 59) +@test dt + Dates.Millisecond(1) == Dates.DateTime(1972, 6, 30, 23, 59, 59, 1) +@test dt - Dates.Millisecond(1) == Dates.DateTime(1972, 6, 30, 23, 59, 58, 999) +@test dt + Dates.Millisecond(-1) == Dates.DateTime(1972, 6, 30, 23, 59, 58, 999) + +dt = Dates.Date(1999, 12, 27) +@test dt + Dates.Year(1) == Dates.Date(2000, 12, 27) +@test dt + Dates.Year(100) == Dates.Date(2099, 12, 27) +@test dt + Dates.Year(1000) == Dates.Date(2999, 12, 27) +@test dt - Dates.Year(1) == Dates.Date(1998, 12, 27) +@test dt - Dates.Year(100) == Dates.Date(1899, 12, 27) +@test dt - Dates.Year(1000) == Dates.Date(999, 12, 27) +dt = Dates.Date(2000, 2, 29) +@test dt + Dates.Year(1) == Dates.Date(2001, 2, 28) +@test dt - Dates.Year(1) == Dates.Date(1999, 2, 28) +@test dt + Dates.Year(4) == Dates.Date(2004, 2, 29) +@test dt - Dates.Year(4) == Dates.Date(1996, 2, 29) + +dt = Dates.Date(1999, 12, 27) +@test dt + Dates.Month(1) == Dates.Date(2000, 1, 27) +@test dt + Dates.Month(100) == Dates.Date(2008, 4, 27) +@test dt + Dates.Month(1000) == Dates.Date(2083, 4, 27) +@test dt - Dates.Month(1) == Dates.Date(1999, 11, 27) +@test dt - Dates.Month(100) == Dates.Date(1991, 8, 27) +@test dt - Dates.Month(1000) == Dates.Date(1916, 8, 27) +dt = Dates.Date(2000, 2, 29) +@test dt + Dates.Month(1) == Dates.Date(2000, 3, 29) +@test dt - Dates.Month(1) == Dates.Date(2000, 1, 29) + +dt = Dates.Date(1999, 12, 27) +@test dt + Dates.Week(1) == Dates.Date(2000, 1, 3) +@test dt + Dates.Week(100) == Dates.Date(2001, 11, 26) +@test dt + Dates.Week(1000) == Dates.Date(2019, 2, 25) +@test dt - Dates.Week(1) == Dates.Date(1999, 12, 20) +@test dt - Dates.Week(100) == Dates.Date(1998, 1, 26) +@test dt - Dates.Week(1000) == Dates.Date(1980, 10, 27) +dt = Dates.Date(2000, 2, 29) +@test dt + Dates.Week(1) == Dates.Date(2000, 3, 7) +@test dt - Dates.Week(1) == Dates.Date(2000, 2, 22) + +dt = Dates.Date(1999, 12, 27) +@test dt + Dates.Day(1) == Dates.Date(1999, 12, 28) +@test dt + Dates.Day(100) == Dates.Date(2000, 4, 5) +@test dt + Dates.Day(1000) == Dates.Date(2002, 9, 22) +@test dt - Dates.Day(1) == Dates.Date(1999, 12, 26) +@test dt - Dates.Day(100) == Dates.Date(1999, 9, 18) +@test dt - Dates.Day(1000) == Dates.Date(1997, 4, 1) # Test Time-TimePeriod arithmetic t = Dates.Time(0) @test t + Dates.Hour(1) == Dates.Time(1) @test t - Dates.Hour(1) == Dates.Time(23) -@test t - Dates.Nanosecond(1) == Dates.Time(23,59,59,999,999,999) -@test t + Dates.Nanosecond(-1) == Dates.Time(23,59,59,999,999,999) +@test t - Dates.Nanosecond(1) == Dates.Time(23, 59, 59, 999, 999, 999) +@test t + Dates.Nanosecond(-1) == Dates.Time(23, 59, 59, 999, 999, 999) @test t + Dates.Hour(24) == t @test t + Dates.Nanosecond(86400000000000) == t @test t - Dates.Nanosecond(86400000000000) == t -@test t + Dates.Minute(1) == Dates.Time(0,1) -@test t + Dates.Second(1) == Dates.Time(0,0,1) -@test t + Dates.Millisecond(1) == Dates.Time(0,0,0,1) -@test t + Dates.Microsecond(1) == Dates.Time(0,0,0,0,1) +@test t + Dates.Minute(1) == Dates.Time(0, 1) +@test t + Dates.Second(1) == Dates.Time(0, 0, 1) +@test t + Dates.Millisecond(1) == Dates.Time(0, 0, 0, 1) +@test t + Dates.Microsecond(1) == Dates.Time(0, 0, 0, 0, 1) @test_throws MethodError t + Dates.Day(1) # Vectorized Time arithmetic -a = Dates.Time(1,1,1) -dr = [a,a,a,a,a,a,a,a,a,a] +a = Dates.Time(1, 1, 1) +dr = [a, a, a, a, a, a, a, a, a, a] b = a + Dates.Hour(1) -@test dr .+ Dates.Hour(1) == repmat([b],10) +@test dr .+ Dates.Hour(1) == repmat([b], 10) b = a + Dates.Second(1) -@test dr .+ Dates.Second(1) == repmat([b],10) +@test dr .+ Dates.Second(1) == repmat([b], 10) b = a + Dates.Millisecond(1) -@test dr .+ Dates.Millisecond(1) == repmat([b],10) +@test dr .+ Dates.Millisecond(1) == repmat([b], 10) b = a + Dates.Microsecond(1) -@test dr .+ Dates.Microsecond(1) == repmat([b],10) +@test dr .+ Dates.Microsecond(1) == repmat([b], 10) b = a + Dates.Nanosecond(1) -@test dr .+ Dates.Nanosecond(1) == repmat([b],10) +@test dr .+ Dates.Nanosecond(1) == repmat([b], 10) b = a - Dates.Hour(1) -@test dr .- Dates.Hour(1) == repmat([b],10) +@test dr .- Dates.Hour(1) == repmat([b], 10) b = a - Dates.Second(1) -@test dr .- Dates.Second(1) == repmat([b],10) +@test dr .- Dates.Second(1) == repmat([b], 10) b = a - Dates.Millisecond(1) -@test dr .- Dates.Millisecond(1) == repmat([b],10) +@test dr .- Dates.Millisecond(1) == repmat([b], 10) b = a - Dates.Microsecond(1) -@test dr .- Dates.Microsecond(1) == repmat([b],10) +@test dr .- Dates.Microsecond(1) == repmat([b], 10) b = a - Dates.Nanosecond(1) -@test dr .- Dates.Nanosecond(1) == repmat([b],10) +@test dr .- Dates.Nanosecond(1) == repmat([b], 10) # Vectorized arithmetic -a = Dates.Date(2014,1,1) -dr = [a,a,a,a,a,a,a,a,a,a] +a = Dates.Date(2014, 1, 1) +dr = [a, a, a, a, a, a, a, a, a, a] b = a + Dates.Year(1) -@test dr .+ Dates.Year(1) == repmat([b],10) +@test dr .+ Dates.Year(1) == repmat([b], 10) b = a + Dates.Month(1) -@test dr .+ Dates.Month(1) == repmat([b],10) +@test dr .+ Dates.Month(1) == repmat([b], 10) b = a + Dates.Day(1) -@test dr .+ Dates.Day(1) == repmat([b],10) +@test dr .+ Dates.Day(1) == repmat([b], 10) b = a - Dates.Year(1) -@test dr .- Dates.Year(1) == repmat([b],10) +@test dr .- Dates.Year(1) == repmat([b], 10) b = a - Dates.Month(1) -@test dr .- Dates.Month(1) == repmat([b],10) +@test dr .- Dates.Month(1) == repmat([b], 10) b = a - Dates.Day(1) -@test dr .- Dates.Day(1) == repmat([b],10) +@test dr .- Dates.Day(1) == repmat([b], 10) # Vectorized arithmetic b = a + Dates.Year(1) -@test dr .+ Dates.Year(1) == repmat([b],10) +@test dr .+ Dates.Year(1) == repmat([b], 10) b = a + Dates.Month(1) -@test dr .+ Dates.Month(1) == repmat([b],10) +@test dr .+ Dates.Month(1) == repmat([b], 10) b = a + Dates.Day(1) -@test dr .+ Dates.Day(1) == repmat([b],10) +@test dr .+ Dates.Day(1) == repmat([b], 10) b = a - Dates.Year(1) -@test dr .- Dates.Year(1) == repmat([b],10) +@test dr .- Dates.Year(1) == repmat([b], 10) b = a - Dates.Month(1) -@test dr .- Dates.Month(1) == repmat([b],10) +@test dr .- Dates.Month(1) == repmat([b], 10) b = a - Dates.Day(1) -@test dr .- Dates.Day(1) == repmat([b],10) +@test dr .- Dates.Day(1) == repmat([b], 10) # Month arithmetic minimizes "edit distance", or number of changes # needed to get a correct answer # This approach results in a few cases of non-associativity -a = Dates.Date(2012,1,29) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,1,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,2,29) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,3,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,4,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,5,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,6,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,8,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,9,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,10,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) -a = Dates.Date(2012,11,30) -@test (a+Dates.Day(1))+Dates.Month(1) != (a+Dates.Month(1))+Dates.Day(1) - - -dt = Dates.DateTime(2000,1,1,12,30,45,500) +a = Dates.Date(2012, 1, 29) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 1, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 2, 29) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 3, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 4, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 5, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 6, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 8, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 9, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 10, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) +a = Dates.Date(2012, 11, 30) +@test (a + Dates.Day(1)) + Dates.Month(1) != (a + Dates.Month(1)) + Dates.Day(1) + + +dt = Dates.DateTime(2000, 1, 1, 12, 30, 45, 500) dt2 = dt + Dates.Year(1) @test Dates.year(dt2) == 2001 @test Dates.month(dt2) == 1 @@ -376,65 +376,56 @@ dt2 = dt + Dates.Year(1) @test Dates.second(dt2) == 45 @test Dates.millisecond(dt2) == 500 -t1 = [Dates.Date(2009,1,1) Dates.Date(2009,1,2) Dates.Date(2009,1,3); Dates.Date(2009,2,1) Dates.Date(2009,2,2) Dates.Date(2009,2,3)] -t2 = [Dates.Date(2009,1,2) Dates.Date(2009,2,2) Dates.Date(2010,1,3); Dates.Date(2010,2,1) Dates.Date(2009,3,2) Dates.Date(2009,2,4)] -t3 = [Dates.DateTime(2009,1,1), Dates.DateTime(2009,1,2), Dates.DateTime(2009,1,3)] -t4 = [Dates.DateTime(2009,1,1,0,0,1), Dates.DateTime(2009,1,2,0,1), Dates.DateTime(2009,1,3,1)] -t5 = [Dates.Time(0,0,0) Dates.Time(0,0,1) Dates.Time(0,0,2); Dates.Time(0,1,0) Dates.Time(0,2,0) Dates.Time(0,3,0)] +t1 = [Dates.Date(2009, 1, 1) Dates.Date(2009, 1, 2) Dates.Date(2009, 1, 3); Dates.Date(2009, 2, 1) Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3)] +t2 = [Dates.Date(2009, 1, 2) Dates.Date(2009, 2, 2) Dates.Date(2010, 1, 3); Dates.Date(2010, 2, 1) Dates.Date(2009, 3, 2) Dates.Date(2009, 2, 4)] +t3 = [Dates.DateTime(2009, 1, 1), Dates.DateTime(2009, 1, 2), Dates.DateTime(2009, 1, 3)] +t4 = [Dates.DateTime(2009, 1, 1, 0, 0, 1), Dates.DateTime(2009, 1, 2, 0, 1), Dates.DateTime(2009, 1, 3, 1)] +t5 = [Dates.Time(0, 0, 0) Dates.Time(0, 0, 1) Dates.Time(0, 0, 2); Dates.Time(0, 1, 0) Dates.Time(0, 2, 0) Dates.Time(0, 3, 0)] # TimeType, Array{TimeType} -@test Dates.Date(2010,1,1) .- t1 == [Dates.Day(365) Dates.Day(364) Dates.Day(363); Dates.Day(334) Dates.Day(333) Dates.Day(332)] -@test t1 .- Dates.Date(2010,1,1) == [Dates.Day(-365) Dates.Day(-364) Dates.Day(-363); Dates.Day(-334) Dates.Day(-333) Dates.Day(-332)] -@test Dates.DateTime(2009,1,1) .- t3 == [Dates.Millisecond(0), Dates.Millisecond(-86400000), Dates.Millisecond(-172800000)] -@test t3 .- Dates.DateTime(2009,1,1) == [Dates.Millisecond(0), Dates.Millisecond(86400000), Dates.Millisecond(172800000)] -@test Dates.Date(2010,1,1) - t1 == [Dates.Day(365) Dates.Day(364) Dates.Day(363); Dates.Day(334) Dates.Day(333) Dates.Day(332)] -@test t1 - Dates.Date(2010,1,1) == [Dates.Day(-365) Dates.Day(-364) Dates.Day(-363); Dates.Day(-334) Dates.Day(-333) Dates.Day(-332)] -@test Dates.DateTime(2009,1,1) - t3 == [Dates.Millisecond(0), Dates.Millisecond(-86400000), Dates.Millisecond(-172800000)] -@test t3 - Dates.DateTime(2009,1,1) == [Dates.Millisecond(0), Dates.Millisecond(86400000), Dates.Millisecond(172800000)] +@test Dates.Date(2010, 1, 1) .- t1 == [Dates.Day(365) Dates.Day(364) Dates.Day(363); Dates.Day(334) Dates.Day(333) Dates.Day(332)] +@test t1 .- Dates.Date(2010, 1, 1) == [Dates.Day(-365) Dates.Day(-364) Dates.Day(-363); Dates.Day(-334) Dates.Day(-333) Dates.Day(-332)] +@test Dates.DateTime(2009, 1, 1) .- t3 == [Dates.Millisecond(0), Dates.Millisecond(-86400000), Dates.Millisecond(-172800000)] +@test t3 .- Dates.DateTime(2009, 1, 1) == [Dates.Millisecond(0), Dates.Millisecond(86400000), Dates.Millisecond(172800000)] +@test Dates.Date(2010, 1, 1) - t1 == [Dates.Day(365) Dates.Day(364) Dates.Day(363); Dates.Day(334) Dates.Day(333) Dates.Day(332)] +@test t1 - Dates.Date(2010, 1, 1) == [Dates.Day(-365) Dates.Day(-364) Dates.Day(-363); Dates.Day(-334) Dates.Day(-333) Dates.Day(-332)] +@test Dates.DateTime(2009, 1, 1) - t3 == [Dates.Millisecond(0), Dates.Millisecond(-86400000), Dates.Millisecond(-172800000)] +@test t3 - Dates.DateTime(2009, 1, 1) == [Dates.Millisecond(0), Dates.Millisecond(86400000), Dates.Millisecond(172800000)] @test Dates.Time(2) .- t5 == [Dates.Nanosecond(7200000000000) Dates.Nanosecond(7199000000000) Dates.Nanosecond(7198000000000); Dates.Nanosecond(7140000000000) Dates.Nanosecond(7080000000000) Dates.Nanosecond(7020000000000)] # GeneralPeriod, Array{TimeType} -@test Dates.Day(1) .+ t1 == [Dates.Date(2009,1,2) Dates.Date(2009,1,3) Dates.Date(2009,1,4); Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4)] -@test Dates.Hour(1) .+ t3 == [Dates.DateTime(2009,1,1,1), Dates.DateTime(2009,1,2,1), Dates.DateTime(2009,1,3,1)] -@test t1 .+ Dates.Day(1) == [Dates.Date(2009,1,2) Dates.Date(2009,1,3) Dates.Date(2009,1,4); Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4)] -@test t3 .+ Dates.Hour(1) == [Dates.DateTime(2009,1,1,1), Dates.DateTime(2009,1,2,1), Dates.DateTime(2009,1,3,1)] -@test Dates.Day(1) + t1 == [Dates.Date(2009,1,2) Dates.Date(2009,1,3) Dates.Date(2009,1,4); Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4)] -@test Dates.Hour(1) + t3 == [Dates.DateTime(2009,1,1,1), Dates.DateTime(2009,1,2,1), Dates.DateTime(2009,1,3,1)] -@test t1 + Dates.Day(1) == [Dates.Date(2009,1,2) Dates.Date(2009,1,3) Dates.Date(2009,1,4); Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4)] -@test t3 + Dates.Hour(1) == [Dates.DateTime(2009,1,1,1), Dates.DateTime(2009,1,2,1), Dates.DateTime(2009,1,3,1)] -@test t5 + Dates.Hour(1) == [Dates.Time(1,0,0) Dates.Time(1,0,1) Dates.Time(1,0,2); Dates.Time(1,1,0) Dates.Time(1,2,0) Dates.Time(1,3,0)] - -@test (Dates.Month(1) + Dates.Day(1)) .+ t1 == [Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4); Dates.Date(2009,3,2) Dates.Date(2009,3,3) Dates.Date(2009,3,4)] -@test (Dates.Hour(1) + Dates.Minute(1)) .+ t3 == [Dates.DateTime(2009,1,1,1,1), Dates.DateTime(2009,1,2,1,1), Dates.DateTime(2009,1,3,1,1)] -@test t1 .+ (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4); Dates.Date(2009,3,2) Dates.Date(2009,3,3) Dates.Date(2009,3,4)] -@test t3 .+ (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2009,1,1,1,1), Dates.DateTime(2009,1,2,1,1), Dates.DateTime(2009,1,3,1,1)] -@test (Dates.Month(1) + Dates.Day(1)) + t1 == [Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4); Dates.Date(2009,3,2) Dates.Date(2009,3,3) Dates.Date(2009,3,4)] -@test (Dates.Hour(1) + Dates.Minute(1)) + t3 == [Dates.DateTime(2009,1,1,1,1), Dates.DateTime(2009,1,2,1,1), Dates.DateTime(2009,1,3,1,1)] -@test t1 + (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2009,2,2) Dates.Date(2009,2,3) Dates.Date(2009,2,4); Dates.Date(2009,3,2) Dates.Date(2009,3,3) Dates.Date(2009,3,4)] -@test t3 + (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2009,1,1,1,1), Dates.DateTime(2009,1,2,1,1), Dates.DateTime(2009,1,3,1,1)] - -@test Dates.Day(1) .- t1 == [Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2); Dates.Date(2009,1,31) Dates.Date(2009,2,1) Dates.Date(2009,2,2)] -@test Dates.Hour(1) .- t3 == [Dates.DateTime(2008,12,31,23), Dates.DateTime(2009,1,1,23), Dates.DateTime(2009,1,2,23)] -@test t1 .- Dates.Day(1) == [Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2); Dates.Date(2009,1,31) Dates.Date(2009,2,1) Dates.Date(2009,2,2)] -@test t3 .- Dates.Hour(1) == [Dates.DateTime(2008,12,31,23), Dates.DateTime(2009,1,1,23), Dates.DateTime(2009,1,2,23)] -@test Dates.Day(1) - t1 == [Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2); Dates.Date(2009,1,31) Dates.Date(2009,2,1) Dates.Date(2009,2,2)] -@test Dates.Hour(1) - t3 == [Dates.DateTime(2008,12,31,23), Dates.DateTime(2009,1,1,23), Dates.DateTime(2009,1,2,23)] -@test t1 - Dates.Day(1) == [Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2); Dates.Date(2009,1,31) Dates.Date(2009,2,1) Dates.Date(2009,2,2)] -@test t3 - Dates.Hour(1) == [Dates.DateTime(2008,12,31,23), Dates.DateTime(2009,1,1,23), Dates.DateTime(2009,1,2,23)] - -@test (Dates.Month(1) + Dates.Day(1)) .- t1 == [Dates.Date(2008,11,30) Dates.Date(2008,12,1) Dates.Date(2008,12,2); Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2)] -@test (Dates.Hour(1) + Dates.Minute(1)) .- t3 == [Dates.DateTime(2008,12,31,22,59), Dates.DateTime(2009,1,1,22,59), Dates.DateTime(2009,1,2,22,59)] -@test t1 .- (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2008,11,30) Dates.Date(2008,12,1) Dates.Date(2008,12,2); Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2)] -@test t3 .- (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2008,12,31,22,59), Dates.DateTime(2009,1,1,22,59), Dates.DateTime(2009,1,2,22,59)] -@test (Dates.Month(1) + Dates.Day(1)) - t1 == [Dates.Date(2008,11,30) Dates.Date(2008,12,1) Dates.Date(2008,12,2); Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2)] -@test (Dates.Hour(1) + Dates.Minute(1)) - t3 == [Dates.DateTime(2008,12,31,22,59), Dates.DateTime(2009,1,1,22,59), Dates.DateTime(2009,1,2,22,59)] -@test t1 - (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2008,11,30) Dates.Date(2008,12,1) Dates.Date(2008,12,2); Dates.Date(2008,12,31) Dates.Date(2009,1,1) Dates.Date(2009,1,2)] -@test t3 - (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2008,12,31,22,59), Dates.DateTime(2009,1,1,22,59), Dates.DateTime(2009,1,2,22,59)] +@test Dates.Day(1) .+ t1 == [Dates.Date(2009, 1, 2) Dates.Date(2009, 1, 3) Dates.Date(2009, 1, 4); Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4)] +@test Dates.Hour(1) .+ t3 == [Dates.DateTime(2009, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1), Dates.DateTime(2009, 1, 3, 1)] +@test t1 .+ Dates.Day(1) == [Dates.Date(2009, 1, 2) Dates.Date(2009, 1, 3) Dates.Date(2009, 1, 4); Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4)] +@test t3 .+ Dates.Hour(1) == [Dates.DateTime(2009, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1), Dates.DateTime(2009, 1, 3, 1)] +@test Dates.Day(1) + t1 == [Dates.Date(2009, 1, 2) Dates.Date(2009, 1, 3) Dates.Date(2009, 1, 4); Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4)] +@test Dates.Hour(1) + t3 == [Dates.DateTime(2009, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1), Dates.DateTime(2009, 1, 3, 1)] +@test t1 + Dates.Day(1) == [Dates.Date(2009, 1, 2) Dates.Date(2009, 1, 3) Dates.Date(2009, 1, 4); Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4)] +@test t3 + Dates.Hour(1) == [Dates.DateTime(2009, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1), Dates.DateTime(2009, 1, 3, 1)] +@test t5 + Dates.Hour(1) == [Dates.Time(1, 0, 0) Dates.Time(1, 0, 1) Dates.Time(1, 0, 2); Dates.Time(1, 1, 0) Dates.Time(1, 2, 0) Dates.Time(1, 3, 0)] + +@test (Dates.Month(1) + Dates.Day(1)) .+ t1 == [Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4); Dates.Date(2009, 3, 2) Dates.Date(2009, 3, 3) Dates.Date(2009, 3, 4)] +@test (Dates.Hour(1) + Dates.Minute(1)) .+ t3 == [Dates.DateTime(2009, 1, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1, 1), Dates.DateTime(2009, 1, 3, 1, 1)] +@test t1 .+ (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4); Dates.Date(2009, 3, 2) Dates.Date(2009, 3, 3) Dates.Date(2009, 3, 4)] +@test t3 .+ (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2009, 1, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1, 1), Dates.DateTime(2009, 1, 3, 1, 1)] +@test (Dates.Month(1) + Dates.Day(1)) + t1 == [Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4); Dates.Date(2009, 3, 2) Dates.Date(2009, 3, 3) Dates.Date(2009, 3, 4)] +@test (Dates.Hour(1) + Dates.Minute(1)) + t3 == [Dates.DateTime(2009, 1, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1, 1), Dates.DateTime(2009, 1, 3, 1, 1)] +@test t1 + (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2009, 2, 2) Dates.Date(2009, 2, 3) Dates.Date(2009, 2, 4); Dates.Date(2009, 3, 2) Dates.Date(2009, 3, 3) Dates.Date(2009, 3, 4)] +@test t3 + (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2009, 1, 1, 1, 1), Dates.DateTime(2009, 1, 2, 1, 1), Dates.DateTime(2009, 1, 3, 1, 1)] + +@test t1 .- Dates.Day(1) == [Dates.Date(2008, 12, 31) Dates.Date(2009, 1, 1) Dates.Date(2009, 1, 2); Dates.Date(2009, 1, 31) Dates.Date(2009, 2, 1) Dates.Date(2009, 2, 2)] +@test t3 .- Dates.Hour(1) == [Dates.DateTime(2008, 12, 31, 23), Dates.DateTime(2009, 1, 1, 23), Dates.DateTime(2009, 1, 2, 23)] +@test t1 - Dates.Day(1) == [Dates.Date(2008, 12, 31) Dates.Date(2009, 1, 1) Dates.Date(2009, 1, 2); Dates.Date(2009, 1, 31) Dates.Date(2009, 2, 1) Dates.Date(2009, 2, 2)] +@test t3 - Dates.Hour(1) == [Dates.DateTime(2008, 12, 31, 23), Dates.DateTime(2009, 1, 1, 23), Dates.DateTime(2009, 1, 2, 23)] + +@test t1 .- (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2008, 11, 30) Dates.Date(2008, 12, 1) Dates.Date(2008, 12, 2); Dates.Date(2008, 12, 31) Dates.Date(2009, 1, 1) Dates.Date(2009, 1, 2)] +@test t3 .- (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2008, 12, 31, 22, 59), Dates.DateTime(2009, 1, 1, 22, 59), Dates.DateTime(2009, 1, 2, 22, 59)] +@test t1 - (Dates.Month(1) + Dates.Day(1)) == [Dates.Date(2008, 11, 30) Dates.Date(2008, 12, 1) Dates.Date(2008, 12, 2); Dates.Date(2008, 12, 31) Dates.Date(2009, 1, 1) Dates.Date(2009, 1, 2)] +@test t3 - (Dates.Hour(1) + Dates.Minute(1)) == [Dates.DateTime(2008, 12, 31, 22, 59), Dates.DateTime(2009, 1, 1, 22, 59), Dates.DateTime(2009, 1, 2, 22, 59)] # Array{TimeType}, Array{TimeType} @test t2 - t1 == [Dates.Day(1) Dates.Day(31) Dates.Day(365); Dates.Day(365) Dates.Day(28) Dates.Day(1)] @test t4 - t3 == [Dates.Millisecond(1000), Dates.Millisecond(60000), Dates.Millisecond(3600000)] -@test (Dates.Date(2009,1,1):Dates.Week(1):Dates.Date(2009,1,21)) - (Dates.Date(2009,1,1):Dates.Day(1):Dates.Date(2009,1,3)) == [Dates.Day(0), Dates.Day(6), Dates.Day(12)] -@test (Dates.DateTime(2009,1,1,1,1,1):Dates.Second(1):Dates.DateTime(2009,1,1,1,1,3)) - (Dates.DateTime(2009,1,1,1,1):Dates.Second(1):Dates.DateTime(2009,1,1,1,1,2)) == [Dates.Second(1), Dates.Second(1), Dates.Second(1)] - +@test (Dates.Date(2009, 1, 1):Dates.Week(1):Dates.Date(2009, 1, 21)) - (Dates.Date(2009, 1, 1):Dates.Day(1):Dates.Date(2009, 1, 3)) == [Dates.Day(0), Dates.Day(6), Dates.Day(12)] +@test (Dates.DateTime(2009, 1, 1, 1, 1, 1):Dates.Second(1):Dates.DateTime(2009, 1, 1, 1, 1, 3)) - (Dates.DateTime(2009, 1, 1, 1, 1):Dates.Second(1):Dates.DateTime(2009, 1, 1, 1, 1, 2)) == [Dates.Second(1), Dates.Second(1), Dates.Second(1)] From 2dd9ff8ce97ab0bc9c4ac1506057eaf3a48db7b7 Mon Sep 17 00:00:00 2001 From: quinnj Date: Tue, 24 Jan 2017 18:25:10 -0700 Subject: [PATCH 03/11] Update Time adjuster function documentation to be more explicit about default step period --- base/dates/adjusters.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/base/dates/adjusters.jl b/base/dates/adjusters.jl index f1c9a3075a985..8199509b7e557 100644 --- a/base/dates/adjusters.jl +++ b/base/dates/adjusters.jl @@ -197,7 +197,10 @@ function DateTime(func::Function, y, m, d, h, mi, s; step::Period=Millisecond(1) end """ - Time(f::Function, h[, mi, s, ms, us]; step=Second(1), limit=10000) -> Time + Time(func::Function, h, mi=0; step::Period=Second(1), negate::Bool=false, limit::Int=10000) + Time(func::Function, h, mi, s; step::Period=Millisecond(1), negate::Bool=false, limit::Int=10000) + Time(func::Function, h, mi, s, ms; step::Period=Microsecond(1), negate::Bool=false, limit::Int=10000) + Time(func::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), negate::Bool=false, limit::Int=10000) Create a `Time` through the adjuster API. The starting point will be constructed from the provided `h, mi, s, ms, us` arguments, and will be adjusted until `f::Function` returns `true`. From 760e8596c0b7f68eb0a6f42ea6459fee0b129ce9 Mon Sep 17 00:00:00 2001 From: quinnj Date: Tue, 24 Jan 2017 18:25:21 -0700 Subject: [PATCH 04/11] Whitespace fixes for dates code --- base/dates/accessors.jl | 59 ++-- base/dates/adjusters.jl | 32 +-- base/dates/conversions.jl | 14 +- base/dates/io.jl | 84 +++--- base/dates/parse.jl | 30 +- base/dates/periods.jl | 174 ++++++------ base/dates/query.jl | 32 +-- base/dates/types.jl | 42 +-- test/dates/accessors.jl | 172 ++++++------ test/dates/adjusters.jl | 572 +++++++++++++++++++------------------- test/dates/conversions.jl | 30 +- test/dates/io.jl | 388 +++++++++++++------------- test/dates/periods.jl | 198 ++++++------- test/dates/query.jl | 268 +++++++++--------- test/dates/ranges.jl | 320 ++++++++++----------- test/dates/types.jl | 246 ++++++++-------- 16 files changed, 1335 insertions(+), 1326 deletions(-) diff --git a/base/dates/accessors.jl b/base/dates/accessors.jl index f3e9b6981f304..4022f4172beb3 100644 --- a/base/dates/accessors.jl +++ b/base/dates/accessors.jl @@ -3,62 +3,63 @@ # Convert # of Rata Die days to proleptic Gregorian calendar y,m,d,w # Reference: http://mysite.verizon.net/aesir_research/date/date0.htm function yearmonthday(days) - z = days + 306; h = 100z - 25; a = fld(h,3652425); b = a - fld(a,4) - y = fld(100b+h,36525); c = b + z - 365y - fld(y,4); m = div(5c+456,153) - d = c - div(153m-457,5); return m > 12 ? (y+1,m-12,d) : (y,m,d) + z = days + 306; h = 100z - 25; a = fld(h, 3652425); b = a - fld(a, 4) + y = fld(100b + h, 36525); c = b + z - 365y - fld(y, 4); m = div(5c + 456, 153) + d = c - div(153m - 457, 5); return m > 12 ? (y + 1, m - 12, d) : (y, m, d) end function year(days) - z = days + 306; h = 100z - 25; a = fld(h,3652425); b = a - fld(a,4) - y = fld(100b+h,36525); c = b + z - 365y - fld(y,4); m = div(5c+456,153) - return m > 12 ? y+1 : y + z = days + 306; h = 100z - 25; a = fld(h, 3652425); b = a - fld(a, 4) + y = fld(100b + h, 36525); c = b + z - 365y - fld(y, 4); m = div(5c + 456, 153) + return m > 12 ? y + 1 : y end function yearmonth(days) z = days + 306; h = 100z - 25; a = fld(h,3652425); b = a - fld(a,4) - y = fld(100b+h,36525); c = b + z - 365y - fld(y,4); m = div(5c+456,153) - return m > 12 ? (y+1,m-12) : (y,m) + y = fld(100b + h, 36525); c = b + z - 365y - fld(y, 4); m = div(5c + 456, 153) + return m > 12 ? (y + 1, m - 12) : (y, m) end function month(days) z = days + 306; h = 100z - 25; a = fld(h,3652425); b = a - fld(a,4) - y = fld(100b+h,36525); c = b + z - 365y - fld(y,4); m = div(5c+456,153) - return m > 12 ? m-12 : m + y = fld(100b + h, 36525); c = b + z - 365y - fld(y, 4); m = div(5c + 456, 153) + return m > 12 ? m - 12 : m end function monthday(days) z = days + 306; h = 100z - 25; a = fld(h,3652425); b = a - fld(a,4) - y = fld(100b+h,36525); c = b + z - 365y - fld(y,4); m = div(5c+456,153) - d = c - div(153m-457,5); return m > 12 ? (m-12,d) : (m,d) + y = fld(100b + h, 36525); c = b + z - 365y - fld(y, 4); m = div(5c + 456, 153) + d = c - div(153m - 457, 5); return m > 12 ? (m - 12, d) : (m, d) end function day(days) z = days + 306; h = 100z - 25; a = fld(h,3652425); b = a - fld(a,4) - y = fld(100b+h,36525); c = b + z - 365y - fld(y,4); m = div(5c+456,153) - return c - div(153m-457,5) + y = fld(100b + h, 36525); c = b + z - 365y - fld(y, 4); m = div(5c + 456, 153) + return c - div(153m - 457, 5) end # https://en.wikipedia.org/wiki/Talk:ISO_week_date#Algorithms +const WEEK_INDEX = (15, 23, 3, 11) function week(days) - w = div(abs(days-1),7) % 20871 - c,w = divrem((w + (w >= 10435)),5218) - w = (w*28+[15,23,3,11][c+1]) % 1461 - return div(w,28) + 1 + w = div(abs(days - 1), 7) % 20871 + c, w = divrem((w + (w >= 10435)), 5218) + w = (w * 28 + WEEK_INDEX[c + 1]) % 1461 + return div(w, 28) + 1 end # Accessor functions value(dt::TimeType) = dt.instant.periods.value value(t::Time) = t.instant.value days(dt::Date) = value(dt) -days(dt::DateTime) = fld(value(dt),86400000) +days(dt::DateTime) = fld(value(dt), 86400000) year(dt::TimeType) = year(days(dt)) month(dt::TimeType) = month(days(dt)) week(dt::TimeType) = week(days(dt)) day(dt::TimeType) = day(days(dt)) -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),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)) +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), 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) diff --git a/base/dates/adjusters.jl b/base/dates/adjusters.jl index 8199509b7e557..9ca5391cd8abb 100644 --- a/base/dates/adjusters.jl +++ b/base/dates/adjusters.jl @@ -159,7 +159,7 @@ The step size in adjusting can be provided manually through the `step` keyword. `limit` provides a limit to the max number of iterations the adjustment API will pursue before throwing an error (given that `f::Function` is never satisfied). """ -function Date(func::Function, y, m=1, d=1;step::Period=Day(1), negate=nothing, limit::Int=10000) +function Date(func::Function, y, m=1, d=1; step::Period=Day(1), negate=nothing, limit::Int=10000) func = deprecate_negate(:Date, func, "func,y,m,d", negate) return adjust(DateFunction(func, Date(y, m, d)), Date(y, m, d), step, limit) end @@ -197,10 +197,10 @@ function DateTime(func::Function, y, m, d, h, mi, s; step::Period=Millisecond(1) end """ - Time(func::Function, h, mi=0; step::Period=Second(1), negate::Bool=false, limit::Int=10000) - Time(func::Function, h, mi, s; step::Period=Millisecond(1), negate::Bool=false, limit::Int=10000) - Time(func::Function, h, mi, s, ms; step::Period=Microsecond(1), negate::Bool=false, limit::Int=10000) - Time(func::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), negate::Bool=false, limit::Int=10000) + Time(func::Function, h, mi=0; step::Period=Second(1), limit::Int=10000) + Time(func::Function, h, mi, s; step::Period=Millisecond(1), limit::Int=10000) + Time(func::Function, h, mi, s, ms; step::Period=Microsecond(1), limit::Int=10000) + Time(func::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), limit::Int=10000) Create a `Time` through the adjuster API. The starting point will be constructed from the provided `h, mi, s, ms, us` arguments, and will be adjusted until `f::Function` returns `true`. @@ -240,38 +240,38 @@ ISDAYOFWEEK = Dict(Mon => DateFunction(ismonday, Date(0)), # "same" indicates whether the current date can be considered or not """ - tonext(dt::TimeType,dow::Int;same::Bool=false) -> TimeType + tonext(dt::TimeType, dow::Int; same::Bool=false) -> TimeType Adjusts `dt` to the next day of week corresponding to `dow` with `1 = Monday, 2 = Tuesday, etc`. Setting `same=true` allows the current `dt` to be considered as the next `dow`, allowing for no adjustment to occur. """ -tonext(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same ? dt : dt+Day(1), Day(1), 7) +tonext(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same ? dt : dt + Day(1), Day(1), 7) # Return the next TimeType where func evals true using step in incrementing """ - tonext(func::Function,dt::TimeType;step=Day(1),limit=10000,same=false) -> TimeType + tonext(func::Function, dt::TimeType; step=Day(1), limit=10000, same=false) -> TimeType Adjusts `dt` by iterating at most `limit` iterations by `step` increments until `func` returns `true`. `func` must take a single `TimeType` argument and return a `Bool`. `same` allows `dt` to be considered in satisfying `func`. """ -function tonext(func::Function, dt::TimeType;step::Period=Day(1), negate=nothing, limit::Int=10000, same::Bool=false) +function tonext(func::Function, dt::TimeType; step::Period=Day(1), negate=nothing, limit::Int=10000, same::Bool=false) func = deprecate_negate(:tonext, func, "func,dt", negate) - return adjust(DateFunction(func, dt), same ? dt : dt+step, step, limit) + return adjust(DateFunction(func, dt), same ? dt : dt + step, step, limit) end """ - toprev(dt::TimeType,dow::Int;same::Bool=false) -> TimeType + toprev(dt::TimeType, dow::Int; same::Bool=false) -> TimeType Adjusts `dt` to the previous day of week corresponding to `dow` with `1 = Monday, 2 = Tuesday, etc`. Setting `same=true` allows the current `dt` to be considered as the previous `dow`, allowing for no adjustment to occur. """ -toprev(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same ? dt : dt+Day(-1), Day(-1), 7) +toprev(dt::TimeType, dow::Int; same::Bool=false) = adjust(ISDAYOFWEEK[dow], same ? dt : dt + Day(-1), Day(-1), 7) """ - toprev(func::Function,dt::TimeType;step=Day(-1),limit=10000,same=false) -> TimeType + toprev(func::Function, dt::TimeType; step=Day(-1), limit=10000, same=false) -> TimeType Adjusts `dt` by iterating at most `limit` iterations by `step` increments until `func` returns `true`. `func` must take a single `TimeType` argument and return a `Bool`. `same` @@ -279,12 +279,12 @@ allows `dt` to be considered in satisfying `func`. """ function toprev(func::Function, dt::TimeType; step::Period=Day(-1), negate=nothing, limit::Int=10000, same::Bool=false) func = deprecate_negate(:toprev, func, "func,dt", negate) - return adjust(DateFunction(func, dt), same ? dt : dt+step, step, limit) + return adjust(DateFunction(func, dt), same ? dt : dt + step, step, limit) end # Return the first TimeType that falls on dow in the Month or Year """ - tofirst(dt::TimeType,dow::Int;of=Month) -> TimeType + tofirst(dt::TimeType, dow::Int; of=Month) -> TimeType Adjusts `dt` to the first `dow` of its month. Alternatively, `of=Year` will adjust to the first `dow` of the year. @@ -296,7 +296,7 @@ end # Return the last TimeType that falls on dow in the Month or Year """ - tolast(dt::TimeType,dow::Int;of=Month) -> TimeType + tolast(dt::TimeType, dow::Int; of=Month) -> TimeType Adjusts `dt` to the last `dow` of its month. Alternatively, `of=Year` will adjust to the last `dow` of the year. diff --git a/base/dates/conversions.jl b/base/dates/conversions.jl index 22b0819b7b18c..b406d1d47b78c 100644 --- a/base/dates/conversions.jl +++ b/base/dates/conversions.jl @@ -9,7 +9,7 @@ Converts a `DateTime` to a `Date`. The hour, minute, second, and millisecond par the `DateTime` are truncated, so only the year, month and day parts are used in construction. """ -Date(dt::TimeType) = convert(Date,dt) +Date(dt::TimeType) = convert(Date, dt) """ DateTime(dt::Date) -> DateTime @@ -17,7 +17,7 @@ Date(dt::TimeType) = convert(Date,dt) Converts a `Date` to a `DateTime`. The hour, minute, second, and millisecond parts of the new `DateTime` are assumed to be zero. """ -DateTime(dt::TimeType) = convert(DateTime,dt) +DateTime(dt::TimeType) = convert(DateTime, dt) """ Time(dt::DateTime) -> Time @@ -27,7 +27,7 @@ the `DateTime` are used to create the new `Time`. Microsecond and nanoseconds ar """ Time(dt::DateTime) = convert(Time, dt) -Base.convert(::Type{DateTime}, dt::Date) = DateTime(UTM(value(dt)*86400000)) +Base.convert(::Type{DateTime}, dt::Date) = DateTime(UTM(value(dt) * 86400000)) Base.convert(::Type{Date}, dt::DateTime) = Date(UTD(days(dt))) Base.convert(::Type{Time}, dt::DateTime) = Time(Nanosecond((value(dt) % 86400000) * 1000000)) @@ -55,7 +55,7 @@ end Takes the given `DateTime` and returns the number of seconds since the unix epoch `1970-01-01T00:00:00` as a `Float64`. """ -datetime2unix(dt::DateTime) = (value(dt) - UNIXEPOCH)/1000.0 +datetime2unix(dt::DateTime) = (value(dt) - UNIXEPOCH) / 1000.0 """ now() -> DateTime @@ -66,7 +66,7 @@ locale. function now() tv = Libc.TimeVal() tm = Libc.TmStruct(tv.sec) - return DateTime(tm.year+1900,tm.month+1,tm.mday,tm.hour,tm.min,tm.sec,div(tv.usec,1000)) + return DateTime(tm.year + 1900, tm.month + 1, tm.mday, tm.hour, tm.min, tm.sec, div(tv.usec, 1000)) end """ @@ -99,7 +99,7 @@ Returns the number of Rata Die days since epoch from the given `Date` or `DateTi datetime2rata(dt::TimeType) = days(dt) # Julian conversions -const JULIANEPOCH = value(DateTime(-4713,11,24,12)) +const JULIANEPOCH = value(DateTime(-4713, 11, 24, 12)) """ julian2datetime(julian_days) -> DateTime @@ -118,4 +118,4 @@ end Takes the given `DateTime` and returns the number of Julian calendar days since the julian epoch `-4713-11-24T12:00:00` as a `Float64`. """ -datetime2julian(dt::DateTime) = (value(dt) - JULIANEPOCH)/86400000.0 +datetime2julian(dt::DateTime) = (value(dt) - JULIANEPOCH) / 86400000.0 diff --git a/base/dates/io.jl b/base/dates/io.jl index 307232589e6d9..a1ba3c4a1f85f 100644 --- a/base/dates/io.jl +++ b/base/dates/io.jl @@ -37,7 +37,6 @@ containing that token. """ function format end - # fallback to tryparsenext/format methods that don't care about locale @inline function tryparsenext(d::AbstractDateToken, str, i, len, locale) tryparsenext(d, str, i, len) @@ -60,7 +59,7 @@ Base.show(io::IO, x::Time) = print(io, string(x)) end # Information for parsing and formatting date time values. -immutable DateFormat{S,T<:Tuple} +immutable DateFormat{S, T<:Tuple} tokens::T locale::DateLocale end @@ -117,7 +116,6 @@ end return ms, ii end - ### Format tokens for (c, fn) in zip("YmdHMS", [year, month, day, hour, minute, second]) @@ -133,7 +131,7 @@ for (tok, fn) in zip("uU", [monthabbr, monthname]) end for (tok, fn) in zip("eE", [dayabbr, dayname]) - @eval function format(io, ::DatePart{$tok}, dt, locale) + @eval function format(io, ::DatePart{$tok}, dt, locale) write(io, $fn(dayofweek(dt), locale)) end end @@ -144,13 +142,13 @@ end # the last n digits of y # will be 0 padded if y has less than n digits - str = dec(y,n) + str = dec(y, n) l = endof(str) if l == n # fast path write(io, str) else - write(io, SubString(str,l-(n-1),l)) + write(io, SubString(str, l - (n - 1), l)) end end @@ -167,17 +165,16 @@ function format(io, d::DatePart{'s'}, dt) end end - ### Delimiters immutable Delim{T, length} <: AbstractDateToken d::T end -Delim(d::Char) = Delim{Char,1}(d) -Delim(d::String) = Delim{String,length(d)}(d) +Delim(d::Char) = Delim{Char, 1}(d) +Delim(d::String) = Delim{String, length(d)}(d) -@inline function tryparsenext{N}(d::Delim{Char,N}, str, i::Int, len) +@inline function tryparsenext{N}(d::Delim{Char, N}, str, i::Int, len) R = Nullable{Int64} for j=1:N i > len && return (R(), i) @@ -187,11 +184,11 @@ Delim(d::String) = Delim{String,length(d)}(d) return R(0), i end -@inline function tryparsenext{N}(d::Delim{String,N}, str, i::Int, len) +@inline function tryparsenext{N}(d::Delim{String, N}, str, i::Int, len) R = Nullable{Int64} - i1=i - i2=start(d.d) - for j=1:N + i1 = i + i2 = start(d.d) + for j = 1:N if i1 > len return R(), i1 end @@ -208,13 +205,13 @@ end write(io, d.d) end -function _show_content{N}(io::IO, d::Delim{Char,N}) +function _show_content{N}(io::IO, d::Delim{Char, N}) if d.d in keys(SLOT_RULE) - for i=1:N + for i = 1:N write(io, '\\', d.d) end else - for i=1:N + for i = 1:N write(io, d.d) end end @@ -237,7 +234,6 @@ end ### DateFormat construction - abstract DayOfWeekToken # special addition to Period types # mapping format specifiers to period types @@ -306,7 +302,7 @@ function DateFormat(f::AbstractString, locale::DateLocale=ENGLISH) letters = String(collect(keys(Base.Dates.SLOT_RULE))) for m in eachmatch(Regex("(? DateTime @@ -393,7 +390,7 @@ the [`DateFormat`](@ref) object. Similar to `DateTime(::AbstractString, ::AbstractString)` but more efficient when repeatedly parsing similarly formatted date strings with a pre-created `DateFormat` object. """ -DateTime(dt::AbstractString,df::DateFormat=ISODateTimeFormat) = parse(DateTime,dt,df) +DateTime(dt::AbstractString, df::DateFormat=ISODateTimeFormat) = parse(DateTime, dt, df) """ Date(dt::AbstractString, format::AbstractString; locale="english") -> Date @@ -402,16 +399,17 @@ Construct a `Date` object by parsing a `dt` date string following the pattern gi `format` string. Follows the same conventions as `DateTime(::AbstractString, ::AbstractString)`. """ -Date(dt::AbstractString,format::AbstractString;locale=ENGLISH) = parse(Date,dt,DateFormat(format,locale)) +Date(dt::AbstractString, format::AbstractString; + locale::Union{DateLocale, String}=ENGLISH) = parse(Date, dt, DateFormat(format, locale)) """ Date(dt::AbstractString, df::DateFormat) -> Date Parse a date from a date string `dt` using a `DateFormat` object `df`. """ -Date(dt::AbstractString,df::DateFormat=ISODateFormat) = parse(Date,dt,df) +Date(dt::AbstractString,df::DateFormat=ISODateFormat) = parse(Date, dt, df) -@generated function format{S,T}(io::IO, dt::TimeType, fmt::DateFormat{S,T}) +@generated function format{S, T}(io::IO, dt::TimeType, fmt::DateFormat{S, T}) N = nfields(T) quote ts = fmt.tokens @@ -424,7 +422,7 @@ function format(dt::TimeType, fmt::DateFormat, bufsize=12) # preallocate to reduce resizing io = IOBuffer(Vector{UInt8}(bufsize), true, true) format(io, dt, fmt) - String(io.data[1:io.ptr-1]) + String(io.data[1:io.ptr - 1]) end @@ -461,7 +459,8 @@ generate the string "1996-01-15T00:00:00" you could use `format`: "yyyy-mm-ddTHH Note that if you need to use a code character as a literal you can use the escape character backslash. The string "1996y01m" can be produced with the format "yyyy\\ymm\\m". """ -format(dt::TimeType,f::AbstractString;locale=ENGLISH) = format(dt,DateFormat(f,locale)) +format(dt::TimeType, f::AbstractString; + locale::Union{DateLocale, String}=ENGLISH) = format(dt, DateFormat(f, locale)) # show @@ -489,26 +488,29 @@ function Base.string(dt::Date) # don't use format - bypassing IOBuffer creation # saves a bit of time here. y,m,d = yearmonthday(value(dt)) - yy = y < 0 ? @sprintf("%05i",y) : lpad(y,4,"0") - mm = lpad(m,2,"0") - dd = lpad(d,2,"0") - "$yy-$mm-$dd" + yy = y < 0 ? @sprintf("%05i", y) : lpad(y, 4, "0") + mm = lpad(m, 2, "0") + dd = lpad(d, 2, "0") + return "$yy-$mm-$dd" end # vectorized -DateTime{T<:AbstractString}(Y::AbstractArray{T},format::AbstractString;locale=ENGLISH) = DateTime(Y,DateFormat(format,locale)) -function DateTime{T<:AbstractString}(Y::AbstractArray{T},df::DateFormat=ISODateTimeFormat) - return reshape(DateTime[parse(DateTime,y,df) for y in Y], size(Y)) +DateTime{T<:AbstractString}(Y::AbstractArray{T}, format::AbstractString; + locale::Union{DateLocale, String}=ENGLISH) = DateTime(Y, DateFormat(format, locale)) +function DateTime{T<:AbstractString}(Y::AbstractArray{T}, df::DateFormat=ISODateTimeFormat) + return reshape(DateTime[parse(DateTime, y, df) for y in Y], size(Y)) end -Date{T<:AbstractString}(Y::AbstractArray{T},format::AbstractString;locale=ENGLISH) = Date(Y,DateFormat(format,locale)) -function Date{T<:AbstractString}(Y::AbstractArray{T},df::DateFormat=ISODateFormat) - return reshape(Date[Date(parse(Date,y,df)) for y in Y], size(Y)) +Date{T<:AbstractString}(Y::AbstractArray{T}, format::AbstractString; + locale::Union{DateLocale, String}=ENGLISH) = Date(Y, DateFormat(format, locale)) +function Date{T<:AbstractString}(Y::AbstractArray{T}, df::DateFormat=ISODateFormat) + return reshape(Date[Date(parse(Date, y, df)) for y in Y], size(Y)) end -format{T<:TimeType}(Y::AbstractArray{T},fmt::AbstractString;locale=ENGLISH) = format(Y,DateFormat(fmt,locale)) -function format(Y::AbstractArray{Date},df::DateFormat=ISODateFormat) - return reshape([format(y,df) for y in Y], size(Y)) +format{T<:TimeType}(Y::AbstractArray{T}, fmt::AbstractString; + locale::Union{DateLocale, String}=ENGLISH) = format(Y, DateFormat(fmt, locale)) +function format(Y::AbstractArray{Date}, df::DateFormat=ISODateFormat) + return reshape([format(y, df) for y in Y], size(Y)) end -function format(Y::AbstractArray{DateTime},df::DateFormat=ISODateTimeFormat) - return reshape([format(y,df) for y in Y], size(Y)) +function format(Y::AbstractArray{DateTime}, df::DateFormat=ISODateTimeFormat) + return reshape([format(y, df) for y in Y], size(Y)) end diff --git a/base/dates/parse.jl b/base/dates/parse.jl index 33856b65a370f..e638319f10b09 100644 --- a/base/dates/parse.jl +++ b/base/dates/parse.jl @@ -1,6 +1,6 @@ ### Parsing utilities -@generated function tryparse_internal{T<:TimeType,S,F}(::Type{T}, str::AbstractString, df::DateFormat{S,F}, raise::Bool=false) +@generated function tryparse_internal{T<:TimeType, S, F}(::Type{T}, str::AbstractString, df::DateFormat{S, F}, raise::Bool=false) token_types = Type[dp <: DatePart ? SLOT_RULE[first(dp.parameters)] : Void for dp in F.parameters] N = length(F.parameters) @@ -140,62 +140,62 @@ function Base.parse(::Type{DateTime}, s::AbstractString, df::typeof(ISODateTimeF dm = dd = Int64(1) th = tm = ts = tms = Int64(0) - nv, i = tryparsenext_base10(s,i,end_pos,1) + nv, i = tryparsenext_base10(s, i, end_pos, 1) dy = isnull(nv) ? (@goto error) : unsafe_get(nv) i > end_pos && @goto error - c, i = next(s,i) + c, i = next(s, i) c != '-' && @goto error i > end_pos && @goto done - nv, i = tryparsenext_base10(s,i,end_pos,1,2) + nv, i = tryparsenext_base10(s, i, end_pos, 1, 2) dm = isnull(nv) ? (@goto error) : unsafe_get(nv) i > end_pos && @goto done - c, i = next(s,i) + c, i = next(s, i) c != '-' && @goto error i > end_pos && @goto done - nv, i = tryparsenext_base10(s,i,end_pos,1,2) + nv, i = tryparsenext_base10(s, i, end_pos, 1, 2) dd = isnull(nv) ? (@goto error) : unsafe_get(nv) i > end_pos && @goto done - c, i = next(s,i) + c, i = next(s, i) c != 'T' && @goto error i > end_pos && @goto done - nv, i = tryparsenext_base10(s,i,end_pos,1,2) + nv, i = tryparsenext_base10(s, i, end_pos, 1, 2) th = isnull(nv) ? (@goto error) : unsafe_get(nv) i > end_pos && @goto done - c, i = next(s,i) + c, i = next(s, i) c != ':' && @goto error i > end_pos && @goto done - nv, i = tryparsenext_base10(s,i,end_pos,1,2) + nv, i = tryparsenext_base10(s, i, end_pos, 1, 2) tm = isnull(nv) ? (@goto error) : unsafe_get(nv) i > end_pos && @goto done - c, i = next(s,i) + c, i = next(s, i) c != ':' && @goto error i > end_pos && @goto done - nv, i = tryparsenext_base10(s,i,end_pos,1,2) + nv, i = tryparsenext_base10(s, i, end_pos, 1, 2) ts = isnull(nv) ? (@goto error) : unsafe_get(nv) i > end_pos && @goto done - c, i = next(s,i) + c, i = next(s, i) c != '.' && @goto error i > end_pos && @goto done - nv, j = tryparsenext_base10(s,i,end_pos,1,3) + nv, j = tryparsenext_base10(s, i, end_pos, 1, 3) tms = isnull(nv) ? (@goto error) : unsafe_get(nv) tms *= 10 ^ (3 - (j - i)) j > end_pos || @goto error @label done - return DateTime(dy,dm,dd,th,tm,ts,tms) + return DateTime(dy, dm, dd, th, tm, ts, tms) @label error throw(ArgumentError("Invalid DateTime string")) diff --git a/base/dates/periods.jl b/base/dates/periods.jl index 07a8c5b9fddb0..c5aa78917003d 100644 --- a/base/dates/periods.jl +++ b/base/dates/periods.jl @@ -12,12 +12,12 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond # Convenience method for show() @eval _units(x::$period) = " " * $accessor_str * (abs(value(x)) == 1 ? "" : "s") # periodisless - @eval periodisless(x::$period,y::$period) = value(x) < value(y) + @eval periodisless(x::$period, y::$period) = value(x) < value(y) # AbstractString parsing (mainly for IO code) - @eval $period(x::AbstractString) = $period(Base.parse(Int64,x)) + @eval $period(x::AbstractString) = $period(Base.parse(Int64, x)) # Period accessors typs = period in (:Microsecond, :Nanosecond) ? ["Time"] : - period in (:Hour, :Minute, :Second, :Millisecond) ? ["Time", "DateTime"] : ["Date","DateTime"] + period in (:Hour, :Minute, :Second, :Millisecond) ? ["Time", "DateTime"] : ["Date", "DateTime"] reference = period == :Week ? " For details see [`$accessor_str(::Union{Date, DateTime})`](@ref)." : "" for typ_str in typs @eval begin @@ -39,8 +39,8 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond end #Print/show/traits -Base.string{P<:Period}(x::P) = string(value(x),_units(x)) -Base.show(io::IO,x::Period) = print(io,string(x)) +Base.string{P<:Period}(x::P) = string(value(x), _units(x)) +Base.show(io::IO,x::Period) = print(io, string(x)) Base.zero{P<:Period}(::Union{Type{P},P}) = P(0) Base.one{P<:Period}(::Union{Type{P},P}) = 1 # see #16116 Base.typemin{P<:Period}(::Type{P}) = P(typemin(Int64)) @@ -59,38 +59,38 @@ default{T<:DatePeriod}(p::Union{T,Type{T}}) = T(1) default{T<:TimePeriod}(p::Union{T,Type{T}}) = T(0) (-){P<:Period}(x::P) = P(-value(x)) -Base.isless{P<:Period}(x::P,y::P) = isless(value(x),value(y)) -=={P<:Period}(x::P,y::P) = value(x) == value(y) +Base.isless{P<:Period}(x::P, y::P) = isless(value(x), value(y)) +=={P<:Period}(x::P, y::P) = value(x) == value(y) # Period Arithmetic, grouped by dimensionality: import Base: div, fld, mod, rem, gcd, lcm, +, -, *, /, % -for op in (:+,:-,:lcm,:gcd) - @eval ($op){P<:Period}(x::P,y::P) = P(($op)(value(x),value(y))) +for op in (:+, :-, :lcm, :gcd) + @eval ($op){P<:Period}(x::P, y::P) = P(($op)(value(x), value(y))) end -for op in (:/,:div,:fld) +for op in (:/, :div, :fld) @eval begin - ($op){P<:Period}(x::P,y::P) = ($op)(value(x),value(y)) - ($op){P<:Period}(x::P,y::Real) = P(($op)(value(x),Int64(y))) + ($op){P<:Period}(x::P, y::P) = ($op)(value(x), value(y)) + ($op){P<:Period}(x::P, y::Real) = P(($op)(value(x), Int64(y))) end end -for op in (:rem,:mod) +for op in (:rem, :mod) @eval begin - ($op){P<:Period}(x::P,y::P) = P(($op)(value(x),value(y))) - ($op){P<:Period}(x::P,y::Real) = P(($op)(value(x),Int64(y))) + ($op){P<:Period}(x::P, y::P) = P(($op)(value(x), value(y))) + ($op){P<:Period}(x::P, y::Real) = P(($op)(value(x), Int64(y))) end end -*{P<:Period}(x::P,y::Real) = P(value(x) * Int64(y)) -*(y::Real,x::Period) = x * y -for (op,Ty,Tz) in ((:*,Real,:P), - (:/,:P,Float64), (:/,Real,:P)) +*{P<:Period}(x::P, y::Real) = P(value(x) * Int64(y)) +*(y::Real, x::Period) = x * y +for (op, Ty, Tz) in ((:*, Real, :P), + (:/, :P, Float64), (:/, Real, :P)) @eval begin - function ($op){P<:Period}(X::StridedArray{P},y::$Ty) + function ($op){P<:Period}(X::StridedArray{P}, y::$Ty) Z = similar(X, $Tz) for (Idst, Isrc) in zip(eachindex(Z), eachindex(X)) - @inbounds Z[Idst] = ($op)(X[Isrc],y) + @inbounds Z[Idst] = ($op)(X[Isrc], y) end return Z end @@ -98,7 +98,7 @@ for (op,Ty,Tz) in ((:*,Real,:P), end # intfuncs -Base.gcdx{T<:Period}(a::T,b::T) = ((g,x,y)=gcdx(value(a),value(b)); return T(g),x,y) +Base.gcdx{T<:Period}(a::T, b::T) = ((g, x, y) = gcdx(value(a), value(b)); return T(g), x, y) Base.abs{T<:Period}(a::T) = T(abs(value(a))) periodisless(::Period,::Year) = true @@ -134,15 +134,15 @@ periodisless(::Nanosecond,::Microsecond) = true periodisless(::Period,::Nanosecond) = false # return (next coarser period, conversion factor): -coarserperiod{P<:Period}(::Type{P}) = (P,1) -coarserperiod(::Type{Nanosecond}) = (Microsecond,1000) -coarserperiod(::Type{Microsecond}) = (Millisecond,1000) -coarserperiod(::Type{Millisecond}) = (Second,1000) -coarserperiod(::Type{Second}) = (Minute,60) -coarserperiod(::Type{Minute}) = (Hour,60) -coarserperiod(::Type{Hour}) = (Day,24) -coarserperiod(::Type{Day}) = (Week,7) -coarserperiod(::Type{Month}) = (Year,12) +coarserperiod{P<:Period}(::Type{P}) = (P, 1) +coarserperiod(::Type{Nanosecond}) = (Microsecond, 1000) +coarserperiod(::Type{Microsecond}) = (Millisecond, 1000) +coarserperiod(::Type{Millisecond}) = (Second, 1000) +coarserperiod(::Type{Second}) = (Minute, 60) +coarserperiod(::Type{Minute}) = (Hour, 60) +coarserperiod(::Type{Hour}) = (Day, 24) +coarserperiod(::Type{Day}) = (Week, 7) +coarserperiod(::Type{Month}) = (Year, 12) # Stores multiple periods in greatest to least order by type, not values, # canonicalized to eliminate zero periods, merge equal period types, @@ -157,7 +157,7 @@ generated by addition of different period types, e.g. `Year(1) + Day(1)` produce `CompoundPeriod` result. """ type CompoundPeriod <: AbstractTime - periods::Array{Period,1} + periods::Array{Period, 1} function CompoundPeriod(p::Vector{Period}) n = length(p) if n > 1 @@ -165,7 +165,7 @@ type CompoundPeriod <: AbstractTime # canonicalize p by merging equal period types and removing zeros i = j = 1 while j <= n - k = j+1 + k = j + 1 while k <= n if typeof(p[j]) == typeof(p[k]) p[j] += p[k] @@ -338,29 +338,29 @@ function Base.string(x::CompoundPeriod) return s[3:end] end end -Base.show(io::IO,x::CompoundPeriod) = print(io,string(x)) +Base.show(io::IO,x::CompoundPeriod) = print(io, string(x)) # E.g. Year(1) + Day(1) -(+)(x::Period,y::Period) = CompoundPeriod(Period[x,y]) -(+)(x::CompoundPeriod,y::Period) = CompoundPeriod(vcat(x.periods,y)) -(+)(y::Period,x::CompoundPeriod) = x + y -(+)(x::CompoundPeriod,y::CompoundPeriod) = CompoundPeriod(vcat(x.periods,y.periods)) +(+)(x::Period,y::Period) = CompoundPeriod(Period[x, y]) +(+)(x::CompoundPeriod, y::Period) = CompoundPeriod(vcat(x.periods, y)) +(+)(y::Period, x::CompoundPeriod) = x + y +(+)(x::CompoundPeriod, y::CompoundPeriod) = CompoundPeriod(vcat(x.periods, y.periods)) # E.g. Year(1) - Month(1) -(-)(x::Period,y::Period) = CompoundPeriod(Period[x,-y]) -(-)(x::CompoundPeriod,y::Period) = CompoundPeriod(vcat(x.periods,-y)) +(-)(x::Period, y::Period) = CompoundPeriod(Period[x, -y]) +(-)(x::CompoundPeriod, y::Period) = CompoundPeriod(vcat(x.periods, -y)) (-)(x::CompoundPeriod) = CompoundPeriod(-x.periods) -(-)(y::Union{Period,CompoundPeriod},x::CompoundPeriod) = (-x) + y +(-)(y::Union{Period, CompoundPeriod}, x::CompoundPeriod) = (-x) + y -GeneralPeriod = Union{Period,CompoundPeriod} +GeneralPeriod = Union{Period, CompoundPeriod} (+)(x::GeneralPeriod) = x (+){P<:GeneralPeriod}(x::StridedArray{P}) = x for op in (:+, :-) @eval begin - ($op){P<:GeneralPeriod}(x::GeneralPeriod,Y::StridedArray{P}) = broadcast($op,x,Y) - ($op){P<:GeneralPeriod}(Y::StridedArray{P},x::GeneralPeriod) = broadcast($op,Y,x) + ($op){P<:GeneralPeriod}(x::GeneralPeriod, Y::StridedArray{P}) = broadcast($op, x, Y) + ($op){P<:GeneralPeriod}(Y::StridedArray{P}, x::GeneralPeriod) = broadcast($op, Y, x) ($op){P<:GeneralPeriod, Q<:GeneralPeriod}(X::StridedArray{P}, Y::StridedArray{Q}) = - reshape(CompoundPeriod[($op)(x,y) for (x,y) in zip(X, Y)], promote_shape(size(X),size(Y))) + reshape(CompoundPeriod[($op)(x, y) for (x, y) in zip(X, Y)], promote_shape(size(X), size(Y))) end end @@ -373,99 +373,99 @@ Base.isequal(x::Period, y::CompoundPeriod) = isequal(y, x) Base.isequal(x::CompoundPeriod, y::CompoundPeriod) = x.periods == y.periods # Capture TimeType+-Period methods -(+)(a::TimeType,b::Period,c::Period) = (+)(a,b+c) -(-)(a::TimeType,b::Period,c::Period) = (-)(a,b-c) -(+)(a::TimeType,b::Period,c::Period,d::Period...) = (+)((+)(a,b+c),d...) -(-)(a::TimeType,b::Period,c::Period,d::Period...) = (-)((-)(a,b-c),d...) +(+)(a::TimeType, b::Period, c::Period) = (+)(a, b + c) +(-)(a::TimeType, b::Period, c::Period) = (-)(a, b - c) +(+)(a::TimeType, b::Period, c::Period, d::Period...) = (+)((+)(a, b + c), d...) +(-)(a::TimeType, b::Period, c::Period, d::Period...) = (-)((-)(a, b - c), d...) -function (+)(x::TimeType,y::CompoundPeriod) +function (+)(x::TimeType, y::CompoundPeriod) for p in y.periods x += p end return x end -(+)(x::CompoundPeriod,y::TimeType) = y + x +(+)(x::CompoundPeriod, y::TimeType) = y + x -function (-)(x::TimeType,y::CompoundPeriod) +function (-)(x::TimeType, y::CompoundPeriod) for p in y.periods x -= p end return x end -(-)(x::CompoundPeriod,y::TimeType) = y - x +(-)(x::CompoundPeriod, y::TimeType) = y - x # Fixed-value Periods (periods corresponding to a well-defined time interval, # as opposed to variable calendar intervals like Year). -typealias FixedPeriod Union{Week,Day,Hour,Minute,Second,Millisecond,Microsecond,Nanosecond} +typealias FixedPeriod Union{Week, Day, Hour, Minute, Second, Millisecond, Microsecond, Nanosecond} # like div but throw an error if remainder is nonzero -function divexact(x,y) - q,r = divrem(x, y) +function divexact(x, y) + q, r = divrem(x, y) r == 0 || throw(InexactError()) return q end # FixedPeriod conversions and promotion rules -const fixedperiod_conversions = [(Week,7),(Day,24),(Hour,60),(Minute,60),(Second,1000),(Millisecond,1000),(Microsecond,1000),(Nanosecond,1)] +const fixedperiod_conversions = [(Week, 7), (Day, 24), (Hour, 60), (Minute, 60), (Second, 1000), (Millisecond, 1000), (Microsecond, 1000), (Nanosecond, 1)] for i = 1:length(fixedperiod_conversions) - (T,n) = fixedperiod_conversions[i] + T, n = fixedperiod_conversions[i] N = Int64(1) - for j = i-1:-1:1 # less-precise periods - (Tc,nc) = fixedperiod_conversions[j] + for j = (i - 1):-1:1 # less-precise periods + Tc, nc = fixedperiod_conversions[j] N *= nc vmax = typemax(Int64) ÷ N vmin = typemin(Int64) ÷ N @eval function Base.convert(::Type{$T}, x::$Tc) $vmin ≤ value(x) ≤ $vmax || throw(InexactError()) - return $T(value(x)*$N) + return $T(value(x) * $N) end end N = n - for j = i+1:length(fixedperiod_conversions) # more-precise periods - (Tc,nc) = fixedperiod_conversions[j] + for j = (i + 1):length(fixedperiod_conversions) # more-precise periods + Tc, nc = fixedperiod_conversions[j] @eval Base.convert(::Type{$T}, x::$Tc) = $T(divexact(value(x), $N)) - @eval Base.promote_rule(::Type{$T},::Type{$Tc}) = $Tc + @eval Base.promote_rule(::Type{$T}, ::Type{$Tc}) = $Tc N *= nc end end # have to declare thusly so that diagonal dispatch above takes precedence: -(==){T<:FixedPeriod,S<:FixedPeriod}(x::T,y::S) = (==)(promote(x,y)...) -Base.isless{T<:FixedPeriod,S<:FixedPeriod}(x::T,y::S) = isless(promote(x,y)...) +(==){T<:FixedPeriod, S<:FixedPeriod}(x::T, y::S) = (==)(promote(x, y)...) +Base.isless{T<:FixedPeriod, S<:FixedPeriod}(x::T, y::S) = isless(promote(x, y)...) # other periods with fixed conversions but which aren't fixed time periods -typealias OtherPeriod Union{Month,Year} +typealias OtherPeriod Union{Month, Year} let vmax = typemax(Int64) ÷ 12, vmin = typemin(Int64) ÷ 12 @eval function Base.convert(::Type{Month}, x::Year) $vmin ≤ value(x) ≤ $vmax || throw(InexactError()) - Month(value(x)*12) + Month(value(x) * 12) end end -Base.convert(::Type{Year}, x::Month) = Year(divexact(value(x),12)) +Base.convert(::Type{Year}, x::Month) = Year(divexact(value(x), 12)) Base.promote_rule(::Type{Year}, ::Type{Month}) = Month -(==){T<:OtherPeriod,S<:OtherPeriod}(x::T,y::S) = (==)(promote(x,y)...) -Base.isless{T<:OtherPeriod,S<:OtherPeriod}(x::T,y::S) = isless(promote(x,y)...) +(==){T<:OtherPeriod, S<:OtherPeriod}(x::T, y::S) = (==)(promote(x, y)...) +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) -toms(c::Hour) = 3600000*value(c) -toms(c::Day) = 86400000*value(c) -toms(c::Week) = 604800000*value(c) -toms(c::Month) = 86400000.0*30.436875*value(c) -toms(c::Year) = 86400000.0*365.2425*value(c) -toms(c::CompoundPeriod) = isempty(c.periods)? 0.0 : Float64(sum(toms, c.periods)) +toms(c::Second) = 1000 * value(c) +toms(c::Minute) = 60000 * value(c) +toms(c::Hour) = 3600000 * value(c) +toms(c::Day) = 86400000 * value(c) +toms(c::Week) = 604800000 * value(c) +toms(c::Month) = 86400000.0 * 30.436875 * value(c) +toms(c::Year) = 86400000.0 * 365.2425 * value(c) +toms(c::CompoundPeriod) = isempty(c.periods) ? 0.0 : Float64(sum(toms, c.periods)) tons(x) = toms(x) * 1000000 tons(x::Microsecond) = value(x) * 1000 tons(x::Nanosecond) = value(x) -days(c::Millisecond) = div(value(c),86400000) -days(c::Second) = div(value(c),86400) -days(c::Minute) = div(value(c),1440) -days(c::Hour) = div(value(c),24) +days(c::Millisecond) = div(value(c), 86400000) +days(c::Second) = div(value(c), 86400) +days(c::Minute) = div(value(c), 1440) +days(c::Hour) = div(value(c), 24) days(c::Day) = value(c) -days(c::Week) = 7*value(c) -days(c::Year) = 365.2425*value(c) -days(c::Month) = 30.436875*value(c) -days(c::CompoundPeriod) = isempty(c.periods)?0.0 : Float64(sum(days,c.periods)) +days(c::Week) = 7 * value(c) +days(c::Year) = 365.2425 * value(c) +days(c::Month) = 30.436875 * value(c) +days(c::CompoundPeriod) = isempty(c.periods) ? 0.0 : Float64(sum(days, c.periods)) diff --git a/base/dates/query.jl b/base/dates/query.jl index 10c513ede72f2..64ae360a7b275 100644 --- a/base/dates/query.jl +++ b/base/dates/query.jl @@ -83,7 +83,7 @@ end ### Core query functions # Monday = 1....Sunday = 7 -dayofweek(days) = mod1(days,7) +dayofweek(days) = mod1(days, 7) # Number of days in year """ @@ -94,8 +94,8 @@ Returns 366 if the year of `dt` is a leap year, otherwise returns 365. daysinyear(y) = 365 + isleapyear(y) # Day of the year -const MONTHDAYS = [0,31,59,90,120,151,181,212,243,273,304,334] -dayofyear(y,m,d) = MONTHDAYS[m] + d + (m > 2 && isleapyear(y)) +const MONTHDAYS = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334) +dayofyear(y, m, d) = MONTHDAYS[m] + d + (m > 2 && isleapyear(y)) ### Days of the Week """ @@ -105,8 +105,8 @@ Returns the day of the week as an `Int64` with `1 = Monday, 2 = Tuesday, etc.`. """ dayofweek(dt::TimeType) = dayofweek(days(dt)) -const Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday = 1,2,3,4,5,6,7 -const Mon,Tue,Wed,Thu,Fri,Sat,Sun = 1,2,3,4,5,6,7 +const Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = 1, 2, 3, 4, 5, 6, 7 +const Mon, Tue, Wed, Thu, Fri, Sat, Sun = 1, 2, 3, 4, 5, 6, 7 dayname(day::Integer, locale::DateLocale) = locale.days_of_week[day] dayabbr(day::Integer, locale::DateLocale) = locale.days_of_week_abbr[day] @@ -157,9 +157,9 @@ end # Total number of a day of week in the month # e.g. are there 4 or 5 Mondays in this month? -const TWENTYNINE = IntSet([1,8,15,22,29]) -const THIRTY = IntSet([1,2,8,9,15,16,22,23,29,30]) -const THIRTYONE = IntSet([1,2,3,8,9,10,15,16,17,22,23,24,29,30,31]) +const TWENTYNINE = IntSet([1, 8, 15, 22, 29]) +const THIRTY = IntSet([1, 2, 8, 9, 15, 16, 22, 23, 29, 30]) +const THIRTYONE = IntSet([1, 2, 3, 8, 9, 10, 15, 16, 17, 22, 23, 24, 29, 30, 31]) """ daysofweekinmonth(dt::TimeType) -> Int @@ -170,17 +170,17 @@ in a month by including `dayofweekofmonth(dt) == daysofweekinmonth(dt)` in the a function. """ function daysofweekinmonth(dt::TimeType) - y,m,d = yearmonthday(dt) - ld = daysinmonth(y,m) + y, m, d = yearmonthday(dt) + ld = daysinmonth(y, m) return ld == 28 ? 4 : ld == 29 ? ((d in TWENTYNINE) ? 5 : 4) : ld == 30 ? ((d in THIRTY) ? 5 : 4) : (d in THIRTYONE) ? 5 : 4 end ### Months -const January,February,March,April,May,June = 1,2,3,4,5,6 -const July,August,September,October,November,December = 7,8,9,10,11,12 -const Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec = 1,2,3,4,5,6,7,8,9,10,11,12 +const January, February, March, April, May, June = 1, 2, 3, 4, 5, 6 +const July, August, September, October, November, December = 7, 8, 9, 10, 11, 12 +const Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 monthname(month::Integer, locale::DateLocale) = locale.months[month] monthabbr(month::Integer, locale::DateLocale) = locale.months_abbr[month] @@ -210,7 +210,7 @@ end Returns the number of days in the month of `dt`. Value will be 28, 29, 30, or 31. """ -daysinmonth(dt::TimeType) = ((y,m) = yearmonth(dt); return daysinmonth(y,m)) +daysinmonth(dt::TimeType) = ((y, m) = yearmonth(dt); return daysinmonth(y, m)) ### Years """ @@ -225,7 +225,7 @@ isleapyear(dt::TimeType) = isleapyear(year(dt)) Returns the day of the year for `dt` with January 1st being day 1. """ -dayofyear(dt::TimeType) = ((y,m,d) = yearmonthday(dt); return dayofyear(y,m,d)) +dayofyear(dt::TimeType) = ((y, m, d) = yearmonthday(dt); return dayofyear(y, m, d)) daysinyear(dt::TimeType) = 365 + isleapyear(dt) @@ -239,7 +239,7 @@ function quarterofyear(dt::TimeType) m = month(dt) return m < 4 ? 1 : m < 7 ? 2 : m < 10 ? 3 : 4 end -const QUARTERDAYS = (0,90,181,273) +const QUARTERDAYS = (0, 90, 181, 273) """ dayofquarter(dt::TimeType) -> Int diff --git a/base/dates/types.jl b/base/dates/types.jl index db85b10cdd7ad..e21ea2ce252f2 100644 --- a/base/dates/types.jl +++ b/base/dates/types.jl @@ -21,13 +21,13 @@ abstract Period <: AbstractTime abstract DatePeriod <: Period abstract TimePeriod <: Period -for T in (:Year,:Month,:Week,:Day) +for T in (:Year, :Month, :Week, :Day) @eval immutable $T <: DatePeriod value::Int64 $T(v::Number) = new(v) end end -for T in (:Hour,:Minute,:Second,:Millisecond,:Microsecond,:Nanosecond) +for T in (:Hour, :Minute, :Second, :Millisecond, :Microsecond, :Nanosecond) @eval immutable $T <: TimePeriod value::Int64 $T(v::Number) = new(v) @@ -128,20 +128,20 @@ end # Convert y,m,d to # of Rata Die days # Works by shifting the beginning of the year to March 1, # so a leap day is the very last day of the year -const SHIFTEDMONTHDAYS = [306,337,0,31,61,92,122,153,184,214,245,275] -function totaldays(y,m,d) +const SHIFTEDMONTHDAYS = (306, 337, 0, 31, 61, 92, 122, 153, 184, 214, 245, 275) +function totaldays(y, m, d) # If we're in Jan/Feb, shift the given year back one z = m < 3 ? y - 1 : y mdays = SHIFTEDMONTHDAYS[m] # days + month_days + year_days - return d + mdays + 365z + fld(z,4) - fld(z,100) + fld(z,400) - 306 + return d + mdays + 365z + fld(z, 4) - fld(z, 100) + fld(z, 400) - 306 end # If the year is divisible by 4, except for every 100 years, except for every 400 years isleapyear(y) = ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0) # Number of days in month -const DAYSINMONTH = [31,28,31,30,31,30,31,31,30,31,30,31] +const DAYSINMONTH = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) daysinmonth(y,m) = DAYSINMONTH[m] + (m == 2 && isleapyear(y)) ### CONSTRUCTORS ### @@ -151,15 +151,15 @@ daysinmonth(y,m) = DAYSINMONTH[m] + (m == 2 && isleapyear(y)) Construct a `DateTime` type by parts. Arguments must be convertible to `Int64`. """ -function DateTime(y::Int64,m::Int64=1,d::Int64=1, - h::Int64=0,mi::Int64=0,s::Int64=0,ms::Int64=0) +function DateTime(y::Int64, m::Int64=1, d::Int64=1, + h::Int64=0, mi::Int64=0, s::Int64=0, ms::Int64=0) 0 < m < 13 || throw(ArgumentError("Month: $m out of range (1:12)")) - 0 < d < daysinmonth(y,m)+1 || throw(ArgumentError("Day: $d out of range (1:$(daysinmonth(y,m)))")) + 0 < d < daysinmonth(y, m) + 1 || throw(ArgumentError("Day: $d out of range (1:$(daysinmonth(y, m)))")) -1 < h < 24 || throw(ArgumentError("Hour: $h out of range (0:23)")) -1 < mi < 60 || throw(ArgumentError("Minute: $mi 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)")) - rata = ms + 1000*(s + 60mi + 3600h + 86400*totaldays(y,m,d)) + rata = ms + 1000 * (s + 60mi + 3600h + 86400 * totaldays(y, m, d)) return DateTime(UTM(rata)) end @@ -168,10 +168,10 @@ end Construct a `Date` type by parts. Arguments must be convertible to `Int64`. """ -function Date(y::Int64,m::Int64=1,d::Int64=1) +function Date(y::Int64, m::Int64=1, d::Int64=1) 0 < m < 13 || throw(ArgumentError("Month: $m out of range (1:12)")) - 0 < d < daysinmonth(y,m)+1 || throw(ArgumentError("Day: $d out of range (1:$(daysinmonth(y,m)))")) - return Date(UTD(totaldays(y,m,d))) + 0 < d < daysinmonth(y, m) + 1 || throw(ArgumentError("Day: $d out of range (1:$(daysinmonth(y, m)))")) + return Date(UTD(totaldays(y, m, d))) end """ @@ -225,7 +225,7 @@ function DateTime(periods::Period...) isa(p, Second) && (s = p::Second) isa(p, Millisecond) && (ms = p::Millisecond) end - return DateTime(y,m,d,h,mi,s,ms) + return DateTime(y, m, d, h, mi, s, ms) end """ @@ -241,7 +241,7 @@ function Date(periods::Period...) isa(p, Month) && (m = p::Month) isa(p, Day) && (d = p::Day) end - return Date(y,m,d) + return Date(y, m, d) end """ @@ -265,12 +265,12 @@ function Time(periods::TimePeriod...) end # Fallback constructors -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)) -Date(y,m=1,d=1) = Date(Int64(y), Int64(m), Int64(d)) -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)) +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)) +Date(y, m=1, d=1) = Date(Int64(y), Int64(m), Int64(d)) +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)) # Traits, Equality -Base.isfinite{T<:TimeType}(::Union{Type{T},T}) = true +Base.isfinite{T<:TimeType}(::Union{Type{T}, T}) = true calendar(dt::DateTime) = ISOCalendar calendar(dt::Date) = ISOCalendar @@ -297,9 +297,9 @@ Base.typemin(::Union{Time, Type{Time}}) = Time(0) Base.eltype{T<:Period}(::Type{T}) = T Base.promote_rule(::Type{Date}, x::Type{DateTime}) = DateTime Base.isless{T<:TimeType}(x::T, y::T) = isless(value(x), value(y)) -Base.isless(x::TimeType,y::TimeType) = isless(Base.promote_noncircular(x,y)...) +Base.isless(x::TimeType, y::TimeType) = isless(Base.promote_noncircular(x, y)...) =={T<:TimeType}(x::T, y::T) = ==(value(x), value(y)) -function ==(a::Time,b::Time) +function ==(a::Time, b::Time) return hour(a) == hour(b) && minute(a) == minute(b) && second(a) == second(b) && millisecond(a) == millisecond(b) && microsecond(a) == microsecond(b) && nanosecond(a) == nanosecond(b) diff --git a/test/dates/accessors.jl b/test/dates/accessors.jl index e61d7fa38571c..3ce9048f3b1b3 100644 --- a/test/dates/accessors.jl +++ b/test/dates/accessors.jl @@ -2,24 +2,24 @@ # yearmonthday is the opposite of totaldays # taking Rata Die Day # and returning proleptic Gregorian date -@test Dates.yearmonthday(-306) == (0,2,29) -@test Dates.yearmonth(-306) == (0,2) -@test Dates.monthday(-306) == (2,29) -@test Dates.yearmonthday(-305) == (0,3,1) -@test Dates.yearmonth(-305) == (0,3) -@test Dates.monthday(-305) == (3,1) -@test Dates.yearmonthday(-2) == (0,12,29) -@test Dates.yearmonth(-2) == (0,12) -@test Dates.monthday(-2) == (12,29) -@test Dates.yearmonthday(-1) == (0,12,30) -@test Dates.yearmonth(-1) == (0,12) -@test Dates.monthday(-1) == (12,30) -@test Dates.yearmonthday(0) == (0,12,31) -@test Dates.yearmonth(-0) == (0,12) -@test Dates.monthday(-0) == (12,31) -@test Dates.yearmonthday(1) == (1,1,1) -@test Dates.yearmonth(1) == (1,1) -@test Dates.monthday(1) == (1,1) +@test Dates.yearmonthday(-306) == (0, 2, 29) +@test Dates.yearmonth(-306) == (0, 2) +@test Dates.monthday(-306) == (2, 29) +@test Dates.yearmonthday(-305) == (0, 3, 1) +@test Dates.yearmonth(-305) == (0, 3) +@test Dates.monthday(-305) == (3, 1) +@test Dates.yearmonthday(-2) == (0, 12, 29) +@test Dates.yearmonth(-2) == (0, 12) +@test Dates.monthday(-2) == (12, 29) +@test Dates.yearmonthday(-1) == (0, 12, 30) +@test Dates.yearmonth(-1) == (0, 12) +@test Dates.monthday(-1) == (12, 30) +@test Dates.yearmonthday(0) == (0, 12, 31) +@test Dates.yearmonth(-0) == (0, 12) +@test Dates.monthday(-0) == (12, 31) +@test Dates.yearmonthday(1) == (1, 1, 1) +@test Dates.yearmonth(1) == (1, 1) +@test Dates.monthday(1) == (1, 1) # year, month, and day return the indivial components # of yearmonthday, avoiding additional calculations when possible @test Dates.year(-1) == 0 @@ -31,23 +31,23 @@ @test Dates.year(1) == 1 @test Dates.month(1) == 1 @test Dates.day(1) == 1 -@test Dates.yearmonthday(730120) == (2000,1,1) +@test Dates.yearmonthday(730120) == (2000, 1, 1) @test Dates.year(730120) == 2000 @test Dates.month(730120) == 1 @test Dates.day(730120) == 1 # Test totaldays and yearmonthday from January 1st of "from" to December 31st of "to" -# test_dates(-10000,10000) takes about 15 seconds -# test_dates(year(typemin(Date)),year(typemax(Date))) is full range +# test_dates(-10000, 10000) takes about 15 seconds +# test_dates(year(typemin(Date)), year(typemax(Date))) is full range # and would take.......a really long time let from=0, to=2100, y=0, m=0, d=0 - test_day = Dates.totaldays(from,1,1) + test_day = Dates.totaldays(from, 1, 1) for y in from:to for m = 1:12 - for d = 1:Dates.daysinmonth(y,m) - days = Dates.totaldays(y,m,d) + for d = 1:Dates.daysinmonth(y, m) + days = Dates.totaldays(y, m, d) @test days == test_day - @test (y,m,d) == Dates.yearmonthday(days) + @test (y, m, d) == Dates.yearmonthday(days) test_day += 1 end end @@ -57,17 +57,17 @@ end # Test year, month, day, hour, minute let y=0, m=0, d=0, h=0, mi=0 for m = 1:12 - for d = 1:Dates.daysinmonth(y,m) + for d = 1:Dates.daysinmonth(y, m) for h = 0:23 for mi = 0:59 - dt = Dates.DateTime(y,m,d,h,mi) + dt = Dates.DateTime(y, m, d, h, mi) @test y == Dates.year(dt) @test m == Dates.month(dt) @test d == Dates.day(dt) @test d == Dates.dayofmonth(dt) @test h == Dates.hour(dt) @test mi == Dates.minute(dt) - @test (m,d) == Dates.monthday(dt) + @test (m, d) == Dates.monthday(dt) end end end @@ -76,13 +76,13 @@ end # Test second, millisecond let y=0, m=0, d=0, h=0, mi=0, s=0, ms=0 - for y in [-2013,-1,0,1,2013] - for m in [1,6,12] - for d in [1,15,Dates.daysinmonth(y,m)] - for h in [0,12,23] + for y in [-2013, -1, 0, 1, 2013] + for m in [1, 6, 12] + for d in [1, 15, Dates.daysinmonth(y, m)] + for h in [0, 12, 23] for s = 0:59 - for ms in [0,1,500,999] - dt = Dates.DateTime(y,m,d,h,mi,s,ms) + for ms in [0, 1, 500, 999] + dt = Dates.DateTime(y, m, d, h, mi, s, ms) @test y == Dates.year(dt) @test m == Dates.month(dt) @test d == Dates.day(dt) @@ -100,8 +100,8 @@ end let from=0, to=2100, y=0, m=0, d=0 for y in from:to for m = 1:12 - for d = 1:Dates.daysinmonth(y,m) - dt = Dates.Date(y,m,d) + for d = 1:Dates.daysinmonth(y, m) + dt = Dates.Date(y, m, d) @test y == Dates.year(dt) @test m == Dates.month(dt) @test d == Dates.day(dt) @@ -112,8 +112,8 @@ end # test hour, minute, second let h=0, mi=0, s=0, ms=0, us=0, ns=0 - for h = (0,23), mi = (0,59), s = (0,59), - ms in (0,1,500,999), us in (0,1,500,999), ns in (0,1,500,999) + for h = (0, 23), mi = (0, 59), s = (0, 59), + ms in (0, 1, 500, 999), us in (0, 1, 500, 999), ns in (0, 1, 500, 999) t = Dates.Time(h, mi, s, ms, us, ns) @test h == Dates.hour(t) @test mi == Dates.minute(t) @@ -126,26 +126,26 @@ end # week function # Tests from https://en.wikipedia.org/wiki/ISO_week_date -@test Dates.week(Dates.Date(2005,1,1)) == 53 -@test Dates.week(Dates.Date(2005,1,2)) == 53 -@test Dates.week(Dates.Date(2005,12,31)) == 52 -@test Dates.week(Dates.Date(2007,1,1)) == 1 -@test Dates.week(Dates.Date(2007,12,30)) == 52 -@test Dates.week(Dates.Date(2007,12,31)) == 1 -@test Dates.week(Dates.Date(2008,1,1)) == 1 -@test Dates.week(Dates.Date(2008,12,28)) == 52 -@test Dates.week(Dates.Date(2008,12,29)) == 1 -@test Dates.week(Dates.Date(2008,12,30)) == 1 -@test Dates.week(Dates.Date(2008,12,31)) == 1 -@test Dates.week(Dates.Date(2009,1,1)) == 1 -@test Dates.week(Dates.Date(2009,12,31)) == 53 -@test Dates.week(Dates.Date(2010,1,1)) == 53 -@test Dates.week(Dates.Date(2010,1,2)) == 53 -@test Dates.week(Dates.Date(2010,1,2)) == 53 +@test Dates.week(Dates.Date(2005, 1, 1)) == 53 +@test Dates.week(Dates.Date(2005, 1, 2)) == 53 +@test Dates.week(Dates.Date(2005, 12, 31)) == 52 +@test Dates.week(Dates.Date(2007, 1, 1)) == 1 +@test Dates.week(Dates.Date(2007, 12, 30)) == 52 +@test Dates.week(Dates.Date(2007, 12, 31)) == 1 +@test Dates.week(Dates.Date(2008, 1, 1)) == 1 +@test Dates.week(Dates.Date(2008, 12, 28)) == 52 +@test Dates.week(Dates.Date(2008, 12, 29)) == 1 +@test Dates.week(Dates.Date(2008, 12, 30)) == 1 +@test Dates.week(Dates.Date(2008, 12, 31)) == 1 +@test Dates.week(Dates.Date(2009, 1, 1)) == 1 +@test Dates.week(Dates.Date(2009, 12, 31)) == 53 +@test Dates.week(Dates.Date(2010, 1, 1)) == 53 +@test Dates.week(Dates.Date(2010, 1, 2)) == 53 +@test Dates.week(Dates.Date(2010, 1, 2)) == 53 # Tests from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=1999 -dt = Dates.DateTime(1999,12,27) -dt1 = Dates.Date(1999,12,27) -check = (52,52,52,52,52,52,52,1,1,1,1,1,1,1,2,2,2,2,2,2,2) +dt = Dates.DateTime(1999, 12, 27) +dt1 = Dates.Date(1999, 12, 27) +check = (52, 52, 52, 52, 52, 52, 52, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2) for i = 1:21 @test Dates.week(dt) == check[i] @test Dates.week(dt1) == check[i] @@ -153,8 +153,8 @@ for i = 1:21 dt1 = dt1 + Dates.Day(1) end # Tests from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2000 -dt = Dates.DateTime(2000,12,25) -dt1 = Dates.Date(2000,12,25) +dt = Dates.DateTime(2000, 12, 25) +dt1 = Dates.Date(2000, 12, 25) for i = 1:21 @test Dates.week(dt) == check[i] @test Dates.week(dt1) == check[i] @@ -162,8 +162,8 @@ for i = 1:21 dt1 = dt1 + Dates.Day(1) end # Test from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2030 -dt = Dates.DateTime(2030,12,23) -dt1 = Dates.Date(2030,12,23) +dt = Dates.DateTime(2030, 12, 23) +dt1 = Dates.Date(2030, 12, 23) for i = 1:21 @test Dates.week(dt) == check[i] @test Dates.week(dt1) == check[i] @@ -171,9 +171,9 @@ for i = 1:21 dt1 = dt1 + Dates.Day(1) end # Tests from http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php?year=2004 -dt = Dates.DateTime(2004,12,20) -dt1 = Dates.Date(2004,12,20) -check = (52,52,52,52,52,52,52,53,53,53,53,53,53,53,1,1,1,1,1,1,1) +dt = Dates.DateTime(2004, 12, 20) +dt1 = Dates.Date(2004, 12, 20) +check = (52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 1, 1, 1, 1, 1, 1, 1) for i = 1:21 @test Dates.week(dt) == check[i] @test Dates.week(dt1) == check[i] @@ -182,27 +182,27 @@ for i = 1:21 end # Vectorized accessors -a = Dates.Date(2014,1,1) -dr = [a,a,a,a,a,a,a,a,a,a] -@test Dates.year.(dr) == repmat([2014],10) -@test Dates.month.(dr) == repmat([1],10) -@test Dates.day.(dr) == repmat([1],10) +a = Dates.Date(2014, 1, 1) +dr = [a, a, a, a, a, a, a, a, a, a] +@test Dates.year.(dr) == repmat([2014], 10) +@test Dates.month.(dr) == repmat([1], 10) +@test Dates.day.(dr) == repmat([1], 10) -a = Dates.DateTime(2014,1,1) -dr = [a,a,a,a,a,a,a,a,a,a] -@test Dates.year.(dr) == repmat([2014],10) -@test Dates.month.(dr) == repmat([1],10) -@test Dates.day.(dr) == repmat([1],10) -@test Dates.hour.(dr) == repmat([0],10) -@test Dates.minute.(dr) == repmat([0],10) -@test Dates.second.(dr) == repmat([0],10) -@test Dates.millisecond.(dr) == repmat([0],10) +a = Dates.DateTime(2014, 1, 1) +dr = [a, a, a, a, a, a, a, a, a, a] +@test Dates.year.(dr) == repmat([2014], 10) +@test Dates.month.(dr) == repmat([1], 10) +@test Dates.day.(dr) == repmat([1], 10) +@test Dates.hour.(dr) == repmat([0], 10) +@test Dates.minute.(dr) == repmat([0], 10) +@test Dates.second.(dr) == repmat([0], 10) +@test Dates.millisecond.(dr) == repmat([0], 10) -b = Dates.Time(1,2,3,4,5,6) -tr = [b,b,b,b,b,b,b,b,b,b] -@test Dates.hour.(tr) == repmat([1],10) -@test Dates.minute.(tr) == repmat([2],10) -@test Dates.second.(tr) == repmat([3],10) -@test Dates.millisecond.(tr) == repmat([4],10) -@test Dates.microsecond.(tr) == repmat([5],10) -@test Dates.nanosecond.(tr) == repmat([6],10) \ No newline at end of file +b = Dates.Time(1, 2, 3, 4, 5, 6) +tr = [b, b, b, b, b, b, b, b, b, b] +@test Dates.hour.(tr) == repmat([1], 10) +@test Dates.minute.(tr) == repmat([2], 10) +@test Dates.second.(tr) == repmat([3], 10) +@test Dates.millisecond.(tr) == repmat([4], 10) +@test Dates.microsecond.(tr) == repmat([5], 10) +@test Dates.nanosecond.(tr) == repmat([6], 10) \ No newline at end of file diff --git a/test/dates/adjusters.jl b/test/dates/adjusters.jl index 45808d0203893..46c7045d2221e 100644 --- a/test/dates/adjusters.jl +++ b/test/dates/adjusters.jl @@ -1,100 +1,100 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license #trunc -dt = Dates.Date(2012,12,21) -@test trunc(dt,Dates.Year) == Dates.Date(2012) -@test trunc(dt,Dates.Month) == Dates.Date(2012,12) -@test trunc(dt,Dates.Day) == Dates.Date(2012,12,21) -dt = Dates.DateTime(2012,12,21,16,30,20,200) -@test trunc(dt,Dates.Year) == Dates.DateTime(2012) -@test trunc(dt,Dates.Month) == Dates.DateTime(2012,12) -@test trunc(dt,Dates.Day) == Dates.DateTime(2012,12,21) -@test trunc(dt,Dates.Hour) == Dates.DateTime(2012,12,21,16) -@test trunc(dt,Dates.Minute) == Dates.DateTime(2012,12,21,16,30) -@test trunc(dt,Dates.Second) == Dates.DateTime(2012,12,21,16,30,20) -@test trunc(dt,Dates.Millisecond) == Dates.DateTime(2012,12,21,16,30,20,200) -t = Dates.Time(1,2,3,4,5,6) -@test trunc(t,Dates.Hour) == Dates.Time(1) -@test trunc(t,Dates.Minute) == Dates.Time(1,2) -@test trunc(t,Dates.Second) == Dates.Time(1,2,3) -@test trunc(t,Dates.Millisecond) == Dates.Time(1,2,3,4) -@test trunc(t,Dates.Microsecond) == Dates.Time(1,2,3,4,5) -@test trunc(t,Dates.Nanosecond) == Dates.Time(1,2,3,4,5,6) +dt = Dates.Date(2012, 12, 21) +@test trunc(dt, Dates.Year) == Dates.Date(2012) +@test trunc(dt, Dates.Month) == Dates.Date(2012, 12) +@test trunc(dt, Dates.Day) == Dates.Date(2012, 12, 21) +dt = Dates.DateTime(2012, 12, 21, 16, 30, 20, 200) +@test trunc(dt, Dates.Year) == Dates.DateTime(2012) +@test trunc(dt, Dates.Month) == Dates.DateTime(2012, 12) +@test trunc(dt, Dates.Day) == Dates.DateTime(2012, 12, 21) +@test trunc(dt, Dates.Hour) == Dates.DateTime(2012, 12, 21, 16) +@test trunc(dt, Dates.Minute) == Dates.DateTime(2012, 12, 21, 16, 30) +@test trunc(dt, Dates.Second) == Dates.DateTime(2012, 12, 21, 16, 30, 20) +@test trunc(dt, Dates.Millisecond) == Dates.DateTime(2012, 12, 21, 16, 30, 20, 200) +t = Dates.Time(1, 2, 3, 4, 5, 6) +@test trunc(t, Dates.Hour) == Dates.Time(1) +@test trunc(t, Dates.Minute) == Dates.Time(1, 2) +@test trunc(t, Dates.Second) == Dates.Time(1, 2, 3) +@test trunc(t, Dates.Millisecond) == Dates.Time(1, 2, 3, 4) +@test trunc(t, Dates.Microsecond) == Dates.Time(1, 2, 3, 4, 5) +@test trunc(t, Dates.Nanosecond) == Dates.Time(1, 2, 3, 4, 5, 6) # Date functions -Jan = Dates.DateTime(2013,1,1) #Tuesday -Feb = Dates.DateTime(2013,2,2) #Saturday -Mar = Dates.DateTime(2013,3,3) #Sunday -Apr = Dates.DateTime(2013,4,4) #Thursday -May = Dates.DateTime(2013,5,5) #Sunday -Jun = Dates.DateTime(2013,6,7) #Friday -Jul = Dates.DateTime(2013,7,7) #Sunday -Aug = Dates.DateTime(2013,8,8) #Thursday -Sep = Dates.DateTime(2013,9,9) #Monday -Oct = Dates.DateTime(2013,10,10) #Thursday -Nov = Dates.DateTime(2013,11,11) #Monday -Dec = Dates.DateTime(2013,12,11) #Wednesday - -@test Dates.lastdayofmonth(Jan) == Dates.DateTime(2013,1,31) -@test Dates.lastdayofmonth(Feb) == Dates.DateTime(2013,2,28) -@test Dates.lastdayofmonth(Mar) == Dates.DateTime(2013,3,31) -@test Dates.lastdayofmonth(Apr) == Dates.DateTime(2013,4,30) -@test Dates.lastdayofmonth(May) == Dates.DateTime(2013,5,31) -@test Dates.lastdayofmonth(Jun) == Dates.DateTime(2013,6,30) -@test Dates.lastdayofmonth(Jul) == Dates.DateTime(2013,7,31) -@test Dates.lastdayofmonth(Aug) == Dates.DateTime(2013,8,31) -@test Dates.lastdayofmonth(Sep) == Dates.DateTime(2013,9,30) -@test Dates.lastdayofmonth(Oct) == Dates.DateTime(2013,10,31) -@test Dates.lastdayofmonth(Nov) == Dates.DateTime(2013,11,30) -@test Dates.lastdayofmonth(Dec) == Dates.DateTime(2013,12,31) - -@test Dates.lastdayofmonth(Date(Jan)) == Dates.Date(2013,1,31) -@test Dates.lastdayofmonth(Date(Feb)) == Dates.Date(2013,2,28) -@test Dates.lastdayofmonth(Date(Mar)) == Dates.Date(2013,3,31) -@test Dates.lastdayofmonth(Date(Apr)) == Dates.Date(2013,4,30) -@test Dates.lastdayofmonth(Date(May)) == Dates.Date(2013,5,31) -@test Dates.lastdayofmonth(Date(Jun)) == Dates.Date(2013,6,30) -@test Dates.lastdayofmonth(Date(Jul)) == Dates.Date(2013,7,31) -@test Dates.lastdayofmonth(Date(Aug)) == Dates.Date(2013,8,31) -@test Dates.lastdayofmonth(Date(Sep)) == Dates.Date(2013,9,30) -@test Dates.lastdayofmonth(Date(Oct)) == Dates.Date(2013,10,31) -@test Dates.lastdayofmonth(Date(Nov)) == Dates.Date(2013,11,30) -@test Dates.lastdayofmonth(Date(Dec)) == Dates.Date(2013,12,31) - -@test Dates.firstdayofmonth(Jan) == Dates.DateTime(2013,1,1) -@test Dates.firstdayofmonth(Feb) == Dates.DateTime(2013,2,1) -@test Dates.firstdayofmonth(Mar) == Dates.DateTime(2013,3,1) -@test Dates.firstdayofmonth(Apr) == Dates.DateTime(2013,4,1) -@test Dates.firstdayofmonth(May) == Dates.DateTime(2013,5,1) -@test Dates.firstdayofmonth(Jun) == Dates.DateTime(2013,6,1) -@test Dates.firstdayofmonth(Jul) == Dates.DateTime(2013,7,1) -@test Dates.firstdayofmonth(Aug) == Dates.DateTime(2013,8,1) -@test Dates.firstdayofmonth(Sep) == Dates.DateTime(2013,9,1) -@test Dates.firstdayofmonth(Oct) == Dates.DateTime(2013,10,1) -@test Dates.firstdayofmonth(Nov) == Dates.DateTime(2013,11,1) -@test Dates.firstdayofmonth(Dec) == Dates.DateTime(2013,12,1) - -@test Dates.firstdayofmonth(Date(Jan)) == Dates.Date(2013,1,1) -@test Dates.firstdayofmonth(Date(Feb)) == Dates.Date(2013,2,1) -@test Dates.firstdayofmonth(Date(Mar)) == Dates.Date(2013,3,1) -@test Dates.firstdayofmonth(Date(Apr)) == Dates.Date(2013,4,1) -@test Dates.firstdayofmonth(Date(May)) == Dates.Date(2013,5,1) -@test Dates.firstdayofmonth(Date(Jun)) == Dates.Date(2013,6,1) -@test Dates.firstdayofmonth(Date(Jul)) == Dates.Date(2013,7,1) -@test Dates.firstdayofmonth(Date(Aug)) == Dates.Date(2013,8,1) -@test Dates.firstdayofmonth(Date(Sep)) == Dates.Date(2013,9,1) -@test Dates.firstdayofmonth(Date(Oct)) == Dates.Date(2013,10,1) -@test Dates.firstdayofmonth(Date(Nov)) == Dates.Date(2013,11,1) -@test Dates.firstdayofmonth(Date(Dec)) == Dates.Date(2013,12,1) +Jan = Dates.DateTime(2013, 1, 1) #Tuesday +Feb = Dates.DateTime(2013, 2, 2) #Saturday +Mar = Dates.DateTime(2013, 3, 3) #Sunday +Apr = Dates.DateTime(2013, 4, 4) #Thursday +May = Dates.DateTime(2013, 5, 5) #Sunday +Jun = Dates.DateTime(2013, 6, 7) #Friday +Jul = Dates.DateTime(2013, 7, 7) #Sunday +Aug = Dates.DateTime(2013, 8, 8) #Thursday +Sep = Dates.DateTime(2013, 9, 9) #Monday +Oct = Dates.DateTime(2013, 10, 10) #Thursday +Nov = Dates.DateTime(2013, 11, 11) #Monday +Dec = Dates.DateTime(2013, 12, 11) #Wednesday + +@test Dates.lastdayofmonth(Jan) == Dates.DateTime(2013, 1, 31) +@test Dates.lastdayofmonth(Feb) == Dates.DateTime(2013, 2, 28) +@test Dates.lastdayofmonth(Mar) == Dates.DateTime(2013, 3, 31) +@test Dates.lastdayofmonth(Apr) == Dates.DateTime(2013, 4, 30) +@test Dates.lastdayofmonth(May) == Dates.DateTime(2013, 5, 31) +@test Dates.lastdayofmonth(Jun) == Dates.DateTime(2013, 6, 30) +@test Dates.lastdayofmonth(Jul) == Dates.DateTime(2013, 7, 31) +@test Dates.lastdayofmonth(Aug) == Dates.DateTime(2013, 8, 31) +@test Dates.lastdayofmonth(Sep) == Dates.DateTime(2013, 9, 30) +@test Dates.lastdayofmonth(Oct) == Dates.DateTime(2013, 10, 31) +@test Dates.lastdayofmonth(Nov) == Dates.DateTime(2013, 11, 30) +@test Dates.lastdayofmonth(Dec) == Dates.DateTime(2013, 12, 31) + +@test Dates.lastdayofmonth(Date(Jan)) == Dates.Date(2013, 1, 31) +@test Dates.lastdayofmonth(Date(Feb)) == Dates.Date(2013, 2, 28) +@test Dates.lastdayofmonth(Date(Mar)) == Dates.Date(2013, 3, 31) +@test Dates.lastdayofmonth(Date(Apr)) == Dates.Date(2013, 4, 30) +@test Dates.lastdayofmonth(Date(May)) == Dates.Date(2013, 5, 31) +@test Dates.lastdayofmonth(Date(Jun)) == Dates.Date(2013, 6, 30) +@test Dates.lastdayofmonth(Date(Jul)) == Dates.Date(2013, 7, 31) +@test Dates.lastdayofmonth(Date(Aug)) == Dates.Date(2013, 8, 31) +@test Dates.lastdayofmonth(Date(Sep)) == Dates.Date(2013, 9, 30) +@test Dates.lastdayofmonth(Date(Oct)) == Dates.Date(2013, 10, 31) +@test Dates.lastdayofmonth(Date(Nov)) == Dates.Date(2013, 11, 30) +@test Dates.lastdayofmonth(Date(Dec)) == Dates.Date(2013, 12, 31) + +@test Dates.firstdayofmonth(Jan) == Dates.DateTime(2013, 1, 1) +@test Dates.firstdayofmonth(Feb) == Dates.DateTime(2013, 2, 1) +@test Dates.firstdayofmonth(Mar) == Dates.DateTime(2013, 3, 1) +@test Dates.firstdayofmonth(Apr) == Dates.DateTime(2013, 4, 1) +@test Dates.firstdayofmonth(May) == Dates.DateTime(2013, 5, 1) +@test Dates.firstdayofmonth(Jun) == Dates.DateTime(2013, 6, 1) +@test Dates.firstdayofmonth(Jul) == Dates.DateTime(2013, 7, 1) +@test Dates.firstdayofmonth(Aug) == Dates.DateTime(2013, 8, 1) +@test Dates.firstdayofmonth(Sep) == Dates.DateTime(2013, 9, 1) +@test Dates.firstdayofmonth(Oct) == Dates.DateTime(2013, 10, 1) +@test Dates.firstdayofmonth(Nov) == Dates.DateTime(2013, 11, 1) +@test Dates.firstdayofmonth(Dec) == Dates.DateTime(2013, 12, 1) + +@test Dates.firstdayofmonth(Date(Jan)) == Dates.Date(2013, 1, 1) +@test Dates.firstdayofmonth(Date(Feb)) == Dates.Date(2013, 2, 1) +@test Dates.firstdayofmonth(Date(Mar)) == Dates.Date(2013, 3, 1) +@test Dates.firstdayofmonth(Date(Apr)) == Dates.Date(2013, 4, 1) +@test Dates.firstdayofmonth(Date(May)) == Dates.Date(2013, 5, 1) +@test Dates.firstdayofmonth(Date(Jun)) == Dates.Date(2013, 6, 1) +@test Dates.firstdayofmonth(Date(Jul)) == Dates.Date(2013, 7, 1) +@test Dates.firstdayofmonth(Date(Aug)) == Dates.Date(2013, 8, 1) +@test Dates.firstdayofmonth(Date(Sep)) == Dates.Date(2013, 9, 1) +@test Dates.firstdayofmonth(Date(Oct)) == Dates.Date(2013, 10, 1) +@test Dates.firstdayofmonth(Date(Nov)) == Dates.Date(2013, 11, 1) +@test Dates.firstdayofmonth(Date(Dec)) == Dates.Date(2013, 12, 1) # Test first day of week; 2014-01-06 is a Monday = 1st day of week -a = Dates.Date(2014,1,6) -b = Dates.Date(2014,1,7) -c = Dates.Date(2014,1,8) -d = Dates.Date(2014,1,9) -e = Dates.Date(2014,1,10) -f = Dates.Date(2014,1,11) -g = Dates.Date(2014,1,12) +a = Dates.Date(2014, 1, 6) +b = Dates.Date(2014, 1, 7) +c = Dates.Date(2014, 1, 8) +d = Dates.Date(2014, 1, 9) +e = Dates.Date(2014, 1, 10) +f = Dates.Date(2014, 1, 11) +g = Dates.Date(2014, 1, 12) @test Dates.firstdayofweek(a) == a @test Dates.firstdayofweek(b) == a @test Dates.firstdayofweek(c) == a @@ -105,16 +105,16 @@ g = Dates.Date(2014,1,12) # Test firstdayofweek over the course of the year dt = a for i = 0:364 - @test Dates.firstdayofweek(dt) == a + Dates.Week(div(i,7)) + @test Dates.firstdayofweek(dt) == a + Dates.Week(div(i, 7)) dt += Dates.Day(1) end -a = Dates.DateTime(2014,1,6) -b = Dates.DateTime(2014,1,7) -c = Dates.DateTime(2014,1,8) -d = Dates.DateTime(2014,1,9) -e = Dates.DateTime(2014,1,10) -f = Dates.DateTime(2014,1,11) -g = Dates.DateTime(2014,1,12) +a = Dates.DateTime(2014, 1, 6) +b = Dates.DateTime(2014, 1, 7) +c = Dates.DateTime(2014, 1, 8) +d = Dates.DateTime(2014, 1, 9) +e = Dates.DateTime(2014, 1, 10) +f = Dates.DateTime(2014, 1, 11) +g = Dates.DateTime(2014, 1, 12) @test Dates.firstdayofweek(a) == a @test Dates.firstdayofweek(b) == a @test Dates.firstdayofweek(c) == a @@ -124,19 +124,19 @@ g = Dates.DateTime(2014,1,12) @test Dates.firstdayofweek(g) == a dt = a for i = 0:364 - @test Dates.firstdayofweek(dt) == a + Dates.Week(div(i,7)) + @test Dates.firstdayofweek(dt) == a + Dates.Week(div(i, 7)) dt += Dates.Day(1) end -@test Dates.firstdayofweek(Dates.DateTime(2013,12,24)) == Dates.DateTime(2013,12,23) +@test Dates.firstdayofweek(Dates.DateTime(2013, 12, 24)) == Dates.DateTime(2013, 12, 23) # Test last day of week; Sunday = last day of week # 2014-01-12 is a Sunday -a = Dates.Date(2014,1,6) -b = Dates.Date(2014,1,7) -c = Dates.Date(2014,1,8) -d = Dates.Date(2014,1,9) -e = Dates.Date(2014,1,10) -f = Dates.Date(2014,1,11) -g = Dates.Date(2014,1,12) +a = Dates.Date(2014, 1, 6) +b = Dates.Date(2014, 1, 7) +c = Dates.Date(2014, 1, 8) +d = Dates.Date(2014, 1, 9) +e = Dates.Date(2014, 1, 10) +f = Dates.Date(2014, 1, 11) +g = Dates.Date(2014, 1, 12) @test Dates.lastdayofweek(a) == g @test Dates.lastdayofweek(b) == g @test Dates.lastdayofweek(c) == g @@ -146,16 +146,16 @@ g = Dates.Date(2014,1,12) @test Dates.lastdayofweek(g) == g dt = a for i = 0:364 - @test Dates.lastdayofweek(dt) == g + Dates.Week(div(i,7)) + @test Dates.lastdayofweek(dt) == g + Dates.Week(div(i, 7)) dt += Dates.Day(1) end -a = Dates.DateTime(2014,1,6) -b = Dates.DateTime(2014,1,7) -c = Dates.DateTime(2014,1,8) -d = Dates.DateTime(2014,1,9) -e = Dates.DateTime(2014,1,10) -f = Dates.DateTime(2014,1,11) -g = Dates.DateTime(2014,1,12) +a = Dates.DateTime(2014, 1, 6) +b = Dates.DateTime(2014, 1, 7) +c = Dates.DateTime(2014, 1, 8) +d = Dates.DateTime(2014, 1, 9) +e = Dates.DateTime(2014, 1, 10) +f = Dates.DateTime(2014, 1, 11) +g = Dates.DateTime(2014, 1, 12) @test Dates.lastdayofweek(a) == g @test Dates.lastdayofweek(b) == g @test Dates.lastdayofweek(c) == g @@ -165,33 +165,33 @@ g = Dates.DateTime(2014,1,12) @test Dates.lastdayofweek(g) == g dt = a for i = 0:364 - @test Dates.lastdayofweek(dt) == g + Dates.Week(div(i,7)) + @test Dates.lastdayofweek(dt) == g + Dates.Week(div(i, 7)) dt += Dates.Day(1) end -@test Dates.lastdayofweek(Dates.DateTime(2013,12,24)) == Dates.DateTime(2013,12,29) - -@test Dates.firstdayofquarter(Dates.Date(2014,2,2)) == Dates.Date(2014,1,1) -@test Dates.firstdayofquarter(Dates.Date(2014,5,2)) == Dates.Date(2014,4,1) -@test Dates.firstdayofquarter(Dates.Date(2014,8,2)) == Dates.Date(2014,7,1) -@test Dates.firstdayofquarter(Dates.Date(2014,12,2)) == Dates.Date(2014,10,1) - -@test Dates.firstdayofquarter(Dates.DateTime(2014,2,2)) == Dates.DateTime(2014,1,1) -@test Dates.firstdayofquarter(Dates.DateTime(2014,5,2)) == Dates.DateTime(2014,4,1) -@test Dates.firstdayofquarter(Dates.DateTime(2014,8,2)) == Dates.DateTime(2014,7,1) -@test Dates.firstdayofquarter(Dates.DateTime(2014,12,2)) == Dates.DateTime(2014,10,1) - -@test Dates.lastdayofquarter(Dates.Date(2014,2,2)) == Dates.Date(2014,3,31) -@test Dates.lastdayofquarter(Dates.Date(2014,5,2)) == Dates.Date(2014,6,30) -@test Dates.lastdayofquarter(Dates.Date(2014,8,2)) == Dates.Date(2014,9,30) -@test Dates.lastdayofquarter(Dates.Date(2014,12,2)) == Dates.Date(2014,12,31) - -@test Dates.lastdayofquarter(Dates.DateTime(2014,2,2)) == Dates.DateTime(2014,3,31) -@test Dates.lastdayofquarter(Dates.DateTime(2014,5,2)) == Dates.DateTime(2014,6,30) -@test Dates.lastdayofquarter(Dates.DateTime(2014,8,2)) == Dates.DateTime(2014,9,30) -@test Dates.lastdayofquarter(Dates.DateTime(2014,12,2)) == Dates.DateTime(2014,12,31) - -firstday = Dates.Date(2014,1,1) -lastday = Dates.Date(2014,12,31) +@test Dates.lastdayofweek(Dates.DateTime(2013, 12, 24)) == Dates.DateTime(2013, 12, 29) + +@test Dates.firstdayofquarter(Dates.Date(2014, 2, 2)) == Dates.Date(2014, 1, 1) +@test Dates.firstdayofquarter(Dates.Date(2014, 5, 2)) == Dates.Date(2014, 4, 1) +@test Dates.firstdayofquarter(Dates.Date(2014, 8, 2)) == Dates.Date(2014, 7, 1) +@test Dates.firstdayofquarter(Dates.Date(2014, 12, 2)) == Dates.Date(2014, 10, 1) + +@test Dates.firstdayofquarter(Dates.DateTime(2014, 2, 2)) == Dates.DateTime(2014, 1, 1) +@test Dates.firstdayofquarter(Dates.DateTime(2014, 5, 2)) == Dates.DateTime(2014, 4, 1) +@test Dates.firstdayofquarter(Dates.DateTime(2014, 8, 2)) == Dates.DateTime(2014, 7, 1) +@test Dates.firstdayofquarter(Dates.DateTime(2014, 12, 2)) == Dates.DateTime(2014, 10, 1) + +@test Dates.lastdayofquarter(Dates.Date(2014, 2, 2)) == Dates.Date(2014, 3, 31) +@test Dates.lastdayofquarter(Dates.Date(2014, 5, 2)) == Dates.Date(2014, 6, 30) +@test Dates.lastdayofquarter(Dates.Date(2014, 8, 2)) == Dates.Date(2014, 9, 30) +@test Dates.lastdayofquarter(Dates.Date(2014, 12, 2)) == Dates.Date(2014, 12, 31) + +@test Dates.lastdayofquarter(Dates.DateTime(2014, 2, 2)) == Dates.DateTime(2014, 3, 31) +@test Dates.lastdayofquarter(Dates.DateTime(2014, 5, 2)) == Dates.DateTime(2014, 6, 30) +@test Dates.lastdayofquarter(Dates.DateTime(2014, 8, 2)) == Dates.DateTime(2014, 9, 30) +@test Dates.lastdayofquarter(Dates.DateTime(2014, 12, 2)) == Dates.DateTime(2014, 12, 31) + +firstday = Dates.Date(2014, 1, 1) +lastday = Dates.Date(2014, 12, 31) for i = 0:364 dt = firstday + Dates.Day(i) @@ -204,140 +204,142 @@ end # Adjusters # Adjuster Constructors -@test Dates.Date(Dates.ismonday,2014) == Dates.Date(2014,1,6) -@test Dates.Date(Dates.ismonday,2014,5) == Dates.Date(2014,5,5) +@test Dates.Date(Dates.ismonday, 2014) == Dates.Date(2014, 1, 6) +@test Dates.Date(Dates.ismonday, 2014, 5) == Dates.Date(2014, 5, 5) -@test Dates.DateTime(Dates.ismonday,2014) == Dates.DateTime(2014,1,6) -@test Dates.DateTime(Dates.ismonday,2014,5) == Dates.DateTime(2014,5,5) -@test Dates.DateTime(x->Dates.hour(x)==12,2014,5,21) == Dates.DateTime(2014,5,21,12) -@test Dates.DateTime(x->Dates.minute(x)==30,2014,5,21,12) == Dates.DateTime(2014,5,21,12,30) -@test Dates.DateTime(x->Dates.second(x)==30,2014,5,21,12,30) == Dates.DateTime(2014,5,21,12,30,30) -@test Dates.DateTime(x->Dates.millisecond(x)==500,2014,5,21,12,30,30) == Dates.DateTime(2014,5,21,12,30,30,500) +@test Dates.DateTime(Dates.ismonday, 2014) == Dates.DateTime(2014, 1, 6) +@test Dates.DateTime(Dates.ismonday, 2014, 5) == Dates.DateTime(2014, 5, 5) +@test Dates.DateTime(x->Dates.hour(x)==12, 2014, 5, 21) == Dates.DateTime(2014, 5, 21, 12) +@test Dates.DateTime(x->Dates.minute(x)==30, 2014, 5, 21, 12) == Dates.DateTime(2014, 5, 21, 12, 30) +@test Dates.DateTime(x->Dates.second(x)==30, 2014, 5, 21, 12, 30) == Dates.DateTime(2014, 5, 21, 12, 30, 30) +@test Dates.DateTime(x->Dates.millisecond(x)==500, 2014, 5, 21, 12, 30, 30) == Dates.DateTime(2014, 5, 21, 12, 30, 30, 500) # tonext, toprev, tofirst, tolast -dt = Dates.Date(2014,5,21) -@test Dates.tonext(dt,Dates.Wed) == Dates.Date(2014,5,28) -@test Dates.tonext(dt,Dates.Wed;same=true) == dt -@test Dates.tonext(dt,Dates.Thu) == Dates.Date(2014,5,22) -@test Dates.tonext(dt,Dates.Fri) == Dates.Date(2014,5,23) -@test Dates.tonext(dt,Dates.Sat) == Dates.Date(2014,5,24) -@test Dates.tonext(dt,Dates.Sun) == Dates.Date(2014,5,25) -@test Dates.tonext(dt,Dates.Mon) == Dates.Date(2014,5,26) -@test Dates.tonext(dt,Dates.Tue) == Dates.Date(2014,5,27) +dt = Dates.Date(2014, 5, 21) +@test Dates.tonext(dt, Dates.Wed) == Dates.Date(2014, 5, 28) +@test Dates.tonext(dt, Dates.Wed; same=true) == dt +@test Dates.tonext(dt, Dates.Thu) == Dates.Date(2014, 5, 22) +@test Dates.tonext(dt, Dates.Fri) == Dates.Date(2014, 5, 23) +@test Dates.tonext(dt, Dates.Sat) == Dates.Date(2014, 5, 24) +@test Dates.tonext(dt, Dates.Sun) == Dates.Date(2014, 5, 25) +@test Dates.tonext(dt, Dates.Mon) == Dates.Date(2014, 5, 26) +@test Dates.tonext(dt, Dates.Tue) == Dates.Date(2014, 5, 27) # No dayofweek function for out of range values -@test_throws KeyError Dates.tonext(dt,8) +@test_throws KeyError Dates.tonext(dt, 8) -@test Dates.tonext(Dates.Date(0),Dates.Mon) == Dates.Date(0,1,3) +@test Dates.tonext(Dates.Date(0), Dates.Mon) == Dates.Date(0, 1, 3) #test func, diff steps, same -@test Dates.tonext(Dates.iswednesday,dt) == Dates.Date(2014,5,28) -@test Dates.tonext(Dates.iswednesday,dt;same=true) == dt -@test Dates.tonext(Dates.isthursday,dt) == Dates.Date(2014,5,22) -@test Dates.tonext(Dates.isfriday,dt) == Dates.Date(2014,5,23) -@test Dates.tonext(Dates.issaturday,dt) == Dates.Date(2014,5,24) -@test Dates.tonext(Dates.issunday,dt) == Dates.Date(2014,5,25) -@test Dates.tonext(Dates.ismonday,dt) == Dates.Date(2014,5,26) -@test Dates.tonext(Dates.istuesday,dt) == Dates.Date(2014,5,27) -@test Dates.tonext(Dates.ismonday,Dates.Date(0)) == Dates.Date(0,1,3) - -@test Dates.tonext(!Dates.iswednesday,dt) == Dates.Date(2014,5,22) -@test Dates.tonext(!Dates.isthursday,dt) == Dates.Date(2014,5,23) +@test Dates.tonext(Dates.iswednesday, dt) == Dates.Date(2014, 5, 28) +@test Dates.tonext(Dates.iswednesday, dt; same=true) == dt +@test Dates.tonext(Dates.isthursday, dt) == Dates.Date(2014, 5, 22) +@test Dates.tonext(Dates.isfriday, dt) == Dates.Date(2014, 5, 23) +@test Dates.tonext(Dates.issaturday, dt) == Dates.Date(2014, 5, 24) +@test Dates.tonext(Dates.issunday, dt) == Dates.Date(2014, 5, 25) +@test Dates.tonext(Dates.ismonday, dt) == Dates.Date(2014, 5, 26) +@test Dates.tonext(Dates.istuesday, dt) == Dates.Date(2014, 5, 27) +@test Dates.tonext(Dates.ismonday, Dates.Date(0)) == Dates.Date(0, 1, 3) + +@test Dates.tonext(!Dates.iswednesday, dt) == Dates.Date(2014, 5, 22) +@test Dates.tonext(!Dates.isthursday, dt) == Dates.Date(2014, 5, 23) + # Reach adjust limit -@test_throws ArgumentError Dates.tonext(Dates.iswednesday,dt;limit=6) - -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Day(2)) == Dates.Date(2014,6,4) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Day(3)) == Dates.Date(2014,6,11) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Day(4)) == Dates.Date(2014,6,18) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Day(5)) == Dates.Date(2014,6,25) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Day(6)) == Dates.Date(2014,7,2) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Day(7)) == Dates.Date(2014,5,28) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Week(1)) == Dates.Date(2014,5,28) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Week(2)) == Dates.Date(2014,6,4) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Week(3)) == Dates.Date(2014,6,11) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Week(4)) == Dates.Date(2014,6,18) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Week(5)) == Dates.Date(2014,6,25) -@test Dates.tonext(Dates.iswednesday,dt;step=Dates.Week(6)) == Dates.Date(2014,7,2) - -@test Dates.tonext(Dates.iswednesday,dt;same=true) == dt -@test Dates.tonext(Dates.isthursday,dt) == Dates.Date(2014,5,22) +@test_throws ArgumentError Dates.tonext(Dates.iswednesday, dt; limit=6) + +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Day(2)) == Dates.Date(2014, 6, 4) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Day(3)) == Dates.Date(2014, 6, 11) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Day(4)) == Dates.Date(2014, 6, 18) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Day(5)) == Dates.Date(2014, 6, 25) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Day(6)) == Dates.Date(2014, 7, 2) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Day(7)) == Dates.Date(2014, 5, 28) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Week(1)) == Dates.Date(2014, 5, 28) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Week(2)) == Dates.Date(2014, 6, 4) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Week(3)) == Dates.Date(2014, 6, 11) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Week(4)) == Dates.Date(2014, 6, 18) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Week(5)) == Dates.Date(2014, 6, 25) +@test Dates.tonext(Dates.iswednesday, dt;step=Dates.Week(6)) == Dates.Date(2014, 7, 2) + +@test Dates.tonext(Dates.iswednesday, dt; same=true) == dt +@test Dates.tonext(Dates.isthursday, dt) == Dates.Date(2014, 5, 22) #toprev -@test Dates.toprev(dt,Dates.Wed) == Dates.Date(2014,5,14) -@test Dates.toprev(dt,Dates.Wed;same=true) == dt -@test Dates.toprev(dt,Dates.Thu) == Dates.Date(2014,5,15) -@test Dates.toprev(dt,Dates.Fri) == Dates.Date(2014,5,16) -@test Dates.toprev(dt,Dates.Sat) == Dates.Date(2014,5,17) -@test Dates.toprev(dt,Dates.Sun) == Dates.Date(2014,5,18) -@test Dates.toprev(dt,Dates.Mon) == Dates.Date(2014,5,19) -@test Dates.toprev(dt,Dates.Tue) == Dates.Date(2014,5,20) +@test Dates.toprev(dt, Dates.Wed) == Dates.Date(2014, 5, 14) +@test Dates.toprev(dt, Dates.Wed; same=true) == dt +@test Dates.toprev(dt, Dates.Thu) == Dates.Date(2014, 5, 15) +@test Dates.toprev(dt, Dates.Fri) == Dates.Date(2014, 5, 16) +@test Dates.toprev(dt, Dates.Sat) == Dates.Date(2014, 5, 17) +@test Dates.toprev(dt, Dates.Sun) == Dates.Date(2014, 5, 18) +@test Dates.toprev(dt, Dates.Mon) == Dates.Date(2014, 5, 19) +@test Dates.toprev(dt, Dates.Tue) == Dates.Date(2014, 5, 20) # No dayofweek function for out of range values -@test_throws KeyError Dates.toprev(dt,8) +@test_throws KeyError Dates.toprev(dt, 8) -@test Dates.toprev(Dates.Date(0),Dates.Mon) == Dates.Date(-1,12,27) +@test Dates.toprev(Dates.Date(0), Dates.Mon) == Dates.Date(-1, 12, 27) #tofirst -@test Dates.tofirst(dt,Dates.Mon) == Dates.Date(2014,5,5) -@test Dates.tofirst(dt,Dates.Tue) == Dates.Date(2014,5,6) -@test Dates.tofirst(dt,Dates.Wed) == Dates.Date(2014,5,7) -@test Dates.tofirst(dt,Dates.Thu) == Dates.Date(2014,5,1) -@test Dates.tofirst(dt,Dates.Fri) == Dates.Date(2014,5,2) -@test Dates.tofirst(dt,Dates.Sat) == Dates.Date(2014,5,3) -@test Dates.tofirst(dt,Dates.Sun) == Dates.Date(2014,5,4) - -@test Dates.tofirst(dt,Dates.Mon,of=Dates.Year) == Dates.Date(2014,1,6) -@test Dates.tofirst(dt,Dates.Tue,of=Dates.Year) == Dates.Date(2014,1,7) -@test Dates.tofirst(dt,Dates.Wed,of=Dates.Year) == Dates.Date(2014,1,1) -@test Dates.tofirst(dt,Dates.Thu,of=Dates.Year) == Dates.Date(2014,1,2) -@test Dates.tofirst(dt,Dates.Fri,of=Dates.Year) == Dates.Date(2014,1,3) -@test Dates.tofirst(dt,Dates.Sat,of=Dates.Year) == Dates.Date(2014,1,4) -@test Dates.tofirst(dt,Dates.Sun,of=Dates.Year) == Dates.Date(2014,1,5) - -@test Dates.tofirst(Dates.Date(0),Dates.Mon) == Dates.Date(0,1,3) +@test Dates.tofirst(dt, Dates.Mon) == Dates.Date(2014, 5, 5) +@test Dates.tofirst(dt, Dates.Tue) == Dates.Date(2014, 5, 6) +@test Dates.tofirst(dt, Dates.Wed) == Dates.Date(2014, 5, 7) +@test Dates.tofirst(dt, Dates.Thu) == Dates.Date(2014, 5, 1) +@test Dates.tofirst(dt, Dates.Fri) == Dates.Date(2014, 5, 2) +@test Dates.tofirst(dt, Dates.Sat) == Dates.Date(2014, 5, 3) +@test Dates.tofirst(dt, Dates.Sun) == Dates.Date(2014, 5, 4) + +@test Dates.tofirst(dt, Dates.Mon, of=Dates.Year) == Dates.Date(2014, 1, 6) +@test Dates.tofirst(dt, Dates.Tue, of=Dates.Year) == Dates.Date(2014, 1, 7) +@test Dates.tofirst(dt, Dates.Wed, of=Dates.Year) == Dates.Date(2014, 1, 1) +@test Dates.tofirst(dt, Dates.Thu, of=Dates.Year) == Dates.Date(2014, 1, 2) +@test Dates.tofirst(dt, Dates.Fri, of=Dates.Year) == Dates.Date(2014, 1, 3) +@test Dates.tofirst(dt, Dates.Sat, of=Dates.Year) == Dates.Date(2014, 1, 4) +@test Dates.tofirst(dt, Dates.Sun, of=Dates.Year) == Dates.Date(2014, 1, 5) + +@test Dates.tofirst(Dates.Date(0), Dates.Mon) == Dates.Date(0, 1, 3) #tolast -@test Dates.tolast(dt,Dates.Mon) == Dates.Date(2014,5,26) -@test Dates.tolast(dt,Dates.Tue) == Dates.Date(2014,5,27) -@test Dates.tolast(dt,Dates.Wed) == Dates.Date(2014,5,28) -@test Dates.tolast(dt,Dates.Thu) == Dates.Date(2014,5,29) -@test Dates.tolast(dt,Dates.Fri) == Dates.Date(2014,5,30) -@test Dates.tolast(dt,Dates.Sat) == Dates.Date(2014,5,31) -@test Dates.tolast(dt,Dates.Sun) == Dates.Date(2014,5,25) - -@test Dates.tolast(dt,Dates.Mon,of=Dates.Year) == Dates.Date(2014,12,29) -@test Dates.tolast(dt,Dates.Tue,of=Dates.Year) == Dates.Date(2014,12,30) -@test Dates.tolast(dt,Dates.Wed,of=Dates.Year) == Dates.Date(2014,12,31) -@test Dates.tolast(dt,Dates.Thu,of=Dates.Year) == Dates.Date(2014,12,25) -@test Dates.tolast(dt,Dates.Fri,of=Dates.Year) == Dates.Date(2014,12,26) -@test Dates.tolast(dt,Dates.Sat,of=Dates.Year) == Dates.Date(2014,12,27) -@test Dates.tolast(dt,Dates.Sun,of=Dates.Year) == Dates.Date(2014,12,28) - -@test Dates.tolast(Dates.Date(0),Dates.Mon) == Dates.Date(0,1,31) +@test Dates.tolast(dt, Dates.Mon) == Dates.Date(2014, 5, 26) +@test Dates.tolast(dt, Dates.Tue) == Dates.Date(2014, 5, 27) +@test Dates.tolast(dt, Dates.Wed) == Dates.Date(2014, 5, 28) +@test Dates.tolast(dt, Dates.Thu) == Dates.Date(2014, 5, 29) +@test Dates.tolast(dt, Dates.Fri) == Dates.Date(2014, 5, 30) +@test Dates.tolast(dt, Dates.Sat) == Dates.Date(2014, 5, 31) +@test Dates.tolast(dt, Dates.Sun) == Dates.Date(2014, 5, 25) + +@test Dates.tolast(dt, Dates.Mon, of=Dates.Year) == Dates.Date(2014, 12, 29) +@test Dates.tolast(dt, Dates.Tue, of=Dates.Year) == Dates.Date(2014, 12, 30) +@test Dates.tolast(dt, Dates.Wed, of=Dates.Year) == Dates.Date(2014, 12, 31) +@test Dates.tolast(dt, Dates.Thu, of=Dates.Year) == Dates.Date(2014, 12, 25) +@test Dates.tolast(dt, Dates.Fri, of=Dates.Year) == Dates.Date(2014, 12, 26) +@test Dates.tolast(dt, Dates.Sat, of=Dates.Year) == Dates.Date(2014, 12, 27) +@test Dates.tolast(dt, Dates.Sun, of=Dates.Year) == Dates.Date(2014, 12, 28) + +@test Dates.tolast(Dates.Date(0), Dates.Mon) == Dates.Date(0, 1, 31) # filter (was recur) -startdate = Dates.Date(2014,1,1) -stopdate = Dates.Date(2014,2,1) -@test length(filter(x->true,startdate:stopdate)) == 32 -@test length(filter(x->true,stopdate:Dates.Day(-1):startdate)) == 32 +startdate = Dates.Date(2014, 1, 1) +stopdate = Dates.Date(2014, 2, 1) +@test length(filter(x->true, startdate:stopdate)) == 32 +@test length(filter(x->true, stopdate:Dates.Day(-1):startdate)) == 32 -Januarymondays2014 = [Dates.Date(2014,1,6),Dates.Date(2014,1,13),Dates.Date(2014,1,20),Dates.Date(2014,1,27)] -@test filter(Dates.ismonday,startdate:stopdate) == Januarymondays2014 +Januarymondays2014 = [Dates.Date(2014, 1, 6), Dates.Date(2014, 1, 13), Dates.Date(2014, 1, 20), Dates.Date(2014, 1, 27)] +@test filter(Dates.ismonday, startdate:stopdate) == Januarymondays2014 -@test_throws MethodError filter((x,y)->x+y,Dates.Date(2013):Dates.Date(2014)) -@test_throws MethodError Dates.DateFunction((x,y)->x+y, Date(0)) +@test_throws MethodError filter((x, y)->x + y, Dates.Date(2013):Dates.Date(2014)) +@test_throws MethodError Dates.DateFunction((x, y)->x + y, Date(0)) @test_throws ArgumentError Dates.DateFunction((dt)->2, Date(0)) -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,2))) == 32 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,1))) == 1 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,2))) == 2 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,3))) == 3 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,4))) == 4 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,5))) == 5 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,6))) == 6 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,7))) == 7 -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2013,1,8))) == 8 -@test length(filter(x->true,Dates.Date(2013):Dates.Month(1):Dates.Date(2013,1,1))) == 1 -@test length(filter(x->true,Dates.Date(2013):Dates.Day(-1):Dates.Date(2012,1,1))) == 367 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 2))) == 32 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 1))) == 1 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 2))) == 2 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 3))) == 3 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 4))) == 4 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 5))) == 5 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 6))) == 6 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 7))) == 7 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2013, 1, 8))) == 8 +@test length(filter(x->true, Dates.Date(2013):Dates.Month(1):Dates.Date(2013, 1, 1))) == 1 +@test length(filter(x->true, Dates.Date(2013):Dates.Day(-1):Dates.Date(2012, 1, 1))) == 367 + # Empty range -@test length(filter(x->true,Dates.Date(2013):Dates.Date(2012,1,1))) == 0 +@test length(filter(x->true, Dates.Date(2013):Dates.Date(2012, 1, 1))) == 0 # All leap days in 20th century @test length(filter(Dates.Date(1900):Dates.Date(2000)) do x @@ -349,15 +351,15 @@ thanksgiving = x->Dates.dayofweek(x) == Dates.Thu && Dates.month(x) == Dates.Nov && Dates.dayofweekofmonth(x) == 4 -d = Dates.Date(2014,6,5) +d = Dates.Date(2014, 6, 5) @test Dates.tonext(d) do x thanksgiving(x) -end == Dates.Date(2014,11,27) +end == Dates.Date(2014, 11, 27) @test Dates.toprev(d) do x thanksgiving(x) -end == Dates.Date(2013,11,28) +end == Dates.Date(2013, 11, 28) # Pittsburgh street cleaning dr = Dates.Date(2014):Dates.Date(2015) @@ -368,10 +370,10 @@ dr = Dates.Date(2014):Dates.Date(2015) end) == 6 # U.S. Federal Holidays -newyears(y) = (y,1,1) -independenceday(y) = (y,7,4) -veteransday(y) = (y,11,11) -christmas(y) = (y,12,25) +newyears(y) = (y, 1, 1) +independenceday(y) = (y, 7, 4) +veteransday(y) = (y, 11, 11) +christmas(y) = (y, 12, 25) isnewyears(dt) = Dates.yearmonthday(dt) == newyears(Dates.year(dt)) isindependenceday(dt) = Dates.yearmonthday(dt) == independenceday(Dates.year(dt)) @@ -394,21 +396,21 @@ isthanksgiving(dt) = Dates.dayofweek(dt) == Dates.Thu && function easter(y) # Butcher's Algorithm: http://www.smart.net/~mmontes/butcher.html - a=y%19 - b=div(y,100) - c=y%100 - d=div(b,4) - e=b%4 - f=div(b+8,25) - g=div(b-f+1,3) - h=(19*a+b-d-g+15)%30 - i=div(c,4) - k=c%4 - l=(32+2*e+2*i-h-k)%7 - m=div(a+11*h+22*l,451) - month=div(h+l-7*m+114,31) - p=(h+l-7*m+114)%31 - return (y,month,p+1) + a = y % 19 + b = div(y, 100) + c = y % 100 + d = div(b, 4) + e = b % 4 + f = div(b + 8, 25) + g = div(b - f + 1, 3) + h = (19 * a + b - d - g + 15) % 30 + i = div(c, 4) + k = c % 4 + l = (32 + 2 * e + 2 * i - h - k) % 7 + m = div(a + 11 * h + 22 * l, 451) + month = div(h + l - 7 * m + 114, 31) + p = (h + l - 7 * m + 114) % 31 + return (y, month, p + 1) end iseaster(dt) = Dates.yearmonthday(dt) == easter(Dates.year(dt)) @@ -418,27 +420,27 @@ const HOLIDAYS = x->isnewyears(x) || isindependenceday(x) || ismemorialday(x) || islaborday(x) || iscolumbusday(x) || isthanksgiving(x) -@test length(filter(HOLIDAYS,dr)) == 11 +@test length(filter(HOLIDAYS, dr)) == 11 const OBSERVEDHOLIDAYS = x->begin # If the holiday is on a weekday if HOLIDAYS(x) && Dates.dayofweek(x) < Dates.Saturday return true # Holiday is observed Monday if falls on Sunday - elseif Dates.dayofweek(x) == 1 && HOLIDAYS(x-Dates.Day(1)) + elseif Dates.dayofweek(x) == 1 && HOLIDAYS(x - Dates.Day(1)) return true # Holiday is observed Friday if falls on Saturday - elseif Dates.dayofweek(x) == 5 && HOLIDAYS(x+Dates.Day(1)) + elseif Dates.dayofweek(x) == 5 && HOLIDAYS(x + Dates.Day(1)) return true else return false end end -observed = filter(OBSERVEDHOLIDAYS,Dates.Date(1999):Dates.Date(2000)) +observed = filter(OBSERVEDHOLIDAYS, Dates.Date(1999):Dates.Date(2000)) @test length(observed) == 11 -@test observed[10] == Dates.Date(1999,12,24) -@test observed[11] == Dates.Date(1999,12,31) +@test observed[10] == Dates.Date(1999, 12, 24) +@test observed[11] == Dates.Date(1999, 12, 31) # Get all business/working days of 2014 # Since we have already defined observed holidays, @@ -449,8 +451,8 @@ observed = filter(OBSERVEDHOLIDAYS,Dates.Date(1999):Dates.Date(2000)) end) == 251 # First day of the next month for each day of 2014 -@test length([Dates.firstdayofmonth(i+Dates.Month(1)) - for i in Dates.Date(2014):Dates.Date(2014,12,31)]) == 365 +@test length([Dates.firstdayofmonth(i + Dates.Month(1)) + for i in Dates.Date(2014):Dates.Date(2014, 12, 31)]) == 365 # From those goofy email forwards claiming a "special, lucky month" # that has 5 Fridays, 5 Saturdays, and 5 Sundays and that it only @@ -465,9 +467,9 @@ end) == 251 end) == 15 # On average, there's one of those months every year r = Dates.Time(x->Dates.second(x) == 5, 1) -@test r == Dates.Time(1,0,5) +@test r == Dates.Time(1, 0, 5) r = filter(x->Dates.second(x) == 5, Dates.Time(0):Dates.Time(10)) @test length(r) == 600 -@test first(r) == Dates.Time(0,0,5) -@test last(r) == Dates.Time(9,59,5) +@test first(r) == Dates.Time(0, 0, 5) +@test last(r) == Dates.Time(9, 59, 5) diff --git a/test/dates/conversions.jl b/test/dates/conversions.jl index 00c7538b5ca6b..7767de7b9c019 100644 --- a/test/dates/conversions.jl +++ b/test/dates/conversions.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license # Test conversion to and from unix -@test Dates.unix2datetime(Dates.datetime2unix(DateTime(2000,1,1))) == DateTime(2000,1,1) +@test Dates.unix2datetime(Dates.datetime2unix(DateTime(2000, 1, 1))) == DateTime(2000, 1, 1) @test Dates.value(Dates.DateTime(1970)) == Dates.UNIXEPOCH # Tests from here: https://en.wikipedia.org/wiki/Unix_time @@ -29,20 +29,20 @@ @test string(Dates.unix2datetime(915148801.25)) == string("1999-01-01T00:00:01.25") # Test conversion to and from Rata Die -@test Date(Dates.rata2datetime(734869)) == Dates.Date(2013,1,1) +@test Date(Dates.rata2datetime(734869)) == Dates.Date(2013, 1, 1) @test Dates.datetime2rata(Dates.rata2datetime(734869)) == 734869 # Tests from here: http://mysite.verizon.net/aesir_research/date/back.htm#JDN -@test Dates.julian2datetime(1721119.5) == Dates.DateTime(0,3,1) -@test Dates.julian2datetime(1721424.5) == Dates.DateTime(0,12,31) -@test Dates.julian2datetime(1721425.5) == Dates.DateTime(1,1,1) -@test Dates.julian2datetime(2299149.5) == Dates.DateTime(1582,10,4) -@test Dates.julian2datetime(2415020.5) == Dates.DateTime(1900,1,1) -@test Dates.julian2datetime(2415385.5) == Dates.DateTime(1901,1,1) -@test Dates.julian2datetime(2440587.5) == Dates.DateTime(1970,1,1) -@test Dates.julian2datetime(2444239.5) == Dates.DateTime(1980,1,1) -@test Dates.julian2datetime(2452695.625) == Dates.DateTime(2003,2,25,3) -@test Dates.datetime2julian(Dates.DateTime(2013,12,3,21)) == 2456630.375 +@test Dates.julian2datetime(1721119.5) == Dates.DateTime(0, 3, 1) +@test Dates.julian2datetime(1721424.5) == Dates.DateTime(0, 12, 31) +@test Dates.julian2datetime(1721425.5) == Dates.DateTime(1, 1, 1) +@test Dates.julian2datetime(2299149.5) == Dates.DateTime(1582, 10, 4) +@test Dates.julian2datetime(2415020.5) == Dates.DateTime(1900, 1, 1) +@test Dates.julian2datetime(2415385.5) == Dates.DateTime(1901, 1, 1) +@test Dates.julian2datetime(2440587.5) == Dates.DateTime(1970, 1, 1) +@test Dates.julian2datetime(2444239.5) == Dates.DateTime(1980, 1, 1) +@test Dates.julian2datetime(2452695.625) == Dates.DateTime(2003, 2, 25, 3) +@test Dates.datetime2julian(Dates.DateTime(2013, 12, 3, 21)) == 2456630.375 @test typeof(Dates.now()) <: Dates.DateTime @test typeof(Dates.today()) <: Dates.Date @@ -56,7 +56,7 @@ end @test abs(Dates.now() - now(Dates.UTC)) < Dates.Hour(16) # Issue #9171, #9169 -let t = Dates.Period[Dates.Week(2), Dates.Day(14), Dates.Hour(14*24), Dates.Minute(14*24*60), Dates.Second(14*24*60*60), Dates.Millisecond(14*24*60*60*1000)] +let t = Dates.Period[Dates.Week(2), Dates.Day(14), Dates.Hour(14 * 24), Dates.Minute(14 * 24 * 60), Dates.Second(14 * 24 * 60 * 60), Dates.Millisecond(14 * 24 * 60 * 60 * 1000)] for i = 1:length(t) Pi = typeof(t[i]) for j = 1:length(t) @@ -79,7 +79,7 @@ end @test_throws InexactError Dates.Month(Dates.Year(typemax(Int64))) # Ensure that conversion of 32-bit integers work -let dt = DateTime(1915,1,1,12) +let dt = DateTime(1915, 1, 1, 12) unix = Int32(Dates.datetime2unix(dt)) julian = Int32(Dates.datetime2julian(dt)) @@ -102,7 +102,7 @@ b = Dates.Date(2000) @test Dates.Date(Dates.UTD(730120.0)) == b @test Dates.Date(Dates.UTD(Int32(730120))) == b -dt = Dates.DateTime(2000,1,1,23,59,59,50) +dt = Dates.DateTime(2000, 1, 1, 23, 59, 59, 50) t = Dates.Time(dt) @test Dates.hour(t) == 23 @test Dates.minute(t) == 59 diff --git a/test/dates/io.jl b/test/dates/io.jl index e7b4951eff8b5..e8172db8c6cbd 100644 --- a/test/dates/io.jl +++ b/test/dates/io.jl @@ -1,30 +1,30 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license # Test string/show representation of Date -@test string(Dates.Date(1,1,1)) == "0001-01-01" # January 1st, 1 AD/CE -@test sprint(show, Dates.Date(1,1,1)) == "0001-01-01" -@test string(Dates.Date(0,12,31)) == "0000-12-31" # December 31, 1 BC/BCE -@test Dates.Date(1,1,1) - Dates.Date(0,12,31) == Dates.Day(1) -@test Dates.Date(Dates.UTD(-306)) == Dates.Date(0,2,29) -@test string(Dates.Date(0,1,1)) == "0000-01-01" # January 1st, 1 BC/BCE -@test string(Dates.Date(-1,1,1)) == "-0001-01-01" # January 1st, 2 BC/BCE -@test string(Dates.Date(-1000000,1,1)) == "-1000000-01-01" -@test string(Dates.Date(1000000,1,1)) == "1000000-01-01" -@test string(Dates.DateTime(2000,1,1,0,0,0,1)) == "2000-01-01T00:00:00.001" -@test sprint(show,Dates.DateTime(2000,1,1,0,0,0,1)) == "2000-01-01T00:00:00.001" -@test string(Dates.DateTime(2000,1,1,0,0,0,2)) == "2000-01-01T00:00:00.002" -@test string(Dates.DateTime(2000,1,1,0,0,0,500)) == "2000-01-01T00:00:00.5" -@test string(Dates.DateTime(2000,1,1,0,0,0,998)) == "2000-01-01T00:00:00.998" -@test string(Dates.DateTime(2000,1,1,0,0,0,999)) == "2000-01-01T00:00:00.999" +@test string(Dates.Date(1, 1, 1)) == "0001-01-01" # January 1st, 1 AD/CE +@test sprint(show, Dates.Date(1, 1, 1)) == "0001-01-01" +@test string(Dates.Date(0, 12, 31)) == "0000-12-31" # December 31, 1 BC/BCE +@test Dates.Date(1, 1, 1) - Dates.Date(0, 12, 31) == Dates.Day(1) +@test Dates.Date(Dates.UTD(-306)) == Dates.Date(0, 2, 29) +@test string(Dates.Date(0, 1, 1)) == "0000-01-01" # January 1st, 1 BC/BCE +@test string(Dates.Date(-1, 1, 1)) == "-0001-01-01" # January 1st, 2 BC/BCE +@test string(Dates.Date(-1000000, 1, 1)) == "-1000000-01-01" +@test string(Dates.Date(1000000, 1, 1)) == "1000000-01-01" +@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "2000-01-01T00:00:00.001" +@test sprint(show, Dates.DateTime(2000, 1, 1, 0, 0, 0, 1)) == "2000-01-01T00:00:00.001" +@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 2)) == "2000-01-01T00:00:00.002" +@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 500)) == "2000-01-01T00:00:00.5" +@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 998)) == "2000-01-01T00:00:00.998" +@test string(Dates.DateTime(2000, 1, 1, 0, 0, 0, 999)) == "2000-01-01T00:00:00.999" @test string(Dates.Time(0)) == "00:00:00" -@test string(Dates.Time(0,1)) == "00:01:00" -@test string(Dates.Time(0,1,2)) == "00:01:02" -@test string(Dates.Time(0,1,2,3)) == "00:01:02.003" -@test string(Dates.Time(0,1,2,3,4)) == "00:01:02.003004" -@test string(Dates.Time(0,1,2,3,4,5)) == "00:01:02.003004005" -@test string(Dates.Time(0,0,0,0,1)) == "00:00:00.000001" -@test string(Dates.Time(0,0,0,0,0,1)) == "00:00:00.000000001" -@test string(Dates.Time(0,0,0,1)) == "00:00:00.001" +@test string(Dates.Time(0, 1)) == "00:01:00" +@test string(Dates.Time(0, 1, 2)) == "00:01:02" +@test string(Dates.Time(0, 1, 2, 3)) == "00:01:02.003" +@test string(Dates.Time(0, 1, 2, 3, 4)) == "00:01:02.003004" +@test string(Dates.Time(0, 1, 2, 3, 4, 5)) == "00:01:02.003004005" +@test string(Dates.Time(0, 0, 0, 0, 1)) == "00:00:00.000001" +@test string(Dates.Time(0, 0, 0, 0, 0, 1)) == "00:00:00.000000001" +@test string(Dates.Time(0, 0, 0, 1)) == "00:00:00.001" # DateTime parsing # Useful reference for different locales: http://library.princeton.edu/departments/tsd/katmandu/reference/months.html @@ -45,36 +45,36 @@ end # Common Parsing Patterns #'1996-January-15' -dt = Dates.DateTime(1996,1,15) +dt = Dates.DateTime(1996, 1, 15) f = "yy-mm-dd" a = "96-01-15" -@test DateTime(a,f) + Dates.Year(1900) == dt -@test Dates.format(dt,f) == a +@test DateTime(a, f) + Dates.Year(1900) == dt +@test Dates.format(dt, f) == a a1 = "96-1-15" -@test Dates.DateTime(a1,f) + Dates.Year(1900) == dt -@test Dates.format(dt,"yy-m-dd") == a1 +@test Dates.DateTime(a1, f) + Dates.Year(1900) == dt +@test Dates.format(dt, "yy-m-dd") == a1 a2 = "96-1-1" -@test Dates.DateTime(a2,f) + Dates.Year(1900) + Dates.Day(14) == dt -@test Dates.format(dt-Dates.Day(14),"yy-m-d") == a2 +@test Dates.DateTime(a2, f) + Dates.Year(1900) + Dates.Day(14) == dt +@test Dates.format(dt - Dates.Day(14), "yy-m-d") == a2 a3 = "1996-1-15" -@test Dates.DateTime(a3,f) == dt -@test Dates.format(dt,"yyyy-m-d") == a3 +@test Dates.DateTime(a3, f) == dt +@test Dates.format(dt, "yyyy-m-d") == a3 a4 = "1996-Jan-15" -@test_throws ArgumentError Dates.DateTime(a4,f) # Trying to use month name, but specified only "mm" +@test_throws ArgumentError Dates.DateTime(a4, f) # Trying to use month name, but specified only "mm" f = "yy/uuu/dd" b = "96/Feb/15" -@test Dates.DateTime(b,f) + Dates.Year(1900) == dt + Dates.Month(1) -@test Dates.format(dt+Dates.Month(1),f) == b +@test Dates.DateTime(b, f) + Dates.Year(1900) == dt + Dates.Month(1) +@test Dates.format(dt + Dates.Month(1), f) == b b1 = "1996/Feb/15" -@test Dates.DateTime(b1,f) == dt + Dates.Month(1) -@test Dates.format(dt+Dates.Month(1),"yyyy/uuu/dd") == b1 +@test Dates.DateTime(b1, f) == dt + Dates.Month(1) +@test Dates.format(dt + Dates.Month(1), "yyyy/uuu/dd") == b1 b2 = "96/Feb/1" -@test Dates.DateTime(b2,f) + Dates.Year(1900) + Dates.Day(14) == dt + Dates.Month(1) -@test Dates.format(dt+Dates.Month(1)-Dates.Day(14),"yy/uuu/d") == b2 +@test Dates.DateTime(b2, f) + Dates.Year(1900) + Dates.Day(14) == dt + Dates.Month(1) +@test Dates.format(dt + Dates.Month(1) - Dates.Day(14), "yy/uuu/d") == b2 # Here we've specifed a text month name, but given a number b3 = "96/2/15" -@test_throws ArgumentError Dates.DateTime(b3,f) +@test_throws ArgumentError Dates.DateTime(b3, f) try Dates.tryparse_internal(DateTime, "2012/02/20T09:09:31.25i90", dateformat"yyyy/mm/ddTHH:MM:SS.s", true) @test false @@ -92,247 +92,247 @@ end f = "yy:dd:mm" c = "96:15:01" -@test Dates.DateTime(c,f) + Dates.Year(1900) == dt -@test Dates.format(dt,f) == c +@test Dates.DateTime(c, f) + Dates.Year(1900) == dt +@test Dates.format(dt, f) == c c1 = "1996:15:01" -@test Dates.DateTime(c1,f) == dt -@test Dates.format(dt,"yyyy:dd:mm") == c1 +@test Dates.DateTime(c1, f) == dt +@test Dates.format(dt, "yyyy:dd:mm") == c1 c2 = "96:15:1" -@test Dates.DateTime(c2,f) + Dates.Year(1900) == dt -@test Dates.format(dt,"yy:dd:m") == c2 +@test Dates.DateTime(c2, f) + Dates.Year(1900) == dt +@test Dates.format(dt, "yy:dd:m") == c2 c3 = "96:1:01" -@test Dates.DateTime(c3,f) + Dates.Year(1900) + Dates.Day(14) == dt -@test Dates.format(dt-Dates.Day(14),"yy:m:dd") == c3 +@test Dates.DateTime(c3, f) + Dates.Year(1900) + Dates.Day(14) == dt +@test Dates.format(dt - Dates.Day(14), "yy:m:dd") == c3 c4 = "1996:15:01 # random comment" -@test_throws ArgumentError Dates.DateTime(c4,f) - -f = "yyyy,uuu,dd" -d = "1996,Jan,15" -@test Dates.DateTime(d,f) == dt -@test Dates.format(dt,f) == d -d1 = "96,Jan,15" -@test Dates.DateTime(d1,f) + Dates.Year(1900) == dt -@test Dates.format(dt,"yy,uuu,dd") == d1 -d2 = "1996,Jan,1" -@test Dates.DateTime(d2,f) + Dates.Day(14) == dt -@test Dates.format(dt-Dates.Day(14),"yyyy,uuu,d") == d2 -d3 = "1996,2,15" -@test_throws ArgumentError Dates.DateTime(d3,f) +@test_throws ArgumentError Dates.DateTime(c4, f) + +f = "yyyy, uuu, dd" +d = "1996, Jan, 15" +@test Dates.DateTime(d, f) == dt +@test Dates.format(dt, f) == d +d1 = "96, Jan, 15" +@test Dates.DateTime(d1, f) + Dates.Year(1900) == dt +@test Dates.format(dt, "yy, uuu, dd") == d1 +d2 = "1996, Jan, 1" +@test Dates.DateTime(d2, f) + Dates.Day(14) == dt +@test Dates.format(dt - Dates.Day(14), "yyyy, uuu, d") == d2 +d3 = "1996, 2, 15" +@test_throws ArgumentError Dates.DateTime(d3, f) f = "yyyy.U.dd" e = "1996.January.15" -@test Dates.DateTime(e,f) == dt -@test Dates.format(dt,f) == e +@test Dates.DateTime(e, f) == dt +@test Dates.format(dt, f) == e e1 = "96.January.15" -@test Dates.DateTime(e1,f) + Dates.Year(1900) == dt -@test Dates.format(dt,"yy.U.dd") == e1 +@test Dates.DateTime(e1, f) + Dates.Year(1900) == dt +@test Dates.format(dt, "yy.U.dd") == e1 fo = "yyyy m dd" f = "1996 1 15" -@test Dates.DateTime(f,fo) == dt -@test Dates.format(dt,fo) == f +@test Dates.DateTime(f, fo) == dt +@test Dates.format(dt, fo) == f f1 = "1996 01 15" -@test Dates.DateTime(f1,fo) == dt -@test Dates.format(dt,"yyyy mm dd") == f1 +@test Dates.DateTime(f1, fo) == dt +@test Dates.format(dt, "yyyy mm dd") == f1 f2 = "1996 1 1" -@test Dates.DateTime(f2,fo) + Dates.Day(14) == dt -@test Dates.format(dt-Dates.Day(14),"yyyy m d") == f2 +@test Dates.DateTime(f2, fo) + Dates.Day(14) == dt +@test Dates.format(dt - Dates.Day(14), "yyyy m d") == f2 j = "1996-01-15" f = "yyyy-mm-dd zzz" -@test Dates.DateTime(j,f) == dt -@test Dates.format(dt,f) == j*" zzz" +@test Dates.DateTime(j, f) == dt +@test Dates.format(dt, f) == j * " zzz" k = "1996-01-15 10:00:00" f = "yyyy-mm-dd HH:MM:SS zzz" -@test Dates.DateTime(k,f) == dt + Dates.Hour(10) -@test Dates.format(dt+Dates.Hour(10),f) == k*" zzz" +@test Dates.DateTime(k, f) == dt + Dates.Hour(10) +@test Dates.format(dt + Dates.Hour(10), f) == k * " zzz" l = "1996-01-15 10:10:10.25" f = "yyyy-mm-dd HH:MM:SS.ss zzz" -@test Dates.DateTime(l,f) == dt + Dates.Hour(10) + Dates.Minute(10) + Dates.Second(10) + Dates.Millisecond(250) -@test Dates.format(dt+Dates.Hour(10)+Dates.Minute(10)+Dates.Second(10)+Dates.Millisecond(250),f) == l*" zzz" +@test Dates.DateTime(l, f) == dt + Dates.Hour(10) + Dates.Minute(10) + Dates.Second(10) + Dates.Millisecond(250) +@test Dates.format(dt + Dates.Hour(10) + Dates.Minute(10) + Dates.Second(10) + Dates.Millisecond(250), f) == l * " zzz" r = "1/15/1996" # Excel f = "m/dd/yyyy" -@test Dates.DateTime(r,f) == dt -@test Dates.format(dt,f) == r +@test Dates.DateTime(r, f) == dt +@test Dates.format(dt, f) == r s = "19960115" f = "yyyymmdd" -@test Dates.DateTime(s,f) == dt -@test Dates.format(dt,f) == s +@test Dates.DateTime(s, f) == dt +@test Dates.format(dt, f) == s v = "1996-01-15 10:00:00" f = "yyyy-mm-dd HH:MM:SS" -@test Dates.DateTime(v,f) == dt + Dates.Hour(10) -@test Dates.format(dt+Dates.Hour(10),f) == v +@test Dates.DateTime(v, f) == dt + Dates.Hour(10) +@test Dates.format(dt + Dates.Hour(10), f) == v w = "1996-01-15T10:00:00" f = "yyyy-mm-ddTHH:MM:SS zzz" -@test Dates.DateTime(w,f) == dt + Dates.Hour(10) -@test Dates.format(dt+Dates.Hour(10),f) == w*" zzz" +@test Dates.DateTime(w, f) == dt + Dates.Hour(10) +@test Dates.format(dt + Dates.Hour(10), f) == w * " zzz" f = "yyyy/m" y = "1996/1" -@test Dates.DateTime(y,f) == dt - Dates.Day(14) -@test Dates.format(dt,f) == y +@test Dates.DateTime(y, f) == dt - Dates.Day(14) +@test Dates.format(dt, f) == y y1 = "1996/1/15" -@test_throws ArgumentError Dates.DateTime(y1,f) +@test_throws ArgumentError Dates.DateTime(y1, f) y2 = "96/1" -@test Dates.DateTime(y2,f) + Dates.Year(1900) == dt - Dates.Day(14) -@test Dates.format(dt,"yy/m") == y2 +@test Dates.DateTime(y2, f) + Dates.Year(1900) == dt - Dates.Day(14) +@test Dates.format(dt, "yy/m") == y2 f = "yyyy" z = "1996" -@test Dates.DateTime(z,f) == dt - Dates.Day(14) -@test Dates.format(dt,f) == z +@test Dates.DateTime(z, f) == dt - Dates.Day(14) +@test Dates.format(dt, f) == z z1 = "1996-3" -@test_throws ArgumentError Dates.DateTime(z1,f) +@test_throws ArgumentError Dates.DateTime(z1, f) z2 = "1996-3-1" -@test_throws ArgumentError Dates.DateTime(z2,f) +@test_throws ArgumentError Dates.DateTime(z2, f) aa = "1/5/1996" f = "m/d/yyyy" -@test Dates.DateTime(aa,f) == dt - Dates.Day(10) -@test Dates.format(dt-Dates.Day(10),f) == aa +@test Dates.DateTime(aa, f) == dt - Dates.Day(10) +@test Dates.format(dt - Dates.Day(10), f) == aa bb = "5/1/1996" f = "d/m/yyyy" -@test Dates.DateTime(bb,f) == dt - Dates.Day(10) -@test Dates.format(dt-Dates.Day(10),f) == bb +@test Dates.DateTime(bb, f) == dt - Dates.Day(10) +@test Dates.format(dt - Dates.Day(10), f) == bb cc = "01151996" f = "mmddyyyy" -@test Dates.DateTime(cc,f) == dt -@test Dates.format(dt,f) == cc +@test Dates.DateTime(cc, f) == dt +@test Dates.format(dt, f) == cc dd = "15011996" f = "ddmmyyyy" -@test Dates.DateTime(dd,f) == dt -@test Dates.format(dt,f) == dd +@test Dates.DateTime(dd, f) == dt +@test Dates.format(dt, f) == dd ee = "01199615" f = "mmyyyydd" -@test Dates.DateTime(ee,f) == dt -@test Dates.format(dt,f) == ee +@test Dates.DateTime(ee, f) == dt +@test Dates.format(dt, f) == ee ff = "1996-15-Jan" f = "yyyy-dd-uuu" -@test Dates.DateTime(ff,f) == dt -@test Dates.format(dt,f) == ff +@test Dates.DateTime(ff, f) == dt +@test Dates.format(dt, f) == ff gg = "Jan-1996-15" f = "uuu-yyyy-dd" -@test Dates.DateTime(gg,f) == dt -@test Dates.format(dt,f) == gg +@test Dates.DateTime(gg, f) == dt +@test Dates.format(dt, f) == gg hh = "1996#1#15" f = "yyyy#m#d" -@test Dates.DateTime(hh,f) == dt -@test Dates.format(dt,f) == hh +@test Dates.DateTime(hh, f) == dt +@test Dates.format(dt, f) == hh # test prefix. s = "/1996/1/15" f = "/yyyy/m/d" -@test Dates.DateTime(s,f) == dt -@test Dates.format(dt,f) == s -@test_throws ArgumentError Dates.DateTime("1996/1/15",f) +@test Dates.DateTime(s, f) == dt +@test Dates.format(dt, f) == s +@test_throws ArgumentError Dates.DateTime("1996/1/15", f) # from Jiahao -@test Dates.Date("2009年12月01日","yyyy年mm月dd日") == Dates.Date(2009,12,1) -@test Dates.format(Dates.Date(2009,12,1),"yyyy年mm月dd日") == "2009年12月01日" -@test Dates.Date("2009-12-01","yyyy-mm-dd") == Dates.Date(2009,12,1) +@test Dates.Date("2009年12月01日", "yyyy年mm月dd日") == Dates.Date(2009, 12, 1) +@test Dates.format(Dates.Date(2009, 12, 1), "yyyy年mm月dd日") == "2009年12月01日" +@test Dates.Date("2009-12-01", "yyyy-mm-dd") == Dates.Date(2009, 12, 1) # French: from Milan f = "dd/mm/yyyy" f2 = "dd/mm/yy" -@test Dates.Date("28/05/2014",f) == Dates.Date(2014,5,28) -@test Dates.Date("28/05/14",f2) + Dates.Year(2000) == Dates.Date(2014,5,28) +@test Dates.Date("28/05/2014", f) == Dates.Date(2014, 5, 28) +@test Dates.Date("28/05/14", f2) + Dates.Year(2000) == Dates.Date(2014, 5, 28) # Customizing locale Dates.LOCALES["french"] = Dates.DateLocale( ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"], - ["janv","févr","mars","avril","mai","juin", - "juil","août","sept","oct","nov","déc"], - ["lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"], + ["janv", "févr", "mars", "avril", "mai", "juin", + "juil", "août", "sept", "oct", "nov", "déc"], + ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"], [""], ) f = "dd uuuuu yyyy" -@test Dates.Date("28 mai 2014",f;locale="french") == Dates.Date(2014,5,28) -@test Dates.format(Dates.Date(2014,5,28),f;locale="french") == "28 mai 2014" -@test Dates.Date("28 févr 2014",f;locale="french") == Dates.Date(2014,2,28) -@test Dates.format(Dates.Date(2014,2,28),f;locale="french") == "28 févr 2014" -@test Dates.Date("28 août 2014",f;locale="french") == Dates.Date(2014,8,28) -@test Dates.format(Dates.Date(2014,8,28),f;locale="french") == "28 août 2014" -@test Dates.Date("28 avril 2014",f;locale="french") == Dates.Date(2014,4,28) -@test Dates.format(Dates.Date(2014,4,28),f;locale="french") == "28 avril 2014" +@test Dates.Date("28 mai 2014", f; locale="french") == Dates.Date(2014, 5, 28) +@test Dates.format(Dates.Date(2014, 5, 28), f; locale="french") == "28 mai 2014" +@test Dates.Date("28 févr 2014", f; locale="french") == Dates.Date(2014, 2, 28) +@test Dates.format(Dates.Date(2014, 2, 28), f; locale="french") == "28 févr 2014" +@test Dates.Date("28 août 2014", f; locale="french") == Dates.Date(2014, 8, 28) +@test Dates.format(Dates.Date(2014, 8, 28), f; locale="french") == "28 août 2014" +@test Dates.Date("28 avril 2014", f; locale="french") == Dates.Date(2014, 4, 28) +@test Dates.format(Dates.Date(2014, 4, 28), f; locale="french") == "28 avril 2014" f = "dd u yyyy" -@test Dates.Date("28 avril 2014",f;locale="french") == Dates.Date(2014,4,28) +@test Dates.Date("28 avril 2014", f; locale="french") == Dates.Date(2014, 4, 28) f = "dduuuuyyyy" # parses 3 and 4 character month names -@test Dates.Date("28mai2014",f;locale="french") == Dates.Date(2014,5,28) -@test Dates.Date("28août2014",f;locale="french") == Dates.Date(2014,8,28) +@test Dates.Date("28mai2014", f; locale="french") == Dates.Date(2014, 5, 28) +@test Dates.Date("28août2014", f; locale="french") == Dates.Date(2014, 8, 28) # doesn't parse month name greater than 4 chars -@test_throws ArgumentError Dates.Date("28avril2014",f;locale="french") +@test_throws ArgumentError Dates.Date("28avril2014", f; locale="french") # From Tony Fong f = "dduuuyy" -@test Dates.Date("01Dec09",f) + Dates.Year(2000) == Dates.Date(2009,12,1) -@test Dates.format(Dates.Date(2009,12,1),f) == "01Dec09" +@test Dates.Date("01Dec09", f) + Dates.Year(2000) == Dates.Date(2009, 12, 1) +@test Dates.format(Dates.Date(2009, 12, 1), f) == "01Dec09" f = "dduuuyyyy" -@test Dates.Date("01Dec2009",f) == Dates.Date(2009,12,1) -@test Dates.format(Dates.Date(2009,12,1),f) == "01Dec2009" +@test Dates.Date("01Dec2009", f) == Dates.Date(2009, 12, 1) +@test Dates.format(Dates.Date(2009, 12, 1), f) == "01Dec2009" f = "duy" -const globex = ["f","g","h","j","k","m","n","q","u","v","x","z"] +const globex = ["f", "g", "h", "j", "k", "m", "n", "q", "u", "v", "x", "z"] locale = Dates.DateLocale(globex, map(uppercase, globex), globex[1:7], globex[1:7]) -@test Dates.Date("1F4",f;locale=locale) + Dates.Year(2010) == Dates.Date(2014,1,1) -@test Dates.format(Dates.Date(2014,1,1),f;locale=locale) == "1F4" +@test Dates.Date("1F4", f; locale=locale) + Dates.Year(2010) == Dates.Date(2014, 1, 1) +@test Dates.format(Dates.Date(2014, 1, 1), f; locale=locale) == "1F4" # From Matt Bauman f = "yyyy-mm-ddTHH:MM:SS" -@test Dates.DateTime("2014-05-28T16:46:04",f) == Dates.DateTime(2014,5,28,16,46,04) +@test Dates.DateTime("2014-05-28T16:46:04", f) == Dates.DateTime(2014, 5, 28, 16, 46, 04) # Try to break stuff # Specified mm/dd, but date string has day/mm -@test_throws ArgumentError Dates.DateTime("18/05/2009","mm/dd/yyyy") -@test_throws ArgumentError Dates.DateTime("18/05/2009 16","mm/dd/yyyy hh") +@test_throws ArgumentError Dates.DateTime("18/05/2009", "mm/dd/yyyy") +@test_throws ArgumentError Dates.DateTime("18/05/2009 16", "mm/dd/yyyy hh") # Used "mm" for months AND minutes -@test_throws ArgumentError Dates.DateTime("18/05/2009 16:12","mm/dd/yyyy hh:mm") +@test_throws ArgumentError Dates.DateTime("18/05/2009 16:12", "mm/dd/yyyy hh:mm") # Date string has different delimiters than format string -@test_throws ArgumentError Dates.DateTime("18:05:2009","mm/dd/yyyy") +@test_throws ArgumentError Dates.DateTime("18:05:2009", "mm/dd/yyyy") f = "y m d" -@test Dates.Date("1 1 1",f) == Dates.Date(1) -@test Dates.Date("10000000000 1 1",f) == Dates.Date(10000000000) -@test_throws ArgumentError Dates.Date("1 13 1",f) -@test_throws ArgumentError Dates.Date("1 1 32",f) -@test_throws ArgumentError Dates.Date(" 1 1 32",f) -@test_throws ArgumentError Dates.Date("# 1 1 32",f) -@test Dates.Date("1",f) == Dates.Date(1) -@test Dates.Date("1 ",f) == Dates.Date(1) -@test Dates.Date("1 2",f) == Dates.Date(1,2) +@test Dates.Date("1 1 1", f) == Dates.Date(1) +@test Dates.Date("10000000000 1 1", f) == Dates.Date(10000000000) +@test_throws ArgumentError Dates.Date("1 13 1", f) +@test_throws ArgumentError Dates.Date("1 1 32", f) +@test_throws ArgumentError Dates.Date(" 1 1 32", f) +@test_throws ArgumentError Dates.Date("# 1 1 32", f) +@test Dates.Date("1", f) == Dates.Date(1) +@test Dates.Date("1 ", f) == Dates.Date(1) +@test Dates.Date("1 2", f) == Dates.Date(1, 2) # can't find space delimiter (finds '/'), so fails -@test_throws ArgumentError Dates.Date("2000/1",f) +@test_throws ArgumentError Dates.Date("2000/1", f) f = "ymd" @test Dates.Date("111", f) == Dates.Date(1) @test Dates.Date("1", f) == Dates.Date(1) -@test Dates.DateTime("20140529 120000","yyyymmdd HHMMSS") == Dates.DateTime(2014,5,29,12) +@test Dates.DateTime("20140529 120000", "yyyymmdd HHMMSS") == Dates.DateTime(2014, 5, 29, 12) @test Dates.Date(string(Dates.Date(dt))) == Dates.Date(dt) @test Dates.DateTime(string(dt)) == dt # Vectorized -dr = ["2000-01-01","2000-01-02","2000-01-03","2000-01-04","2000-01-05" - ,"2000-01-06","2000-01-07","2000-01-08","2000-01-09","2000-01-10"] -dr2 = [Dates.Date(2000):Dates.Date(2000,1,10);] +dr = ["2000-01-01", "2000-01-02", "2000-01-03", "2000-01-04", "2000-01-05" + , "2000-01-06", "2000-01-07", "2000-01-08", "2000-01-09", "2000-01-10"] +dr2 = [Dates.Date(2000):Dates.Date(2000, 1, 10);] @test Dates.Date(dr) == dr2 -@test Dates.Date(dr,"yyyy-mm-dd") == dr2 +@test Dates.Date(dr, "yyyy-mm-dd") == dr2 @test Dates.DateTime.(dr) == Dates.DateTime.(dr2) -@test Dates.DateTime(dr,"yyyy-mm-dd") == Dates.DateTime.(dr2) +@test Dates.DateTime(dr, "yyyy-mm-dd") == Dates.DateTime.(dr2) @test Dates.format(dr2) == dr -@test Dates.format(dr2,"yyyy-mm-dd") == dr +@test Dates.format(dr2, "yyyy-mm-dd") == dr -@test typeof(Dates.Date(dr)) == Array{Date,1} +@test typeof(Dates.Date(dr)) == Array{Date, 1} # Issue 13 -t = Dates.DateTime(1,1,1,14,51,0,118) -@test Dates.DateTime("[14:51:00.118]","[HH:MM:SS.sss]") == t +t = Dates.DateTime(1, 1, 1, 14, 51, 0, 118) +@test Dates.DateTime("[14:51:00.118]", "[HH:MM:SS.sss]") == t @test Dates.DateTime("14:51:00.118", "HH:MM:SS.sss") == t @test Dates.DateTime("[14:51:00.118?", "[HH:MM:SS.sss?") == t @test Dates.DateTime("?14:51:00.118?", "?HH:MM:SS.sss?") == t @@ -340,32 +340,32 @@ t = Dates.DateTime(1,1,1,14,51,0,118) @test Dates.DateTime("14:51:00.118]", "HH:MM:SS.sss]") == t # RFC1123Format -dt = Dates.DateTime(2014,8,23,17,22,15) -@test Dates.format(dt,Dates.RFC1123Format) == "Sat, 23 Aug 2014 17:22:15" -@test Dates.DateTime(Dates.format(dt,Dates.RFC1123Format),Dates.RFC1123Format) == dt -@test Dates.format(dt,"yyyy-mm-ddTHH:MM:SS E") == "2014-08-23T17:22:15 Saturday" -@test Dates.format(dt,"yyyy-mm-ddTHH:MM:SS e") == "2014-08-23T17:22:15 Sat" -@test Dates.format(dt,"yyyy-mm-dd E") == "2014-08-23 Saturday" -@test Dates.format(dt,"yyyy-mm-dd e") == "2014-08-23 Sat" -@test Dates.format(dt,"yyyy-e-mm-dd") == "2014-Sat-08-23" - -@test Dates.format(Dates.DateTime(2014,1,2,0,0,0,999),Dates.RFC1123Format) == "Thu, 02 Jan 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,2,18,0,0,0,9),Dates.RFC1123Format) == "Tue, 18 Feb 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,3,8,0,0,0,9),Dates.RFC1123Format) == "Sat, 08 Mar 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,4,28,0,0,0,9),Dates.RFC1123Format) == "Mon, 28 Apr 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,5,10,0,0,0,9),Dates.RFC1123Format) == "Sat, 10 May 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,6,4,0,0,0,9),Dates.RFC1123Format) == "Wed, 04 Jun 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,7,13,0,0,0,9),Dates.RFC1123Format) == "Sun, 13 Jul 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,8,17,0,0,0,9),Dates.RFC1123Format) == "Sun, 17 Aug 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,9,20,0,0,0,9),Dates.RFC1123Format) == "Sat, 20 Sep 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,10,31,0,0,0,9),Dates.RFC1123Format) == "Fri, 31 Oct 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,11,2,0,0,0,9),Dates.RFC1123Format) == "Sun, 02 Nov 2014 00:00:00" -@test Dates.format(Dates.DateTime(2014,12,5,0,0,0,9),Dates.RFC1123Format) == "Fri, 05 Dec 2014 00:00:00" - -dt = Dates.DateTime(2016,11,12,7,45,36) -@test parse(Dates.DateTime,"Sat, 12 Nov 2016 07:45:36",Dates.RFC1123Format) == dt -@test parse(Dates.DateTime,"Mon, 12 Nov 2016 07:45:36",Dates.RFC1123Format) == dt # Wrong day of week -@test_throws ArgumentError parse(Date,"Foo, 12 Nov 2016 07:45:36",Dates.RFC1123Format) +dt = Dates.DateTime(2014, 8, 23, 17, 22, 15) +@test Dates.format(dt, Dates.RFC1123Format) == "Sat, 23 Aug 2014 17:22:15" +@test Dates.DateTime(Dates.format(dt, Dates.RFC1123Format), Dates.RFC1123Format) == dt +@test Dates.format(dt, "yyyy-mm-ddTHH:MM:SS E") == "2014-08-23T17:22:15 Saturday" +@test Dates.format(dt, "yyyy-mm-ddTHH:MM:SS e") == "2014-08-23T17:22:15 Sat" +@test Dates.format(dt, "yyyy-mm-dd E") == "2014-08-23 Saturday" +@test Dates.format(dt, "yyyy-mm-dd e") == "2014-08-23 Sat" +@test Dates.format(dt, "yyyy-e-mm-dd") == "2014-Sat-08-23" + +@test Dates.format(Dates.DateTime(2014, 1, 2, 0, 0, 0, 999), Dates.RFC1123Format) == "Thu, 02 Jan 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 2, 18, 0, 0, 0, 9), Dates.RFC1123Format) == "Tue, 18 Feb 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 3, 8, 0, 0, 0, 9), Dates.RFC1123Format) == "Sat, 08 Mar 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 4, 28, 0, 0, 0, 9), Dates.RFC1123Format) == "Mon, 28 Apr 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 5, 10, 0, 0, 0, 9), Dates.RFC1123Format) == "Sat, 10 May 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 6, 4, 0, 0, 0, 9), Dates.RFC1123Format) == "Wed, 04 Jun 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 7, 13, 0, 0, 0, 9), Dates.RFC1123Format) == "Sun, 13 Jul 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 8, 17, 0, 0, 0, 9), Dates.RFC1123Format) == "Sun, 17 Aug 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 9, 20, 0, 0, 0, 9), Dates.RFC1123Format) == "Sat, 20 Sep 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 10, 31, 0, 0, 0, 9), Dates.RFC1123Format) == "Fri, 31 Oct 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 11, 2, 0, 0, 0, 9), Dates.RFC1123Format) == "Sun, 02 Nov 2014 00:00:00" +@test Dates.format(Dates.DateTime(2014, 12, 5, 0, 0, 0, 9), Dates.RFC1123Format) == "Fri, 05 Dec 2014 00:00:00" + +dt = Dates.DateTime(2016, 11, 12, 7, 45, 36) +@test parse(Dates.DateTime, "Sat, 12 Nov 2016 07:45:36", Dates.RFC1123Format) == dt +@test parse(Dates.DateTime, "Mon, 12 Nov 2016 07:45:36", Dates.RFC1123Format) == dt # Wrong day of week +@test_throws ArgumentError parse(Date, "Foo, 12 Nov 2016 07:45:36", Dates.RFC1123Format) # Issue 15195 let f = "YY" @@ -376,7 +376,7 @@ end # Issue: https://github.com/quinnj/TimeZones.jl/issues/19 let ds = "2015-07-24T05:38:19.591Z", - dt = Dates.DateTime(2015,7,24,5,38,19,591), + dt = Dates.DateTime(2015, 7, 24, 5, 38, 19, 591), format = "yyyy-mm-ddTHH:MM:SS.sssZ", escaped_format = "yyyy-mm-dd\\THH:MM:SS.sss\\Z" @@ -401,6 +401,6 @@ let ds = "2015-07-24T05:38:19.591Z", end # Issue 10817 -@test Dates.Date("Apr 01 2014", "uuu dd yyyy") == Dates.Date(2014,4,1) +@test Dates.Date("Apr 01 2014", "uuu dd yyyy") == Dates.Date(2014, 4, 1) @test_throws ArgumentError Dates.Date("Apr 01 xx 2014", "uuu dd zz yyyy") @test_throws ArgumentError Dates.Date("Apr 01 xx 2014", "uuu dd yyyy") diff --git a/test/dates/periods.jl b/test/dates/periods.jl index f0e36908848b5..3b523f0816086 100644 --- a/test/dates/periods.jl +++ b/test/dates/periods.jl @@ -12,32 +12,32 @@ @test Dates.Year(10) % Dates.Year(4) == Dates.Year(2) @test gcd(Dates.Year(10), Dates.Year(4)) == Dates.Year(2) @test lcm(Dates.Year(10), Dates.Year(4)) == Dates.Year(20) -@test div(Dates.Year(10),Dates.Year(3)) == 3 -@test div(Dates.Year(10),Dates.Year(4)) == 2 -@test div(Dates.Year(10),4) == Dates.Year(2) +@test div(Dates.Year(10), Dates.Year(3)) == 3 +@test div(Dates.Year(10), Dates.Year(4)) == 2 +@test div(Dates.Year(10), 4) == Dates.Year(2) @test Dates.Year(10) / Dates.Year(4) == 2.5 -@test mod(Dates.Year(10),Dates.Year(4)) == Dates.Year(2) -@test mod(Dates.Year(-10),Dates.Year(4)) == Dates.Year(2) -@test mod(Dates.Year(10),4) == Dates.Year(2) -@test mod(Dates.Year(-10),4) == Dates.Year(2) +@test mod(Dates.Year(10), Dates.Year(4)) == Dates.Year(2) +@test mod(Dates.Year(-10), Dates.Year(4)) == Dates.Year(2) +@test mod(Dates.Year(10), 4) == Dates.Year(2) +@test mod(Dates.Year(-10), 4) == Dates.Year(2) -@test rem(Dates.Year(10),Dates.Year(4)) == Dates.Year(2) -@test rem(Dates.Year(-10),Dates.Year(4)) == Dates.Year(-2) -@test rem(Dates.Year(10),4) == Dates.Year(2) -@test rem(Dates.Year(-10),4) == Dates.Year(-2) +@test rem(Dates.Year(10), Dates.Year(4)) == Dates.Year(2) +@test rem(Dates.Year(-10), Dates.Year(4)) == Dates.Year(-2) +@test rem(Dates.Year(10), 4) == Dates.Year(2) +@test rem(Dates.Year(-10), 4) == Dates.Year(-2) t = Dates.Year(1) t2 = Dates.Year(2) -@test ([t,t,t,t,t] + Dates.Year(1)) == ([t2,t2,t2,t2,t2]) -@test (Dates.Year(1) + [t,t,t,t,t]) == ([t2,t2,t2,t2,t2]) -@test ([t2,t2,t2,t2,t2] - Dates.Year(1)) == ([t,t,t,t,t]) -@test_throws MethodError ([t,t,t,t,t] .* Dates.Year(1)) == ([t,t,t,t,t]) -@test ([t,t,t,t,t] * 1) == ([t,t,t,t,t]) -@test ([t,t,t,t,t] .% t2) == ([t,t,t,t,t]) -@test div.([t,t,t,t,t],Dates.Year(1)) == ([1,1,1,1,1]) -@test mod.([t,t,t,t,t],Dates.Year(2)) == ([t,t,t,t,t]) -@test [t,t,t] / t2 == [0.5,0.5,0.5] +@test ([t, t, t, t, t] + Dates.Year(1)) == ([t2, t2, t2, t2, t2]) +@test (Dates.Year(1) + [t, t, t, t, t]) == ([t2, t2, t2, t2, t2]) +@test ([t2, t2, t2, t2, t2] - Dates.Year(1)) == ([t, t, t, t, t]) +@test_throws MethodError ([t, t, t, t, t] .* Dates.Year(1)) == ([t, t, t, t, t]) +@test ([t, t, t, t, t] * 1) == ([t, t, t, t, t]) +@test ([t, t, t, t, t] .% t2) == ([t, t, t, t, t]) +@test div.([t, t, t, t, t], Dates.Year(1)) == ([1, 1, 1, 1, 1]) +@test mod.([t, t, t, t, t], Dates.Year(2)) == ([t, t, t, t, t]) +@test [t, t, t] / t2 == [0.5, 0.5, 0.5] @test abs(-t) == t #Period arithmetic @@ -61,23 +61,23 @@ ns = Dates.Nanosecond(1) @test Dates.Millisecond(ms) == ms @test Dates.Microsecond(us) == us @test Dates.Nanosecond(ns) == ns -@test Dates.Year(convert(Int8,1)) == y -@test Dates.Year(convert(UInt8,1)) == y -@test Dates.Year(convert(Int16,1)) == y -@test Dates.Year(convert(UInt16,1)) == y -@test Dates.Year(convert(Int32,1)) == y -@test Dates.Year(convert(UInt32,1)) == y -@test Dates.Year(convert(Int64,1)) == y -@test Dates.Year(convert(UInt64,1)) == y -@test Dates.Year(convert(Int128,1)) == y -@test Dates.Year(convert(UInt128,1)) == y -@test Dates.Year(convert(BigInt,1)) == y -@test Dates.Year(convert(BigFloat,1)) == y -@test Dates.Year(convert(Complex,1)) == y -@test Dates.Year(convert(Rational,1)) == y -@test Dates.Year(convert(Float16,1)) == y -@test Dates.Year(convert(Float32,1)) == y -@test Dates.Year(convert(Float64,1)) == y +@test Dates.Year(convert(Int8, 1)) == y +@test Dates.Year(convert(UInt8, 1)) == y +@test Dates.Year(convert(Int16, 1)) == y +@test Dates.Year(convert(UInt16, 1)) == y +@test Dates.Year(convert(Int32, 1)) == y +@test Dates.Year(convert(UInt32, 1)) == y +@test Dates.Year(convert(Int64, 1)) == y +@test Dates.Year(convert(UInt64, 1)) == y +@test Dates.Year(convert(Int128, 1)) == y +@test Dates.Year(convert(UInt128, 1)) == y +@test Dates.Year(convert(BigInt, 1)) == y +@test Dates.Year(convert(BigFloat, 1)) == y +@test Dates.Year(convert(Complex, 1)) == y +@test Dates.Year(convert(Rational, 1)) == y +@test Dates.Year(convert(Float16, 1)) == y +@test Dates.Year(convert(Float32, 1)) == y +@test Dates.Year(convert(Float64, 1)) == y @test y == y @test m == m @test w == w @@ -126,18 +126,18 @@ y2 = Dates.Year(2) @test_throws MethodError Dates.Year(mi) == y @test_throws MethodError Dates.Year(s) == y @test_throws MethodError Dates.Year(ms) == y -@test Dates.Year(Dates.Date(2013,1,1)) == Dates.Year(2013) -@test Dates.Year(Dates.DateTime(2013,1,1)) == Dates.Year(2013) -@test typeof(y+m) <: Dates.CompoundPeriod -@test typeof(m+y) <: Dates.CompoundPeriod -@test typeof(y+w) <: Dates.CompoundPeriod -@test typeof(y+d) <: Dates.CompoundPeriod -@test typeof(y+h) <: Dates.CompoundPeriod -@test typeof(y+mi) <: Dates.CompoundPeriod -@test typeof(y+s) <: Dates.CompoundPeriod -@test typeof(y+ms) <: Dates.CompoundPeriod -@test typeof(y+us) <: Dates.CompoundPeriod -@test typeof(y+ns) <: Dates.CompoundPeriod +@test Dates.Year(Dates.Date(2013, 1, 1)) == Dates.Year(2013) +@test Dates.Year(Dates.DateTime(2013, 1, 1)) == Dates.Year(2013) +@test typeof(y + m) <: Dates.CompoundPeriod +@test typeof(m + y) <: Dates.CompoundPeriod +@test typeof(y + w) <: Dates.CompoundPeriod +@test typeof(y + d) <: Dates.CompoundPeriod +@test typeof(y + h) <: Dates.CompoundPeriod +@test typeof(y + mi) <: Dates.CompoundPeriod +@test typeof(y + s) <: Dates.CompoundPeriod +@test typeof(y + ms) <: Dates.CompoundPeriod +@test typeof(y + us) <: Dates.CompoundPeriod +@test typeof(y + ns) <: Dates.CompoundPeriod @test y > m @test d < w @test mi < h @@ -160,23 +160,23 @@ y2 = Dates.Year(2) @test y * 4 == Dates.Year(4) @test y * 4f0 == Dates.Year(4) @test_throws InexactError y * 3//4 == Dates.Year(1) -@test div(y,2) == Dates.Year(0) -@test_throws MethodError div(2,y) == Dates.Year(2) -@test div(y,y) == 1 +@test div(y, 2) == Dates.Year(0) +@test_throws MethodError div(2, y) == Dates.Year(2) +@test div(y, y) == 1 @test y*10 % Dates.Year(5) == Dates.Year(0) @test_throws MethodError (y > 3) == false @test_throws MethodError (4 < y) == false @test 1 != y -t = [y,y,y,y,y] -@test t .+ Dates.Year(2) == [Dates.Year(3),Dates.Year(3),Dates.Year(3),Dates.Year(3),Dates.Year(3)] +t = [y, y, y, y, y] +@test t .+ Dates.Year(2) == [Dates.Year(3), Dates.Year(3), Dates.Year(3), Dates.Year(3), Dates.Year(3)] let x = Dates.Year(5), y = Dates.Year(2) - @test div(x,y)*y + rem(x,y) == x - @test fld(x,y)*y + mod(x,y) == x + @test div(x, y) * y + rem(x, y) == x + @test fld(x, y) * y + mod(x, y) == x end # Associativity -dt = Dates.DateTime(2012,12,21) +dt = Dates.DateTime(2012, 12, 21) test = ((((((((dt + y) - m) + w) - d) + h) - mi) + s) - ms) @test test == dt + y - m + w - d + h - mi + s - ms @test test == y - m + w - d + dt + h - mi + s - ms @@ -184,9 +184,9 @@ test = ((((((((dt + y) - m) + w) - d) + h) - mi) + s) - ms) @test test == dt + (y - m + w - d + h - mi + s - ms) @test test == dt + y - m + w - d + (h - mi + s - ms) @test (dt + Dates.Year(4)) + Dates.Day(1) == dt + (Dates.Year(4) + Dates.Day(1)) -@test Dates.Date(2014,1,29) + Dates.Month(1) + Dates.Day(1) + Dates.Month(1) + Dates.Day(1) == - Dates.Date(2014,1,29) + Dates.Day(1) + Dates.Month(1) + Dates.Month(1) + Dates.Day(1) -@test Dates.Date(2014,1,29) + Dates.Month(1) + Dates.Day(1) == Dates.Date(2014,1,29) + Dates.Day(1) + Dates.Month(1) +@test Dates.Date(2014, 1, 29) + Dates.Month(1) + Dates.Day(1) + Dates.Month(1) + Dates.Day(1) == + Dates.Date(2014, 1, 29) + Dates.Day(1) + Dates.Month(1) + Dates.Month(1) + Dates.Day(1) +@test Dates.Date(2014, 1, 29) + Dates.Month(1) + Dates.Day(1) == Dates.Date(2014, 1, 29) + Dates.Day(1) + Dates.Month(1) # traits @test Dates._units(Dates.Year(0)) == " years" @test Dates._units(Dates.Year(1)) == " year" @@ -246,7 +246,7 @@ test = ((((((((dt + y) - m) + w) - d) + h) - mi) + s) - ms) @test Dates.Microsecond("1") == us @test Dates.Nanosecond("1") == ns @test_throws ArgumentError Dates.Year("1.0") -@test Dates.Year(parse(Float64,"1.0")) == y +@test Dates.Year(parse(Float64, "1.0")) == y dt = Dates.DateTime(2014) @test typeof(Dates.Year(dt)) <: Dates.Year @@ -314,63 +314,63 @@ emptyperiod = ((y + d) - d) - y @test Dates.canonicalize(y - m + w - d + h - mi + s - ms).periods == Dates.Period[11m, 6d, 59mi, 999ms] @test Dates.canonicalize(-y + m - w + d - h + mi - s + ms).periods == Dates.Period[-11m, -6d, -59mi, -999ms] -@test Dates.Date(2009,2,1) - (Dates.Month(1) + Dates.Day(1)) == Dates.Date(2008,12,31) -@test (Dates.Month(1) + Dates.Day(1)) - Dates.Date(2009,2,1) == Dates.Date(2008,12,31) +@test Dates.Date(2009, 2, 1) - (Dates.Month(1) + Dates.Day(1)) == Dates.Date(2008, 12, 31) +@test (Dates.Month(1) + Dates.Day(1)) - Dates.Date(2009, 2, 1) == Dates.Date(2008, 12, 31) pa = [1y 1m 1w 1d; 1h 1mi 1s 1ms] -cpa = [1y+1s 1m+1s 1w+1s 1d+1s; 1h+1s 1mi+1s 2m+1s 1s+1ms] +cpa = [1y + 1s 1m + 1s 1w + 1s 1d + 1s; 1h + 1s 1mi + 1s 2m + 1s 1s + 1ms] @test +pa == pa == -(-pa) @test -pa == map(-, pa) -@test 1y .+ pa == [2y 1y+1m 1y+1w 1y+1d; 1y+1h 1y+1mi 1y+1s 1y+1ms] -@test (1y+1m) .+ pa == [2y+1m 1y+2m 1y+1m+1w 1y+1m+1d; 1y+1m+1h 1y+1m+1mi 1y+1m+1s 1y+1m+1ms] -@test pa .+ 1y == [2y 1y+1m 1y+1w 1y+1d; 1y+1h 1y+1mi 1y+1s 1y+1ms] -@test pa .+ (1y+1m) == [2y+1m 1y+2m 1y+1m+1w 1y+1m+1d; 1y+1m+1h 1y+1m+1mi 1y+1m+1s 1y+1m+1ms] +@test 1y .+ pa == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] +@test (1y + 1m) .+ pa == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] +@test pa .+ 1y == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] +@test pa .+ (1y + 1m) == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] -@test 1y .+ cpa == [2y+1s 1y+1m+1s 1y+1w+1s 1y+1d+1s; 1y+1h+1s 1y+1mi+1s 1y+2m+1s 1y+1ms+1s] -@test (1y+1m) .+ cpa == [2y+1m+1s 1y+2m+1s 1y+1m+1w+1s 1y+1m+1d+1s; 1y+1m+1h+1s 1y+1m+1mi+1s 1y+3m+1s 1y+1m+1s+1ms] -@test cpa .+ 1y == [2y+1s 1y+1m+1s 1y+1w+1s 1y+1d+1s; 1y+1h+1s 1y+1mi+1s 1y+2m+1s 1y+1ms+1s] -@test cpa .+ (1y+1m) == [2y+1m+1s 1y+2m+1s 1y+1m+1w+1s 1y+1m+1d+1s; 1y+1m+1h+1s 1y+1m+1mi+1s 1y+3m+1s 1y+1m+1s+1ms] +@test 1y .+ cpa == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] +@test (1y + 1m) .+ cpa == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] +@test cpa .+ 1y == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] +@test cpa .+ (1y + 1m) == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] -@test 1y + pa == [2y 1y+1m 1y+1w 1y+1d; 1y+1h 1y+1mi 1y+1s 1y+1ms] -@test (1y+1m) + pa == [2y+1m 1y+2m 1y+1m+1w 1y+1m+1d; 1y+1m+1h 1y+1m+1mi 1y+1m+1s 1y+1m+1ms] -@test pa + 1y == [2y 1y+1m 1y+1w 1y+1d; 1y+1h 1y+1mi 1y+1s 1y+1ms] -@test pa + (1y+1m) == [2y+1m 1y+2m 1y+1m+1w 1y+1m+1d; 1y+1m+1h 1y+1m+1mi 1y+1m+1s 1y+1m+1ms] +@test 1y + pa == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] +@test (1y + 1m) + pa == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] +@test pa + 1y == [2y 1y + 1m 1y + 1w 1y + 1d; 1y + 1h 1y + 1mi 1y + 1s 1y + 1ms] +@test pa + (1y + 1m) == [2y + 1m 1y + 2m 1y + 1m + 1w 1y + 1m + 1d; 1y + 1m + 1h 1y + 1m + 1mi 1y + 1m + 1s 1y + 1m + 1ms] -@test 1y + cpa == [2y+1s 1y+1m+1s 1y+1w+1s 1y+1d+1s; 1y+1h+1s 1y+1mi+1s 1y+2m+1s 1y+1ms+1s] -@test (1y+1m) + cpa == [2y+1m+1s 1y+2m+1s 1y+1m+1w+1s 1y+1m+1d+1s; 1y+1m+1h+1s 1y+1m+1mi+1s 1y+3m+1s 1y+1m+1s+1ms] -@test cpa + 1y == [2y+1s 1y+1m+1s 1y+1w+1s 1y+1d+1s; 1y+1h+1s 1y+1mi+1s 1y+2m+1s 1y+1ms+1s] -@test cpa + (1y+1m) == [2y+1m+1s 1y+2m+1s 1y+1m+1w+1s 1y+1m+1d+1s; 1y+1m+1h+1s 1y+1m+1mi+1s 1y+3m+1s 1y+1m+1s+1ms] +@test 1y + cpa == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] +@test (1y + 1m) + cpa == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] +@test cpa + 1y == [2y + 1s 1y + 1m + 1s 1y + 1w + 1s 1y + 1d + 1s; 1y + 1h + 1s 1y + 1mi + 1s 1y + 2m + 1s 1y + 1ms + 1s] +@test cpa + (1y + 1m) == [2y + 1m + 1s 1y + 2m + 1s 1y + 1m + 1w + 1s 1y + 1m + 1d + 1s; 1y + 1m + 1h + 1s 1y + 1m + 1mi + 1s 1y + 3m + 1s 1y + 1m + 1s + 1ms] @test 1y .- pa == [0y 1y-1m 1y-1w 1y-1d; 1y-1h 1y-1mi 1y-1s 1y-1ms] -@test (1y+1m) .- pa == [1m 1y 1y+1m-1w 1y+1m-1d; 1y+1m-1h 1y+1m-1mi 1y+1m-1s 1y+1m-1ms] -@test pa .- (1y+1m) == [-1m -1y -1y-1m+1w -1y-1m+1d; -1y-1m+1h -1y-1m+1mi -1y-1m+1s -1y-1m+1ms] -@test pa .- 1y == [0y 1m-1y -1y+1w -1y+1d; -1y+1h -1y+1mi -1y+1s -1y+1ms] +@test (1y + 1m) .- pa == [1m 1y 1y + 1m-1w 1y + 1m-1d; 1y + 1m-1h 1y + 1m-1mi 1y + 1m-1s 1y + 1m-1ms] +@test pa .- (1y + 1m) == [-1m -1y -1y-1m + 1w -1y-1m + 1d; -1y-1m + 1h -1y-1m + 1mi -1y-1m + 1s -1y-1m + 1ms] +@test pa .- 1y == [0y 1m-1y -1y + 1w -1y + 1d; -1y + 1h -1y + 1mi -1y + 1s -1y + 1ms] @test 1y .- cpa == [-1s 1y-1m-1s 1y-1w-1s 1y-1d-1s; 1y-1h-1s 1y-1mi-1s 1y-2m-1s 1y-1ms-1s] -@test (1y+1m) .- cpa == [1m-1s 1y-1s 1y+1m-1w-1s 1y+1m-1d-1s; 1y+1m-1h-1s 1y+1m-1mi-1s 1y-1m-1s 1y+1m-1s-1ms] -@test cpa .- 1y == [1s -1y+1m+1s -1y+1w+1s -1y+1d+1s; -1y+1h+1s -1y+1mi+1s -1y+2m+1s -1y+1ms+1s] -@test cpa .- (1y+1m) == [-1m+1s -1y+1s -1y-1m+1w+1s -1y-1m+1d+1s; -1y-1m+1h+1s -1y-1m+1mi+1s -1y+1m+1s -1y+-1m+1s+1ms] +@test (1y + 1m) .- cpa == [1m-1s 1y-1s 1y + 1m-1w-1s 1y + 1m-1d-1s; 1y + 1m-1h-1s 1y + 1m-1mi-1s 1y-1m-1s 1y + 1m-1s-1ms] +@test cpa .- 1y == [1s -1y + 1m + 1s -1y + 1w + 1s -1y + 1d + 1s; -1y + 1h + 1s -1y + 1mi + 1s -1y + 2m + 1s -1y + 1ms + 1s] +@test cpa .- (1y + 1m) == [-1m + 1s -1y + 1s -1y-1m + 1w + 1s -1y-1m + 1d + 1s; -1y-1m + 1h + 1s -1y-1m + 1mi + 1s -1y + 1m + 1s -1y + -1m + 1s + 1ms] @test 1y - pa == [0y 1y-1m 1y-1w 1y-1d; 1y-1h 1y-1mi 1y-1s 1y-1ms] -@test (1y+1m) - pa == [1m 1y 1y+1m-1w 1y+1m-1d; 1y+1m-1h 1y+1m-1mi 1y+1m-1s 1y+1m-1ms] -@test pa - (1y+1m) == [-1m -1y -1y-1m+1w -1y-1m+1d; -1y-1m+1h -1y-1m+1mi -1y-1m+1s -1y-1m+1ms] -@test pa - 1y == [0y 1m-1y -1y+1w -1y+1d; -1y+1h -1y+1mi -1y+1s -1y+1ms] +@test (1y + 1m) - pa == [1m 1y 1y + 1m-1w 1y + 1m-1d; 1y + 1m-1h 1y + 1m-1mi 1y + 1m-1s 1y + 1m-1ms] +@test pa - (1y + 1m) == [-1m -1y -1y-1m + 1w -1y-1m + 1d; -1y-1m + 1h -1y-1m + 1mi -1y-1m + 1s -1y-1m + 1ms] +@test pa - 1y == [0y 1m-1y -1y + 1w -1y + 1d; -1y + 1h -1y + 1mi -1y + 1s -1y + 1ms] @test 1y - cpa == [-1s 1y-1m-1s 1y-1w-1s 1y-1d-1s; 1y-1h-1s 1y-1mi-1s 1y-2m-1s 1y-1ms-1s] -@test (1y+1m) - cpa == [1m-1s 1y-1s 1y+1m-1w-1s 1y+1m-1d-1s; 1y+1m-1h-1s 1y+1m-1mi-1s 1y-1m-1s 1y+1m-1s-1ms] -@test cpa - 1y == [1s -1y+1m+1s -1y+1w+1s -1y+1d+1s; -1y+1h+1s -1y+1mi+1s -1y+2m+1s -1y+1ms+1s] -@test cpa - (1y+1m) == [-1m+1s -1y+1s -1y-1m+1w+1s -1y-1m+1d+1s; -1y-1m+1h+1s -1y-1m+1mi+1s -1y+1m+1s -1y+-1m+1s+1ms] +@test (1y + 1m) - cpa == [1m-1s 1y-1s 1y + 1m-1w-1s 1y + 1m-1d-1s; 1y + 1m-1h-1s 1y + 1m-1mi-1s 1y-1m-1s 1y + 1m-1s-1ms] +@test cpa - 1y == [1s -1y + 1m + 1s -1y + 1w + 1s -1y + 1d + 1s; -1y + 1h + 1s -1y + 1mi + 1s -1y + 2m + 1s -1y + 1ms + 1s] +@test cpa - (1y + 1m) == [-1m + 1s -1y + 1s -1y-1m + 1w + 1s -1y-1m + 1d + 1s; -1y-1m + 1h + 1s -1y-1m + 1mi + 1s -1y + 1m + 1s -1y + -1m + 1s + 1ms] -@test [1y 1m; 1w 1d] + [1h 1mi; 1s 1ms] == [1y+1h 1m+1mi; 1w+1s 1d+1ms] +@test [1y 1m; 1w 1d] + [1h 1mi; 1s 1ms] == [1y + 1h 1m + 1mi; 1w + 1s 1d + 1ms] @test [1y 1m; 1w 1d] - [1h 1mi; 1s 1ms] == [1y-1h 1m-1mi; 1w-1s 1d-1ms] @test [1y 1m; 1w 1d] - [1h 1mi; 1s 1ms] - [1y-1h 1m-1mi; 1w-1s 1d-1ms] == [emptyperiod emptyperiod; emptyperiod emptyperiod] -@test [1y+1s 1m+1s; 1w+1s 1d+1s] + [1h 1mi; 1s 1ms] == [1y+1h+1s 1m+1mi+1s; 1w+2s 1d+1s+1ms] -@test [1y+1s 1m+1s; 1w+1s 1d+1s] - [1h 1mi; 1s 1ms] == [1y-1h+1s 1m-1mi+1s; 1w 1d+1s-1ms] +@test [1y + 1s 1m + 1s; 1w + 1s 1d + 1s] + [1h 1mi; 1s 1ms] == [1y + 1h + 1s 1m + 1mi + 1s; 1w + 2s 1d + 1s + 1ms] +@test [1y + 1s 1m + 1s; 1w + 1s 1d + 1s] - [1h 1mi; 1s 1ms] == [1y-1h + 1s 1m-1mi + 1s; 1w 1d + 1s-1ms] -@test [1y 1m; 1w 1d] + [1h+1s 1mi+1s; 1m+1s 1s+1ms] == [1y+1h+1s 1m+1mi+1s; 1w+1m+1s 1d+1s+1ms] -@test [1y 1m; 1w 1d] - [1h+1s 1mi+1s; 1m+1s 1s+1ms] == [1y-1h-1s 1m-1mi-1s; 1w-1m-1s 1d-1s-1ms] +@test [1y 1m; 1w 1d] + [1h + 1s 1mi + 1s; 1m + 1s 1s + 1ms] == [1y + 1h + 1s 1m + 1mi + 1s; 1w + 1m + 1s 1d + 1s + 1ms] +@test [1y 1m; 1w 1d] - [1h + 1s 1mi + 1s; 1m + 1s 1s + 1ms] == [1y-1h-1s 1m-1mi-1s; 1w-1m-1s 1d-1s-1ms] -@test [1y+1s 1m+1s; 1w+1s 1d+1s] + [1y+1h 1y+1mi; 1y+1s 1y+1ms] == [2y+1h+1s 1y+1m+1mi+1s; 1y+1w+2s 1y+1d+1s+1ms] -@test [1y+1s 1m+1s; 1w+1s 1d+1s] - [1y+1h 1y+1mi; 1y+1s 1y+1ms] == [1s-1h 1m+1s-1y-1mi; 1w-1y 1d+1s-1y-1ms] +@test [1y + 1s 1m + 1s; 1w + 1s 1d + 1s] + [1y + 1h 1y + 1mi; 1y + 1s 1y + 1ms] == [2y + 1h + 1s 1y + 1m + 1mi + 1s; 1y + 1w + 2s 1y + 1d + 1s + 1ms] +@test [1y + 1s 1m + 1s; 1w + 1s 1d + 1s] - [1y + 1h 1y + 1mi; 1y + 1s 1y + 1ms] == [1s-1h 1m + 1s-1y-1mi; 1w-1y 1d + 1s-1y-1ms] diff --git a/test/dates/query.jl b/test/dates/query.jl index 907acf687ef98..967e04c391be7 100644 --- a/test/dates/query.jl +++ b/test/dates/query.jl @@ -1,27 +1,27 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license # Name functions -Jan = Dates.DateTime(2013,1,1) #Tuesday -Feb = Dates.DateTime(2013,2,2) #Saturday -Mar = Dates.DateTime(2013,3,3) #Sunday -Apr = Dates.DateTime(2013,4,4) #Thursday -May = Dates.DateTime(2013,5,5) #Sunday -Jun = Dates.DateTime(2013,6,7) #Friday -Jul = Dates.DateTime(2013,7,7) #Sunday -Aug = Dates.DateTime(2013,8,8) #Thursday -Sep = Dates.DateTime(2013,9,9) #Monday -Oct = Dates.DateTime(2013,10,10) #Thursday -Nov = Dates.DateTime(2013,11,11) #Monday -Dec = Dates.DateTime(2013,12,11) #Wednesday -monthnames = ["January","February","March","April", - "May","June","July","August","September", - "October","November","December"] -daysofweek = [Dates.Tue,Dates.Sat,Dates.Sun,Dates.Thu,Dates.Sun,Dates.Fri, - Dates.Sun,Dates.Thu,Dates.Mon,Dates.Thu,Dates.Mon,Dates.Wed] -dows = ["Tuesday","Saturday","Sunday","Thursday","Sunday","Friday", - "Sunday","Thursday","Monday","Thursday","Monday","Wednesday"] -daysinmonth = [31,28,31,30,31,30,31,31,30,31,30,31] -for (i,dt) in enumerate([Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec]) +Jan = Dates.DateTime(2013, 1, 1) # Tuesday +Feb = Dates.DateTime(2013, 2, 2) # Saturday +Mar = Dates.DateTime(2013, 3, 3) # Sunday +Apr = Dates.DateTime(2013, 4, 4) # Thursday +May = Dates.DateTime(2013, 5, 5) # Sunday +Jun = Dates.DateTime(2013, 6, 7) # Friday +Jul = Dates.DateTime(2013, 7, 7) # Sunday +Aug = Dates.DateTime(2013, 8, 8) # Thursday +Sep = Dates.DateTime(2013, 9, 9) # Monday +Oct = Dates.DateTime(2013, 10, 10) # Thursday +Nov = Dates.DateTime(2013, 11, 11) # Monday +Dec = Dates.DateTime(2013, 12, 11) # Wednesday +monthnames = ["January", "February", "March", "April", + "May", "June", "July", "August", "September", + "October", "November", "December"] +daysofweek = [Dates.Tue, Dates.Sat, Dates.Sun, Dates.Thu, Dates.Sun, Dates.Fri, + Dates.Sun, Dates.Thu, Dates.Mon, Dates.Thu, Dates.Mon, Dates.Wed] +dows = ["Tuesday", "Saturday", "Sunday", "Thursday", "Sunday", "Friday", + "Sunday", "Thursday", "Monday", "Thursday", "Monday", "Wednesday"] +daysinmonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] +for (i, dt) in enumerate([Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec]) @test Dates.month(dt) == i @test Dates.monthname(dt) == monthnames[i] @test Dates.monthname(i) == monthnames[i] @@ -39,31 +39,31 @@ end Dates.LOCALES["french"] = Dates.DateLocale( ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"], - ["janv","févr","mars","avril","mai","juin", - "juil","août","sept","oct","nov","déc"], - ["lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"], + ["janv", "févr", "mars", "avril", "mai", "juin", + "juil", "août", "sept", "oct", "nov", "déc"], + ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"], [""], ) -@test Dates.dayname(Nov;locale="french") == "lundi" -@test Dates.dayname(Jan;locale="french") == "mardi" -@test Dates.dayname(Dec;locale="french") == "mercredi" -@test Dates.dayname(Apr;locale="french") == "jeudi" -@test Dates.dayname(Jun;locale="french") == "vendredi" -@test Dates.dayname(Feb;locale="french") == "samedi" -@test Dates.dayname(May;locale="french") == "dimanche" +@test Dates.dayname(Nov; locale="french") == "lundi" +@test Dates.dayname(Jan; locale="french") == "mardi" +@test Dates.dayname(Dec; locale="french") == "mercredi" +@test Dates.dayname(Apr; locale="french") == "jeudi" +@test Dates.dayname(Jun; locale="french") == "vendredi" +@test Dates.dayname(Feb; locale="french") == "samedi" +@test Dates.dayname(May; locale="french") == "dimanche" -@test Dates.monthname(Jan;locale="french") == "janvier" -@test Dates.monthname(Feb;locale="french") == "février" -@test Dates.monthname(Mar;locale="french") == "mars" -@test Dates.monthname(Apr;locale="french") == "avril" -@test Dates.monthname(May;locale="french") == "mai" -@test Dates.monthname(Jun;locale="french") == "juin" -@test Dates.monthname(Jul;locale="french") == "juillet" -@test Dates.monthname(Aug;locale="french") == "août" -@test Dates.monthname(Sep;locale="french") == "septembre" -@test Dates.monthname(Oct;locale="french") == "octobre" -@test Dates.monthname(Nov;locale="french") == "novembre" -@test Dates.monthname(Dec;locale="french") == "décembre" +@test Dates.monthname(Jan; locale="french") == "janvier" +@test Dates.monthname(Feb; locale="french") == "février" +@test Dates.monthname(Mar; locale="french") == "mars" +@test Dates.monthname(Apr; locale="french") == "avril" +@test Dates.monthname(May; locale="french") == "mai" +@test Dates.monthname(Jun; locale="french") == "juin" +@test Dates.monthname(Jul; locale="french") == "juillet" +@test Dates.monthname(Aug; locale="french") == "août" +@test Dates.monthname(Sep; locale="french") == "septembre" +@test Dates.monthname(Oct; locale="french") == "octobre" +@test Dates.monthname(Nov; locale="french") == "novembre" +@test Dates.monthname(Dec; locale="french") == "décembre" @test Dates.isleapyear(Dates.DateTime(1900)) == false @test Dates.isleapyear(Dates.DateTime(2000)) == true @@ -86,117 +86,117 @@ Dates.LOCALES["french"] = Dates.DateLocale( @test Dates.daysinyear(Dates.DateTime(2001)) == 365 # Days of week from Monday = 1 to Sunday = 7 -@test Dates.dayofweek(Dates.DateTime(2013,12,22)) == 7 -@test Dates.dayofweek(Dates.DateTime(2013,12,23)) == 1 -@test Dates.dayofweek(Dates.DateTime(2013,12,24)) == 2 -@test Dates.dayofweek(Dates.DateTime(2013,12,25)) == 3 -@test Dates.dayofweek(Dates.DateTime(2013,12,26)) == 4 -@test Dates.dayofweek(Dates.DateTime(2013,12,27)) == 5 -@test Dates.dayofweek(Dates.DateTime(2013,12,28)) == 6 -@test Dates.dayofweek(Dates.DateTime(2013,12,29)) == 7 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 22)) == 7 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 23)) == 1 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 24)) == 2 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 25)) == 3 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 26)) == 4 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 27)) == 5 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 28)) == 6 +@test Dates.dayofweek(Dates.DateTime(2013, 12, 29)) == 7 # There are 5 Sundays in December, 2013 -@test Dates.daysofweekinmonth(Dates.DateTime(2013,12,1)) == 5 +@test Dates.daysofweekinmonth(Dates.DateTime(2013, 12, 1)) == 5 # There are 4 Sundays in November, 2013 -@test Dates.daysofweekinmonth(Dates.DateTime(2013,11,24)) == 4 +@test Dates.daysofweekinmonth(Dates.DateTime(2013, 11, 24)) == 4 -@test Dates.dayofweekofmonth(Dates.DateTime(2013,12,1)) == 1 -@test Dates.dayofweekofmonth(Dates.DateTime(2013,12,8)) == 2 -@test Dates.dayofweekofmonth(Dates.DateTime(2013,12,15)) == 3 -@test Dates.dayofweekofmonth(Dates.DateTime(2013,12,22)) == 4 -@test Dates.dayofweekofmonth(Dates.DateTime(2013,12,29)) == 5 +@test Dates.dayofweekofmonth(Dates.DateTime(2013, 12, 1)) == 1 +@test Dates.dayofweekofmonth(Dates.DateTime(2013, 12, 8)) == 2 +@test Dates.dayofweekofmonth(Dates.DateTime(2013, 12, 15)) == 3 +@test Dates.dayofweekofmonth(Dates.DateTime(2013, 12, 22)) == 4 +@test Dates.dayofweekofmonth(Dates.DateTime(2013, 12, 29)) == 5 -@test Dates.dayofyear(2000,1,1) == 1 -@test Dates.dayofyear(2004,1,1) == 1 -@test Dates.dayofyear(20013,1,1) == 1 +@test Dates.dayofyear(2000, 1, 1) == 1 +@test Dates.dayofyear(2004, 1, 1) == 1 +@test Dates.dayofyear(20013, 1, 1) == 1 # Leap year -@test Dates.dayofyear(2000,12,31) == 366 +@test Dates.dayofyear(2000, 12, 31) == 366 # Non-leap year -@test Dates.dayofyear(2001,12,31) == 365 +@test Dates.dayofyear(2001, 12, 31) == 365 -@test Dates.dayofyear(Dates.DateTime(2000,1,1)) == 1 -@test Dates.dayofyear(Dates.DateTime(2004,1,1)) == 1 -@test Dates.dayofyear(Dates.DateTime(20013,1,1)) == 1 +@test Dates.dayofyear(Dates.DateTime(2000, 1, 1)) == 1 +@test Dates.dayofyear(Dates.DateTime(2004, 1, 1)) == 1 +@test Dates.dayofyear(Dates.DateTime(20013, 1, 1)) == 1 # Leap year -@test Dates.dayofyear(Dates.DateTime(2000,12,31)) == 366 +@test Dates.dayofyear(Dates.DateTime(2000, 12, 31)) == 366 # Non-leap year -@test Dates.dayofyear(Dates.DateTime(2001,12,31)) == 365 +@test Dates.dayofyear(Dates.DateTime(2001, 12, 31)) == 365 # Test every day of a year -dt = Dates.DateTime(2000,1,1) +dt = Dates.DateTime(2000, 1, 1) for i = 1:366 @test Dates.dayofyear(dt) == i dt += Dates.Day(1) end -dt = Dates.DateTime(2001,1,1) +dt = Dates.DateTime(2001, 1, 1) for i = 1:365 @test Dates.dayofyear(dt) == i dt += Dates.Day(1) end -@test Dates.quarterofyear(Dates.Date(2000,1,1)) == 1 -@test Dates.quarterofyear(Dates.Date(2000,1,31)) == 1 -@test Dates.quarterofyear(Dates.Date(2000,2,1)) == 1 -@test Dates.quarterofyear(Dates.Date(2000,2,29)) == 1 -@test Dates.quarterofyear(Dates.Date(2000,3,1)) == 1 -@test Dates.quarterofyear(Dates.Date(2000,3,31)) == 1 -@test Dates.quarterofyear(Dates.Date(2000,4,1)) == 2 -@test Dates.quarterofyear(Dates.Date(2000,4,30)) == 2 -@test Dates.quarterofyear(Dates.Date(2000,5,1)) == 2 -@test Dates.quarterofyear(Dates.Date(2000,5,31)) == 2 -@test Dates.quarterofyear(Dates.Date(2000,6,1)) == 2 -@test Dates.quarterofyear(Dates.Date(2000,6,30)) == 2 -@test Dates.quarterofyear(Dates.Date(2000,7,1)) == 3 -@test Dates.quarterofyear(Dates.Date(2000,7,31)) == 3 -@test Dates.quarterofyear(Dates.Date(2000,8,1)) == 3 -@test Dates.quarterofyear(Dates.Date(2000,8,31)) == 3 -@test Dates.quarterofyear(Dates.Date(2000,9,1)) == 3 -@test Dates.quarterofyear(Dates.Date(2000,9,30)) == 3 -@test Dates.quarterofyear(Dates.Date(2000,10,1)) == 4 -@test Dates.quarterofyear(Dates.Date(2000,10,31)) == 4 -@test Dates.quarterofyear(Dates.Date(2000,11,1)) == 4 -@test Dates.quarterofyear(Dates.Date(2000,11,30)) == 4 -@test Dates.quarterofyear(Dates.Date(2000,12,1)) == 4 -@test Dates.quarterofyear(Dates.Date(2000,12,31)) == 4 +@test Dates.quarterofyear(Dates.Date(2000, 1, 1)) == 1 +@test Dates.quarterofyear(Dates.Date(2000, 1, 31)) == 1 +@test Dates.quarterofyear(Dates.Date(2000, 2, 1)) == 1 +@test Dates.quarterofyear(Dates.Date(2000, 2, 29)) == 1 +@test Dates.quarterofyear(Dates.Date(2000, 3, 1)) == 1 +@test Dates.quarterofyear(Dates.Date(2000, 3, 31)) == 1 +@test Dates.quarterofyear(Dates.Date(2000, 4, 1)) == 2 +@test Dates.quarterofyear(Dates.Date(2000, 4, 30)) == 2 +@test Dates.quarterofyear(Dates.Date(2000, 5, 1)) == 2 +@test Dates.quarterofyear(Dates.Date(2000, 5, 31)) == 2 +@test Dates.quarterofyear(Dates.Date(2000, 6, 1)) == 2 +@test Dates.quarterofyear(Dates.Date(2000, 6, 30)) == 2 +@test Dates.quarterofyear(Dates.Date(2000, 7, 1)) == 3 +@test Dates.quarterofyear(Dates.Date(2000, 7, 31)) == 3 +@test Dates.quarterofyear(Dates.Date(2000, 8, 1)) == 3 +@test Dates.quarterofyear(Dates.Date(2000, 8, 31)) == 3 +@test Dates.quarterofyear(Dates.Date(2000, 9, 1)) == 3 +@test Dates.quarterofyear(Dates.Date(2000, 9, 30)) == 3 +@test Dates.quarterofyear(Dates.Date(2000, 10, 1)) == 4 +@test Dates.quarterofyear(Dates.Date(2000, 10, 31)) == 4 +@test Dates.quarterofyear(Dates.Date(2000, 11, 1)) == 4 +@test Dates.quarterofyear(Dates.Date(2000, 11, 30)) == 4 +@test Dates.quarterofyear(Dates.Date(2000, 12, 1)) == 4 +@test Dates.quarterofyear(Dates.Date(2000, 12, 31)) == 4 -@test Dates.quarterofyear(Dates.DateTime(2000,1,1)) == 1 -@test Dates.quarterofyear(Dates.DateTime(2000,1,31)) == 1 -@test Dates.quarterofyear(Dates.DateTime(2000,2,1)) == 1 -@test Dates.quarterofyear(Dates.DateTime(2000,2,29)) == 1 -@test Dates.quarterofyear(Dates.DateTime(2000,3,1)) == 1 -@test Dates.quarterofyear(Dates.DateTime(2000,3,31)) == 1 -@test Dates.quarterofyear(Dates.DateTime(2000,4,1)) == 2 -@test Dates.quarterofyear(Dates.DateTime(2000,4,30)) == 2 -@test Dates.quarterofyear(Dates.DateTime(2000,5,1)) == 2 -@test Dates.quarterofyear(Dates.DateTime(2000,5,31)) == 2 -@test Dates.quarterofyear(Dates.DateTime(2000,6,1)) == 2 -@test Dates.quarterofyear(Dates.DateTime(2000,6,30)) == 2 -@test Dates.quarterofyear(Dates.DateTime(2000,7,1)) == 3 -@test Dates.quarterofyear(Dates.DateTime(2000,7,31)) == 3 -@test Dates.quarterofyear(Dates.DateTime(2000,8,1)) == 3 -@test Dates.quarterofyear(Dates.DateTime(2000,8,31)) == 3 -@test Dates.quarterofyear(Dates.DateTime(2000,9,1)) == 3 -@test Dates.quarterofyear(Dates.DateTime(2000,9,30)) == 3 -@test Dates.quarterofyear(Dates.DateTime(2000,10,1)) == 4 -@test Dates.quarterofyear(Dates.DateTime(2000,10,31)) == 4 -@test Dates.quarterofyear(Dates.DateTime(2000,11,1)) == 4 -@test Dates.quarterofyear(Dates.DateTime(2000,11,30)) == 4 -@test Dates.quarterofyear(Dates.DateTime(2000,12,1)) == 4 -@test Dates.quarterofyear(Dates.DateTime(2000,12,31)) == 4 +@test Dates.quarterofyear(Dates.DateTime(2000, 1, 1)) == 1 +@test Dates.quarterofyear(Dates.DateTime(2000, 1, 31)) == 1 +@test Dates.quarterofyear(Dates.DateTime(2000, 2, 1)) == 1 +@test Dates.quarterofyear(Dates.DateTime(2000, 2, 29)) == 1 +@test Dates.quarterofyear(Dates.DateTime(2000, 3, 1)) == 1 +@test Dates.quarterofyear(Dates.DateTime(2000, 3, 31)) == 1 +@test Dates.quarterofyear(Dates.DateTime(2000, 4, 1)) == 2 +@test Dates.quarterofyear(Dates.DateTime(2000, 4, 30)) == 2 +@test Dates.quarterofyear(Dates.DateTime(2000, 5, 1)) == 2 +@test Dates.quarterofyear(Dates.DateTime(2000, 5, 31)) == 2 +@test Dates.quarterofyear(Dates.DateTime(2000, 6, 1)) == 2 +@test Dates.quarterofyear(Dates.DateTime(2000, 6, 30)) == 2 +@test Dates.quarterofyear(Dates.DateTime(2000, 7, 1)) == 3 +@test Dates.quarterofyear(Dates.DateTime(2000, 7, 31)) == 3 +@test Dates.quarterofyear(Dates.DateTime(2000, 8, 1)) == 3 +@test Dates.quarterofyear(Dates.DateTime(2000, 8, 31)) == 3 +@test Dates.quarterofyear(Dates.DateTime(2000, 9, 1)) == 3 +@test Dates.quarterofyear(Dates.DateTime(2000, 9, 30)) == 3 +@test Dates.quarterofyear(Dates.DateTime(2000, 10, 1)) == 4 +@test Dates.quarterofyear(Dates.DateTime(2000, 10, 31)) == 4 +@test Dates.quarterofyear(Dates.DateTime(2000, 11, 1)) == 4 +@test Dates.quarterofyear(Dates.DateTime(2000, 11, 30)) == 4 +@test Dates.quarterofyear(Dates.DateTime(2000, 12, 1)) == 4 +@test Dates.quarterofyear(Dates.DateTime(2000, 12, 31)) == 4 -@test Dates.dayofquarter(Dates.Date(2014,1,1)) == 1 -@test Dates.dayofquarter(Dates.Date(2014,4,1)) == 1 -@test Dates.dayofquarter(Dates.Date(2014,7,1)) == 1 -@test Dates.dayofquarter(Dates.Date(2014,10,1)) == 1 -@test Dates.dayofquarter(Dates.Date(2014,3,31)) == 90 -@test Dates.dayofquarter(Dates.Date(2014,6,30)) == 91 -@test Dates.dayofquarter(Dates.Date(2014,9,30)) == 92 -@test Dates.dayofquarter(Dates.Date(2014,12,31)) == 92 -@test Dates.dayofquarter(Dates.DateTime(2014,1,1)) == 1 -@test Dates.dayofquarter(Dates.DateTime(2014,4,1)) == 1 -@test Dates.dayofquarter(Dates.DateTime(2014,7,1)) == 1 -@test Dates.dayofquarter(Dates.DateTime(2014,10,1)) == 1 -@test Dates.dayofquarter(Dates.DateTime(2014,3,31)) == 90 -@test Dates.dayofquarter(Dates.DateTime(2014,6,30)) == 91 -@test Dates.dayofquarter(Dates.DateTime(2014,9,30)) == 92 -@test Dates.dayofquarter(Dates.DateTime(2014,12,31)) == 92 +@test Dates.dayofquarter(Dates.Date(2014, 1, 1)) == 1 +@test Dates.dayofquarter(Dates.Date(2014, 4, 1)) == 1 +@test Dates.dayofquarter(Dates.Date(2014, 7, 1)) == 1 +@test Dates.dayofquarter(Dates.Date(2014, 10, 1)) == 1 +@test Dates.dayofquarter(Dates.Date(2014, 3, 31)) == 90 +@test Dates.dayofquarter(Dates.Date(2014, 6, 30)) == 91 +@test Dates.dayofquarter(Dates.Date(2014, 9, 30)) == 92 +@test Dates.dayofquarter(Dates.Date(2014, 12, 31)) == 92 +@test Dates.dayofquarter(Dates.DateTime(2014, 1, 1)) == 1 +@test Dates.dayofquarter(Dates.DateTime(2014, 4, 1)) == 1 +@test Dates.dayofquarter(Dates.DateTime(2014, 7, 1)) == 1 +@test Dates.dayofquarter(Dates.DateTime(2014, 10, 1)) == 1 +@test Dates.dayofquarter(Dates.DateTime(2014, 3, 31)) == 90 +@test Dates.dayofquarter(Dates.DateTime(2014, 6, 30)) == 91 +@test Dates.dayofquarter(Dates.DateTime(2014, 9, 30)) == 92 +@test Dates.dayofquarter(Dates.DateTime(2014, 12, 31)) == 92 diff --git a/test/dates/ranges.jl b/test/dates/ranges.jl index a986a3961a47a..622382b9c6da3 100644 --- a/test/dates/ranges.jl +++ b/test/dates/ranges.jl @@ -1,14 +1,14 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license let - for T in (Dates.Date,Dates.DateTime) - f1 = T(2014); l1 = T(2013,12,31) + for T in (Dates.Date, Dates.DateTime) + f1 = T(2014); l1 = T(2013, 12, 31) f2 = T(2014); l2 = T(2014) f3 = T(-2000); l3 = T(2000) f4 = typemin(T); l4 = typemax(T) for P in subtypes(Dates.DatePeriod) - for pos_step in (P(1),P(2),P(50),P(2048),P(10000)) + for pos_step in (P(1), P(2), P(50), P(2048), P(10000)) # empty range dr = f1:pos_step:l1 @test length(dr) == 0 @@ -19,7 +19,7 @@ let @test_throws ArgumentError minimum(dr) @test_throws ArgumentError maximum(dr) @test_throws BoundsError dr[1] - @test findin(dr,dr) == Int64[] + @test findin(dr, dr) == Int64[] @test [dr;] == T[] @test isempty(reverse(dr)) @test length(reverse(dr)) == 0 @@ -29,10 +29,10 @@ let @test sortperm(dr) == 1:1:0 @test !(f1 in dr) @test !(l1 in dr) - @test !(f1-pos_step in dr) - @test !(l1+pos_step in dr) + @test !(f1 - pos_step in dr) + @test !(l1 + pos_step in dr) - for (f,l) in ((f2,l2),(f3,l3),(f4,l4)) + for (f, l) in ((f2, l2), (f3, l3), (f4, l4)) dr = f:pos_step:l len = length(dr) @test len > 0 @@ -44,12 +44,12 @@ let @test maximum(dr) == last(dr) @test dr[1] == f @test dr[end] <= l - @test next(dr,start(dr)) == (first(dr),1) + @test next(dr, start(dr)) == (first(dr), 1) if len < 10000 dr1 = [i for i in dr] @test length(dr1) == len - @test findin(dr,dr) == [1:len;] + @test findin(dr, dr) == [1:len;] @test length([dr;]) == len end @test !isempty(reverse(dr)) @@ -60,7 +60,7 @@ let end end - for neg_step in (P(-1),P(-2),P(-50),P(-2048),P(-10000)) + for neg_step in (P(-1), P(-2), P(-50), P(-2048), P(-10000)) # empty range dr = l1:neg_step:f1 @test length(dr) == 0 @@ -71,7 +71,7 @@ let @test_throws ArgumentError minimum(dr) @test_throws ArgumentError maximum(dr) @test_throws BoundsError dr[1] - @test findin(dr,dr) == Int64[] + @test findin(dr, dr) == Int64[] @test [dr;] == T[] @test isempty(reverse(dr)) @test length(reverse(dr)) == 0 @@ -81,10 +81,10 @@ let @test sortperm(dr) == 1:1:0 @test !(l1 in dr) @test !(l1 in dr) - @test !(l1-neg_step in dr) - @test !(l1+neg_step in dr) + @test !(l1 - neg_step in dr) + @test !(l1 + neg_step in dr) - for (f,l) in ((f2,l2),(f3,l3),(f4,l4)) + for (f, l) in ((f2, l2), (f3, l3), (f4, l4)) dr = l:neg_step:f len = length(dr) @test len > 0 @@ -96,12 +96,12 @@ let @test maximum(dr) == first(dr) @test dr[1] == l @test dr[end] >= f - @test next(dr,start(dr)) == (first(dr),1) + @test next(dr, start(dr)) == (first(dr), 1) if len < 10000 dr1 = [i for i in dr] @test length(dr1) == len - @test findin(dr,dr) == [1:len;] + @test findin(dr, dr) == [1:len;] @test length([dr;]) == len end @test !isempty(reverse(dr)) @@ -114,7 +114,7 @@ let if T == Dates.DateTime for P in subtypes(Dates.TimePeriod) P in (Dates.Microsecond, Dates.Nanosecond) && continue - for pos_step in (P(1),P(2),P(50),P(2048),P(10000)) + for pos_step in (P(1), P(2), P(50), P(2048), P(10000)) # empty range dr = f1:pos_step:l1 @test length(dr) == 0 @@ -125,7 +125,7 @@ let @test_throws ArgumentError minimum(dr) @test_throws ArgumentError maximum(dr) @test_throws BoundsError dr[1] - @test findin(dr,dr) == Int64[] + @test findin(dr, dr) == Int64[] @test [dr;] == T[] @test isempty(reverse(dr)) @test length(reverse(dr)) == 0 @@ -135,10 +135,10 @@ let @test sortperm(dr) == 1:1:0 @test !(f1 in dr) @test !(l1 in dr) - @test !(f1-pos_step in dr) - @test !(l1+pos_step in dr) + @test !(f1 - pos_step in dr) + @test !(l1 + pos_step in dr) - for (f,l) in ((f2,l2),(f3,l3),(f4,l4)) + for (f, l) in ((f2, l2), (f3, l3), (f4, l4)) dr = f:pos_step:l len = length(dr) @test len > 0 @@ -150,12 +150,12 @@ let @test maximum(dr) == last(dr) @test dr[1] == f @test dr[end] <= l - @test next(dr,start(dr)) == (first(dr),1) + @test next(dr, start(dr)) == (first(dr), 1) if len < 10000 dr1 = [i for i in dr] @test length(dr1) == len - @test findin(dr,dr) == [1:len;] + @test findin(dr, dr) == [1:len;] @test length([dr;]) == len end @test !isempty(reverse(dr)) @@ -166,7 +166,7 @@ let end end - for neg_step in (P(-1),P(-2),P(-50),P(-2048),P(-10000)) + for neg_step in (P(-1), P(-2), P(-50), P(-2048), P(-10000)) # empty range dr = l1:neg_step:f1 @test length(dr) == 0 @@ -177,7 +177,7 @@ let @test_throws ArgumentError minimum(dr) @test_throws ArgumentError maximum(dr) @test_throws BoundsError dr[1] - @test findin(dr,dr) == Int64[] + @test findin(dr, dr) == Int64[] @test [dr;] == T[] @test isempty(reverse(dr)) @test length(reverse(dr)) == 0 @@ -187,10 +187,10 @@ let @test sortperm(dr) == 1:1:0 @test !(l1 in dr) @test !(l1 in dr) - @test !(l1-neg_step in dr) - @test !(l1+neg_step in dr) + @test !(l1 - neg_step in dr) + @test !(l1 + neg_step in dr) - for (f,l) in ((f2,l2),(f3,l3),(f4,l4)) + for (f, l) in ((f2, l2), (f3, l3), (f4, l4)) dr = l:neg_step:f len = length(dr) @test len > 0 @@ -202,12 +202,12 @@ let @test maximum(dr) == first(dr) @test dr[1] == l @test dr[end] >= f - @test next(dr,start(dr)) == (first(dr),1) + @test next(dr, start(dr)) == (first(dr), 1) if len < 10000 dr1 = [i for i in dr] @test length(dr1) == len - @test findin(dr,dr) == [1:len;] + @test findin(dr, dr) == [1:len;] @test length([dr;]) == len end @test !isempty(reverse(dr)) @@ -225,16 +225,16 @@ end # All the range representations we want to test # Date ranges -dr = Dates.DateTime(2013,1,1):Dates.DateTime(2013,2,1) -dr1 = Dates.DateTime(2013,1,1):Dates.DateTime(2013,1,1) -dr2 = Dates.DateTime(2013,1,1):Dates.DateTime(2012,2,1) # empty range -dr3 = Dates.DateTime(2013,1,1):Dates.Day(-1):Dates.DateTime(2012) # negative step +dr = Dates.DateTime(2013, 1, 1):Dates.DateTime(2013, 2, 1) +dr1 = Dates.DateTime(2013, 1, 1):Dates.DateTime(2013, 1, 1) +dr2 = Dates.DateTime(2013, 1, 1):Dates.DateTime(2012, 2, 1) # empty range +dr3 = Dates.DateTime(2013, 1, 1):Dates.Day(-1):Dates.DateTime(2012) # negative step # Big ranges -dr4 = Dates.DateTime(0):Dates.DateTime(20000,1,1) -dr5 = Dates.DateTime(0):Dates.DateTime(200000,1,1) -dr6 = Dates.DateTime(0):Dates.DateTime(2000000,1,1) -dr7 = Dates.DateTime(0):Dates.DateTime(20000000,1,1) -dr8 = Dates.DateTime(0):Dates.DateTime(200000000,1,1) +dr4 = Dates.DateTime(0):Dates.DateTime(20000, 1, 1) +dr5 = Dates.DateTime(0):Dates.DateTime(200000, 1, 1) +dr6 = Dates.DateTime(0):Dates.DateTime(2000000, 1, 1) +dr7 = Dates.DateTime(0):Dates.DateTime(20000000, 1, 1) +dr8 = Dates.DateTime(0):Dates.DateTime(200000000, 1, 1) dr9 = typemin(Dates.DateTime):typemax(Dates.DateTime) # Non-default steps dr10 = typemax(Dates.DateTime):Dates.Day(-1):typemin(Dates.DateTime) @@ -251,73 +251,73 @@ dr18 = typemax(Dates.DateTime):Dates.Month(-100000):typemin(Dates.DateTime) dr19 = typemax(Dates.DateTime):Dates.Year(-1000000):typemin(Dates.DateTime) dr20 = typemin(Dates.DateTime):Dates.Day(2):typemax(Dates.DateTime) -drs = Any[dr,dr1,dr2,dr3,dr4,dr5,dr6,dr7,dr8,dr9,dr10, - dr11,dr12,dr13,dr14,dr15,dr16,dr17,dr18,dr19,dr20] -drs2 = map(x->Dates.Date(first(x)):step(x):Dates.Date(last(x)),drs) +drs = Any[dr, dr1, dr2, dr3, dr4, dr5, dr6, dr7, dr8, dr9, dr10, + dr11, dr12, dr13, dr14, dr15, dr16, dr17, dr18, dr19, dr20] +drs2 = map(x->Dates.Date(first(x)):step(x):Dates.Date(last(x)), drs) -@test map(length,drs) == map(x->size(x)[1],drs) -@test map(length,drs) == map(x->length(Dates.Date(first(x)):step(x):Dates.Date(last(x))),drs) -@test map(length,drs) == map(x->length(reverse(x)),drs) -@test all(x->findin(x,x)==[1:length(x);],drs[1:4]) +@test map(length, drs) == map(x->size(x)[1], drs) +@test map(length, drs) == map(x->length(Dates.Date(first(x)):step(x):Dates.Date(last(x))), drs) +@test map(length, drs) == map(x->length(reverse(x)), drs) +@test all(x->findin(x, x)==[1:length(x);], drs[1:4]) @test isempty(dr2) -@test all(x->reverse(x) == range(last(x), -step(x), length(x)),drs) -@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)),drs[4:end]) -@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)),drs[4:end]) +@test all(x->reverse(x) == range(last(x), -step(x), length(x)), drs) +@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end]) +@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)), drs[4:end]) @test all(drs[1:3]) do dd - for (i,d) in enumerate(dd) - @test d == (first(dd) + Dates.Day(i-1)) + for (i, d) in enumerate(dd) + @test d == (first(dd) + Dates.Day(i - 1)) end true end @test_throws MethodError dr + 1 -a = Dates.DateTime(2013,1,1) -b = Dates.DateTime(2013,2,1) -@test map!(x->x+Dates.Day(1),Array{Dates.DateTime}(32),dr) == [(a+Dates.Day(1)):(b+Dates.Day(1));] -@test map(x->x+Dates.Day(1),dr) == [(a+Dates.Day(1)):(b+Dates.Day(1));] +a = Dates.DateTime(2013, 1, 1) +b = Dates.DateTime(2013, 2, 1) +@test map!(x->x + Dates.Day(1), Array{Dates.DateTime}(32), dr) == [(a + Dates.Day(1)):(b + Dates.Day(1));] +@test map(x->x + Dates.Day(1), dr) == [(a + Dates.Day(1)):(b + Dates.Day(1));] -@test map(x->a in x,drs[1:4]) == [true,true,false,true] +@test map(x->a in x, drs[1:4]) == [true, true, false, true] @test a in dr @test b in dr -@test Dates.DateTime(2013,1,3) in dr -@test Dates.DateTime(2013,1,15) in dr -@test Dates.DateTime(2013,1,26) in dr -@test !(Dates.DateTime(2012,1,1) in dr) +@test Dates.DateTime(2013, 1, 3) in dr +@test Dates.DateTime(2013, 1, 15) in dr +@test Dates.DateTime(2013, 1, 26) in dr +@test !(Dates.DateTime(2012, 1, 1) in dr) -@test all(x->sort(x) == (step(x) < zero(step(x)) ? reverse(x) : x),drs) -@test all(x->step(x) < zero(step(x)) ? issorted(reverse(x)) : issorted(x),drs) +@test all(x->sort(x) == (step(x) < zero(step(x)) ? reverse(x) : x), drs) +@test all(x->step(x) < zero(step(x)) ? issorted(reverse(x)) : issorted(x), drs) @test length(b:Dates.Day(-1):a) == 32 @test length(b:a) == 0 @test length(b:Dates.Day(1):a) == 0 @test length(a:Dates.Day(2):b) == 16 -@test last(a:Dates.Day(2):b) == Dates.DateTime(2013,1,31) +@test last(a:Dates.Day(2):b) == Dates.DateTime(2013, 1, 31) @test length(a:Dates.Day(7):b) == 5 -@test last(a:Dates.Day(7):b) == Dates.DateTime(2013,1,29) +@test last(a:Dates.Day(7):b) == Dates.DateTime(2013, 1, 29) @test length(a:Dates.Day(32):b) == 1 -@test last(a:Dates.Day(32):b) == Dates.DateTime(2013,1,1) -@test (a:b)[1] == Dates.DateTime(2013,1,1) -@test (a:b)[2] == Dates.DateTime(2013,1,2) -@test (a:b)[7] == Dates.DateTime(2013,1,7) +@test last(a:Dates.Day(32):b) == Dates.DateTime(2013, 1, 1) +@test (a:b)[1] == Dates.DateTime(2013, 1, 1) +@test (a:b)[2] == Dates.DateTime(2013, 1, 2) +@test (a:b)[7] == Dates.DateTime(2013, 1, 7) @test (a:b)[end] == b -@test first(a:Dates.DateTime(20000,1,1)) == a -@test first(a:Dates.DateTime(200000,1,1)) == a -@test first(a:Dates.DateTime(2000000,1,1)) == a -@test first(a:Dates.DateTime(20000000,1,1)) == a -@test first(a:Dates.DateTime(200000000,1,1)) == a +@test first(a:Dates.DateTime(20000, 1, 1)) == a +@test first(a:Dates.DateTime(200000, 1, 1)) == a +@test first(a:Dates.DateTime(2000000, 1, 1)) == a +@test first(a:Dates.DateTime(20000000, 1, 1)) == a +@test first(a:Dates.DateTime(200000000, 1, 1)) == a @test first(a:typemax(Dates.DateTime)) == a @test first(typemin(Dates.DateTime):typemax(Dates.DateTime)) == typemin(Dates.DateTime) # Date ranges -dr = Dates.Date(2013,1,1):Dates.Date(2013,2,1) -dr1 = Dates.Date(2013,1,1):Dates.Date(2013,1,1) -dr2 = Dates.Date(2013,1,1):Dates.Date(2012,2,1) # empty range -dr3 = Dates.Date(2013,1,1):Dates.Day(-1):Dates.Date(2012,1,1) # negative step +dr = Dates.Date(2013, 1, 1):Dates.Date(2013, 2, 1) +dr1 = Dates.Date(2013, 1, 1):Dates.Date(2013, 1, 1) +dr2 = Dates.Date(2013, 1, 1):Dates.Date(2012, 2, 1) # empty range +dr3 = Dates.Date(2013, 1, 1):Dates.Day(-1):Dates.Date(2012, 1, 1) # negative step # Big ranges -dr4 = Dates.Date(0):Dates.Date(20000,1,1) -dr5 = Dates.Date(0):Dates.Date(200000,1,1) -dr6 = Dates.Date(0):Dates.Date(2000000,1,1) -dr7 = Dates.Date(0):Dates.Date(20000000,1,1) -dr8 = Dates.Date(0):Dates.Date(200000000,1,1) +dr4 = Dates.Date(0):Dates.Date(20000, 1, 1) +dr5 = Dates.Date(0):Dates.Date(200000, 1, 1) +dr6 = Dates.Date(0):Dates.Date(2000000, 1, 1) +dr7 = Dates.Date(0):Dates.Date(20000000, 1, 1) +dr8 = Dates.Date(0):Dates.Date(200000000, 1, 1) dr9 = typemin(Dates.Date):typemax(Dates.Date) # Non-default steps dr10 = typemax(Dates.Date):Dates.Day(-1):typemin(Dates.Date) @@ -332,56 +332,56 @@ dr18 = typemax(Dates.Date):Dates.Month(-100000):typemin(Dates.Date) dr19 = typemax(Dates.Date):Dates.Year(-1000000):typemin(Dates.Date) dr20 = typemin(Dates.Date):Dates.Day(2):typemax(Dates.Date) -drs = Any[dr,dr1,dr2,dr3,dr4,dr5,dr6,dr7,dr8,dr9,dr10, - dr11,dr12,dr13,dr14,dr15,dr16,dr17,dr18,dr19,dr20] +drs = Any[dr, dr1, dr2, dr3, dr4, dr5, dr6, dr7, dr8, dr9, dr10, + dr11, dr12, dr13, dr14, dr15, dr16, dr17, dr18, dr19, dr20] -@test map(length,drs) == map(x->size(x)[1],drs) -@test all(x->findin(x,x) == [1:length(x);], drs[1:4]) +@test map(length, drs) == map(x->size(x)[1], drs) +@test all(x->findin(x, x) == [1:length(x);], drs[1:4]) @test isempty(dr2) -@test all(x->reverse(x) == last(x):-step(x):first(x),drs) -@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)),drs[4:end]) -@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)),drs[4:end]) +@test all(x->reverse(x) == last(x): - step(x):first(x), drs) +@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end]) +@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)), drs[4:end]) @test all(drs[1:3]) do dd - for (i,d) in enumerate(dd) - @test d == (first(dd) + Dates.Day(i-1)) + for (i, d) in enumerate(dd) + @test d == (first(dd) + Dates.Day(i - 1)) end true end @test_throws MethodError dr + 1 -a = Dates.Date(2013,1,1) -b = Dates.Date(2013,2,1) -@test map!(x->x+Dates.Day(1),Array{Dates.Date}(32),dr) == [(a+Dates.Day(1)):(b+Dates.Day(1));] -@test map(x->x+Dates.Day(1),dr) == [(a+Dates.Day(1)):(b+Dates.Day(1));] +a = Dates.Date(2013, 1, 1) +b = Dates.Date(2013, 2, 1) +@test map!(x->x + Dates.Day(1), Array{Dates.Date}(32), dr) == [(a + Dates.Day(1)):(b + Dates.Day(1));] +@test map(x->x + Dates.Day(1), dr) == [(a + Dates.Day(1)):(b + Dates.Day(1));] -@test map(x->a in x,drs[1:4]) == [true,true,false,true] +@test map(x->a in x, drs[1:4]) == [true, true, false, true] @test a in dr @test b in dr -@test Dates.Date(2013,1,3) in dr -@test Dates.Date(2013,1,15) in dr -@test Dates.Date(2013,1,26) in dr -@test !(Dates.Date(2012,1,1) in dr) +@test Dates.Date(2013, 1, 3) in dr +@test Dates.Date(2013, 1, 15) in dr +@test Dates.Date(2013, 1, 26) in dr +@test !(Dates.Date(2012, 1, 1) in dr) -@test all(x->sort(x) == (step(x) < zero(step(x)) ? reverse(x) : x),drs) -@test all(x->step(x) < zero(step(x)) ? issorted(reverse(x)) : issorted(x),drs) +@test all(x->sort(x) == (step(x) < zero(step(x)) ? reverse(x) : x), drs) +@test all(x->step(x) < zero(step(x)) ? issorted(reverse(x)) : issorted(x), drs) @test length(b:Dates.Day(-1):a) == 32 @test length(b:a) == 0 @test length(b:Dates.Day(1):a) == 0 @test length(a:Dates.Day(2):b) == 16 -@test last(a:Dates.Day(2):b) == Dates.Date(2013,1,31) +@test last(a:Dates.Day(2):b) == Dates.Date(2013, 1, 31) @test length(a:Dates.Day(7):b) == 5 -@test last(a:Dates.Day(7):b) == Dates.Date(2013,1,29) +@test last(a:Dates.Day(7):b) == Dates.Date(2013, 1, 29) @test length(a:Dates.Day(32):b) == 1 -@test last(a:Dates.Day(32):b) == Dates.Date(2013,1,1) -@test (a:b)[1] == Dates.Date(2013,1,1) -@test (a:b)[2] == Dates.Date(2013,1,2) -@test (a:b)[7] == Dates.Date(2013,1,7) +@test last(a:Dates.Day(32):b) == Dates.Date(2013, 1, 1) +@test (a:b)[1] == Dates.Date(2013, 1, 1) +@test (a:b)[2] == Dates.Date(2013, 1, 2) +@test (a:b)[7] == Dates.Date(2013, 1, 7) @test (a:b)[end] == b -@test first(a:Dates.Date(20000,1,1)) == a -@test first(a:Dates.Date(200000,1,1)) == a -@test first(a:Dates.Date(2000000,1,1)) == a -@test first(a:Dates.Date(20000000,1,1)) == a -@test first(a:Dates.Date(200000000,1,1)) == a +@test first(a:Dates.Date(20000, 1, 1)) == a +@test first(a:Dates.Date(200000, 1, 1)) == a +@test first(a:Dates.Date(2000000, 1, 1)) == a +@test first(a:Dates.Date(20000000, 1, 1)) == a +@test first(a:Dates.Date(200000000, 1, 1)) == a @test first(a:typemax(Dates.Date)) == a @test first(typemin(Dates.Date):typemax(Dates.Date)) == typemin(Dates.Date) @@ -400,27 +400,27 @@ b = Dates.Date(2013,2,1) @test length(typemin(Dates.DateTime):Dates.Second(1):typemax(Dates.DateTime)) == 9223372017043200 @test length(typemin(DateTime):Dates.Millisecond(1):typemax(DateTime)) == 9223372017043199001 -c = Dates.Date(2013,6,1) +c = Dates.Date(2013, 6, 1) @test length(a:Dates.Month(1):c) == 6 @test [a:Dates.Month(1):c;] == [a + Dates.Month(1)*i for i in 0:5] -@test [a:Dates.Month(2):Dates.Date(2013,1,2);] == [a] +@test [a:Dates.Month(2):Dates.Date(2013, 1, 2);] == [a] @test [c:Dates.Month(-1):a;] == reverse([a:Dates.Month(1):c;]) -@test length(range(Date(2000),366)) == 366 +@test length(range(Date(2000), 366)) == 366 let n=100000 a = Dates.Date(2000) for i = 1:n - @test length(range(a,i)) == i + @test length(range(a, i)) == i end - return a+Dates.Day(n) + return a + Dates.Day(n) end # Custom definition to override default step of DateTime ranges @test typeof(step(Dates.DateTime(2000):Dates.DateTime(2001))) == Dates.Day -a = Dates.Date(2013,1,1) -b = Dates.Date(2013,2,1) -d = Dates.Date(2020,1,1) +a = Dates.Date(2013, 1, 1) +b = Dates.Date(2013, 2, 1) +d = Dates.Date(2020, 1, 1) @test length(a:Dates.Year(1):d) == 8 @test first(a:Dates.Year(1):d) == a @test last(a:Dates.Year(1):d) == d @@ -429,21 +429,21 @@ d = Dates.Date(2020,1,1) @test last(a:Dates.Month(12):d) == d @test length(a:Dates.Week(52):d) == 8 @test first(a:Dates.Week(52):d) == a -@test last(a:Dates.Week(52):d) == Dates.Date(2019,12,24) +@test last(a:Dates.Week(52):d) == Dates.Date(2019, 12, 24) @test length(a:Dates.Day(365):d) == 8 @test first(a:Dates.Day(365):d) == a -@test last(a:Dates.Day(365):d) == Dates.Date(2019,12,31) - -a = Dates.Date(2013,1,1) -b = Dates.Date(2013,2,1) -@test length(a:Dates.Year(1):Dates.Date(2020,2,1)) == 8 -@test length(a:Dates.Year(1):Dates.Date(2020,6,1)) == 8 -@test length(a:Dates.Year(1):Dates.Date(2020,11,1)) == 8 -@test length(a:Dates.Year(1):Dates.Date(2020,12,31)) == 8 -@test length(a:Dates.Year(1):Dates.Date(2021,1,1)) == 9 +@test last(a:Dates.Day(365):d) == Dates.Date(2019, 12, 31) + +a = Dates.Date(2013, 1, 1) +b = Dates.Date(2013, 2, 1) +@test length(a:Dates.Year(1):Dates.Date(2020, 2, 1)) == 8 +@test length(a:Dates.Year(1):Dates.Date(2020, 6, 1)) == 8 +@test length(a:Dates.Year(1):Dates.Date(2020, 11, 1)) == 8 +@test length(a:Dates.Year(1):Dates.Date(2020, 12, 31)) == 8 +@test length(a:Dates.Year(1):Dates.Date(2021, 1, 1)) == 9 @test length(Dates.Date(2000):Dates.Year(-10):Dates.Date(1900)) == 11 -@test length(Dates.Date(2000,6,23):Dates.Year(-10):Dates.Date(1900,2,28)) == 11 -@test length(Dates.Date(2000,1,1):Dates.Year(1):Dates.Date(2000,2,1)) == 1 +@test length(Dates.Date(2000, 6, 23):Dates.Year(-10):Dates.Date(1900, 2, 28)) == 11 +@test length(Dates.Date(2000, 1, 1):Dates.Year(1):Dates.Date(2000, 2, 1)) == 1 let n=100000 a = b = Dates.Date(0) @@ -454,10 +454,10 @@ let n=100000 end let n=10000 - a = Dates.Date(1985,12,5) - b = Dates.Date(1986,12,27) - c = Dates.DateTime(1985,12,5) - d = Dates.DateTime(1986,12,27) + a = Dates.Date(1985, 12, 5) + b = Dates.Date(1986, 12, 27) + c = Dates.DateTime(1985, 12, 5) + d = Dates.DateTime(1986, 12, 27) for i = 1:n @test length(a:Dates.Month(1):b) == 13 @test length(a:Dates.Year(1):b) == 2 @@ -484,31 +484,31 @@ end @test_throws OverflowError length(typemin(Dates.Year):Dates.Year(1):typemax(Dates.Year)) @test_throws MethodError Dates.Date(0):Dates.DateTime(2000) @test_throws MethodError Dates.Date(0):Dates.Year(10) -@test length(range(Dates.Date(2000),366)) == 366 -@test last(range(Dates.Date(2000),366)) == Dates.Date(2000,12,31) -@test last(range(Dates.Date(2001),365)) == Dates.Date(2001,12,31) -@test last(range(Dates.Date(2000),367)) == last(range(Dates.Date(2000),Dates.Month(12),2)) == last(range(Dates.Date(2000),Dates.Year(1),2)) -@test last(range(Dates.DateTime(2000),Dates.Day(366),2)) == last(range(Dates.DateTime(2000),Dates.Hour(8784),2)) +@test length(range(Dates.Date(2000), 366)) == 366 +@test last(range(Dates.Date(2000), 366)) == Dates.Date(2000, 12, 31) +@test last(range(Dates.Date(2001), 365)) == Dates.Date(2001, 12, 31) +@test last(range(Dates.Date(2000), 367)) == last(range(Dates.Date(2000), Dates.Month(12), 2)) == last(range(Dates.Date(2000), Dates.Year(1), 2)) +@test last(range(Dates.DateTime(2000), Dates.Day(366), 2)) == last(range(Dates.DateTime(2000), Dates.Hour(8784), 2)) # Issue 5 -lastdaysofmonth = [Dates.Date(2014,i,Dates.daysinmonth(2014,i)) for i=1:12] -@test [Date(2014,1,31):Dates.Month(1):Date(2015);] == lastdaysofmonth +lastdaysofmonth = [Dates.Date(2014, i, Dates.daysinmonth(2014, i)) for i=1:12] +@test [Date(2014, 1, 31):Dates.Month(1):Date(2015);] == lastdaysofmonth # Range addition/subtraction: let d = Dates.Day(1) - @test (Dates.Date(2000):d:Dates.Date(2001))+d == (Dates.Date(2000)+d:d:Dates.Date(2001)+d) - @test (Dates.Date(2000):d:Dates.Date(2001))-d == (Dates.Date(2000)-d:d:Dates.Date(2001)-d) + @test (Dates.Date(2000):d:Dates.Date(2001)) + d == (Dates.Date(2000) + d:d:Dates.Date(2001) + d) + @test (Dates.Date(2000):d:Dates.Date(2001)) - d == (Dates.Date(2000) - d:d:Dates.Date(2001) - d) end # Time ranges -dr = Dates.Time(23,1,1):Dates.Time(23,2,1) -dr1 = Dates.Time(23,1,1):Dates.Time(23,1,1) -dr2 = Dates.Time(23,1,1):Dates.Time(22,2,1) # empty range -dr3 = Dates.Time(23,1,1):Dates.Minute(-1):Dates.Time(22,1,1) # negative step +dr = Dates.Time(23, 1, 1):Dates.Time(23, 2, 1) +dr1 = Dates.Time(23, 1, 1):Dates.Time(23, 1, 1) +dr2 = Dates.Time(23, 1, 1):Dates.Time(22, 2, 1) # empty range +dr3 = Dates.Time(23, 1, 1):Dates.Minute(-1):Dates.Time(22, 1, 1) # negative step # Big ranges dr8 = typemin(Dates.Time):typemax(Dates.Time) dr9 = typemin(Dates.Time):Dates.Nanosecond(1):typemax(Dates.Time) -# Non-default steps +# Non - default steps dr10 = typemax(Dates.Time):Dates.Microsecond(-1):typemin(Dates.Time) dr11 = typemin(Dates.Time):Dates.Millisecond(1):typemax(Dates.Time) dr12 = typemin(Dates.Time):Dates.Minute(1):typemax(Dates.Time) @@ -521,19 +521,19 @@ dr18 = typemax(Dates.Time):Dates.Minute(-100):typemin(Dates.Time) dr19 = typemax(Dates.Time):Dates.Hour(-10):typemin(Dates.Time) dr20 = typemin(Dates.Time):Dates.Microsecond(2):typemax(Dates.Time) -drs = Any[dr,dr1,dr2,dr3,dr8,dr9,dr10, - dr11,dr12,dr13,dr14,dr15,dr16,dr17,dr18,dr19,dr20] +drs = Any[dr, dr1, dr2, dr3, dr8, dr9, dr10, + dr11, dr12, dr13, dr14, dr15, dr16, dr17, dr18, dr19, dr20] -@test map(length,drs) == map(x->size(x)[1],drs) -@test all(x->findin(x,x) == [1:length(x);], drs[1:4]) +@test map(length, drs) == map(x->size(x)[1], drs) +@test all(x->findin(x, x) == [1:length(x);], drs[1:4]) @test isempty(dr2) -@test all(x->reverse(x) == last(x):-step(x):first(x),drs) -@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)),drs[4:end]) -@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)),drs[4:end]) +@test all(x->reverse(x) == last(x): - step(x):first(x), drs) +@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end]) +@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)), drs[4:end]) @test_throws MethodError dr + 1 -a = Dates.Time(23,1,1) -@test map(x->a in x,drs[1:4]) == [true,true,false,true] +a = Dates.Time(23, 1, 1) +@test map(x->a in x, drs[1:4]) == [true, true, false, true] @test a in dr @test all(x->sort(x) == (step(x) < zero(step(x)) ? reverse(x) : x), drs) diff --git a/test/dates/types.jl b/test/dates/types.jl index 3222f7fcfaef7..0b32a6753ca55 100644 --- a/test/dates/types.jl +++ b/test/dates/types.jl @@ -1,28 +1,28 @@ # This file is a part of Julia. License is MIT: http://julialang.org/license # Date internal algorithms -@test Dates.totaldays(0,2,28) == -307 -@test Dates.totaldays(0,2,29) == -306 -@test Dates.totaldays(0,3,1) == -305 -@test Dates.totaldays(0,12,31) == 0 +@test Dates.totaldays(0, 2, 28) == -307 +@test Dates.totaldays(0, 2, 29) == -306 +@test Dates.totaldays(0, 3, 1) == -305 +@test Dates.totaldays(0, 12, 31) == 0 # Rata Die Days # start from 0001-01-01 -@test Dates.totaldays(1,1,1) == 1 -@test Dates.totaldays(1,1,2) == 2 -@test Dates.totaldays(2013,1,1) == 734869 +@test Dates.totaldays(1, 1, 1) == 1 +@test Dates.totaldays(1, 1, 2) == 2 +@test Dates.totaldays(2013, 1, 1) == 734869 -@test Dates.daysinmonth(2000,1) == 31 -@test Dates.daysinmonth(2000,2) == 29 -@test Dates.daysinmonth(2000,3) == 31 -@test Dates.daysinmonth(2000,4) == 30 -@test Dates.daysinmonth(2000,5) == 31 -@test Dates.daysinmonth(2000,6) == 30 -@test Dates.daysinmonth(2000,7) == 31 -@test Dates.daysinmonth(2000,8) == 31 -@test Dates.daysinmonth(2000,9) == 30 -@test Dates.daysinmonth(2000,10) == 31 -@test Dates.daysinmonth(2000,11) == 30 -@test Dates.daysinmonth(2000,12) == 31 -@test Dates.daysinmonth(2001,2) == 28 +@test Dates.daysinmonth(2000, 1) == 31 +@test Dates.daysinmonth(2000, 2) == 29 +@test Dates.daysinmonth(2000, 3) == 31 +@test Dates.daysinmonth(2000, 4) == 30 +@test Dates.daysinmonth(2000, 5) == 31 +@test Dates.daysinmonth(2000, 6) == 30 +@test Dates.daysinmonth(2000, 7) == 31 +@test Dates.daysinmonth(2000, 8) == 31 +@test Dates.daysinmonth(2000, 9) == 30 +@test Dates.daysinmonth(2000, 10) == 31 +@test Dates.daysinmonth(2000, 11) == 30 +@test Dates.daysinmonth(2000, 12) == 31 +@test Dates.daysinmonth(2001, 2) == 28 @test Dates.isleapyear(1900) == false @test Dates.isleapyear(2000) == true @@ -38,89 +38,89 @@ test = Dates.DateTime(Dates.UTM(63492681600000)) # Test DateTime construction by parts @test Dates.DateTime(2013) == test -@test Dates.DateTime(2013,1) == test -@test Dates.DateTime(2013,1,1) == test -@test Dates.DateTime(2013,1,1,0) == test -@test Dates.DateTime(2013,1,1,0,0) == test -@test Dates.DateTime(2013,1,1,0,0,0) == test -@test Dates.DateTime(2013,1,1,0,0,0,0) == test +@test Dates.DateTime(2013, 1) == test +@test Dates.DateTime(2013, 1, 1) == test +@test Dates.DateTime(2013, 1, 1, 0) == test +@test Dates.DateTime(2013, 1, 1, 0, 0) == test +@test Dates.DateTime(2013, 1, 1, 0, 0, 0) == test +@test Dates.DateTime(2013, 1, 1, 0, 0, 0, 0) == test test = Dates.Date(Dates.UTD(734869)) # Test Date construction by parts @test Dates.Date(2013) == test -@test Dates.Date(2013,1) == test -@test Dates.Date(2013,1,1) == test +@test Dates.Date(2013, 1) == test +@test Dates.Date(2013, 1, 1) == test # Test Time construction by parts t = Dates.Time(Dates.Nanosecond(82800000000000)) @test Dates.Time(23) == t -@test Dates.Time(23,0) == t -@test Dates.Time(23,0,0) == t -@test Dates.Time(23,0,0,0) == t -@test Dates.Time(23,0,0,0,0) == t -@test Dates.Time(23,0,0,0,0,0) == t +@test Dates.Time(23, 0) == t +@test Dates.Time(23, 0, 0) == t +@test Dates.Time(23, 0, 0, 0) == t +@test Dates.Time(23, 0, 0, 0, 0) == t +@test Dates.Time(23, 0, 0, 0, 0, 0) == t # Test various input types for Date/DateTime -test = Dates.Date(1,1,1) -@test Dates.Date(Int8(1),Int8(1),Int8(1)) == test -@test Dates.Date(UInt8(1),UInt8(1),UInt8(1)) == test -@test Dates.Date(Int16(1),Int16(1),Int16(1)) == test -@test Dates.Date(UInt8(1),UInt8(1),UInt8(1)) == test -@test Dates.Date(Int32(1),Int32(1),Int32(1)) == test -@test Dates.Date(UInt32(1),UInt32(1),UInt32(1)) == test -@test Dates.Date(Int64(1),Int64(1),Int64(1)) == test -@test Dates.Date('\x01','\x01','\x01') == test -@test Dates.Date(true,true,true) == test -@test_throws ArgumentError Dates.Date(false,true,false) -@test Dates.Date(false,true,true) == test - Dates.Year(1) -@test_throws ArgumentError Dates.Date(true,true,false) -@test Dates.Date(UInt64(1),UInt64(1),UInt64(1)) == test -@test Dates.Date(-1,UInt64(1),UInt64(1)) == test - Dates.Year(2) -@test Dates.Date(Int128(1),Int128(1),Int128(1)) == test -@test_throws InexactError Dates.Date(170141183460469231731687303715884105727,Int128(1),Int128(1)) -@test Dates.Date(UInt128(1),UInt128(1),UInt128(1)) == test -@test Dates.Date(big(1),big(1),big(1)) == test -@test Dates.Date(big(1),big(1),big(1)) == test +test = Dates.Date(1, 1, 1) +@test Dates.Date(Int8(1), Int8(1), Int8(1)) == test +@test Dates.Date(UInt8(1), UInt8(1), UInt8(1)) == test +@test Dates.Date(Int16(1), Int16(1), Int16(1)) == test +@test Dates.Date(UInt8(1), UInt8(1), UInt8(1)) == test +@test Dates.Date(Int32(1), Int32(1), Int32(1)) == test +@test Dates.Date(UInt32(1), UInt32(1), UInt32(1)) == test +@test Dates.Date(Int64(1), Int64(1), Int64(1)) == test +@test Dates.Date('\x01', '\x01', '\x01') == test +@test Dates.Date(true, true, true) == test +@test_throws ArgumentError Dates.Date(false, true, false) +@test Dates.Date(false, true, true) == test - Dates.Year(1) +@test_throws ArgumentError Dates.Date(true, true, false) +@test Dates.Date(UInt64(1), UInt64(1), UInt64(1)) == test +@test Dates.Date(-1, UInt64(1), UInt64(1)) == test - Dates.Year(2) +@test Dates.Date(Int128(1), Int128(1), Int128(1)) == test +@test_throws InexactError Dates.Date(170141183460469231731687303715884105727, Int128(1), Int128(1)) +@test Dates.Date(UInt128(1), UInt128(1), UInt128(1)) == test +@test Dates.Date(big(1), big(1), big(1)) == test +@test Dates.Date(big(1), big(1), big(1)) == test # Potentially won't work if can't losslessly convert to Int64 -@test Dates.Date(BigFloat(1),BigFloat(1),BigFloat(1)) == test -@test Dates.Date(complex(1),complex(1),complex(1)) == test -@test Dates.Date(Float64(1),Float64(1),Float64(1)) == test -@test Dates.Date(Float32(1),Float32(1),Float32(1)) == test -@test Dates.Date(Float16(1),Float16(1),Float16(1)) == test -@test Dates.Date(Rational(1),Rational(1),Rational(1)) == test -@test_throws InexactError Dates.Date(BigFloat(1.2),BigFloat(1),BigFloat(1)) -@test_throws InexactError Dates.Date(1 + im,complex(1),complex(1)) -@test_throws InexactError Dates.Date(1.2,1.0,1.0) -@test_throws InexactError Dates.Date(1.2f0,1.f0,1.f0) -@test_throws InexactError Dates.Date(3//4,Rational(1),Rational(1)) == test +@test Dates.Date(BigFloat(1), BigFloat(1), BigFloat(1)) == test +@test Dates.Date(complex(1), complex(1), complex(1)) == test +@test Dates.Date(Float64(1), Float64(1), Float64(1)) == test +@test Dates.Date(Float32(1), Float32(1), Float32(1)) == test +@test Dates.Date(Float16(1), Float16(1), Float16(1)) == test +@test Dates.Date(Rational(1), Rational(1), Rational(1)) == test +@test_throws InexactError Dates.Date(BigFloat(1.2), BigFloat(1), BigFloat(1)) +@test_throws InexactError Dates.Date(1 + im, complex(1), complex(1)) +@test_throws InexactError Dates.Date(1.2, 1.0, 1.0) +@test_throws InexactError Dates.Date(1.2f0, 1.f0, 1.f0) +@test_throws InexactError Dates.Date(3//4, Rational(1), Rational(1)) == test # Months, days, hours, minutes, seconds, and milliseconds must be in range -@test_throws ArgumentError Dates.Date(2013,0,1) -@test_throws ArgumentError Dates.Date(2013,13,1) -@test_throws ArgumentError Dates.Date(2013,1,0) -@test_throws ArgumentError Dates.Date(2013,1,32) -@test_throws ArgumentError Dates.DateTime(2013,0,1) -@test_throws ArgumentError Dates.DateTime(2013,13,1) -@test_throws ArgumentError Dates.DateTime(2013,1,0) -@test_throws ArgumentError Dates.DateTime(2013,1,32) -@test_throws ArgumentError Dates.DateTime(2013,1,1,24) -@test_throws ArgumentError Dates.DateTime(2013,1,1,-1) -@test_throws ArgumentError Dates.DateTime(2013,1,1,0,-1) -@test_throws ArgumentError Dates.DateTime(2013,1,1,0,60) -@test_throws ArgumentError Dates.DateTime(2013,1,1,0,0,-1) -@test_throws ArgumentError Dates.DateTime(2013,1,1,0,0,60) -@test_throws ArgumentError Dates.DateTime(2013,1,1,0,0,0,-1) -@test_throws ArgumentError Dates.DateTime(2013,1,1,0,0,0,1000) +@test_throws ArgumentError Dates.Date(2013, 0, 1) +@test_throws ArgumentError Dates.Date(2013, 13, 1) +@test_throws ArgumentError Dates.Date(2013, 1, 0) +@test_throws ArgumentError Dates.Date(2013, 1, 32) +@test_throws ArgumentError Dates.DateTime(2013, 0, 1) +@test_throws ArgumentError Dates.DateTime(2013, 13, 1) +@test_throws ArgumentError Dates.DateTime(2013, 1, 0) +@test_throws ArgumentError Dates.DateTime(2013, 1, 32) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, 24) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, -1) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, 0, -1) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, 0, 60) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, 0, 0, -1) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, 0, 0, 60) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, 0, 0, 0, -1) +@test_throws ArgumentError Dates.DateTime(2013, 1, 1, 0, 0, 0, 1000) @test_throws ArgumentError Dates.Time(24) @test_throws ArgumentError Dates.Time(-1) -@test_throws ArgumentError Dates.Time(0,-1) -@test_throws ArgumentError Dates.Time(0,60) -@test_throws ArgumentError Dates.Time(0,0,-1) -@test_throws ArgumentError Dates.Time(0,0,60) -@test_throws ArgumentError Dates.Time(0,0,0,-1) -@test_throws ArgumentError Dates.Time(0,0,0,1000) -@test_throws ArgumentError Dates.Time(0,0,0,0,-1) -@test_throws ArgumentError Dates.Time(0,0,0,0,1000) -@test_throws ArgumentError Dates.Time(0,0,0,0,0,-1) -@test_throws ArgumentError Dates.Time(0,0,0,0,0,1000) +@test_throws ArgumentError Dates.Time(0, -1) +@test_throws ArgumentError Dates.Time(0, 60) +@test_throws ArgumentError Dates.Time(0, 0, -1) +@test_throws ArgumentError Dates.Time(0, 0, 60) +@test_throws ArgumentError Dates.Time(0, 0, 0, -1) +@test_throws ArgumentError Dates.Time(0, 0, 0, 1000) +@test_throws ArgumentError Dates.Time(0, 0, 0, 0, -1) +@test_throws ArgumentError Dates.Time(0, 0, 0, 0, 1000) +@test_throws ArgumentError Dates.Time(0, 0, 0, 0, 0, -1) +@test_throws ArgumentError Dates.Time(0, 0, 0, 0, 0, 1000) # Test DateTime traits a = Dates.DateTime(2000) @@ -155,19 +155,19 @@ c = Dates.Time(0) @test !(b < a) c = Dates.DateTime(2000) d = Dates.Date(2000) -@test ==(a,c) -@test ==(c,a) -@test ==(d,b) -@test ==(b,d) -@test ==(a,d) -@test ==(d,a) -@test ==(b,c) -@test ==(c,b) +@test ==(a, c) +@test ==(c, a) +@test ==(d, b) +@test ==(b, d) +@test ==(a, d) +@test ==(d, a) +@test ==(b, c) +@test ==(c, b) b = Dates.Date(2001) @test b > a @test a < b @test a != b -@test Dates.Date(Dates.DateTime(Dates.Date(2012,7,1))) == Dates.Date(2012,7,1) +@test Dates.Date(Dates.DateTime(Dates.Date(2012, 7, 1))) == Dates.Date(2012, 7, 1) y = Dates.Year(1) m = Dates.Month(1) @@ -178,29 +178,33 @@ mi = Dates.Minute(1) s = Dates.Second(1) ms = Dates.Millisecond(1) @test Dates.DateTime(y) == Dates.DateTime(1) -@test Dates.DateTime(y,m) == Dates.DateTime(1,1) -@test Dates.DateTime(y,m,d) == Dates.DateTime(1,1,1) -@test Dates.DateTime(y,m,d,h) == Dates.DateTime(1,1,1,1) -@test Dates.DateTime(y,m,d,h,mi) == Dates.DateTime(1,1,1,1,1) -@test Dates.DateTime(y,m,d,h,mi,s) == Dates.DateTime(1,1,1,1,1,1) -@test Dates.DateTime(y,m,d,h,mi,s,ms) == Dates.DateTime(1,1,1,1,1,1,1) -@test Dates.DateTime(Dates.Day(10),Dates.Month(2),y) == Dates.DateTime(1,2,10) -@test Dates.DateTime(Dates.Second(10),Dates.Month(2),y,Dates.Hour(4)) == Dates.DateTime(1,2,1,4,0,10) -@test Dates.DateTime(Dates.Year(1),Dates.Month(2),Dates.Day(1), - Dates.Hour(4),Dates.Second(10)) == Dates.DateTime(1,2,1,4,0,10) +@test Dates.DateTime(y, m) == Dates.DateTime(1, 1) +@test Dates.DateTime(y, m, d) == Dates.DateTime(1, 1, 1) +@test Dates.DateTime(y, m, d, h) == Dates.DateTime(1, 1, 1, 1) +@test Dates.DateTime(y, m, d, h, mi) == Dates.DateTime(1, 1, 1, 1, 1) +@test Dates.DateTime(y, m, d, h, mi, s) == Dates.DateTime(1, 1, 1, 1, 1, 1) +@test Dates.DateTime(y, m, d, h, mi, s, ms) == Dates.DateTime(1, 1, 1, 1, 1, 1, 1) +@test Dates.DateTime(Dates.Day(10), Dates.Month(2), y) == Dates.DateTime(1, 2, 10) +@test Dates.DateTime(Dates.Second(10), Dates.Month(2), y, Dates.Hour(4)) == Dates.DateTime(1, 2, 1, 4, 0, 10) +@test Dates.DateTime(Dates.Year(1), Dates.Month(2), Dates.Day(1), + Dates.Hour(4), Dates.Second(10)) == Dates.DateTime(1, 2, 1, 4, 0, 10) @test Dates.Date(y) == Dates.Date(1) -@test Dates.Date(y,m) == Dates.Date(1,1) -@test Dates.Date(y,m,d) == Dates.Date(1,1,1) -@test Dates.Date(m) == Dates.Date(1,1,1) -@test Dates.Date(d,y) == Dates.Date(1,1,1) -@test Dates.Date(d,m) == Dates.Date(1,1,1) -@test Dates.Date(m,y) == Dates.Date(1,1,1) +@test Dates.Date(y, m) == Dates.Date(1, 1) +@test Dates.Date(y, m, d) == Dates.Date(1, 1, 1) +@test Dates.Date(m) == Dates.Date(1, 1, 1) +@test Dates.Date(d, y) == Dates.Date(1, 1, 1) +@test Dates.Date(d, m) == Dates.Date(1, 1, 1) +@test Dates.Date(m, y) == Dates.Date(1, 1, 1) +@test Dates.Date(Dates.Day(10), Dates.Month(2), y) == Dates.Date(1, 2, 10) us = Dates.Microsecond(1) ns = Dates.Nanosecond(1) @test Dates.Time(h) == Dates.Time(1) -@test Dates.Time(h,mi) == Dates.Time(1,1) -@test Dates.Time(h,mi,s) == Dates.Time(1,1,1) -@test Dates.Time(h,mi,s,ms) == Dates.Time(1,1,1,1) -@test Dates.Time(h,mi,s,ms,us) == Dates.Time(1,1,1,1,1) -@test Dates.Time(h,mi,s,ms,us,ns) == Dates.Time(1,1,1,1,1,1) -@test Dates.Time(us,h,s,ns,mi,ms) == Dates.Time(1,1,1,1,1,1) \ No newline at end of file +@test Dates.Time(h, mi) == Dates.Time(1, 1) +@test Dates.Time(h, mi, s) == Dates.Time(1, 1, 1) +@test Dates.Time(h, mi, s, ms) == Dates.Time(1, 1, 1, 1) +@test Dates.Time(h, mi, s, ms, us) == Dates.Time(1, 1, 1, 1, 1) +@test Dates.Time(h, mi, s, ms, us, ns) == Dates.Time(1, 1, 1, 1, 1, 1) +@test Dates.Time(us, h, s, ns, mi, ms) == Dates.Time(1, 1, 1, 1, 1, 1) +@test Dates.Time(Dates.Second(10), Dates.Minute(2), us, Dates.Hour(4)) == Dates.Time(4, 2, 10, 0, 1) +@test Dates.Time(Dates.Hour(4), Dates.Second(10), Dates.Millisecond(15), + Dates.Microsecond(20), Dates.Nanosecond(25)) == Dates.Time(4, 0, 10, 15, 20, 25) From db31d92b562b0d9ce3803987ff009f006f3feed1 Mon Sep 17 00:00:00 2001 From: quinnj Date: Wed, 25 Jan 2017 22:47:53 -0700 Subject: [PATCH 05/11] Add back one of the subtract methods --- base/dates/arithmetic.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/dates/arithmetic.jl b/base/dates/arithmetic.jl index 09c8d162dcc24..016cae7b8f1ab 100644 --- a/base/dates/arithmetic.jl +++ b/base/dates/arithmetic.jl @@ -86,11 +86,11 @@ for op in (:+, :-) @eval begin ($op){T<:TimeType}(x::AbstractArray{T}, y::GeneralPeriod) = broadcast($op, x, y) ($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = broadcast($op, x, y) + ($op){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = broadcast($op, x, y) end if op == :+ @eval begin ($op){T<:TimeType}(y::GeneralPeriod, x::AbstractArray{T}) = broadcast($op, x, y) - ($op){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = broadcast($op, x, y) end end end From 48f1bc2eb9066526eb12c048bc69c1c8edd65078 Mon Sep 17 00:00:00 2001 From: quinnj Date: Wed, 25 Jan 2017 23:16:10 -0700 Subject: [PATCH 06/11] Update NEWS again --- NEWS.md | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index f27c5adbad181..3fd87b2edb520 100644 --- a/NEWS.md +++ b/NEWS.md @@ -46,6 +46,10 @@ Breaking changes This section lists changes that do not have deprecation warnings. + * `readline`, `readlines` and `eachline` return lines without line endings by default. + You *must* use `readline(s, chomp=false)`, etc. to get the old behavior where lines + returned include trailing end-of-line character(s). ([#19944]) + * `String`s no longer have a `.data` field (as part of a significant performance improvement). Use `Vector{UInt8}(str)` to access a string as a byte array. However, allocating the `Vector` object has overhead. You can also use @@ -129,6 +133,9 @@ This section lists changes that do not have deprecation warnings. Library improvements -------------------- + * `@views` macro to convert a whole expression or block of code to + use views for all slices ([#20164]). + * `max`, `min`, and related functions (`minmax`, `maximum`, `minimum`, `extrema`) now return `NaN` for `NaN` arguments ([#12563]). @@ -205,11 +212,17 @@ Library improvements * `notify` now returns a count of tasks woken up ([#19841]). - * A new `Dates.Time` type was added that supports representing the time of day with up to nanosecond resolution. + * A new `Dates.Time` type was added that supports representing the time of day with up to nanosecond resolution ([#12274]). Compiler/Runtime improvements ----------------------------- +* `ccall` is now implemented as a macro, removing the need for special code-generator support for Intrinsics. + +* `ccall` gained limited support for a `llvmcall` calling-convention. This can replace many uses of `llvmcall` with a simpler, shorter declaration. + +* All Intrinsics are now Builtin functions instead and have proper error checking and fall-back static compilation support. + Deprecated or removed --------------------- @@ -745,10 +758,12 @@ Language tooling improvements [#6190]: https://github.com/JuliaLang/julia/issues/6190 [#6842]: https://github.com/JuliaLang/julia/issues/6842 [#7258]: https://github.com/JuliaLang/julia/issues/7258 +[#7669]: https://github.com/JuliaLang/julia/issues/7669 [#8036]: https://github.com/JuliaLang/julia/issues/8036 [#8599]: https://github.com/JuliaLang/julia/issues/8599 [#8814]: https://github.com/JuliaLang/julia/issues/8814 [#8846]: https://github.com/JuliaLang/julia/issues/8846 +[#8974]: https://github.com/JuliaLang/julia/issues/8974 [#9482]: https://github.com/JuliaLang/julia/issues/9482 [#9503]: https://github.com/JuliaLang/julia/issues/9503 [#9627]: https://github.com/JuliaLang/julia/issues/9627 @@ -824,6 +839,7 @@ Language tooling improvements [#16098]: https://github.com/JuliaLang/julia/issues/16098 [#16107]: https://github.com/JuliaLang/julia/issues/16107 [#16154]: https://github.com/JuliaLang/julia/issues/16154 +[#16213]: https://github.com/JuliaLang/julia/issues/16213 [#16219]: https://github.com/JuliaLang/julia/issues/16219 [#16260]: https://github.com/JuliaLang/julia/issues/16260 [#16285]: https://github.com/JuliaLang/julia/issues/16285 @@ -872,6 +888,7 @@ Language tooling improvements [#18330]: https://github.com/JuliaLang/julia/issues/18330 [#18339]: https://github.com/JuliaLang/julia/issues/18339 [#18346]: https://github.com/JuliaLang/julia/issues/18346 +[#18457]: https://github.com/JuliaLang/julia/issues/18457 [#18473]: https://github.com/JuliaLang/julia/issues/18473 [#18628]: https://github.com/JuliaLang/julia/issues/18628 [#18644]: https://github.com/JuliaLang/julia/issues/18644 @@ -884,10 +901,24 @@ Language tooling improvements [#19233]: https://github.com/JuliaLang/julia/issues/19233 [#19288]: https://github.com/JuliaLang/julia/issues/19288 [#19305]: https://github.com/JuliaLang/julia/issues/19305 +[#19331]: https://github.com/JuliaLang/julia/issues/19331 [#19449]: https://github.com/JuliaLang/julia/issues/19449 +[#19464]: https://github.com/JuliaLang/julia/issues/19464 [#19469]: https://github.com/JuliaLang/julia/issues/19469 [#19543]: https://github.com/JuliaLang/julia/issues/19543 [#19598]: https://github.com/JuliaLang/julia/issues/19598 [#19635]: https://github.com/JuliaLang/julia/issues/19635 +[#19670]: https://github.com/JuliaLang/julia/issues/19670 +[#19677]: https://github.com/JuliaLang/julia/issues/19677 [#19680]: https://github.com/JuliaLang/julia/issues/19680 +[#19692]: https://github.com/JuliaLang/julia/issues/19692 +[#19722]: https://github.com/JuliaLang/julia/issues/19722 +[#19741]: https://github.com/JuliaLang/julia/issues/19741 [#19787]: https://github.com/JuliaLang/julia/issues/19787 +[#19800]: https://github.com/JuliaLang/julia/issues/19800 +[#19841]: https://github.com/JuliaLang/julia/issues/19841 +[#19903]: https://github.com/JuliaLang/julia/issues/19903 +[#19919]: https://github.com/JuliaLang/julia/issues/19919 +[#19944]: https://github.com/JuliaLang/julia/issues/19944 +[#19950]: https://github.com/JuliaLang/julia/issues/19950 +[#20164]: https://github.com/JuliaLang/julia/issues/20164 \ No newline at end of file From e3268cd9f31afbf9220fe575be183245fdcfdc26 Mon Sep 17 00:00:00 2001 From: Tony Kelman Date: Thu, 26 Jan 2017 10:45:40 -0800 Subject: [PATCH 07/11] Remove remaining commutative dates subtraction methods --- base/dates/arithmetic.jl | 18 ++++++------------ base/dates/periods.jl | 1 - test/dates/periods.jl | 1 - 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/base/dates/arithmetic.jl b/base/dates/arithmetic.jl index 016cae7b8f1ab..4389bf59326db 100644 --- a/base/dates/arithmetic.jl +++ b/base/dates/arithmetic.jl @@ -82,18 +82,12 @@ end (-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y))) (+)(y::Period, x::TimeType) = x + y -for op in (:+, :-) - @eval begin - ($op){T<:TimeType}(x::AbstractArray{T}, y::GeneralPeriod) = broadcast($op, x, y) - ($op){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = broadcast($op, x, y) - ($op){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = broadcast($op, x, y) - end - if op == :+ - @eval begin - ($op){T<:TimeType}(y::GeneralPeriod, x::AbstractArray{T}) = broadcast($op, x, y) - end - end -end +(+){T<:TimeType}(x::AbstractArray{T}, y::GeneralPeriod) = x .+ y +(+){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = x .+ y +(+){T<:TimeType}(y::GeneralPeriod, x::AbstractArray{T}) = x .+ y +(+){P<:GeneralPeriod}(y::TimeType, x::StridedArray{P}) = x .+ y +(-){T<:TimeType}(x::AbstractArray{T}, y::GeneralPeriod) = x .- y +(-){T<:TimeType,P<:GeneralPeriod}(x::StridedArray{P}, y::T) = x .- y # TimeType, AbstractArray{TimeType} (-){T<:TimeType}(x::AbstractArray{T}, y::T) = x .- y diff --git a/base/dates/periods.jl b/base/dates/periods.jl index c5aa78917003d..485d64d56af1d 100644 --- a/base/dates/periods.jl +++ b/base/dates/periods.jl @@ -392,7 +392,6 @@ function (-)(x::TimeType, y::CompoundPeriod) end return x end -(-)(x::CompoundPeriod, y::TimeType) = y - x # Fixed-value Periods (periods corresponding to a well-defined time interval, # as opposed to variable calendar intervals like Year). diff --git a/test/dates/periods.jl b/test/dates/periods.jl index 3b523f0816086..24fb166a90a13 100644 --- a/test/dates/periods.jl +++ b/test/dates/periods.jl @@ -315,7 +315,6 @@ emptyperiod = ((y + d) - d) - y @test Dates.canonicalize(-y + m - w + d - h + mi - s + ms).periods == Dates.Period[-11m, -6d, -59mi, -999ms] @test Dates.Date(2009, 2, 1) - (Dates.Month(1) + Dates.Day(1)) == Dates.Date(2008, 12, 31) -@test (Dates.Month(1) + Dates.Day(1)) - Dates.Date(2009, 2, 1) == Dates.Date(2008, 12, 31) pa = [1y 1m 1w 1d; 1h 1mi 1s 1ms] cpa = [1y + 1s 1m + 1s 1w + 1s 1d + 1s; 1h + 1s 1mi + 1s 2m + 1s 1s + 1ms] From 8671e14525e8406cd98d844abf5005ee98e621bf Mon Sep 17 00:00:00 2001 From: Tony Kelman Date: Thu, 26 Jan 2017 12:23:30 -0800 Subject: [PATCH 08/11] Change tests of deleted methods to test_throws MethodError --- test/dates/arithmetic.jl | 10 ++++++++++ test/dates/periods.jl | 1 + 2 files changed, 11 insertions(+) diff --git a/test/dates/arithmetic.jl b/test/dates/arithmetic.jl index 61b39509c7e5b..d5db8f895cd44 100644 --- a/test/dates/arithmetic.jl +++ b/test/dates/arithmetic.jl @@ -429,3 +429,13 @@ t5 = [Dates.Time(0, 0, 0) Dates.Time(0, 0, 1) Dates.Time(0, 0, 2); Dates.Time(0, @test t4 - t3 == [Dates.Millisecond(1000), Dates.Millisecond(60000), Dates.Millisecond(3600000)] @test (Dates.Date(2009, 1, 1):Dates.Week(1):Dates.Date(2009, 1, 21)) - (Dates.Date(2009, 1, 1):Dates.Day(1):Dates.Date(2009, 1, 3)) == [Dates.Day(0), Dates.Day(6), Dates.Day(12)] @test (Dates.DateTime(2009, 1, 1, 1, 1, 1):Dates.Second(1):Dates.DateTime(2009, 1, 1, 1, 1, 3)) - (Dates.DateTime(2009, 1, 1, 1, 1):Dates.Second(1):Dates.DateTime(2009, 1, 1, 1, 1, 2)) == [Dates.Second(1), Dates.Second(1), Dates.Second(1)] + +# ensure commutative subtraction methods are not defined, #20205 +@test_throws MethodError Dates.Day(1) .- t1 +@test_throws MethodError Dates.Hour(1) .- t3 +@test_throws MethodError Dates.Day(1) - t1 +@test_throws MethodError Dates.Hour(1) - t3 +@test_throws MethodError (Dates.Month(1) + Dates.Day(1)) .- t1 +@test_throws MethodError (Dates.Hour(1) + Dates.Minute(1)) .- t3 +@test_throws MethodError (Dates.Month(1) + Dates.Day(1)) - t1 +@test_throws MethodError (Dates.Hour(1) + Dates.Minute(1)) - t3 diff --git a/test/dates/periods.jl b/test/dates/periods.jl index 24fb166a90a13..f951790b85696 100644 --- a/test/dates/periods.jl +++ b/test/dates/periods.jl @@ -315,6 +315,7 @@ emptyperiod = ((y + d) - d) - y @test Dates.canonicalize(-y + m - w + d - h + mi - s + ms).periods == Dates.Period[-11m, -6d, -59mi, -999ms] @test Dates.Date(2009, 2, 1) - (Dates.Month(1) + Dates.Day(1)) == Dates.Date(2008, 12, 31) +@test_throws MethodError (Dates.Month(1) + Dates.Day(1)) - Dates.Date(2009,2,1) pa = [1y 1m 1w 1d; 1h 1mi 1s 1ms] cpa = [1y + 1s 1m + 1s 1w + 1s 1d + 1s; 1h + 1s 1mi + 1s 2m + 1s 1s + 1ms] From c8cabd5ad801728dc11d5491b95c75c1f95ce245 Mon Sep 17 00:00:00 2001 From: Tony Kelman Date: Thu, 26 Jan 2017 13:30:41 -0800 Subject: [PATCH 09/11] Comment out a test of a deprecated method ref https://github.com/JuliaLang/julia/pull/19920/files#r98093930 --- test/dates/conversions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dates/conversions.jl b/test/dates/conversions.jl index 7767de7b9c019..26124cbc82460 100644 --- a/test/dates/conversions.jl +++ b/test/dates/conversions.jl @@ -73,7 +73,7 @@ let t = Dates.Period[Dates.Week(2), Dates.Day(14), Dates.Hour(14 * 24), Dates.Mi end end @test Dates.Year(3) == Dates.Month(36) -@test_throws ErrorException Int(Dates.Month(36)) # eventually change to MethodError +#@test_throws MethodError Int(Dates.Month(36)) # re-enable when deprecation removed @test Dates.Year(3) < Dates.Month(37) @test_throws InexactError convert(Dates.Year, Dates.Month(37)) @test_throws InexactError Dates.Month(Dates.Year(typemax(Int64))) From 8b35e72a7021cdec7f01692f3d7ffeddb3bd89f5 Mon Sep 17 00:00:00 2001 From: Tony Kelman Date: Fri, 27 Jan 2017 06:39:50 -0800 Subject: [PATCH 10/11] use f in signature of Time docstring to match body --- base/dates/adjusters.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/base/dates/adjusters.jl b/base/dates/adjusters.jl index 9ca5391cd8abb..ddc44711c119f 100644 --- a/base/dates/adjusters.jl +++ b/base/dates/adjusters.jl @@ -197,10 +197,10 @@ function DateTime(func::Function, y, m, d, h, mi, s; step::Period=Millisecond(1) end """ - Time(func::Function, h, mi=0; step::Period=Second(1), limit::Int=10000) - Time(func::Function, h, mi, s; step::Period=Millisecond(1), limit::Int=10000) - Time(func::Function, h, mi, s, ms; step::Period=Microsecond(1), limit::Int=10000) - Time(func::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), limit::Int=10000) + Time(f::Function, h, mi=0; step::Period=Second(1), limit::Int=10000) + Time(f::Function, h, mi, s; step::Period=Millisecond(1), limit::Int=10000) + Time(f::Function, h, mi, s, ms; step::Period=Microsecond(1), limit::Int=10000) + Time(f::Function, h, mi, s, ms, us; step::Period=Nanosecond(1), limit::Int=10000) Create a `Time` through the adjuster API. The starting point will be constructed from the provided `h, mi, s, ms, us` arguments, and will be adjusted until `f::Function` returns `true`. From 0fbc43eee367299d9539c19fea89f255f02599ff Mon Sep 17 00:00:00 2001 From: Tony Kelman Date: Fri, 27 Jan 2017 07:14:54 -0800 Subject: [PATCH 11/11] Also remove questionably associative vararg subtraction unless someone tells me otherwise --- base/dates/periods.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/base/dates/periods.jl b/base/dates/periods.jl index 485d64d56af1d..b1074e67e80b0 100644 --- a/base/dates/periods.jl +++ b/base/dates/periods.jl @@ -374,9 +374,7 @@ Base.isequal(x::CompoundPeriod, y::CompoundPeriod) = x.periods == y.periods # Capture TimeType+-Period methods (+)(a::TimeType, b::Period, c::Period) = (+)(a, b + c) -(-)(a::TimeType, b::Period, c::Period) = (-)(a, b - c) (+)(a::TimeType, b::Period, c::Period, d::Period...) = (+)((+)(a, b + c), d...) -(-)(a::TimeType, b::Period, c::Period, d::Period...) = (-)((-)(a, b - c), d...) function (+)(x::TimeType, y::CompoundPeriod) for p in y.periods