all repos — bibel @ 995003ab506a7dc2fc1a212da5b5d9bd2cdd58ad

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	"math"
 38	"time"
 39)
 40
 41// CatholicByYear returns time.Time for midnight on Catholic Easter of a given
 42// year use Delambre and Butcher's and return time based on Gregorian calendar
 43//
 44// @param int year The year as a number greater than 0
 45// @return time.Time The easter date as a time.Time
 46// @return errors.Error Error if exists, else nil
 47func CatholicByYear(year int) (time.Time, error) {
 48	var a, b, c, d, e, r int
 49
 50	if year < 0 {
 51		return time.Now(), errors.New("year have to be greater than 0")
 52	}
 53
 54	a = year % 19
 55	if year >= 1583 {
 56		var f, g, h, i, k, l, m int
 57		b = year / 100
 58		c = year % 100
 59		d = b / 4
 60		e = b % 4
 61		f = (b + 8) / 25
 62		g = (b - f + 1) / 3
 63		h = (19*a + b - d - g + 15) % 30
 64		i = c / 4
 65		k = c % 4
 66		l = (32 + 2*e + 2*i - h - k) % 7
 67		m = (a + 11*h + 22*l) / 451
 68		r = 22 + h + l - 7*m
 69	} else {
 70		b = year % 7
 71		c = year % 4
 72		d = (19*a + 15) % 30
 73		e = (2*c + 4*b - d + 34) % 7
 74		r = 22 + d + e
 75	}
 76
 77	return time.Date(year, time.March, r, 0, 0, 0, 0, time.Local), nil
 78}
 79
 80// OrthodoxByYear returns time.Time for midnight on Orthodox Easter of a given
 81// year use Meeus Julian algorithm and return time based on Gregorian calendar
 82//
 83// @param int year The year as a number greater than 325
 84// @return time.Time The easter date as a time.Time
 85// @return errors.Error Error if exists, else nil
 86func OrthodoxByYear(year int) (time.Time, error) {
 87
 88	if year < 326 {
 89		return time.Now(), errors.New("year have to be greater than 325")
 90	}
 91
 92	var a, b, c, d, e int
 93	var month time.Month
 94	var day float64
 95
 96	a = year % 4
 97	b = year % 7
 98	c = year % 19
 99	d = (19*c + 15) % 30
100	e = (2*a + 4*b - d + 34) % 7
101	month = time.Month((d + e + 114) / 31)
102	day = math.Floor(float64((d+e+114)%31 + 1))
103	day = day + 13
104
105	return time.Date(year, month, int(day), 0, 0, 0, 0, time.Local), nil
106}