11local DateTime = require ("@lune/datetime" )
22
33local now = DateTime .now ()
4- local nowIso = now :toIsoDate ()
4+ local nowRfc = now :toRfc3339 ()
55
66-- Make sure we have separator characters, T to separate date & time, + or Z to separate timezone
77
8- local dateTimeSplitIdx = string.find (nowIso , "T" )
9- local timezoneSplitIdx = string.find (nowIso , "+" )
10- local timezoneZeroedIdx = string.find (nowIso , "Z" )
8+ local dateTimeSplitIdx = string.find (nowRfc , "T" )
9+ local timezoneSplitIdx = string.find (nowRfc , "+" )
10+ local timezoneZeroedIdx = string.find (nowRfc , "Z" )
1111
12- assert (dateTimeSplitIdx ~= nil , "Missing date & time separator 'T' in iso 8601 string" )
13- assert (
14- timezoneSplitIdx ~= nil or timezoneZeroedIdx ~= nil ,
15- "Missing timezone separator '+' or 'Z' in iso date string"
16- )
12+ assert (dateTimeSplitIdx ~= nil , "Missing date & time separator 'T' in RFC 3339 string" )
13+ assert (timezoneSplitIdx ~= nil or timezoneZeroedIdx ~= nil , "Missing timezone separator '+' or 'Z' in RFC 3339 string" )
1714
1815-- Split date (before T) by dashes, split time (after T, before + or Z)
1916-- by colons, we should then get 3 substrings for each of date & time
2017
21- local dateParts = string.split (string.sub (nowIso , 1 , dateTimeSplitIdx - 1 ), "-" )
22- local timeParts = string.split (
23- string.sub (
24- nowIso ,
25- dateTimeSplitIdx + 1 ,
26- ((timezoneSplitIdx or timezoneZeroedIdx ) :: number ) - 1
27- ),
28- ":"
29- )
18+ local dateParts = string.split (string.sub (nowRfc , 1 , dateTimeSplitIdx - 1 ), "-" )
19+ local timeParts =
20+ string.split (string.sub (nowRfc , dateTimeSplitIdx + 1 , ((timezoneSplitIdx or timezoneZeroedIdx ) :: number ) - 1 ), ":" )
3021
31- assert (# dateParts == 3 , "Date partial of iso 8601 should consist of 3 substrings, separated by '-'" )
32- assert (# timeParts == 3 , "Time partial of iso 8601 should consist of 3 substrings, separated by ':'" )
22+ assert (# dateParts == 3 , "Date partial of RFC 3339 should consist of 3 substrings, separated by '-'" )
23+ assert (# timeParts == 3 , "Time partial of RFC 3339 should consist of 3 substrings, separated by ':'" )
3324
3425-- date should be in format YYYY:MM::DD
3526-- time should be in format HH:MM:SS with optional fraction for seconds
@@ -51,22 +42,13 @@ assert(
5142
5243if timezoneZeroedIdx ~= nil then
5344 -- No timezone offset
54- assert (
55- timezoneZeroedIdx == # nowIso ,
56- "Timezone specifier 'Z' must be at the last character in iso 8601 string"
57- )
45+ assert (timezoneZeroedIdx == # nowRfc , "Timezone specifier 'Z' must be at the last character in RFC 3339 string" )
5846elseif timezoneSplitIdx ~= nil then
5947 -- Timezone offset
60- local timezoneParts = string.split (string.sub (nowIso , timezoneSplitIdx + 1 ), ":" )
48+ local timezoneParts = string.split (string.sub (nowRfc , timezoneSplitIdx + 1 ), ":" )
6149 assert (# timezoneParts == 2 , "Timezone partial should consist of 2 substings, separated by ':'" )
62- assert (
63- string.match (timezoneParts [1 ], "^%d%d$" ),
64- "Timezone partial should have 2 digits for hour"
65- )
66- assert (
67- string.match (timezoneParts [2 ], "^%d%d$" ),
68- "Timezone partial should have 2 digits for minute"
69- )
50+ assert (string.match (timezoneParts [1 ], "^%d%d$" ), "Timezone partial should have 2 digits for hour" )
51+ assert (string.match (timezoneParts [2 ], "^%d%d$" ), "Timezone partial should have 2 digits for minute" )
7052else
7153 error ("unreachable" )
7254end
0 commit comments