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 strformat = string.format
2 local floor = math.floor
3 local function idiv ( n , d )
8 local mon_lengths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
9 -- Number of days in year until start of month; not corrected for leap years
10 local months_to_days_cumulative = { 0 }
12 months_to_days_cumulative [ i ] = months_to_days_cumulative [ i-1 ] + mon_lengths [ i-1 ]
15 local function is_leap ( y )
16 return (y % 4) == 0 and (y % 100) ~= 0 or (y % 400) == 0
19 local function year_length ( y )
20 return is_leap ( y ) and 366 or 365
23 local function month_length ( m , y )
24 m = ( m - 1 ) % 12 + 1
26 return is_leap ( y ) and 29 or 28
28 return mon_lengths [ m ]
32 local function leap_years_since ( year )
33 return idiv ( year , 4 ) - idiv ( year , 100 ) + idiv ( year , 400 )
36 local function doomsday ( year )
38 - 1 + year + leap_years_since ( year ) )
41 local doomsday_cache = setmetatable ( { } , {
42 __index = function ( cache , year )
43 local d = doomsday ( year )
49 local function day_of_year ( day , month , year )
50 local yday = months_to_days_cumulative [ month ]
51 if month > 2 and is_leap ( year ) then
57 local function day_of_week ( yday , year )
58 return ( yday - doomsday_cache [ year ] - 1 ) % 7 + 1
61 local function increment ( tens , units , base )
63 tens = tens + idiv ( units , base )
66 tens = tens - 1 + idiv ( -units , base )
67 units = base - ( -units % base )
72 local function unpack_tm ( tm )
73 return assert ( tm.year , "year required" ) ,
74 assert ( tm.month , "month required" ) ,
75 assert ( tm.day , "day required" ) ,
81 -- Modify parameters so they all fit within the "normal" range
82 local function normalise ( year , month , day , hour , min , sec )
83 min , sec = increment ( min , sec , 60 ) -- TODO: consider leap seconds?
84 hour , min = increment ( hour , min , 60 )
85 day , hour = increment ( day , hour , 24 )
89 day = day + year_length ( year )
92 -- This could potentially be slow if `day` is very large
94 local i = month_length ( month , year )
95 if day <= i then break end
100 -- Lua months start from 1, need -1 and +1 around this increment
101 year , month = increment ( year , month - 1 , 12 )
104 return year , month , day , hour , min , sec
107 local leap_years_since_1970 = leap_years_since ( 1970 )
108 local function timestamp ( year , month , day , hour , min , sec )
109 year , month , day , hour , min , sec = normalise ( year , month , day , hour , min , sec )
111 local days_since_epoch = day_of_year ( day , month , year )
112 + 365 * ( year - 1970 )
113 -- Each leap year adds one day
114 + ( leap_years_since ( year - 1 ) - leap_years_since_1970 ) - 1
116 return days_since_epoch * (60*60*24)
123 local timetable_methods = { }
125 function timetable_methods:normalise ( )
126 local year , month , day
127 year , month , day , self.hour , self.min , self.sec = normalise ( unpack_tm ( self ) )
133 local yday = day_of_year ( day , month , year )
134 local wday = day_of_week ( yday , year )
141 timetable_methods.normalize = timetable_methods.normalise -- American English
143 function timetable_methods:timestamp ( )
144 return timestamp ( unpack_tm ( self ) )
147 function timetable_methods:rfc_3339 ( )
148 -- %06.4g gives 3 (=6-4+1) digits after decimal
149 return strformat ( "%04u-%02u-%02uT%02u:%02u:%06.4g" , unpack_tm ( self ) )
152 local timetable_mt = {
153 __index = timetable_methods ;
154 __tostring = timetable_methods.rfc_3339 ;
155 __eq = function ( a , b )
156 return a:timestamp() == b:timestamp()
158 __lt = function ( a , b )
159 return a:timestamp() < b:timestamp()
163 local function cast_timetable ( tm )
164 return setmetatable ( tm , timetable_mt )
167 local function new_timetable ( year , month , day , hour , min , sec )
168 return cast_timetable {
178 local function new_from_timestamp ( ts )
179 return new_timetable ( 1970 , 1 , 1 , 0 , 0 , ts )
183 doomsday = doomsday ;
184 normalise = normalise ;
185 timestamp = timestamp ;
187 new = new_timetable ;
188 new_from_timestamp = new_from_timestamp ;
189 cast = cast_timetable ;
190 timetable_mt = timetable_mt ;