forked from nleeper/goment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goment.go
166 lines (140 loc) · 3.68 KB
/
goment.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package goment
import (
"errors"
"time"
"github.com/nleeper/goment/locales"
)
var timeNow = time.Now
// Goment is the main class.
type Goment struct {
time time.Time
locale locales.LocaleDetails
}
// DateTime is a class to define a date & time.
type DateTime struct {
Year int
Month int
Day int
Hour int
Minute int
Second int
Nanosecond int
Location *time.Location
}
// New creates an instance of the Goment library.
func New(args ...interface{}) (*Goment, error) {
loadParseReplacements()
loadFormatReplacements()
switch len(args) {
case 0:
return fromNow()
case 1:
switch v := args[0].(type) {
case string:
return fromISOString(v)
case time.Time:
return fromExistingTime(v)
case int64:
return fromUnixNanoseconds(v)
case *Goment:
return fromGoment(v)
case Goment:
return fromGoment(&v)
case DateTime:
return fromDateTime(v)
default:
return &Goment{}, errors.New("Invalid argument type")
}
case 2:
if date, ok := args[0].(string); ok {
if format, ok := args[1].(string); ok {
return fromStringWithFormat(date, format, getGlobalLocaleDetails())
}
return &Goment{}, errors.New("Second argument must be a format string")
}
return &Goment{}, errors.New("First argument must be a datetime string")
case 3:
// TODO - cleanup argument parsing.
if date, ok := args[0].(string); ok {
if format, ok := args[1].(string); ok {
if localeCode, ok := args[2].(string); ok {
locale, err := loadLocale(localeCode)
if err != nil {
return &Goment{}, errors.New("Invalid locale code")
}
return fromStringWithFormat(date, format, locale)
}
}
return &Goment{}, errors.New("Second argument must be a format string")
}
return &Goment{}, errors.New("First argument must be a datetime string")
default:
return &Goment{}, errors.New("Invalid number of arguments")
}
}
// Unix creates an instance of the Goment library from the Unix timestamp (seconds since the Unix Epoch).
func Unix(unixSeconds int64) (*Goment, error) {
t := time.Unix(unixSeconds, 0)
return createGoment(t)
}
// Clone creates a new copy of the Goment instance.
func (g *Goment) Clone() *Goment {
copy, _ := New()
copy.time = g.ToTime()
copy.locale = g.locale
return copy
}
func fromDateTime(dt DateTime) (*Goment, error) {
tn := timeNow()
year := tn.Year()
if dt.Year != 0 {
year = dt.Year
}
month := tn.Month()
if dt.Month > 0 {
month = time.Month(dt.Month)
}
day := tn.Day()
if dt.Day > 0 {
day = dt.Day
}
// Default to local time if not provided.
loc := tn.Location()
if dt.Location != nil {
loc = dt.Location
}
d := time.Date(year, month, day, dt.Hour, dt.Minute, dt.Second, dt.Nanosecond, loc)
return fromExistingTime(d)
}
func fromGoment(g *Goment) (*Goment, error) {
return g.Clone(), nil
}
func fromNow() (*Goment, error) {
return fromExistingTime(timeNow())
}
func fromUnixNanoseconds(unixNano int64) (*Goment, error) {
return fromExistingTime(time.Unix(0, unixNano))
}
func fromExistingTime(t time.Time) (*Goment, error) {
return createGoment(t)
}
func fromStringWithFormat(date string, format string, locale locales.LocaleDetails) (*Goment, error) {
parsed, err := parseFromFormat(date, format, locale)
if err != nil {
return &Goment{}, err
}
return createGomentWithLocale(parsed, locale)
}
func fromISOString(date string) (*Goment, error) {
parsed, err := parseISOString(date)
if err != nil {
return &Goment{}, err
}
return createGoment(parsed)
}
func createGoment(t time.Time) (*Goment, error) {
return createGomentWithLocale(t, getGlobalLocaleDetails())
}
func createGomentWithLocale(t time.Time, ld locales.LocaleDetails) (*Goment, error) {
return &Goment{t, ld}, nil
}