# HG changeset patch # User Matthew Wild # Date 1245629761 -3600 # Node ID caf8bbcbaf0712769cf7da52c15c8502c0649622 # Parent 3ae5d7a20cec409988cfa3ed585eb37a4f79c671 Support for accurate 'next month' and 'next year' diff -r 3ae5d7a20cec -r caf8bbcbaf07 ndp.lua --- a/ndp.lua Sun Jun 21 23:57:36 2009 +0100 +++ b/ndp.lua Mon Jun 22 01:16:01 2009 +0100 @@ -108,6 +108,19 @@ return; end +local function advance_months(time, n) + local split_time = os.date("*t", time); + split_time.month = ((split_time.month-1)+n)%12+1; + split_time.year = split_time.year + math.floor(n/12); + return os.time(split_time); +end + +local function advance_years(time, n) + local split_time = os.date("*t", time); + split_time.year = split_time.year + n; + return os.time(split_time); +end + function when(str, relative_to) local time = relative_to or os.time(); local P, Pi = lpeg.P, lpeg.Pi; @@ -122,9 +135,13 @@ function () time = time + seconds_in_a.week; end }; + { Pi"next month" / + function () + time = advance_months(time, 1); + end }; { Pi"next year" / function () - time = adjust_time(time, "year", get_time_part(time, "year") + 1); + time = advance_years(time, 1); end }; { year / function (year) @@ -144,7 +161,13 @@ number = quantities[number] or tonumber(number); - time = time + seconds_in_a[unit] * number; + if unit == "month" then + time = advance_months(time, number); + elseif unit == "year" then + time = advance_years(time, number); + else + time = time + seconds_in_a[unit] * number; + end end }; { (lpeg.one_of{"this ", "in the ", "at "} + true)* time_of_day / function (time_of_day)