forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setter.go
executable file
·193 lines (174 loc) · 4.27 KB
/
setter.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
package carbon
import (
"time"
)
// SetTimezone sets timezone.
// 设置时区
func (c Carbon) SetTimezone(name string) Carbon {
if c.Error != nil {
return c
}
c.loc, c.Error = getLocationByTimezone(name)
return c
}
// SetTimezone sets timezone.
// 设置时区
func SetTimezone(name string) Carbon {
return NewCarbon().SetTimezone(name)
}
// SetLocale sets locale.
// 设置语言区域
func (c Carbon) SetLocale(locale string) Carbon {
if c.Error != nil {
return c
}
c.lang.SetLocale(locale)
c.Error = c.lang.Error
return c
}
// SetLocale sets locale.
// 设置语言区域
func SetLocale(locale string) Carbon {
c := NewCarbon()
c.lang.SetLocale(locale)
c.Error = c.lang.Error
return c
}
// SetLanguage sets language.
// 设置语言对象
func (c Carbon) SetLanguage(lang *Language) Carbon {
if c.Error != nil {
return c
}
c.lang, c.Error = lang, lang.Error
return c
}
// SetLanguage sets language.
// 设置语言对象
func SetLanguage(lang *Language) Carbon {
c := NewCarbon()
lang.SetLocale(lang.locale)
c.lang, c.Error = lang, lang.Error
return c
}
// SetYear sets year.
// 设置年份
func (c Carbon) SetYear(year int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(year, time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.loc)
return c
}
// SetYearNoOverflow sets year without overflowing month.
// 设置年份(月份不溢出)
func (c Carbon) SetYearNoOverflow(year int) Carbon {
if c.IsInvalid() {
return c
}
return c.AddYearsNoOverflow(year - c.Year())
}
// SetMonth sets month.
// 设置月份
func (c Carbon) SetMonth(month int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(month), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.loc)
return c
}
// SetMonthNoOverflow sets month without overflowing month.
// 设置月份(月份不溢出)
func (c Carbon) SetMonthNoOverflow(month int) Carbon {
if c.IsInvalid() {
return c
}
return c.AddMonthsNoOverflow(month - c.Month())
}
// SetWeekStartsAt sets start day of the week.
// 设置一周的开始日期
func (c Carbon) SetWeekStartsAt(day string) Carbon {
if c.IsInvalid() {
return c
}
switch day {
case Sunday:
c.weekStartsAt = time.Sunday
case Monday:
c.weekStartsAt = time.Monday
case Tuesday:
c.weekStartsAt = time.Tuesday
case Wednesday:
c.weekStartsAt = time.Wednesday
case Thursday:
c.weekStartsAt = time.Thursday
case Friday:
c.weekStartsAt = time.Friday
case Saturday:
c.weekStartsAt = time.Saturday
}
return c
}
// SetDay sets day.
// 设置日期
func (c Carbon) SetDay(day int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()), day, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.loc)
return c
}
// SetHour sets hour.
// 设置小时
func (c Carbon) SetHour(hour int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), hour, c.Minute(), c.Second(), c.Nanosecond(), c.loc)
return c
}
// SetMinute sets minute.
// 设置分钟
func (c Carbon) SetMinute(minute int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), minute, c.Second(), c.Nanosecond(), c.loc)
return c
}
// SetSecond sets second.
// 设置秒数
func (c Carbon) SetSecond(second int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), second, c.Nanosecond(), c.loc)
return c
}
// SetMillisecond sets millisecond.
// 设置毫秒
func (c Carbon) SetMillisecond(millisecond int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), millisecond*1e6, c.loc)
return c
}
// SetMicrosecond sets microsecond.
// 设置微秒
func (c Carbon) SetMicrosecond(microsecond int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), microsecond*1e3, c.loc)
return c
}
// SetNanosecond sets nanosecond.
// 设置纳秒
func (c Carbon) SetNanosecond(nanosecond int) Carbon {
if c.IsInvalid() {
return c
}
c.time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), nanosecond, c.loc)
return c
}