From 0c5ade0638e8701e1aafb9dc4ccb0e8e9cdb8f49 Mon Sep 17 00:00:00 2001 From: Alexander Barth Date: Thu, 13 Dec 2018 11:30:48 +0100 Subject: [PATCH] issue #19 --- src/time.jl | 23 +++++++++++++++++++++-- test/test_time.jl | 10 ++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/time.jl b/src/time.jl index 4d760f87..b8d6584f 100644 --- a/src/time.jl +++ b/src/time.jl @@ -5,12 +5,12 @@ if VERSION >= v"0.7.0-beta.0" import Dates: UTInstant, Millisecond import Dates: year, month, day, hour, minute, second, millisecond import Dates: daysinmonth, daysinyear, yearmonthday, yearmonth - import Dates: monthday + import Dates: monthday, len else import Base.Dates: UTInstant, Millisecond import Base.Dates: year, month, day, hour, minute, second, millisecond import Base.Dates: daysinmonth, daysinyear, yearmonthday, yearmonth - import Base.Dates: monthday + import Base.Dates: monthday, len end import Base: +, -, isless, string, show, convert, reinterpret @@ -715,6 +715,25 @@ Simultaneously return the month and day parts of `dt`. """ monthday(dt::AbstractCFDateTime) = (Dates.month(dt),Dates.day(dt)) + +function Dates.len(first::T, last::T, step::DT) where T <: AbstractCFDateTime where + DT <: Union{Dates.Day,Dates.Hour,Dates.Minute,Dates.Second,Dates.Millisecond} + return Dates.value(last-first) รท Dates.value(Dates.Millisecond(step)) +end + +function Dates.len(first::T, last::T, step) where T <: AbstractCFDateTime + if Dates.value(step) == 0 + error("the step should not be zero") + end + len = 0 + next = first+step + while next <= last + next = next+step + len = len+1 + end + return len +end + export daysinmonth, daysinyear, yearmonthday, yearmonth, monthday export DateTimeStandard, DateTimeJulian, DateTimeProlepticGregorian, diff --git a/test/test_time.jl b/test/test_time.jl index c68e3816..5a013d79 100644 --- a/test/test_time.jl +++ b/test/test_time.jl @@ -362,6 +362,16 @@ for T in [DateTimeStandard, DateTimeJulian, DateTimeProlepticGregorian, @test T(Int16(2000),Int32(1),UInt8(1)) == T(2000,1,1) end +# time ranges + +@test length(DateTimeNoLeap(2000, 01, 01):Day(1):DateTimeNoLeap(2000, 12, 31)) == 365 +@test length(DateTimeNoLeap(2000, 01, 01):Month(1):DateTimeNoLeap(2000, 12, 31)) == 12 +@test length(DateTimeNoLeap(2000, 01, 01):Year(1):DateTimeNoLeap(2001, 1, 1)) == 2 + +for T in [DateTimeStandard, DateTimeJulian, DateTimeProlepticGregorian, + DateTimeAllLeap, DateTimeNoLeap, DateTime360Day] + +end # if @isdefined DataArrays