Created
January 30, 2015 01:08
-
-
Save ericlagergren/231c84b9de31373ffa8d to your computer and use it in GitHub Desktop.
gregorian to julian and back in go
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
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
today := time.Now() | |
julian := GregorianToJulian(today.Year(), int(today.Month()), today.Day()) | |
fmt.Println(julian) | |
year, month, day := JulianToGregorian(julian) | |
fmt.Println(time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)) | |
fmt.Println(time.Now()) | |
} | |
func GregorianToJulian(y, m, d int) int { | |
return 367*y - 7*(y+(m+9)/12)/4 - 3*((y+(m-9)/7)/100+1)/4 + (275*m)/9 + d + 1721029 | |
} | |
func JulianToGregorian(jd int) (year, month, day int) { | |
l := jd + 68569 | |
n := (4 * l) / 146097 | |
l = l - (146097*n+3)/4 | |
i := (4000 * (l + 1)) / 1461001 | |
l = l - (1461*i)/4 + 31 | |
j := (80 * l) / 2447 | |
d := l - (2447*j)/80 | |
l = j / 11 | |
m := j + 2 - (12 * l) | |
y := 100*(n-49) + i + l | |
return y, m, d | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well that sucks.
Run on 2023-11-26: