-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconvert.go
More file actions
103 lines (83 loc) · 3.19 KB
/
convert.go
File metadata and controls
103 lines (83 loc) · 3.19 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package EorzeaTimeConvert
import (
"fmt"
"math"
"strconv"
"time"
)
const (
YEAR = 33177600
MONTH = 2764800
DAY = 86400
HOUR = 3600
MINUTE = 60
SECOND = 1
)
type EorzeaTime struct {
YearVal int64
MonthVal int64
DayVal int64
HourVal int64
MinuteVal int64
SecondVal int64
}
var (
EORZEA_TIME_CONSTANT float64 = float64(3600) / float64(175)
)
func ConvertToEorzeaTime(time time.Time) EorzeaTime {
earthTime := float64(time.Unix())
eorzeaTime := int64(math.Floor(earthTime * EORZEA_TIME_CONSTANT))
var ret EorzeaTime
ret.YearVal = int64(math.Floor(float64(eorzeaTime / YEAR))) + 1
ret.MonthVal = int64(math.Floor(float64(eorzeaTime / MONTH % 12))) + 1
ret.DayVal = int64(math.Floor(float64(eorzeaTime / DAY % 32))) + 1
ret.HourVal = int64(math.Floor(float64(eorzeaTime / HOUR % 24)))
ret.MinuteVal = int64(math.Floor(float64(eorzeaTime / MINUTE % 60)))
ret.SecondVal = int64(math.Floor(float64(eorzeaTime / SECOND % 60)))
return ret
}
func ConvertToEorzeaTimeString(time time.Time, format string) string {
earthTime := float64(time.Unix())
eorzeaTime := int64(math.Floor(earthTime * EORZEA_TIME_CONSTANT))
yearVal := "" + strconv.FormatInt(int64(math.Floor(float64(eorzeaTime / YEAR))) + 1, 10)
monthVal := formatZero(strconv.FormatInt(int64(math.Floor(float64(eorzeaTime / MONTH % 12))) + 1, 10))
dayVal := formatZero(strconv.FormatInt(int64(math.Floor(float64(eorzeaTime / DAY % 32 ))) + 1, 10))
hourVal := formatZero(strconv.FormatInt(int64(math.Floor(float64(eorzeaTime / HOUR % 24))), 10))
minuteVal := formatZero(strconv.FormatInt(int64(math.Floor(float64(eorzeaTime / MINUTE % 60))), 10))
secondVal := formatZero(strconv.FormatInt(int64(math.Floor(float64(eorzeaTime / SECOND % 60))), 10))
var ret string
ret = fmt.Sprintf(format, yearVal, monthVal, dayVal, hourVal, minuteVal, secondVal)
return ret
}
func parseEorzeaTimeString(timestring string, format string) (time.Time, error) {
date, err := time.Parse(format, timestring)
var ret time.Time
if err != nil {
return ret, err
}
return date, nil
}
func ConvertToEarthTime(timestring string, format string) (time.Time, error) {
date, err := parseEorzeaTimeString(format, timestring)
var ret time.Time
var utc int64
if err != nil {
return ret, err
}
years := int64(date.Year())
months := int64(date.Month())
days := int64(date.Day())
hours := int64(date.Hour())
minutes := int64(date.Minute())
seconds := int64(date.Second())
utc = int64(float64((years - 1) * YEAR + (months - 1) * MONTH + (days - 1) * DAY + hours * HOUR + minutes * MINUTE + seconds ) / EORZEA_TIME_CONSTANT)
ret = time.Unix(utc, 0)
return ret, nil
}
func formatZero(str string) string {
if len(str) == 1 {
return "0" + str
} else {
return str
}
}