File: date_time.lua

package info (click to toggle)
lua-sandbox-extensions 0~git20161128-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,596 kB
  • ctags: 1,458
  • sloc: ansic: 4,402; cpp: 2,102; makefile: 8
file content (145 lines) | stat: -rw-r--r-- 4,472 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

require "string"

local dt = require "lpeg.date_time"

local function test_valid(grammar, tests, results)
    for i,v in ipairs(tests) do
        t = grammar:match(v)
        if not t then
            error(v)
        else
            ns = dt.time_to_ns(t)
            if ns ~= results[i] then
                error(string.format("test: %d %s expected: %g received: %g", i, v, results[i], ns), 2)
            end
        end
    end
end

local function rfc3339()
    local tests = {"1999-05-05T23:23:59.217-07:00",
        "1985-04-12T23:20:50.52Z",
        "1996-12-19T16:39:57-08:00",
        "1990-12-31T23:59:60Z",
        "1990-12-31T15:59:60-08:00",
        "1937-01-01T12:00:27.87+00:20"}
    local results = {925971839217000064,
        482196050520000000,
        851042397000000000,
        662688000000000000,
        662688000000000000,
        -1041337172130000000}
    if os.date("%c"):find("^%d") then
        results[6] = 0 -- windows will fail to convert this time
    end
    test_valid(dt.rfc3339, tests, results)
end

local function rfc3339_invalid()
    local tests = {"1985-04-12t23:20:50.52Z",
        "1985-04-12T23:20:50.52z",
        "1985-04-12",
        "1999-05-05T23:23:59.217-0700"}
    for i,v in ipairs(tests) do
        if dt.rfc3339:match(v) then
            error(v)
        end
    end
end

local function clf()
    local tests = {"10/Feb/2014:08:46:36 -0800"}
    local results = {1392050796000000000}
    test_valid(dt.clf_timestamp, tests, results)
end

local function rfc3164()
    local tests = {"Feb 10 16:46:36"}
    local results = {os.time{year = os.date("%Y"), month = 2, day = 10, hour = 16, min = 46, sec = 36} * 1e9}
    test_valid(dt.rfc3164_timestamp, tests, results)
end

local function mysql()
    local tests = {"20140210164636"}
    local results = {1392050796000000000}
    test_valid(dt.mysql_timestamp, tests, results)
end

local function pgsql()
    local tests = {"2014-02-10 16:46:36"}
    local results = {1392050796000000000}
    test_valid(dt.pgsql_timestamp, tests, results)
end

local function strftime_all()
    local formats
    if os.date("%c"):find("^%d") then -- windows C89 support only
        formats = {"%a", "%A", "%b", "%B","%c", "%d",
             "%H", "%I", "%j", "%m", "%M", "%p",
             "%S", "%U", "%w", "%W", "%x",
             "%X", "%y", "%Y", "%z", "%Z", "%%", "test string"}
    else
        formats = {"%a", "%A", "%b", "%B","%c","%C", "%d", "%D", "%e",
            "%F", "%g", "%G", "%h", "%H", "%I", "%j", "%k", "%l", "%m",
            "%M", "%n", "%p", "%r", "%R", "%s", "%S", "%t", "%T", "%u",
            "%U", "%V", "%w", "%W", "%x","%X", "%y", "%Y", "%z", "%Z",
            "%%", "test string"}
    end

    for i,v in ipairs(formats) do
        local test = os.date(v)
        local g = dt.build_strftime_grammar(v)
        if not g:match(test) then
            error(string.format("failed parsing: %s %s", v, test))
        end
    end
end

local function strftime_composite()
    local formats = {"%c", "%D %T", "%D %r", "%d/%b/%Y:%H:%M:%S %z"}
    local inputs = {"Mon Feb 10 16:46:36 2014", "02/10/14 16:46:36", "02/10/14 04:46:36 PM", "10/Feb/2014:16:46:36 +0000"}
    if os.date("%c"):find("^%d") then -- windows %c is non-standard
        inputs[1] = inputs[2]
    end
    local result = 1392050796000000000
    for i,v in ipairs(formats) do
        local g = dt.build_strftime_grammar(v)
        local t = g:match(inputs[i])
        if not t then
            error(string.format("failed parsing: %s %s", v, inputs[i]))
        end
        if result ~=  dt.time_to_ns(t) then
            error(string.format("time conversion failed %s", inputs[i]))
        end
    end
end

local function strftime_secfrac()
    local g = dt.build_strftime_grammar("%d/%b/%Y:%H:%M:%S.%f")
    local t = g:match("10/Feb/2014:16:46:36.124")
    assert(t)
    local ns = dt.time_to_ns(t)
    assert(1392050796124000000 ==  ns, string.format("%d", ns))
end

local function strftime_invalid()
    local r, g = pcall(dt.build_strftime_grammar, "%E")
    if r then
        error("allowed to build a grammar with an invalid specifier")
    end
end

rfc3339()
rfc3339_invalid()
clf()
rfc3164()
mysql()
pgsql()
strftime_all()
strftime_composite()
strftime_invalid()
strftime_secfrac()