Commit Diff


commit - 255c808b4ba858f42db61dcd0277c8a4540a8324
commit + 505d0349160688430b9c55dcab93a7b3641433ca
blob - /dev/null
blob + d09b3ea7f5ea4919959a0683620cd92e433d8e10 (mode 644)
--- /dev/null
+++ changelogs/unreleased/gh-10331-tz-in-totable.md
@@ -0,0 +1,4 @@
+## bugfix/datetime
+
+* Added the `tz` field to a table produced by `:totable()`
+  (gh-10331).
blob - c26e1d3aa8c696f4e396c7f095c5c28f2faca84e
blob + 7daecb0761292325abda5a684bdfc40714869d92
--- src/lua/datetime.lua
+++ src/lua/datetime.lua
@@ -973,6 +973,7 @@ local function datetime_totable(self)
         isdst = datetime_isdst(self),
         nsec = self.nsec,
         tzoffset = self.tzoffset,
+        tz = self.tz,
     }
 end
 
blob - 4cf33d8e066bd119f09fdc310c01ca568bb78cc9
blob + a7f1c7f5f729a18225f1370e7aad6e35b39e0d6e
--- test/app-tap/datetime.test.lua
+++ test/app-tap/datetime.test.lua
@@ -8,7 +8,7 @@ local json = require('json')
 local msgpack = require('msgpack')
 local TZ = date.TZ
 
-test:plan(41)
+test:plan(42)
 
 local INT_MAX = 2147483647
 
@@ -151,7 +151,7 @@ test:test("Datetime API checks", function(test)
     local table_expected = {
         sec =  0, min = 0, wday = 5, day = 1, nsec = 0,
         isdst = false, yday = 1, tzoffset = 0, month = 1,
-        year = 1970, hour = 0
+        year = 1970, hour = 0, tz = ''
     }
     test:is_deeply(local_totable(ts), table_expected, "correct :totable")
     local date_expected = date.new()
@@ -1832,7 +1832,8 @@ test:test("totable{}", function(test)
     test:plan(78)
     local exp = {sec = 0, min = 0, wday = 5, day = 1,
                  nsec = 0, isdst = false, yday = 1,
-                 tzoffset = 0, month = 1, year = 1970, hour = 0}
+                 tzoffset = 0, month = 1, year = 1970, hour = 0,
+                 tz = ''}
     local ts = date.new()
     local totable = ts:totable()
     test:is_deeply(totable, exp, 'date:totable()')
@@ -1864,6 +1865,53 @@ test:test("totable{}", function(test)
     for _, key in pairs({'wday', 'day', 'yday', 'month', 'year'}) do
         test:is(ts[key], osdate[key],
                 ('[%s]: %s == %s'):format(key, ts[key], osdate[key]))
+    end
+end)
+
+test:test('totable() with timezone', function(test)
+    local DEFAULT_TZOFFSET = 0
+    local DEFAULT_TZ = ''
+
+    local MOSCOW_TZOFFSET = 180
+    local MOSCOW_TZ = 'Europe/Moscow'
+
+    local test_cases = {
+        -- Empty datetime value except the timezone.
+        {
+            dt = {tz = MOSCOW_TZ},
+            expected = {
+                tzoffset = MOSCOW_TZOFFSET,
+                tz = MOSCOW_TZ,
+            }
+        },
+        -- Empty datetime value except the tzoffset.
+        {
+            dt = {tzoffset = MOSCOW_TZOFFSET},
+            expected = {
+                tzoffset = MOSCOW_TZOFFSET,
+                tz = '',
+            }
+        },
+        -- Empty datetime value.
+        {
+            dt = {},
+            expected = {
+                tz = DEFAULT_TZ,
+                tzoffset = DEFAULT_TZOFFSET,
+            },
+        },
+    }
+
+    test:plan(#test_cases * 2)
+
+    for _, tc in pairs(test_cases) do
+        local dtab = date.new(tc.dt):totable()
+        local expected = tc.expected
+        test:is(dtab.tzoffset, expected.tzoffset,
+                ('[tzoffset]: %q == %q'):format(dtab.tzoffset,
+                                                expected.tzoffset))
+        test:is(dtab.tz, expected.tz,
+                ('[tz]: %q == %q'):format(dtab.tz, expected.tz))
     end
 end)
 
blob - 1ef0c95e7edb664a32491390fa361fb5579d3448
blob + d2efc2449b55c4bd17dba7bcab092d75b98029ad
--- test/sql-luatest/datetime_test.lua
+++ test/sql-luatest/datetime_test.lua
@@ -864,7 +864,8 @@ end
 g.test_datetime_18_3 = function()
     g.server:exec(function()
         local dt = require('datetime')
-        local dt1 = dt.new({year = 2001, month = 1, day = 1, hour = 1})
+        local dt1 = dt.new(
+            {year = 2001, month = 1, day = 1, hour = 1, tz = 'Z'})
         local sql = [[SELECT CAST('2001-01-01T01:00:00Z' AS DATETIME);]]
         local res = {{dt1}}
         local rows = box.execute(sql).rows
@@ -2218,7 +2219,8 @@ end
 g.test_datetime_32_1 = function()
     g.server:exec(function()
         local dt = require('datetime')
-        local dt1 = dt.new({year = 2000, month = 2, day = 29, hour = 1})
+        local dt1 = dt.new(
+            {year = 2000, month = 2, day = 29, hour = 1, tz = 'Z'})
         local sql = [[SELECT CAST('2000-02-29T01:00:00Z' AS DATETIME);]]
         local res = {{dt1}}
         local rows = box.execute(sql).rows