Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,16 @@ CFTime.timeencode(dt,"days since 2000-01-01 00:00:00")

"""
function timeencode(
data::AbstractArray{DT, N}, units,
data::AbstractArray{DT, N}, newunits,
calendar = "standard"
) where {N} where {DT <: Union{DateTime, AbstractCFDateTime, Union{DateTime, AbstractCFDateTime, Missing}}}
) where {N} where {DT <: Union{DateTime, AbstractCFDateTime, Missing}}

DT2 = timetype(calendar)
t0, plength = timeunits(DT2, units)
oldunits = only(unique(units.(data)))
t0, plength = timeunits(DT2, newunits)
if oldunits == newunits && DT <: DT2
return Dates.value.(data .- t0)
end

function encode(dt)
if ismissing(dt)
Expand Down
2 changes: 2 additions & 0 deletions src/datetime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ function units(dt::AbstractCFDateTime{T, Torigintuple}) where {T, Torigintuple}
format_datetuple(unwrap(Torigintuple))
)
end
units(::DateTime) = nothing
units(::Missing) = nothing

+(dt::AbstractCFDateTime, p::Union{Dates.TimePeriod, Dates.Day}) = dt + convert(CFTime.Period, p)

Expand Down
6 changes: 6 additions & 0 deletions test/test_cf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,10 @@ end

t = CFTime.timedecode(1.0e9, "nanoseconds since 2000-01-01 00:00:00.001", "proleptic_gregorian")
@test CFTime.second(t) == 1

t = CFTime.timedecode(1, "nanoseconds since 2000-01-01 00:00:00.001", "proleptic_gregorian")
@test typeof(timeencode(t, "nanoseconds since 2000-01-01T00:00:00.001", "proleptic_gregorian")) == Int

t = CFTime.timedecode(1.0, "nanoseconds since 2000-01-01 00:00:00.001", "proleptic_gregorian")
@test typeof(timeencode(t, "nanoseconds since 2000-01-01T00:00:00.001", "proleptic_gregorian")) == Float64
end
Loading