all repos — bibel @ master

Unnamed repository; edit this file 'description' to name the repository.

pkg/eastertime.go (view raw)

  1// Copyright (c) 2013 eastertime Authors. All rights reserved.
  2//
  3// Redistribution and use in source and binary forms, with or without
  4// modification, are permitted provided that the following conditions are
  5// met:
  6//
  7//    * Redistributions of source code must retain the above copyright
  8// notice, this list of conditions and the following disclaimer.
  9//    * Redistributions in binary form must reproduce the above
 10// copyright notice, this list of conditions and the following disclaimer
 11// in the documentation and/or other materials provided with the
 12// distribution.
 13//    * Neither the name of Google Inc. nor the names of its
 14// contributors may be used to endorse or promote products derived from
 15// this software without specific prior written permission.
 16//
 17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28
 29// eastertime provides functions to get midnight date time of easter day
 30// for Catholic and Orthodox on Gregorian Calendar
 31// web github/vjeantet/eastertime
 32
 33package eastertime
 34
 35import (
 36	"errors"
 37	"time"
 38)
 39
 40// CatholicByYear returns time.Time for midnight on Catholic Easter of a given
 41// year use Delambre and Butcher's and return time based on Gregorian calendar
 42//
 43// @param int year The year as a number greater than 0
 44// @return time.Time The easter date as a time.Time
 45// @return errors.Error Error if exists, else nil
 46func CatholicByYear(year int) (time.Time, error) {
 47	var a, b, c, d, e, r int
 48
 49	if year < 0 {
 50		return time.Now(), errors.New("year have to be greater than 0")
 51	}
 52
 53	a = year % 19
 54	if year >= 1583 {
 55		var f, g, h, i, k, l, m int
 56		b = year / 100
 57		c = year % 100
 58		d = b / 4
 59		e = b % 4
 60		f = (b + 8) / 25
 61		g = (b - f + 1) / 3
 62		h = (19*a + b - d - g + 15) % 30
 63		i = c / 4
 64		k = c % 4
 65		l = (32 + 2*e + 2*i - h - k) % 7
 66		m = (a + 11*h + 22*l) / 451
 67		r = 22 + h + l - 7*m
 68	} else {
 69		b = year % 7
 70		c = year % 4
 71		d = (19*a + 15) % 30
 72		e = (2*c + 4*b - d + 34) % 7
 73		r = 22 + d + e
 74	}
 75
 76	return time.Date(year, time.March, r, 0, 0, 0, 0, time.Local), nil
 77}
 78
 79// OrthodoxByYear returns time.Time for midnight on Orthodox Easter of a given
 80// year use Meeus Julian algorithm and return time based on Gregorian calendar
 81//
 82// @param int year The year as a number greater than 325
 83// @return time.Time The easter date as a time.Time
 84// @return errors.Error Error if exists, else nil
 85func OrthodoxByYear(year int) (time.Time, error) {
 86
 87	if year < 326 {
 88		return time.Now(), errors.New("year have to be greater than 325")
 89	}
 90
 91	var a, b, c, d, e int
 92	var month time.Month
 93	var day int
 94
 95	a = year % 4
 96	b = year % 7
 97	c = year % 19
 98	d = (19*c + 15) % 30
 99	e = (2*a + 4*b - d + 34) % 7
100	month = time.Month((d + e + 114) / 31)
101	day = (d+e+114)%31 + 1
102	day += 13
103
104	return time.Date(year, month, day, 0, 0, 0, 0, time.Local), nil
105}