Last active
March 27, 2021 17:08
-
-
Save cypres/2eabcede718c69670102 to your computer and use it in GitHub Desktop.
Easter calculation in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Easter calculation in swift after Anonymous Gregorian algorithm | |
// Also known as Meeus/Jones/Butcher algorithm | |
import Foundation | |
func easter(Y : Int) -> NSDate { | |
let a = Y % 19 | |
let b = Int(floor(Double(Y) / 100)) | |
let c = Y % 100 | |
let d = Int(floor(Double(b) / 4)) | |
let e = b % 4 | |
let f = Int(floor(Double(b+8) / 25)) | |
let g = Int(floor(Double(b-f+1) / 3)) | |
let h = (19*a + b - d - g + 15) % 30 | |
let i = Int(floor(Double(c) / 4)) | |
let k = c % 4 | |
let L = (32 + 2*e + 2*i - h - k) % 7 | |
let m = Int(floor(Double(a + 11*h + 22*L) / 451)) | |
let components = NSDateComponents() | |
components.year = Y | |
components.month = Int(floor(Double(h + L - 7*m + 114) / 31)) | |
components.day = ((h + L - 7*m + 114) % 31) + 1 | |
components.timeZone = NSTimeZone(forSecondsFromGMT: 0) | |
let cal = NSCalendar(calendarIdentifier: NSGregorianCalendar) | |
return cal.dateFromComponents(components) | |
} | |
println(easter(2014)) // "2014-04-20 00:00:00 +0000" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks so much for this!