Software /
code /
prosody
Comparison
spec/util_datetime_spec.lua @ 12629:4c1d3f817063
util.datetime: Add support for sub-second precision timestamps
Lua since 5.3 raises a fuss when time functions are handed a number with
a fractional part and the underlying C functions are all based on
integer seconds without support for more precision.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 14 Aug 2022 16:57:31 +0200 |
parent | 12628:b95da9a593be |
child | 12755:a09dacf660d2 |
comparison
equal
deleted
inserted
replaced
12628:b95da9a593be | 12629:4c1d3f817063 |
---|---|
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("2006-01-02", date(1136239445)); | 19 assert.equals("2006-01-02", date(1136239445)); |
20 end); | 20 end); |
21 it("should ignore fractional parts", function () | |
22 assert.equals("2006-01-02", date(1136239445.5)); | |
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 () |
25 assert.is_function(time); | 28 assert.is_function(time); |
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("22:04:05", time(1136239445)); | 38 assert.equals("22:04:05", time(1136239445)); |
36 end); | 39 end); |
40 it("should handle precision", function () | |
41 assert.equal("14:46:32.158200", time(1660488392.1582)) | |
42 end) | |
37 end); | 43 end); |
38 describe("#datetime", function () | 44 describe("#datetime", function () |
39 local datetime = util_datetime.datetime; | 45 local datetime = util_datetime.datetime; |
40 it("should exist", function () | 46 it("should exist", function () |
41 assert.is_function(datetime); | 47 assert.is_function(datetime); |
48 assert.truthy(string.find(datetime(), "^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d")); | 54 assert.truthy(string.find(datetime(), "^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d")); |
49 end); | 55 end); |
50 it("should work", function () | 56 it("should work", function () |
51 assert.equals("2006-01-02T22:04:05Z", datetime(1136239445)); | 57 assert.equals("2006-01-02T22:04:05Z", datetime(1136239445)); |
52 end); | 58 end); |
59 it("should handle precision", function () | |
60 assert.equal("2022-08-14T14:46:32.158200Z", datetime(1660488392.1582)) | |
61 end) | |
53 end); | 62 end); |
54 describe("#legacy", function () | 63 describe("#legacy", function () |
55 local legacy = util_datetime.legacy; | 64 local legacy = util_datetime.legacy; |
56 it("should exist", function () | 65 it("should exist", function () |
57 assert.is_function(legacy); | 66 assert.is_function(legacy); |
70 end); | 79 end); |
71 it("should handle timezones", function () | 80 it("should handle timezones", function () |
72 -- https://xmpp.org/extensions/xep-0082.html#example-2 and 3 | 81 -- 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")); | 82 assert.equals(parse("1969-07-21T02:56:15Z"), parse("1969-07-20T21:56:15-05:00")); |
74 end); | 83 end); |
84 it("should handle precision", function () | |
85 -- floating point comparison is not an exact science | |
86 assert.truthy(math.abs(1660488392.1582 - parse("2022-08-14T14:46:32.158200Z")) < 0.001) | |
87 end) | |
75 end); | 88 end); |
76 end); | 89 end); |