Skip to content

Instantly share code, notes, and snippets.

@ericlagergren
Created January 30, 2015 01:08
Show Gist options
  • Save ericlagergren/231c84b9de31373ffa8d to your computer and use it in GitHub Desktop.
Save ericlagergren/231c84b9de31373ffa8d to your computer and use it in GitHub Desktop.
gregorian to julian and back in go
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
}
@KarelKubat
Copy link

Well that sucks.

Run on 2023-11-26:

2455146
2009-11-10 00:00:00 +0000 UTC
2009-11-10 23:00:00 +0000 UTC m=+0.000000001

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment