106 end |
106 end |
107 |
107 |
108 return; |
108 return; |
109 end |
109 end |
110 |
110 |
|
111 local function advance_months(time, n) |
|
112 local split_time = os.date("*t", time); |
|
113 split_time.month = ((split_time.month-1)+n)%12+1; |
|
114 split_time.year = split_time.year + math.floor(n/12); |
|
115 return os.time(split_time); |
|
116 end |
|
117 |
|
118 local function advance_years(time, n) |
|
119 local split_time = os.date("*t", time); |
|
120 split_time.year = split_time.year + n; |
|
121 return os.time(split_time); |
|
122 end |
|
123 |
111 function when(str, relative_to) |
124 function when(str, relative_to) |
112 local time = relative_to or os.time(); |
125 local time = relative_to or os.time(); |
113 local P, Pi = lpeg.P, lpeg.Pi; |
126 local P, Pi = lpeg.P, lpeg.Pi; |
114 |
127 |
115 local patterns = |
128 local patterns = |
120 end }; |
133 end }; |
121 { Pi"next week" / |
134 { Pi"next week" / |
122 function () |
135 function () |
123 time = time + seconds_in_a.week; |
136 time = time + seconds_in_a.week; |
124 end }; |
137 end }; |
|
138 { Pi"next month" / |
|
139 function () |
|
140 time = advance_months(time, 1); |
|
141 end }; |
125 { Pi"next year" / |
142 { Pi"next year" / |
126 function () |
143 function () |
127 time = adjust_time(time, "year", get_time_part(time, "year") + 1); |
144 time = advance_years(time, 1); |
128 end }; |
145 end }; |
129 { year / |
146 { year / |
130 function (year) |
147 function (year) |
131 time = adjust_time(time, "year", tonumber(year)); |
148 time = adjust_time(time, "year", tonumber(year)); |
132 end }; |
149 end }; |
142 function (number_and_unit) |
159 function (number_and_unit) |
143 local number, unit = number_and_unit:gsub("^in ", ""):match("^(.+)%s+(.-)s?$"); |
160 local number, unit = number_and_unit:gsub("^in ", ""):match("^(.+)%s+(.-)s?$"); |
144 |
161 |
145 number = quantities[number] or tonumber(number); |
162 number = quantities[number] or tonumber(number); |
146 |
163 |
147 time = time + seconds_in_a[unit] * number; |
164 if unit == "month" then |
|
165 time = advance_months(time, number); |
|
166 elseif unit == "year" then |
|
167 time = advance_years(time, number); |
|
168 else |
|
169 time = time + seconds_in_a[unit] * number; |
|
170 end |
148 end }; |
171 end }; |
149 { (lpeg.one_of{"this ", "in the ", "at "} + true)* time_of_day / |
172 { (lpeg.one_of{"this ", "in the ", "at "} + true)* time_of_day / |
150 function (time_of_day) |
173 function (time_of_day) |
151 time_of_day = time_of_day:match("%S+$"); |
174 time_of_day = time_of_day:match("%S+$"); |
152 |
175 |