Support for accurate 'next month' and 'next year'

Mon, 22 Jun 2009 01:16:01 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 22 Jun 2009 01:16:01 +0100
changeset 25
caf8bbcbaf07
parent 24
3ae5d7a20cec
child 26
309073a2bb53

Support for accurate 'next month' and 'next year'

ndp.lua file | annotate | diff | comparison | revisions
--- 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)

mercurial