/
mars.go
51 lines (45 loc) · 912 Bytes
/
mars.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"math"
"time"
)
const (
leapSince = 37
)
func getMarsTime() string {
utc := time.Now().UTC()
millis := utc.Unix()
jd_ut := 2440587.5 + (float64(millis) / 86400)
jd_tt := jd_ut + (leapSince+32.184)/86400
j2 := jd_tt - 2451545
msd := ((j2 - 4.5) / 1.027491252) + 44796.0 - 0.00096
mtc := math.Mod((24 * msd), 24)
return toTime(mtc)
}
func toTime(h float64) string {
x := h * 3600
hh := int(math.Floor(x / 3600))
y := math.Mod(x, 3600)
mm := int(math.Floor(y / 60))
out := ""
if hh < 10 {
out += fmt.Sprintf("0%d", hh)
} else {
out += fmt.Sprintf("%d", hh)
}
out += ":"
if mm < 10 {
out += fmt.Sprintf("0%d", mm)
} else {
out += fmt.Sprintf("%d", mm)
}
return out
}
func Round(f float64) float64 {
return math.Floor(f + .5)
}
func RoundPlus(f float64, places int) float64 {
shift := math.Pow(10, float64(places))
return Round(f*shift) / shift
}