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 floor = math.floor
2 local function idiv ( n , d )
7 local mon_lengths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
8 -- Number of days in year until start of month; not corrected for leap years
9 local months_to_days_cumulative = { 0 }
11 months_to_days_cumulative [ i ] = months_to_days_cumulative [ i-1 ] + mon_lengths [ i-1 ]
14 local function is_leap ( y )
15 return (y % 4) == 0 and (y % 100) ~= 0 or (y % 400) == 0
18 local function year_length ( y )
19 return is_leap ( y ) and 366 or 365
22 local function month_length ( m , y )
24 return is_leap ( y ) and 29 or 28
26 return mon_lengths [ m ]
30 local function doomsday ( year )
32 - 1 + year + idiv ( year , 4 ) - idiv ( year , 100 ) + idiv ( year , 400 ) )
35 local doomsday_cache = setmetatable ( { } , {
36 __index = function ( cache , year )
37 local d = doomsday ( year )
43 local function day_of_year ( day , month , year )
44 local yday = months_to_days_cumulative [ month ]
45 if month > 2 and is_leap ( year ) then
51 local function day_of_week ( yday , year )
52 return ( yday - doomsday_cache [ year ] - 1 ) % 7 + 1
55 local function increment ( tens , units , base )
57 tens = tens + idiv ( units , base )
60 tens = tens - 1 + idiv ( -units , base )
61 units = base - ( -units % base )
66 local function unpack_tm ( tm )
67 return assert ( tm.year , "year required" ) ,
68 assert ( tm.month , "month required" ) ,
69 assert ( tm.day , "day required" ) ,
75 -- Modify parameters so they all fit within the "normal" range
76 local function normalise ( year , month , day , hour , min , sec )
77 min , sec = increment ( min , sec , 60 ) -- TODO: consider leap seconds?
78 hour , min = increment ( hour , min , 60 )
79 day , hour = increment ( day , hour , 24 )
83 day = day + year_length ( year )
86 -- This could potentially be slow if `day` is very large
88 local i = month_length ( month , year )
89 if day <= i then break end
94 -- Lua months start from 1, need -1 and +1 around this increment
95 year , month = increment ( year , month - 1 , 12 )
98 return year , month , day , hour , min , sec
101 local function timestamp ( year , month , day , hour , min , sec )
103 year * year_length ( year )
104 + month * month_length ( month , year )
113 local timetable_methods = { }
115 function timetable_methods:normalise ( )
116 local year , month , day
117 year , month , day , self.hour , self.min , self.sec = normalise ( unpack_tm ( self ) )
123 local yday = day_of_year ( day , month , year )
124 local wday = day_of_week ( yday , year )
131 timetable_methods.normalize = timetable_methods.normalise -- American English
133 function timetable_methods:timestamp ( )
134 return timestamp ( unpack_tm ( self ) )
137 local timetable_mt = {
138 __index = timetable_methods ;
139 __eq = function ( a , b )
140 return a:timestamp() < b:timestamp()
144 local function cast_timetable ( tm )
145 return setmetatable ( tm , timetable_mt )
148 local function new_timetable ( year , month , day , hour , min , sec )
149 return cast_timetable {
160 doomsday = doomsday ;
161 normalise = normalise ;
162 timestamp = timestamp ;
164 new = new_timetable ;
165 cast = cast_timetable ;
166 timetable_mt = timetable_mt ;