All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 local strftime = require "luatz.strftime".strftime
2 local strformat = string.format
3 local floor = math.floor
5 -- Try and use actual integer division when available (Lua 5.3+)
6 local idiv_loader = (loadstring or load)([[return function(n,d) return n//d end]], "idiv") -- luacheck: ignore 113
17 local mon_lengths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
18 -- Number of days in year until start of month; not corrected for leap years
19 local months_to_days_cumulative = { 0 }
21 months_to_days_cumulative [ i ] = months_to_days_cumulative [ i-1 ] + mon_lengths [ i-1 ]
23 -- For Sakamoto's Algorithm (day of week)
24 local sakamoto = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
26 local function is_leap ( y )
29 elseif (y % 100) ~= 0 then
36 local function month_length ( m , y )
38 return is_leap ( y ) and 29 or 28
40 return mon_lengths [ m ]
44 local function leap_years_since ( year )
45 return idiv ( year , 4 ) - idiv ( year , 100 ) + idiv ( year , 400 )
48 local function day_of_year ( day , month , year )
49 local yday = months_to_days_cumulative [ month ]
50 if month > 2 and is_leap ( year ) then
56 local function day_of_week ( day , month , year )
60 return ( year + leap_years_since ( year ) + sakamoto[month] + day ) % 7 + 1
63 local function borrow ( tens , units , base )
65 units = units + frac * base
70 local function carry ( tens , units , base )
72 tens = tens + idiv ( units , base )
75 tens = tens + idiv ( units , base )
76 units = ( base + units ) % base
81 -- Modify parameters so they all fit within the "normal" range
82 local function normalise ( year , month , day , hour , min , sec )
83 -- `month` and `day` start from 1, need -1 and +1 so it works modulo
84 month , day = month - 1 , day - 1
86 -- Convert everything (except seconds) to an integer
87 -- by propagating fractional components down.
88 year , month = borrow ( year , month , 12 )
89 -- Carry from month to year first, so we get month length correct in next line around leap years
90 year , month = carry ( year , month , 12 )
91 month , day = borrow ( month , day , month_length ( floor ( month + 1 ) , year ) )
92 day , hour = borrow ( day , hour , 24 )
93 hour , min = borrow ( hour , min , 60 )
94 min , sec = borrow ( min , sec , 60 )
96 -- Propagate out of range values up
97 -- e.g. if `min` is 70, `hour` increments by 1 and `min` becomes 10
98 -- This has to happen for all columns after borrowing, as lower radixes may be pushed out of range
99 min , sec = carry ( min , sec , 60 ) -- TODO: consider leap seconds?
100 hour , min = carry ( hour , min , 60 )
101 day , hour = carry ( day , hour , 24 )
102 -- Ensure `day` is not underflowed
103 -- Add a whole year of days at a time, this is later resolved by adding months
104 -- TODO[OPTIMIZE]: This could be slow if `day` is far out of range
111 day = day + month_length ( month + 1 , year )
113 year , month = carry ( year , month , 12 )
115 -- TODO[OPTIMIZE]: This could potentially be slow if `day` is very large
117 local i = month_length ( month + 1 , year )
118 if day < i then break end
127 -- Now we can place `day` and `month` back in their normal ranges
128 -- e.g. month as 1-12 instead of 0-11
129 month , day = month + 1 , day + 1
131 return year , month , day , hour , min , sec
134 local leap_years_since_1970 = leap_years_since ( 1970 )
135 local function timestamp ( year , month , day , hour , min , sec )
136 year , month , day , hour , min , sec = normalise ( year , month , day , hour , min , sec )
138 local days_since_epoch = day_of_year ( day , month , year )
139 + 365 * ( year - 1970 )
140 -- Each leap year adds one day
141 + ( leap_years_since ( year - 1 ) - leap_years_since_1970 ) - 1
143 return days_since_epoch * (60*60*24)
150 local timetable_methods = { }
152 function timetable_methods:unpack ( )
153 return assert ( self.year , "year required" ) ,
154 assert ( self.month , "month required" ) ,
155 assert ( self.day , "day required" ) ,
163 function timetable_methods:normalise ( )
164 local year , month , day
165 year , month , day , self.hour , self.min , self.sec = normalise ( self:unpack ( ) )
170 self.yday = day_of_year ( day , month , year )
171 self.wday = day_of_week ( day , month , year )
175 timetable_methods.normalize = timetable_methods.normalise -- American English
177 function timetable_methods:timestamp ( )
178 return timestamp ( self:unpack ( ) )
181 function timetable_methods:rfc_3339 ( )
182 local year, month, day, hour, min, fsec = self:unpack()
183 local sec, msec = borrow(fsec, 0, 1000)
184 msec = math.floor(msec)
185 return strformat ( "%04u-%02u-%02uT%02u:%02u:%02d.%03d" , year , month , day , hour , min , sec , msec )
188 function timetable_methods:strftime ( format_string )
189 return strftime ( format_string , self )
194 local function coerce_arg ( t )
195 if getmetatable ( t ) == timetable_mt then
196 return t:timestamp ( )
202 __index = timetable_methods ;
203 __tostring = timetable_methods.rfc_3339 ;
204 __eq = function ( a , b )
205 return a:timestamp ( ) == b:timestamp ( )
207 __lt = function ( a , b )
208 return a:timestamp ( ) < b:timestamp ( )
210 __sub = function ( a , b )
211 return coerce_arg ( a ) - coerce_arg ( b )
215 local function cast_timetable ( tm )
216 return setmetatable ( tm , timetable_mt )
219 local function new_timetable ( year , month , day , hour , min , sec , yday , wday )
220 return cast_timetable {
232 function timetable_methods:clone ( )
233 return new_timetable ( self:unpack ( ) )
236 local function new_from_timestamp ( ts )
237 if type ( ts ) ~= "number" then
238 error ( "bad argument #1 to 'new_from_timestamp' (number expected, got " .. type ( ts ) .. ")" , 2 )
240 return new_timetable ( 1970 , 1 , 1 , 0 , 0 , ts ):normalise ( )
245 day_of_year = day_of_year ;
246 day_of_week = day_of_week ;
247 normalise = normalise ;
248 timestamp = timestamp ;
250 new = new_timetable ;
251 new_from_timestamp = new_from_timestamp ;
252 cast = cast_timetable ;
253 timetable_mt = timetable_mt ;