75 split_time[part] = value; |
75 split_time[part] = value; |
76 |
76 |
77 return os.time(split_time); |
77 return os.time(split_time); |
78 end |
78 end |
79 |
79 |
|
80 local function find_next_day_by_name(time, day_name) |
|
81 day_name = day_name:lower():gsub("^.", string.upper); -- Normalize |
|
82 |
|
83 for i=1,8 do |
|
84 time = time + seconds_in_a.day; |
|
85 if os.date("%A", time) == day_name then |
|
86 return time; |
|
87 end |
|
88 end |
|
89 |
|
90 return; |
|
91 end |
|
92 |
80 function when(str, relative_to) |
93 function when(str, relative_to) |
81 local time = relative_to or os.time(); |
94 local time = relative_to or os.time(); |
82 local P = lpeg.P; |
95 local P = lpeg.P; |
83 |
96 |
84 local patterns = |
97 local patterns = |
95 function () |
108 function () |
96 time = adjust_time(time, "year", get_time_part(time, "year") + 1); |
109 time = adjust_time(time, "year", get_time_part(time, "year") + 1); |
97 end }; |
110 end }; |
98 { P"on "^0 * day_name / |
111 { P"on "^0 * day_name / |
99 function (day_name) |
112 function (day_name) |
100 time = find_nearest_day_by_name(time, day_name); |
113 time = find_next_day_by_name(time, day_name:match("%S+$")); |
101 end }; |
114 end }; |
102 { P"in "^0 * month_name / |
115 { P"in "^0 * month_name / |
103 function (month_name) |
116 function (month_name) |
104 time = find_nearest_month_by_name(time, month_name); |
117 time = find_next_month_by_name(time, month_name:match("%S+$")); |
105 end }; |
118 end }; |
106 { P"in "^0 * ( quantity * P" " * unit_of_time ) * (P"s"^-1) / |
119 { P"in "^0 * ( quantity * P" " * unit_of_time ) * (P"s"^-1) / |
107 function (number_and_unit) |
120 function (number_and_unit) |
108 local number, unit = number_and_unit:gsub("^in ", ""):match("^(.+)%s+(.-)s?$"); |
121 local number, unit = number_and_unit:gsub("^in ", ""):match("^(.+)%s+(.-)s?$"); |
109 |
122 |