Skip to content

Commit

Permalink
more tests for reinterpret
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Mar 4, 2024
1 parent 7462261 commit ada7b83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,24 @@ year, month, day, minute, second and millisecond.
The conversion might fail if a particular date does not exist in the
target calendar.
"""
function reinterpret(::Type{T1}, dt::T2) where T1 <: Union{AbstractCFDateTime,DateTime} where T2 <: Union{AbstractCFDateTime,DateTime}
function reinterpret(::Type{T1}, dt::T2) where T1 <: AbstractCFDateTime where T2 <: Union{AbstractCFDateTime,DateTime}
return T1(
Dates.year(dt),Dates.month(dt),Dates.day(dt),
Dates.hour(dt),Dates.minute(dt),Dates.second(dt),
Dates.millisecond(dt))
end

function reinterpret(::Type{DateTime}, dt::T2) where T2 <: AbstractCFDateTime
return DateTime(
Dates.year(dt),Dates.month(dt),Dates.day(dt),
Dates.hour(dt),Dates.minute(dt),Dates.second(dt),
Dates.millisecond(dt))
end

function reinterpret(::Type{T1}, dt::T2) where T1 <: AbstractCFDateTime where T2 <: AbstractCFDateTime
return T1(datetuple(dt)...)
end

"""
dt2 = convert(::Type{T}, dt)
Expand Down
4 changes: 3 additions & 1 deletion src/period.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ end

for op in (:+,:-,:(==))
@eval begin
$op(p1::Union{Period,Dates.Period},p2::Union{Period,Dates.Period}) = $op(promote(p1,p2)...)
$op(p1::Period,p2::Period) = $op(promote(p1,p2)...)
$op(p1::Period,p2::Dates.Period) = $op(promote(p1,p2)...)
$op(p1::Dates.Period,p2::Period) = $op(promote(p1,p2)...)
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/test_time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ dt = CFTime.reinterpret(DateTime, DateTimeNoLeap(1900,2,28))
@test Dates.month(dt) == 2
@test Dates.day(dt) == 28

dt = CFTime.reinterpret(DateTimeNoLeap, DateTime(1900,2,28))
@test typeof(dt) <: DateTimeNoLeap
@test Dates.year(dt) == 1900
@test Dates.month(dt) == 2
@test Dates.day(dt) == 28

# check ordering

@test DateTimeStandard(2000,01,01) < DateTimeStandard(2000,01,02)
Expand Down

0 comments on commit ada7b83

Please sign in to comment.