108 return; |
108 return; |
109 end |
109 end |
110 |
110 |
111 function when(str, relative_to) |
111 function when(str, relative_to) |
112 local time = relative_to or os.time(); |
112 local time = relative_to or os.time(); |
113 local P = lpeg.P; |
113 local P, Pi = lpeg.P, lpeg.Pi; |
114 |
114 |
115 local patterns = |
115 local patterns = |
116 { |
116 { |
117 { P"tomorrow" / |
117 { Pi"tomorrow" / |
118 function () |
118 function () |
119 time = time + seconds_in_a.day; |
119 time = time + seconds_in_a.day; |
120 end }; |
120 end }; |
121 { P"next week" / |
121 { Pi"next week" / |
122 function () |
122 function () |
123 time = time + seconds_in_a.week; |
123 time = time + seconds_in_a.week; |
124 end }; |
124 end }; |
125 { P"next year" / |
125 { Pi"next year" / |
126 function () |
126 function () |
127 time = adjust_time(time, "year", get_time_part(time, "year") + 1); |
127 time = adjust_time(time, "year", get_time_part(time, "year") + 1); |
128 end }; |
128 end }; |
129 { year / |
129 { year / |
130 function (year) |
130 function (year) |
131 time = adjust_time(time, "year", tonumber(year)); |
131 time = adjust_time(time, "year", tonumber(year)); |
132 end }; |
132 end }; |
133 { P"in "^0 * month_name / |
133 { Pi"in "^0 * month_name / |
134 function (month_name) |
134 function (month_name) |
135 time = find_next_month_by_name(time, month_name:match("%S+$")); |
135 time = find_next_month_by_name(time, month_name:match("%S+$")); |
136 end }; |
136 end }; |
137 { P"on "^0 * day_name / |
137 { Pi"on "^0 * day_name / |
138 function (day_name) |
138 function (day_name) |
139 time = find_next_day_by_name(time, day_name:match("%S+$")); |
139 time = find_next_day_by_name(time, day_name:match("%S+$")); |
140 end }; |
140 end }; |
141 { P"in "^0 * ( quantity * P" " * unit_of_time ) * (P"s"^-1) / |
141 { Pi"in "^0 * ( quantity * P" " * unit_of_time ) * (P"s"^-1) / |
142 function (number_and_unit) |
142 function (number_and_unit) |
143 local number, unit = number_and_unit:gsub("^in ", ""):match("^(.+)%s+(.-)s?$"); |
143 local number, unit = number_and_unit:gsub("^in ", ""):match("^(.+)%s+(.-)s?$"); |
144 |
144 |
145 number = quantities[number] or tonumber(number); |
145 number = quantities[number] or tonumber(number); |
146 |
146 |