From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id A14E16EC5F; Mon, 2 Aug 2021 03:42:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A14E16EC5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627864952; bh=ZZuCVqQLWPvS4t7BmPHBEPnyP2khDRJQaiTey6U64Ms=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=SuaXNwEnlaf2f2xtECKmnaVuk8Ydw/B6oOHBM1wHc/o6yMtUr7mAJEIy5L+Fvjjvm DemBZEfl//tKKT0+xMygEDOfq8ztkLIyy/oHLjRUQZ7LgrnbMpgYNuVdZ/eiDutrFr B9TXxcYXBt0CMV4gyEKLIwsbH5yPVV/XrWqOuQek= Received: from smtp58.i.mail.ru (smtp58.i.mail.ru [217.69.128.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id D7D3B6E46E for ; Mon, 2 Aug 2021 03:41:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D7D3B6E46E Received: by smtp58.i.mail.ru with esmtpa (envelope-from ) id 1mAM1K-0000PY-Qg; Mon, 02 Aug 2021 03:41:31 +0300 To: v.shpilevoy@tarantool.org, olegrok@tarantool.org Date: Mon, 2 Aug 2021 03:40:59 +0300 Message-Id: <08c77429c0ec2e87df21c99fae85a46f33bc8760.1627864075.git.tsafin@tarantool.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD941C43E597735A9C351B198F4576AC7B2770D7874BA03B4AE182A05F538085040FB0E1D5E8522DFF96B47915FD8A95473ADCD480A944D04135D114251A043FE30 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7E9C44F3538ABC873EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006370487B2B06A9A4E218638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8F2970FFD0D180854FB358086E3E8180D117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCF1175FABE1C0F9B6A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735209ECD01F8117BC8BEA471835C12D1D977C4224003CC836476EB9C4185024447017B076A6E789B0E975F5C1EE8F4F765FC3353210B8C984A733AA81AA40904B5D9CF19DD082D7633A078D18283394535A93AA81AA40904B5D98AA50765F7900637D2C0C6E7EC2F300AD81D268191BDAD3D698AB9A7B718F8C4D1B931868CE1C5781A620F70A64A45A98AA50765F79006372E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F83C798A30B85E16BC6EABA9B74D0DA47B5C8C57E37DE458BEDA766A37F9254B7 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458F0AFF96BAACF4158235E5A14AD4A4A4625E192CAD1D9E79DB53CE84373687089881132ABB879431B X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C85811CA530B50491FB1C5A52E569C73B50D95A8FE19502829C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF03D88DA7F5FEDC25699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C5CF6F4B28551E3FB2773F7E47D1AF4A3E5964CF6B4C1D4F09C460977C889A740E796DEAE11D951A1D7E09C32AA3244C0117266D7AB2B2FB075B06AACAD762727C0C08F7987826B98D5DD81C2BAB7D1D X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMCfuYI4PrecaUYTtiCvTog== X-Mailru-Sender: B5B6A6EBBD94DAD8315E1362B7B20B880D6AF14F8199240B80DADE4B61A702D05D70F8E16F5E6B9B5C2808D6142752370A8ED71B308007E3DC85537438B7E1A423D748DE48713E689437F6177E88F7363CDA0F3B3F5B9367 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3 3/9] lua, datetime: datetime tests X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Timur Safin via Tarantool-patches Reply-To: Timur Safin Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" * created app-tap test for new builtin module `datetime.lua` * added case to check datetime string formatting using: - asctime (gmt time); - ctime (local TZ time); - strftime (using given format). * added positive/negative checks to datetime test: - for that,in the datetime.lua, we have extended api of `parse_date()`, `parse_time()`, and `parse_time_zone()` so they return not only parsed object, but also a length of parsed substring; - which allows us to parse even _partially_ valid strings like "20121224 Foo bar". * checks calculated attributes to date object, e.g.: - timestamp, seconds, microseconds, minute, or hours Part of #5941 --- test/app-tap/datetime.test.lua | 206 +++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100755 test/app-tap/datetime.test.lua diff --git a/test/app-tap/datetime.test.lua b/test/app-tap/datetime.test.lua new file mode 100755 index 000000000..f7541567e --- /dev/null +++ b/test/app-tap/datetime.test.lua @@ -0,0 +1,206 @@ +#!/usr/bin/env tarantool + +local tap = require('tap') +local test = tap.test("errno") +local date = require('datetime') +local ffi = require('ffi') + + +test:plan(6) + +test:test("Simple tests for parser", function(test) + test:plan(2) + test:ok(date("1970-01-01T01:00:00Z") == + date {year=1970, month=1, day=1, hour=1, minute=0, second=0}) + test:ok(date("1970-01-01T02:00:00+02:00") == + date {year=1970, month=1, day=1, hour=2, minute=0, second=0, tz=120}) +end) + +test:test("Multiple tests for parser (with nanoseconds)", function(test) + test:plan(165) + -- borrowed from p5-time-moments/t/180_from_string.t + local tests = + { + { '1970-01-01T00:00:00Z', 0, 0, 0 }, + { '1970-01-01T02:00:00+02:00', 0, 0, 120 }, + { '1970-01-01T01:30:00+01:30', 0, 0, 90 }, + { '1970-01-01T01:00:00+01:00', 0, 0, 60 }, + { '1970-01-01T00:01:00+00:01', 0, 0, 1 }, + { '1970-01-01T00:00:00+00:00', 0, 0, 0 }, + { '1969-12-31T23:59:00-00:01', 0, 0, -1 }, + { '1969-12-31T23:00:00-01:00', 0, 0, -60 }, + { '1969-12-31T22:30:00-01:30', 0, 0, -90 }, + { '1969-12-31T22:00:00-02:00', 0, 0, -120 }, + { '1970-01-01T00:00:00.123456789Z', 0, 123456789, 0 }, + { '1970-01-01T00:00:00.12345678Z', 0, 123456780, 0 }, + { '1970-01-01T00:00:00.1234567Z', 0, 123456700, 0 }, + { '1970-01-01T00:00:00.123456Z', 0, 123456000, 0 }, + { '1970-01-01T00:00:00.12345Z', 0, 123450000, 0 }, + { '1970-01-01T00:00:00.1234Z', 0, 123400000, 0 }, + { '1970-01-01T00:00:00.123Z', 0, 123000000, 0 }, + { '1970-01-01T00:00:00.12Z', 0, 120000000, 0 }, + { '1970-01-01T00:00:00.1Z', 0, 100000000, 0 }, + { '1970-01-01T00:00:00.01Z', 0, 10000000, 0 }, + { '1970-01-01T00:00:00.001Z', 0, 1000000, 0 }, + { '1970-01-01T00:00:00.0001Z', 0, 100000, 0 }, + { '1970-01-01T00:00:00.00001Z', 0, 10000, 0 }, + { '1970-01-01T00:00:00.000001Z', 0, 1000, 0 }, + { '1970-01-01T00:00:00.0000001Z', 0, 100, 0 }, + { '1970-01-01T00:00:00.00000001Z', 0, 10, 0 }, + { '1970-01-01T00:00:00.000000001Z', 0, 1, 0 }, + { '1970-01-01T00:00:00.000000009Z', 0, 9, 0 }, + { '1970-01-01T00:00:00.00000009Z', 0, 90, 0 }, + { '1970-01-01T00:00:00.0000009Z', 0, 900, 0 }, + { '1970-01-01T00:00:00.000009Z', 0, 9000, 0 }, + { '1970-01-01T00:00:00.00009Z', 0, 90000, 0 }, + { '1970-01-01T00:00:00.0009Z', 0, 900000, 0 }, + { '1970-01-01T00:00:00.009Z', 0, 9000000, 0 }, + { '1970-01-01T00:00:00.09Z', 0, 90000000, 0 }, + { '1970-01-01T00:00:00.9Z', 0, 900000000, 0 }, + { '1970-01-01T00:00:00.99Z', 0, 990000000, 0 }, + { '1970-01-01T00:00:00.999Z', 0, 999000000, 0 }, + { '1970-01-01T00:00:00.9999Z', 0, 999900000, 0 }, + { '1970-01-01T00:00:00.99999Z', 0, 999990000, 0 }, + { '1970-01-01T00:00:00.999999Z', 0, 999999000, 0 }, + { '1970-01-01T00:00:00.9999999Z', 0, 999999900, 0 }, + { '1970-01-01T00:00:00.99999999Z', 0, 999999990, 0 }, + { '1970-01-01T00:00:00.999999999Z', 0, 999999999, 0 }, + { '1970-01-01T00:00:00.0Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.00Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.000Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.0000Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.00000Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.000000Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.0000000Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.00000000Z', 0, 0, 0 }, + { '1970-01-01T00:00:00.000000000Z', 0, 0, 0 }, + { '1973-11-29T21:33:09Z', 123456789, 0, 0 }, + { '2013-10-28T17:51:56Z', 1382982716, 0, 0 }, + -- { '9999-12-31T23:59:59Z', 253402300799, 0, 0 }, + } + for _, value in ipairs(tests) do + local str, epoch, nsec, offset + str, epoch, nsec, offset = unpack(value) + local dt = date(str) + test:ok(dt.secs == epoch, ('%s: dt.secs == %d'):format(str, epoch)) + test:ok(dt.nsec == nsec, ('%s: dt.nsec == %d'):format(str, nsec)) + test:ok(dt.offset == offset, ('%s: dt.offset == %d'):format(str, offset)) + end +end) + +ffi.cdef [[ + void tzset(void); +]] + +test:test("Datetime string formatting", function(test) + test:plan(7) + local str = "1970-01-01" + local t = date(str) + test:ok(t.secs == 0, ('%s: t.secs == %d'):format(str, tonumber(t.secs))) + test:ok(t.nsec == 0, ('%s: t.nsec == %d'):format(str, t.nsec)) + test:ok(t.offset == 0, ('%s: t.offset == %d'):format(str, t.offset)) + test:ok(date.asctime(t) == 'Thu Jan 1 00:00:00 1970\n', ('%s: asctime'):format(str)) + -- ctime() is local timezone dependent. To make sure that + -- test is deterministic we enforce timezone via TZ environment + -- manipulations and calling tzset() + + -- redefine timezone to be always GMT-2 + os.setenv('TZ', 'GMT-2') + ffi.C.tzset() + test:ok(date.ctime(t) == 'Thu Jan 1 02:00:00 1970\n', ('%s: ctime with timezone'):format(str)) + test:ok(date.strftime('%d/%m/%Y', t) == '01/01/1970', ('%s: strftime #1'):format(str)) + test:ok(date.strftime('%A %d. %B %Y', t) == 'Thursday 01. January 1970', ('%s: strftime #2'):format(str)) +end) + +test:test("Parse iso date - valid strings", function(test) + test:plan(32) + local good = { + {2012, 12, 24, "20121224", 8 }, + {2012, 12, 24, "20121224 Foo bar", 8 }, + {2012, 12, 24, "2012-12-24", 10 }, + {2012, 12, 24, "2012-12-24 23:59:59", 10 }, + {2012, 12, 24, "2012-12-24T00:00:00+00:00", 10 }, + {2012, 12, 24, "2012359", 7 }, + {2012, 12, 24, "2012359T235959+0130", 7 }, + {2012, 12, 24, "2012-359", 8 }, + {2012, 12, 24, "2012W521", 8 }, + {2012, 12, 24, "2012-W52-1", 10 }, + {2012, 12, 24, "2012Q485", 8 }, + {2012, 12, 24, "2012-Q4-85", 10 }, + { 1, 1, 1, "0001-Q1-01", 10 }, + { 1, 1, 1, "0001-W01-1", 10 }, + { 1, 1, 1, "0001-01-01", 10 }, + { 1, 1, 1, "0001-001", 8 }, + } + + for _, value in ipairs(good) do + local year, month, day, str, date_part_len + year, month, day, str, date_part_len = unpack(value) + local expected_date = date{year = year, month = month, day = day} + local date_part, len + date_part, len = date.parse_date(str) + test:ok(len == date_part_len, ('%s: length check %d'):format(str, len)) + test:ok(expected_date == date_part, ('%s: expected date'):format(str)) + end +end) + +test:test("Parse iso date - invalid strings", function(test) + test:plan(62) + local bad = { + "20121232" , -- Invalid day of month + "2012-12-310", -- Invalid day of month + "2012-13-24" , -- Invalid month + "2012367" , -- Invalid day of year + "2012-000" , -- Invalid day of year + "2012W533" , -- Invalid week of year + "2012-W52-8" , -- Invalid day of week + "2012Q495" , -- Invalid day of quarter + "2012-Q5-85" , -- Invalid quarter + "20123670" , -- Trailing digit + "201212320" , -- Trailing digit + "2012-12" , -- Reduced accuracy + "2012-Q4" , -- Reduced accuracy + "2012-Q42" , -- Invalid + "2012-Q1-1" , -- Invalid day of quarter + "2012Q--420" , -- Invalid + "2012-Q-420" , -- Invalid + "2012Q11" , -- Incomplete + "2012Q1234" , -- Trailing digit + "2012W12" , -- Incomplete + "2012W1234" , -- Trailing digit + "2012W-123" , -- Invalid + "2012-W12" , -- Incomplete + "2012-W12-12", -- Trailing digit + "2012U1234" , -- Invalid + "2012-1234" , -- Invalid + "2012-X1234" , -- Invalid + "0000-Q1-01" , -- Year less than 0001 + "0000-W01-1" , -- Year less than 0001 + "0000-01-01" , -- Year less than 0001 + "0000-001" , -- Year less than 0001 + } + + for _, str in ipairs(bad) do + local date_part, len + date_part, len = date.parse_date(str) + test:ok(len == 0, ('%s: length check %d'):format(str, len)) + test:ok(date_part == nil, ('%s: empty date check %s'):format(str, date_part)) + end +end) + +test:test("Parse tiny date into seconds and other parts", function(test) + test:plan(9) + local str = '19700101 00:00:30.528' + local tiny = date(str) + test:ok(tiny.secs == 30, ("secs of '%s'"):format(str)) + test:ok(tiny.nsec == 528000000, ("nsec of '%s'"):format(str)) + test:ok(tiny.nanoseconds == 30528000000, "nanoseconds") + test:ok(tiny.microseconds == 30528000, "microseconds") + test:ok(tiny.milliseconds == 30528, "milliseconds") + test:ok(tiny.seconds == 30.528, "seconds") + test:ok(tiny.timestamp == 30.528, "timestamp") + test:ok(tiny.minutes == 0.5088, "minutes") + test:ok(tiny.hours == 0.00848, "hours") +end) + +os.exit(test:check() and 0 or 1) -- 2.29.2