From cd95045d94ef53e9e79cf8aeb4192ecedf07b019 Mon Sep 17 00:00:00 2001 From: Rimantas Liubertas Date: Mon, 20 Jun 2011 11:06:05 +0300 Subject: [PATCH 1/2] Adds calculation of the date of the Easter Sunday --- .../dates_and_times/date-of-easter.textile | 50 +++++++++++++++++++ wanted-recipes.textile | 1 - 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 chapters/dates_and_times/date-of-easter.textile diff --git a/chapters/dates_and_times/date-of-easter.textile b/chapters/dates_and_times/date-of-easter.textile new file mode 100644 index 0000000..bc51f7d --- /dev/null +++ b/chapters/dates_and_times/date-of-easter.textile @@ -0,0 +1,50 @@ +--- +layout: recipe +title: Calculate the date of Easter Sunday +chapter: Dates and Times +--- + +h2. Problem + +You need to find the month and day of the Easter Sunday for given year + +h2. Solution + +The following function returns array with two elements: month (1-12) and day of the Easter Sunday. If no arguments are given +result is for the current year. +This is an implementation of http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm in CoffeeScript + +{% highlight coffeescript %} + +gregorianEaster = (year = (new Date).getFullYear()) -> + a = year % 19 + b = ~~(year / 100) + c = year % 100 + d = ~~(b / 4) + e = b % 4 + f = ~~((b + 8) / 25) + g = ~~((b - f + 1) / 3) + h = (19 * a + b - d - g + 15) % 30 + i = ~~(c / 4) + k = c % 4 + l = (32 + 2 * e + 2 * i - h - k) % 7 + m = ~~((a + 11 * h + 22 * l) / 451) + n = h + l - 7 * m + 114 + month = ~~(n / 31) + day = (n % 31) + 1 + [month, day] + +{% endhighlight %} + +h2. Discussion + +NB! Javascript numbers months from 0 to 11 so .getMonth() for date in March will return 2, this function will return 3. +You can modify the function if you want this to be consistent. + +{% highlight coffeescript %} + +gregorianEaster() # => [4, 24] (April 24th in 2011) +gregorianEaster 1972 # => [4, 2] + +{% endhighlight %} + diff --git a/wanted-recipes.textile b/wanted-recipes.textile index 33a3cd5..7c1f0f2 100644 --- a/wanted-recipes.textile +++ b/wanted-recipes.textile @@ -42,7 +42,6 @@ evens.every even h2. Dates and Times * Calculating the phase of the moon -* Holidays: Calculating Easter * Holidays: Calculating US Thanksgiving * Holidays: Calculating CA Thanksgiving * Number of days between two dates From 712c078bf2324ce0050d817488ea34717d8d108d Mon Sep 17 00:00:00 2001 From: Rimantas Liubertas Date: Mon, 20 Jun 2011 11:20:35 +0300 Subject: [PATCH 2/2] Fixes typo, adds link --- chapters/dates_and_times/date-of-easter.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chapters/dates_and_times/date-of-easter.textile b/chapters/dates_and_times/date-of-easter.textile index bc51f7d..dfefda6 100644 --- a/chapters/dates_and_times/date-of-easter.textile +++ b/chapters/dates_and_times/date-of-easter.textile @@ -6,13 +6,13 @@ chapter: Dates and Times h2. Problem -You need to find the month and day of the Easter Sunday for given year +You need to find the month and day of the Easter Sunday for given year. h2. Solution The following function returns array with two elements: month (1-12) and day of the Easter Sunday. If no arguments are given result is for the current year. -This is an implementation of http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm in CoffeeScript +This is an implementation of "Anonymous Gregorian algorithm":http://en.wikipedia.org/wiki/Computus#Anonymous_Gregorian_algorithm in CoffeeScript {% highlight coffeescript %}