Skip to content

Commit 1ed4d09

Browse files
committed
Only use function barrier for VariableTimeZone
Adding the function barrier with FixedTimeZone ended up increasing allocations for this benchmark: ```julia @benchmark collect($(ZonedDateTime(2020, tz"America/Winnipeg"):Hour(1):ZonedDateTime(2021, tz"America/Winnipeg"))) ```
1 parent f423b4f commit 1ed4d09

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

src/types/zoneddatetime.jl

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,26 @@ keyword is true the given `DateTime` is assumed to be in UTC instead of in local
3232
converted to the specified `TimeZone`. Note that when `from_utc` is true the given
3333
`DateTime` will always exists and is never ambiguous.
3434
"""
35-
function ZonedDateTime(dt::DateTime, tz::TimeZone; from_utc::Bool=false)
36-
_ZonedDateTime(dt, tz, from_utc ? UTC : Local)
37-
end
38-
39-
function _ZonedDateTime(dt::DateTime, tz::VariableTimeZone, T::Type{<:Union{Local,UTC}})
40-
possible = interpret(dt, tz, T)
41-
42-
num = length(possible)
43-
if num == 1
44-
return first(possible)
45-
elseif num == 0
46-
throw(NonExistentTimeError(dt, tz))
47-
else
48-
throw(AmbiguousTimeError(dt, tz))
35+
function ZonedDateTime(dt::DateTime, tz::VariableTimeZone; from_utc::Bool=false)
36+
# Note: Using a function barrier which reduces allocations
37+
function construct(T::Type{<:Union{Local,UTC}})
38+
possible = interpret(dt, tz, T)
39+
40+
num = length(possible)
41+
if num == 1
42+
return first(possible)
43+
elseif num == 0
44+
throw(NonExistentTimeError(dt, tz))
45+
else
46+
throw(AmbiguousTimeError(dt, tz))
47+
end
4948
end
50-
end
5149

52-
function _ZonedDateTime(local_dt::DateTime, tz::FixedTimeZone, ::Type{Local})
53-
utc_dt = local_dt - tz.offset
54-
return ZonedDateTime(utc_dt, tz, tz)
50+
return construct(from_utc ? UTC : Local)
5551
end
5652

57-
function _ZonedDateTime(utc_dt::DateTime, tz::FixedTimeZone, ::Type{UTC})
53+
function ZonedDateTime(dt::DateTime, tz::FixedTimeZone; from_utc::Bool=false)
54+
utc_dt = from_utc ? dt : dt - tz.offset
5855
return ZonedDateTime(utc_dt, tz, tz)
5956
end
6057

0 commit comments

Comments
 (0)