Comparison

spec/util_datetime_spec.lua @ 12802:4a8740e01813

Merge 0.12->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 12 Dec 2022 07:10:54 +0100
parent 12755:a09dacf660d2
comparison
equal deleted inserted replaced
12801:ebd6b4d8bf04 12802:4a8740e01813
14 end); 14 end);
15 it("should look like a date", function () 15 it("should look like a date", function ()
16 assert.truthy(string.find(date(), "^%d%d%d%d%-%d%d%-%d%d$")); 16 assert.truthy(string.find(date(), "^%d%d%d%d%-%d%d%-%d%d$"));
17 end); 17 end);
18 it("should work", function () 18 it("should work", function ()
19 assert.equals(date(1136239445), "2006-01-02"); 19 assert.equals("2006-01-02", date(1136239445));
20 end);
21 it("should ignore fractional parts", function ()
22 assert.equals("2006-01-02", date(1136239445.5));
20 end); 23 end);
21 end); 24 end);
22 describe("#time", function () 25 describe("#time", function ()
23 local time = util_datetime.time; 26 local time = util_datetime.time;
24 it("should exist", function () 27 it("should exist", function ()
30 it("should look like a timestamp", function () 33 it("should look like a timestamp", function ()
31 -- Note: Sub-second precision and timezones are ignored 34 -- Note: Sub-second precision and timezones are ignored
32 assert.truthy(string.find(time(), "^%d%d:%d%d:%d%d")); 35 assert.truthy(string.find(time(), "^%d%d:%d%d:%d%d"));
33 end); 36 end);
34 it("should work", function () 37 it("should work", function ()
35 assert.equals(time(1136239445), "22:04:05"); 38 assert.equals("22:04:05", time(1136239445));
36 end); 39 end);
40 it("should handle precision", function ()
41 assert.equal("14:46:31.158200", time(1660488391.1582))
42 assert.equal("14:46:32.158200", time(1660488392.1582))
43 assert.equal("14:46:33.158200", time(1660488393.1582))
44 assert.equal("14:46:33.999900", time(1660488393.9999))
45 end)
37 end); 46 end);
38 describe("#datetime", function () 47 describe("#datetime", function ()
39 local datetime = util_datetime.datetime; 48 local datetime = util_datetime.datetime;
40 it("should exist", function () 49 it("should exist", function ()
41 assert.is_function(datetime); 50 assert.is_function(datetime);
46 it("should look like a timestamp", function () 55 it("should look like a timestamp", function ()
47 -- Note: Sub-second precision and timezones are ignored 56 -- Note: Sub-second precision and timezones are ignored
48 assert.truthy(string.find(datetime(), "^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d")); 57 assert.truthy(string.find(datetime(), "^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d"));
49 end); 58 end);
50 it("should work", function () 59 it("should work", function ()
51 assert.equals(datetime(1136239445), "2006-01-02T22:04:05Z"); 60 assert.equals("2006-01-02T22:04:05Z", datetime(1136239445));
52 end); 61 end);
62 it("should handle precision", function ()
63 assert.equal("2022-08-14T14:46:31.158200Z", datetime(1660488391.1582))
64 assert.equal("2022-08-14T14:46:32.158200Z", datetime(1660488392.1582))
65 assert.equal("2022-08-14T14:46:33.158200Z", datetime(1660488393.1582))
66 assert.equal("2022-08-14T14:46:33.999900Z", datetime(1660488393.9999))
67 end)
53 end); 68 end);
54 describe("#legacy", function () 69 describe("#legacy", function ()
55 local legacy = util_datetime.legacy; 70 local legacy = util_datetime.legacy;
56 it("should exist", function () 71 it("should exist", function ()
57 assert.is_function(legacy); 72 assert.is_function(legacy);
73 end);
74 it("should not add precision", function ()
75 assert.equal("20220814T14:46:31", legacy(1660488391.1582));
58 end); 76 end);
59 end); 77 end);
60 describe("#parse", function () 78 describe("#parse", function ()
61 local parse = util_datetime.parse; 79 local parse = util_datetime.parse;
62 it("should exist", function () 80 it("should exist", function ()
63 assert.is_function(parse); 81 assert.is_function(parse);
64 end); 82 end);
65 it("should work", function () 83 it("should work", function ()
66 -- Timestamp used by Go 84 -- Timestamp used by Go
67 assert.equals(parse("2017-11-19T17:58:13Z"), 1511114293); 85 assert.equals(1511114293, parse("2017-11-19T17:58:13Z"));
68 assert.equals(parse("2017-11-19T18:58:50+0100"), 1511114330); 86 assert.equals(1511114330, parse("2017-11-19T18:58:50+0100"));
69 assert.equals(parse("2006-01-02T15:04:05-0700"), 1136239445); 87 assert.equals(1136239445, parse("2006-01-02T15:04:05-0700"));
88 assert.equals(1136239445, parse("2006-01-02T15:04:05-07"));
70 end); 89 end);
71 it("should handle timezones", function () 90 it("should handle timezones", function ()
72 -- https://xmpp.org/extensions/xep-0082.html#example-2 and 3 91 -- https://xmpp.org/extensions/xep-0082.html#example-2 and 3
73 assert.equals(parse("1969-07-21T02:56:15Z"), parse("1969-07-20T21:56:15-05:00")); 92 assert.equals(parse("1969-07-21T02:56:15Z"), parse("1969-07-20T21:56:15-05:00"));
74 end); 93 end);
94 it("should handle precision", function ()
95 -- floating point comparison is not an exact science
96 assert.truthy(math.abs(1660488392.1582 - parse("2022-08-14T14:46:32.158200Z")) < 0.001)
97 end)
98 it("should return nil when given invalid inputs", function ()
99 assert.is_nil(parse(nil));
100 assert.is_nil(parse("hello world"));
101 assert.is_nil(parse("2017-11-19T18:58:50$0100"));
102 end);
75 end); 103 end);
76 end); 104 end);