JuliaTime / TimeZones.jl

IANA time zone database access for the Julia programming language
Other
87 stars 53 forks source link

type Dates.DateTime has no field utc_datetime #424

Open lgeissbauer-btig opened 1 year ago

lgeissbauer-btig commented 1 year ago
julia> function utc_to_ny(date_time_utc::DateTime)::ZonedDateTime
               date_time_utc = ZonedDateTime(date_time_utc, tz"UTC")
               date_time_ny = TimeZones.astimezone(date_time_utc, tz"America/New_York")
                       return date_time_ny
           end
utc_to_ny (generic function with 1 method)

julia> JET.@report_call utc_to_ny(now())
═════ 2 possible errors found ═════
┌ @ REPL[23]:3 date_time_ny = astimezone(date_time_utc, America/New_York)
│┌ @ C:\.julia\packages\TimeZones\V28u7\src\conversions.jl:179 i = Core.kwcall(NamedTuple{(:by,)}(tuple(#24)), TimeZones.searchsortedlast, tz.transitions, zdt.utc_datetime)
││┌ @ sort.jl:285 Base.Sort.:(var"#searchsortedlast#5")(_10, _10, _10, _10, _3, v, x)
│││┌ @ sort.jl:285 searchsortedlast(v, x, Base.Sort.ord(lt, by, rev, order))
││││┌ @ sort.jl:283 searchsortedlast(v, x, Base.Sort.firstindex(v), Base.Sort.lastindex(v), o)
│││││┌ @ sort.jl:189 Base.Sort.lt(o, x, v[m])
││││││┌ @ ordering.jl:119 o.by(a)
│││││││┌ @ C:\.julia\packages\TimeZones\V28u7\src\conversions.jl:181 v.utc_datetime
││││││││┌ @ Base.jl:37 Base.getfield(x, f)
│││││││││ type Dates.DateTime has no field utc_datetime
││││││││└──────────────
││││││┌ @ ordering.jl:119 lt(o.order, o.by(a), o.by(b))
│││││││┌ @ ordering.jl:117 Base.Order.isless(a, b)
││││││││ no matching method found `isless(::DateTime, ::TimeZones.Transition)`: Base.Order.isless(a::DateTime, b::TimeZones.Transition)
│││││││└───────────────────

From JET.jl

A1-Mathematik commented 1 year ago

https://github.com/JuliaTime/TimeZones.jl/blob/f8f39b3100334a0079f525304f7dddc7656204da/src/conversions.jl#L181

lgeissbauer-btig commented 1 year ago
by=v -> typeof(v) == Transition ? (v::Transition).utc_datetime : v
lgeissbauer-btig commented 1 year ago

@aviatesk why isn't it clear that the v after the ? is typeof(v) == Transition

aviatesk commented 1 year ago

That form isn't inferrable. Use v isa Transition instead.

lgeissbauer-btig commented 1 year ago

https://github.com/JuliaTime/TimeZones.jl/blob/f8f39b3100334a0079f525304f7dddc7656204da/src/types/zoneddatetime.jl#L19

lgeissbauer-btig commented 1 year ago

@aviatesk why can't JET infere that timezone.cutoff !== nothing. How to fix it?

lgeissbauer-btig commented 1 year ago
═════ 1 possible error found ═════
┌ @ REPL[31]:12 ZonedDateTime(zdt.utc_datetime, tz, zone)
│┌ @ C:\.julia\packages\TimeZones\V28u7\src\types\zoneddatetime.jl:19 utc_datetime TimeZones.:>= timezone.cutoff
││┌ @ operators.jl:429 y <= x
│││┌ @ operators.jl:405 x < y
││││┌ @ operators.jl:356 isless(x, y)
│││││ no matching method found `isless(::Nothing, ::DateTime)`: isless(x::Nothing, y::DateTime)
││││└────
aviatesk commented 1 year ago

Maybe the timezone.cutoff field is mutable? Then extract it into a local object.

lgeissbauer-btig commented 1 year ago

https://github.com/JuliaTime/TimeZones.jl/blob/906cea08c2ca4a5c76b12e14fb60d5df7990fbf7/src/types/variabletimezone.jl#L16

lgeissbauer-btig commented 1 year ago

is not mutable

struct VariableTimeZone <: TimeZone
    name::String
    transitions::Vector{Transition}
    cutoff::Union{DateTime,Nothing}

    function VariableTimeZone(name::AbstractString, transitions::Vector{Transition}, cutoff::Union{DateTime,Nothing}=nothing)
        new(name, transitions, cutoff)
    end
end