forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creator.go
executable file
·254 lines (219 loc) · 12.2 KB
/
creator.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package carbon
import (
"time"
)
// CreateFromStdTime creates a Carbon instance from standard time.Time.
// 从标准的 time.Time 创建 Carbon 实例
func CreateFromStdTime(tt time.Time, timezone ...string) Carbon {
c := NewCarbon()
c.loc = tt.Location()
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
c.time = tt
return c
}
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second.
// 从给定的秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
}
c.time = time.Unix(timestamp, 0)
return c
}
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second.
// 从给定的秒级时间戳创建 Carbon 实例
func CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
return NewCarbon().CreateFromTimestamp(timestamp, timezone...)
}
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond.
// 从给定的毫秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
}
c.time = time.Unix(timestamp/1e3, (timestamp%1e3)*1e6)
return c
}
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond.
// 从给定的毫秒级时间戳创建 Carbon 实例
func CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon {
return NewCarbon().CreateFromTimestampMilli(timestamp, timezone...)
}
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond.
// 从给定的微秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
}
c.time = time.Unix(timestamp/1e6, (timestamp%1e6)*1e3)
return c
}
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond.
// 从给定的微秒级时间戳创建 Carbon 实例
func CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
return NewCarbon().CreateFromTimestampMicro(timestamp, timezone...)
}
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond.
// 从给定的纳秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
}
c.time = time.Unix(timestamp/1e9, timestamp%1e9)
return c
}
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond.
// 从给定的纳秒级时间戳创建 Carbon 实例
func CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon {
return NewCarbon().CreateFromTimestampNano(timestamp, timezone...)
}
// CreateFromDateTime creates a Carbon instance from a given date and time.
// 从给定的年、月、日、时、分、秒创建 Carbon 实例
func (c Carbon) CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) Carbon {
return c.create(year, month, day, hour, minute, second, 0, timezone...)
}
// CreateFromDateTime creates a Carbon instance from a given date and time.
// 从给定的年、月、日、时、分、秒创建 Carbon 实例
func CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateTime(year, month, day, hour, minute, second, timezone...)
}
// CreateFromDateTimeMilli creates a Carbon instance from a given date, time and millisecond.
// 从给定的年、月、日、时、分、秒、毫秒创建 Carbon 实例
func (c Carbon) CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond int, timezone ...string) Carbon {
return c.create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
}
// CreateFromDateTimeMilli creates a Carbon instance from a given date, time and millisecond.
// 从给定的年、月、日、时、分、秒、毫秒创建 Carbon 实例
func CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond, timezone...)
}
// CreateFromDateTimeMicro creates a Carbon instance from a given date, time and microsecond.
// 从给定的年、月、日、时、分、秒、微秒创建 Carbon 实例
func (c Carbon) CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond int, timezone ...string) Carbon {
return c.create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
}
// CreateFromDateTimeMicro creates a Carbon instance from a given date, time and microsecond.
// 从给定的年、月、日、时、分、秒、微秒创建 Carbon 实例
func CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond, timezone...)
}
// CreateFromDateTimeNano creates a Carbon instance from a given date, time and nanosecond.
// 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例
func (c Carbon) CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
return c.create(year, month, day, hour, minute, second, nanosecond, timezone...)
}
// CreateFromDateTimeNano creates a Carbon instance from a given date, time and nanosecond.
// 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例
func CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond, timezone...)
}
// CreateFromDate creates a Carbon instance from a given date.
// 从给定的年、月、日创建 Carbon 实例
func (c Carbon) CreateFromDate(year, month, day int, timezone ...string) Carbon {
return c.create(year, month, day, 0, 0, 0, 0, timezone...)
}
// CreateFromDate creates a Carbon instance from a given date.
// 从给定的年、月、日创建 Carbon 实例
func CreateFromDate(year, month, day int, timezone ...string) Carbon {
return NewCarbon().CreateFromDate(year, month, day, timezone...)
}
// CreateFromDateMilli creates a Carbon instance from a given date and millisecond.
// 从给定的年、月、日、毫秒创建 Carbon 实例
func (c Carbon) CreateFromDateMilli(year, month, day, millisecond int, timezone ...string) Carbon {
return c.create(year, month, day, 0, 0, 0, millisecond*1e6, timezone...)
}
// CreateFromDateMilli creates a Carbon instance from a given date and millisecond.
// 从给定的年、月、日、毫秒创建 Carbon 实例
func CreateFromDateMilli(year, month, day, millisecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateMilli(year, month, day, millisecond, timezone...)
}
// CreateFromDateMicro creates a Carbon instance from a given date and microsecond.
// 从给定的年、月、日、微秒创建 Carbon 实例
func (c Carbon) CreateFromDateMicro(year, month, day, microsecond int, timezone ...string) Carbon {
return c.create(year, month, day, 0, 0, 0, microsecond*1e3, timezone...)
}
// CreateFromDateMicro creates a Carbon instance from a given date and microsecond.
// 从给定的年、月、日、微秒创建 Carbon 实例
func CreateFromDateMicro(year, month, day, microsecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateMicro(year, month, day, microsecond, timezone...)
}
// CreateFromDateNano creates a Carbon instance from a given date and nanosecond.
// 从给定的年、月、日、纳秒创建 Carbon 实例
func (c Carbon) CreateFromDateNano(year, month, day, nanosecond int, timezone ...string) Carbon {
return c.create(year, month, day, 0, 0, 0, nanosecond, timezone...)
}
// CreateFromDateNano creates a Carbon instance from a given date and nanosecond.
// 从给定的年、月、日、纳秒创建 Carbon 实例
func CreateFromDateNano(year, month, day, nanosecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromDateNano(year, month, day, nanosecond, timezone...)
}
// CreateFromTime creates a Carbon instance from a given time(year, month and day are taken from the current time).
// 从给定的时、分、秒创建 Carbon 实例(年、月、日取自当前时间)
func (c Carbon) CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
year, month, day := c.Now(timezone...).Date()
return c.create(year, month, day, hour, minute, second, 0, timezone...)
}
// CreateFromTime creates a Carbon instance from a given time(year, month and day are taken from the current time).
// 从给定的时、分、秒创建 Carbon 实例(年、月、日取自当前时间)
func CreateFromTime(hour, minute, second int, timezone ...string) Carbon {
return NewCarbon().CreateFromTime(hour, minute, second, timezone...)
}
// CreateFromTimeMilli creates a Carbon instance from a given time and millisecond(year, month and day are taken from the current time).
// 从给定的时、分、秒、毫秒创建 Carbon 实例(年、月、日取自当前时间)
func (c Carbon) CreateFromTimeMilli(hour, minute, second, millisecond int, timezone ...string) Carbon {
year, month, day := c.Now(timezone...).Date()
return c.create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
}
// CreateFromTimeMilli creates a Carbon instance from a given time and millisecond(year, month and day are taken from the current time).
// 从给定的时、分、秒、毫秒创建 Carbon 实例(年、月、日取自当前时间)
func CreateFromTimeMilli(hour, minute, second, millisecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromTimeMilli(hour, minute, second, millisecond, timezone...)
}
// CreateFromTimeMicro creates a Carbon instance from a given time and microsecond(year, month and day are taken from the current time).
// 从给定的时、分、秒、微秒创建 Carbon 实例(年、月、日取自当前时间)
func (c Carbon) CreateFromTimeMicro(hour, minute, second, microsecond int, timezone ...string) Carbon {
year, month, day := c.Now(timezone...).Date()
return c.create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
}
// CreateFromTimeMicro creates a Carbon instance from a given time and microsecond(year, month and day are taken from the current time).
// 从给定的时、分、秒、微秒创建 Carbon 实例(年、月、日取自当前时间)
func CreateFromTimeMicro(hour, minute, second, microsecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromTimeMicro(hour, minute, second, microsecond, timezone...)
}
// CreateFromTimeNano creates a Carbon instance from a given time and nanosecond(year, month and day are taken from the current time).
// 从给定的时、分、秒、纳秒创建 Carbon 实例(年、月、日取自当前时间)
func (c Carbon) CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string) Carbon {
year, month, day := c.Now(timezone...).Date()
return c.create(year, month, day, hour, minute, second, nanosecond, timezone...)
}
// CreateFromTimeNano creates a Carbon instance from a given time and nanosecond(year, month and day are taken from the current time).
// 从给定的时、分、秒、纳秒创建 Carbon 实例(年、月、日取自当前时间)
func CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string) Carbon {
return NewCarbon().CreateFromTimeNano(hour, minute, second, nanosecond, timezone...)
}
// creates a Carbon instance from a given date, time and nanosecond.
// 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例
func (c Carbon) create(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
}
c.time = time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, c.loc)
return c
}