Skip to content

Commit

Permalink
do not convert microseconds or smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Mar 6, 2024
1 parent 630a72b commit a6035a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
46 changes: 16 additions & 30 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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 <: AbstractCFDateTime where T2 <: Union{AbstractCFDateTime,DateTime}
function reinterpret(::Type{T1}, dt::DateTime) where T1 <: AbstractCFDateTime
return T1(
Dates.year(dt),Dates.month(dt),Dates.day(dt),
Dates.hour(dt),Dates.minute(dt),Dates.second(dt),
Expand All @@ -89,30 +89,6 @@ function reinterpret(::Type{T1}, dt::T2) where T1 <: AbstractCFDateTime where T2
return T1(datetuple(dt)...)
end

"""
dt2 = convert(::Type{T}, dt)
Convert a DateTime of type `DateTimeStandard`, `DateTimeProlepticGregorian`,
`DateTimeJulian` or `DateTime` into the type `T` which can also be either
`DateTimeStandard`, `DateTimeProlepticGregorian`, `DateTimeJulian` or `DateTime`.
Conversion is done such that duration (difference of DateTime types) are
preserved. For dates on and after 1582-10-15, the year, month and days are the same for
the types `DateTimeStandard`, `DateTimeProlepticGregorian` and `DateTime`.
For dates before 1582-10-15, the year, month and days are the same for
the types `DateTimeStandard` and `DateTimeJulian`.
"""
# function convert(::Type{T1}, dt::T2) where T1 <: Union{DateTimeStandard,DateTimeProlepticGregorian,DateTimeJulian} where T2 <: Union{DateTimeStandard,DateTimeProlepticGregorian,DateTimeJulian}
# return T1(dt.instant)
# end


# function convert(::Type{T1}, dt::DateTime) where T1 <: Union{DateTimeStandard,DateTimeProlepticGregorian,DateTimeJulian}
# T1(UTInstant{Millisecond}(dt.instant.periods - DATETIME_OFFSET))
# end


function parseDT(::Type{Tuple},str)
str = replace(str,"T" => " ")

Expand Down Expand Up @@ -346,16 +322,26 @@ dt = CFTime.timedecode([0,1,2,3],"days since 2000-01-01 00:00:00","360_day")
"""
function timedecode(data,units,calendar = "standard"; prefer_datetime = true)
function datetime_convert(dt::AbstractCFDateTime{Period{T,Tfactor,Texponent}}) where
{T,Tfactor,Texponent}

if unwrap(Texponent) >= -3
# milliseconds, seconds, ...
convert(DateTime,dt)
else
# do not convert microseconds or smaller
dt
end
end
datetime_convert(dt::Missing) = missing


DT = timetype(calendar)
dt = timedecode(DT,data,units)

if (prefer_datetime &&
(DT in [DateTimeStandard,DateTimeProlepticGregorian,DateTimeJulian])
#&& _exponent(eltype(dt.instant)) >= -3
)
(DT in [DateTimeStandard,DateTimeProlepticGregorian,DateTimeJulian]))

datetime_convert(dt) = convert(DateTime,dt)
datetime_convert(dt::Missing) = missing
return datetime_convert.(dt)
else
return dt
Expand Down
4 changes: 1 addition & 3 deletions test/test_time.jl
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,4 @@ end
@test timeencode(DateTime(2000,1,2), "days since 2000-01-01 00:00:00") == 1


timedecode(0, "seconds since 2000-01-01 00:00:00", "proleptic_gregorian"),

@test_logs (:warn,r".*converted.*") @test_throws InexactError convert(DateTime,DateTimeStandard(2000,1,1,0,0,0,0,1,units = :microsecond))
@test_logs (:warn,r".*cannot.*") @test_throws InexactError convert(DateTime,DateTimeStandard(2000,1,1,0,0,0,0,1,units = :microsecond))

0 comments on commit a6035a4

Please sign in to comment.