carbon/setter.go

250 lines
6.0 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
2021-07-19 09:55:28 +08:00
import (
"time"
)
2021-02-18 14:32:31 +08:00
2021-08-10 10:58:43 +08:00
// SetTimezone sets timezone.
2021-07-28 15:18:05 +08:00
// 设置时区
2021-02-18 14:32:31 +08:00
func (c Carbon) SetTimezone(name string) Carbon {
if c.Error != nil {
return c
}
c.loc, c.Error = getLocationByTimezone(name)
2021-02-18 14:32:31 +08:00
return c
}
2021-08-10 10:58:43 +08:00
// SetTimezone sets timezone.
2021-07-28 15:18:05 +08:00
// 设置时区
2021-02-18 14:32:31 +08:00
func SetTimezone(name string) Carbon {
return NewCarbon().SetTimezone(name)
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// 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
}
2021-08-10 10:58:43 +08:00
// SetLocale sets locale.
// 设置语言区域
func SetLocale(locale string) Carbon {
c := NewCarbon()
c.lang.SetLocale(locale)
c.Error = c.lang.Error
return c
}
2021-08-10 10:58:43 +08:00
// SetLanguage sets language.
2021-07-31 13:34:52 +08:00
// 设置语言对象
2021-02-18 14:32:31 +08:00
func (c Carbon) SetLanguage(lang *Language) Carbon {
if c.Error != nil {
return c
}
c.lang, c.Error = lang, lang.Error
2021-02-18 14:32:31 +08:00
return c
}
2021-08-10 10:58:43 +08:00
// SetLanguage sets language.
2021-07-28 15:18:05 +08:00
// 设置语言对象
2021-02-18 14:32:31 +08:00
func SetLanguage(lang *Language) Carbon {
c := NewCarbon()
lang.SetLocale(lang.locale)
c.lang, c.Error = lang, lang.Error
return c
2021-02-18 14:32:31 +08:00
}
// SetDateTime sets year, month, day, hour, minute and second.
// 设置年月日时分秒
func (c Carbon) SetDateTime(year, month, day, hour, minute, second int) Carbon {
if c.IsInvalid() {
return c
}
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
2022-04-14 11:32:01 +08:00
// SetDateTimeMilli sets year, month, day, hour, minute, second and millisecond.
// 设置年月日时分秒毫秒
func (c Carbon) SetDateTimeMilli(year, month, day, hour, minute, second, millisecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
}
// SetDateTimeMicro sets year, month, day, hour, minute, second and microsecond.
// 设置年月日时分秒微秒
func (c Carbon) SetDateTimeMicro(year, month, day, hour, minute, second, microsecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
}
// SetDateTimeNano sets year, month, day, hour, minute, second and nanosecond.
// 设置年月日时分秒纳秒
func (c Carbon) SetDateTimeNano(year, month, day, hour, minute, second, nanosecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, nanosecond)
}
// SetDate sets year, month and day.
// 设置年月日
func (c Carbon) SetDate(year, month, day int) Carbon {
if c.IsInvalid() {
return c
}
hour, minute, second := c.Time()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetTime sets hour, minute and second.
// 设置时分秒
func (c Carbon) SetTime(hour, minute, second int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day := c.Date()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
2021-08-10 10:58:43 +08:00
// SetYear sets year.
// 设置年份
func (c Carbon) SetYear(year int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
_, month, day, hour, minute, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
2021-08-10 10:58:43 +08:00
// SetYearNoOverflow sets year without overflowing month.
// 设置年份(月份不溢出)
func (c Carbon) SetYearNoOverflow(year int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
return c.AddYearsNoOverflow(year - c.Year())
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// SetMonth sets month.
// 设置月份
2021-02-18 14:32:31 +08:00
func (c Carbon) SetMonth(month int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
year, _, day, hour, minute, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// 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
}
2021-08-10 10:58:43 +08:00
// SetDay sets day.
// 设置日期
2021-02-18 14:32:31 +08:00
func (c Carbon) SetDay(day int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
year, month, _, hour, minute, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// SetHour sets hour.
// 设置小时
2021-02-18 14:32:31 +08:00
func (c Carbon) SetHour(hour int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
year, month, day, _, minute, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// SetMinute sets minute.
// 设置分钟
2021-02-18 14:32:31 +08:00
func (c Carbon) SetMinute(minute int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
year, month, day, hour, _, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
2021-02-18 14:32:31 +08:00
}
2021-08-10 10:58:43 +08:00
// SetSecond sets second.
// 设置秒数
2021-02-18 14:32:31 +08:00
func (c Carbon) SetSecond(second int) Carbon {
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, _ := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
2021-02-18 14:32:31 +08:00
}
2021-04-05 09:45:14 +08:00
2021-08-10 10:58:43 +08:00
// SetMillisecond sets millisecond.
2021-07-28 15:18:05 +08:00
// 设置毫秒
func (c Carbon) SetMillisecond(millisecond int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
}
2021-08-10 10:58:43 +08:00
// SetMicrosecond sets microsecond.
2021-07-28 15:18:05 +08:00
// 设置微秒
func (c Carbon) SetMicrosecond(microsecond int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
}
2021-08-10 10:58:43 +08:00
// SetNanosecond sets nanosecond.
2021-07-28 15:18:05 +08:00
// 设置纳秒
2021-04-05 09:45:14 +08:00
func (c Carbon) SetNanosecond(nanosecond int) Carbon {
2021-07-23 11:07:13 +08:00
if c.IsInvalid() {
return c
}
year, month, day, hour, minute, second := c.DateTime()
2022-04-12 23:24:49 +08:00
return c.create(year, month, day, hour, minute, second, nanosecond)
2021-04-05 09:45:14 +08:00
}