commit 04811e032f29afe0fa6206ef2c7a0f8434861830 from: Sergey Bronnikov via: Sergey Kaplun date: Tue Sep 17 14:52:12 2024 UTC datetime: use tzoffset in a parse() with custom format The patch fixes a behaviour, when `datetime.parse()` ignores `tzoffset` option if custom format is used. Fixes #8333 Relates to #10420 NO_DOC=bugfix commit - d7d3063fbd5a74563fde539f2c74852a1e04c1cd commit + 04811e032f29afe0fa6206ef2c7a0f8434861830 blob - /dev/null blob + 73130e56ce39610b34a0bf3c60130ea8b8174675 (mode 644) --- /dev/null +++ changelogs/unreleased/gh-8333-fix-ignoring-tzoffset-in-parse.md @@ -0,0 +1,4 @@ +## bugfix/lua/datetime + +* Fixed a bug that caused `datetime.parse()` ignore `tzoffset` + option if a custom format was used (gh-8333). blob - 5b174a8ca44f2f5028d099bd9bef8b45e3c9db84 blob + e92a400014c76fb0b5d22c61c21e4f15052cf1b8 --- src/lua/datetime.lua +++ src/lua/datetime.lua @@ -913,9 +913,8 @@ local function datetime_parse_from(str, obj) end check_str_or_nil(fmt, 'datetime.parse()') - local offset if tzoffset ~= nil then - offset = get_timezone(tzoffset, 'tzoffset') + local offset = get_timezone(tzoffset, 'tzoffset') check_range(offset, -720, 840, 'tzoffset') end @@ -923,16 +922,20 @@ local function datetime_parse_from(str, obj) check_str(tzname, 'datetime.parse()') end + local date, len if not fmt or fmt == '' or fmt == 'iso8601' or fmt == 'rfc3339' then - local date, len = datetime_parse_full(str) - -- Override timezone. - if date.tz == '' and date.tzoffset == 0 then - datetime_set(date, obj or {}) - end - return date, tonumber(len) - else - return datetime_parse_format(str, fmt) + date, len = datetime_parse_full(str) + else + date, len = datetime_parse_format(str, fmt) end + + -- Override timezone, if it was not specified in a parsed + -- string. + if date.tz == '' and date.tzoffset == 0 then + datetime_set(date, { tzoffset = tzoffset, tz = tzname }) + end + + return date, len end --[[ blob - d448a6d9b8d1b36ac911d2e52efbd0bf9d00dfa1 blob + 2ca31e4cad13b2cacb8287b3fe94796fb5b9f1be --- test/app-tap/datetime.test.lua +++ test/app-tap/datetime.test.lua @@ -424,7 +424,7 @@ test:test("Formatting limits", function(test) end) test:test("Simple tests for parser", function(test) - test:plan(12) + test:plan(14) test:ok(date.parse("1970-01-01T01:00:00Z") == date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0}) test:ok(date.parse("1970-01-01T01:00:00Z", {format = 'iso8601'}) == @@ -445,6 +445,10 @@ test:test("Simple tests for parser", function(test) date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0, tzoffset=0}) test:ok(date.parse("1970-01-01T01:00:00+01:00", {tzoffset = '+02:00'}) == date.new{year=1970, mon=1, day=1, hour=1, min=0, sec=0, tzoffset=60}) + test:ok(date.parse('1998-11-25', { format = '%Y-%m-%d', tzoffset = 180 }) == + date.new({ year = 1998, month = 11, day = 25, tzoffset = 180 })) + test:ok(date.parse('1998', { format = '%Y', tzoffset = '+03:00' }) == + date.new({ year = 1998, tzoffset = 180 })) test:ok(date.parse("1970-01-01T02:00:00Z") < date.new{year=1970, mon=1, day=1, hour=2, min=0, sec=1})