spec/util_datetime_spec.lua
changeset 12759 a09dacf660d2
parent 12633 4c1d3f817063
equal deleted inserted replaced
12758:a92ca737d05f 12759:a09dacf660d2
    36 		end);
    36 		end);
    37 		it("should work", function ()
    37 		it("should work", function ()
    38 			assert.equals("22:04:05", time(1136239445));
    38 			assert.equals("22:04:05", time(1136239445));
    39 		end);
    39 		end);
    40 		it("should handle precision", function ()
    40 		it("should handle precision", function ()
       
    41 			assert.equal("14:46:31.158200", time(1660488391.1582))
    41 			assert.equal("14:46:32.158200", time(1660488392.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))
    42 		end)
    45 		end)
    43 	end);
    46 	end);
    44 	describe("#datetime", function ()
    47 	describe("#datetime", function ()
    45 		local datetime = util_datetime.datetime;
    48 		local datetime = util_datetime.datetime;
    46 		it("should exist", function ()
    49 		it("should exist", function ()
    55 		end);
    58 		end);
    56 		it("should work", function ()
    59 		it("should work", function ()
    57 			assert.equals("2006-01-02T22:04:05Z", datetime(1136239445));
    60 			assert.equals("2006-01-02T22:04:05Z", datetime(1136239445));
    58 		end);
    61 		end);
    59 		it("should handle precision", function ()
    62 		it("should handle precision", function ()
       
    63 			assert.equal("2022-08-14T14:46:31.158200Z", datetime(1660488391.1582))
    60 			assert.equal("2022-08-14T14:46:32.158200Z", datetime(1660488392.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))
    61 		end)
    67 		end)
    62 	end);
    68 	end);
    63 	describe("#legacy", function ()
    69 	describe("#legacy", function ()
    64 		local legacy = util_datetime.legacy;
    70 		local legacy = util_datetime.legacy;
    65 		it("should exist", function ()
    71 		it("should exist", function ()
    66 			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));
    67 		end);
    76 		end);
    68 	end);
    77 	end);
    69 	describe("#parse", function ()
    78 	describe("#parse", function ()
    70 		local parse = util_datetime.parse;
    79 		local parse = util_datetime.parse;
    71 		it("should exist", function ()
    80 		it("should exist", function ()
    74 		it("should work", function ()
    83 		it("should work", function ()
    75 			-- Timestamp used by Go
    84 			-- Timestamp used by Go
    76 			assert.equals(1511114293, parse("2017-11-19T17:58:13Z"));
    85 			assert.equals(1511114293, parse("2017-11-19T17:58:13Z"));
    77 			assert.equals(1511114330, parse("2017-11-19T18:58:50+0100"));
    86 			assert.equals(1511114330, parse("2017-11-19T18:58:50+0100"));
    78 			assert.equals(1136239445, parse("2006-01-02T15:04:05-0700"));
    87 			assert.equals(1136239445, parse("2006-01-02T15:04:05-0700"));
       
    88 			assert.equals(1136239445, parse("2006-01-02T15:04:05-07"));
    79 		end);
    89 		end);
    80 		it("should handle timezones", function ()
    90 		it("should handle timezones", function ()
    81 			-- https://xmpp.org/extensions/xep-0082.html#example-2 and 3
    91 			-- https://xmpp.org/extensions/xep-0082.html#example-2 and 3
    82 			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"));
    83 		end);
    93 		end);
    84 		it("should handle precision", function ()
    94 		it("should handle precision", function ()
    85 			-- floating point comparison is not an exact science
    95 			-- floating point comparison is not an exact science
    86 			assert.truthy(math.abs(1660488392.1582 - parse("2022-08-14T14:46:32.158200Z")) < 0.001)
    96 			assert.truthy(math.abs(1660488392.1582 - parse("2022-08-14T14:46:32.158200Z")) < 0.001)
    87 		end)
    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);
    88 	end);
   103 	end);
    89 end);
   104 end);