Skip to content

Commit

Permalink
issue #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Dec 13, 2018
1 parent e90ca10 commit 0c5ade0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions test/test_time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0c5ade0

Please sign in to comment.