Skip to content

Commit c50d2bb

Browse files
datetime: get timezone before HMS and YMD on new
This patch makes `datetime:new()` call internally determine the timezone before calculating time since epoch when passing a timestamp as a parameter. It's needed to fix handling timezones with timestamps to be able to take the provided timezone into account when calculating time since epoch. Needed for tarantool#10363 NO_CHANGELOG=no visible changes NO_TEST=no new behavior NO_DOC=no new behavior
1 parent b8cc6b4 commit c50d2bb

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/lua/datetime.lua

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,23 @@ local function datetime_new(obj)
584584
else
585585
nsec = 0
586586
end
587+
588+
local offset = obj.tzoffset
589+
if offset ~= nil then
590+
offset = get_timezone(offset, 'tzoffset')
591+
-- At the moment the range of known timezones is
592+
-- UTC-12:00..UTC+14:00.
593+
--
594+
-- https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
595+
check_range(offset, -720, 840, 'tzoffset')
596+
end
597+
598+
local tzindex = 0
599+
local tzname = obj.tz
600+
if tzname ~= nil then
601+
offset, tzindex = parse_tzname(epoch_from_dt(dt), tzname)
602+
end
603+
587604
local ts = obj.timestamp
588605
if ts ~= nil then
589606
if ymd then
@@ -615,14 +632,6 @@ local function datetime_new(obj)
615632
hms = true
616633
end
617634

618-
local offset = obj.tzoffset
619-
if offset ~= nil then
620-
offset = get_timezone(offset, 'tzoffset')
621-
-- at the moment the range of known timezones is UTC-12:00..UTC+14:00
622-
-- https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
623-
check_range(offset, -720, 840, 'tzoffset')
624-
end
625-
626635
-- .year, .month, .day
627636
if ymd then
628637
y = y or 1970
@@ -640,12 +649,6 @@ local function datetime_new(obj)
640649
dt = dt_from_ymd_checked(y, M, d)
641650
end
642651

643-
local tzindex = 0
644-
local tzname = obj.tz
645-
if tzname ~= nil then
646-
offset, tzindex = parse_tzname(epoch_from_dt(dt), tzname)
647-
end
648-
649652
-- .hour, .minute, .second
650653
local secs = 0
651654
if hms then

0 commit comments

Comments
 (0)