The public fields Loc and Lang in the carbon structure are changed to private fields loc and lang

This commit is contained in:
gouguoyin 2021-08-16 09:29:07 +08:00
parent 4b5e3d9cae
commit 1b99b6e5af
6 changed files with 63 additions and 93 deletions

View File

@ -10,7 +10,7 @@ func (c Carbon) StartOfCentury() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0, c.Loc)
c.Time = time.Date(c.Year()/YearsPerCentury*YearsPerCentury, 1, 1, 0, 0, 0, 0, c.loc)
return c
}
@ -20,7 +20,7 @@ func (c Carbon) EndOfCentury() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999, c.Loc)
c.Time = time.Date(c.Year()/YearsPerCentury*YearsPerCentury+99, 12, 31, 23, 59, 59, 999999999, c.loc)
return c
}
@ -30,7 +30,7 @@ func (c Carbon) StartOfDecade() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0, c.Loc)
c.Time = time.Date(c.Year()/YearsPerDecade*YearsPerDecade, 1, 1, 0, 0, 0, 0, c.loc)
return c
}
@ -40,7 +40,7 @@ func (c Carbon) EndOfDecade() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999, c.Loc)
c.Time = time.Date(c.Year()/YearsPerDecade*YearsPerDecade+9, 12, 31, 23, 59, 59, 999999999, c.loc)
return c
}
@ -50,7 +50,7 @@ func (c Carbon) StartOfYear() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), 1, 1, 0, 0, 0, 0, c.Loc)
c.Time = time.Date(c.Year(), 1, 1, 0, 0, 0, 0, c.loc)
return c
}
@ -60,7 +60,7 @@ func (c Carbon) EndOfYear() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), 12, 31, 23, 59, 59, 999999999, c.Loc)
c.Time = time.Date(c.Year(), 12, 31, 23, 59, 59, 999999999, c.loc)
return c
}
@ -70,7 +70,7 @@ func (c Carbon) StartOfQuarter() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(3*c.Quarter()-2), 1, 0, 0, 0, 0, c.Loc)
c.Time = time.Date(c.Year(), time.Month(3*c.Quarter()-2), 1, 0, 0, 0, 0, c.loc)
return c
}
@ -87,7 +87,7 @@ func (c Carbon) EndOfQuarter() Carbon {
case 2, 3:
day = 30
}
c.Time = time.Date(c.Year(), time.Month(3*quarter), day, 23, 59, 59, 999999999, c.Loc)
c.Time = time.Date(c.Year(), time.Month(3*quarter), day, 23, 59, 59, 999999999, c.loc)
return c
}
@ -97,7 +97,7 @@ func (c Carbon) StartOfMonth() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), 1, 0, 0, 0, 0, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), 1, 0, 0, 0, 0, c.loc)
return c
}
@ -107,45 +107,28 @@ func (c Carbon) EndOfMonth() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), 1, 23, 59, 59, 999999999, c.Loc).AddDate(0, 1, -1)
c.Time = time.Date(c.Year(), time.Month(c.Month()), 1, 23, 59, 59, 999999999, c.loc).AddDate(0, 1, -1)
return c
}
// StartOfWeek returns a Carbon instance for start of the week.
// 本周开始时间
func (c Carbon) StartOfWeek(weekStartDay time.Weekday) Carbon {
func (c Carbon) StartOfWeek() Carbon {
if c.IsInvalid() {
return c
}
weekDay := c.Time.In(c.Loc).Weekday()
if weekDay == weekStartDay {
return c.StartOfDay()
}
days := int(weekDay) - int(weekStartDay)
if weekDay == time.Sunday {
days = 6
}
return c.SubDays(days).StartOfDay()
dayOfWeek, weekStartsAt := c.DayOfWeek(), int(c.weekStartsAt)
return c.SubDays((DaysPerWeek + dayOfWeek - weekStartsAt) % DaysPerWeek).StartOfDay()
}
// EndOfWeek returns a Carbon instance for end of the week.
// 本周结束时间
func (c Carbon) EndOfWeek(weekStartDay time.Weekday) Carbon {
func (c Carbon) EndOfWeek() Carbon {
if c.IsInvalid() {
return c
}
weekDay := c.Time.In(c.Loc).Weekday()
if weekStartDay == 0 && weekDay == 6 {
return c.EndOfDay()
}
if weekStartDay == 1 && weekDay == 0 {
return c.EndOfDay()
}
days := 6 - int(weekDay) + int(weekStartDay)
if weekDay == time.Sunday {
days = 6
}
return c.AddDays(days).EndOfDay()
dayOfWeek, weekEndsAt := c.DayOfWeek(), int(c.weekStartsAt)+DaysPerWeek-1
return c.AddDays((DaysPerWeek - dayOfWeek + weekEndsAt) % DaysPerWeek).EndOfDay()
}
// StartOfDay returns a Carbon instance for start of the day.
@ -154,7 +137,7 @@ func (c Carbon) StartOfDay() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), 0, 0, 0, 0, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), 0, 0, 0, 0, c.loc)
return c
}
@ -164,7 +147,7 @@ func (c Carbon) EndOfDay() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), 23, 59, 59, 999999999, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), 23, 59, 59, 999999999, c.loc)
return c
}
@ -174,7 +157,7 @@ func (c Carbon) StartOfHour() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), 0, 0, 0, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), 0, 0, 0, c.loc)
return c
}
@ -184,7 +167,7 @@ func (c Carbon) EndOfHour() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), 59, 59, 999999999, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), 59, 59, 999999999, c.loc)
return c
}
@ -194,7 +177,7 @@ func (c Carbon) StartOfMinute() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), 0, 0, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), 0, 0, c.loc)
return c
}
@ -204,7 +187,7 @@ func (c Carbon) EndOfMinute() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), 59, 999999999, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), 59, 999999999, c.loc)
return c
}
@ -214,7 +197,7 @@ func (c Carbon) StartOfSecond() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), 0, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), 0, c.loc)
return c
}
@ -224,6 +207,6 @@ func (c Carbon) EndOfSecond() Carbon {
if c.IsInvalid() {
return c
}
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), 999999999, c.Loc)
c.Time = time.Date(c.Year(), time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), 999999999, c.loc)
return c
}

View File

@ -1,7 +1,7 @@
// @Package carbon
// @Description a simple, semantic and developer-friendly golang package for datetime
// @Page github.com/golang-module/carbon
// @Version v1.5.1
// @Version v1.5.2
// @Author gouguoyin
// @Blog www.gouguoyin.cn
// @Email contact@gouguoyin.cn
@ -147,22 +147,23 @@ const (
ShortTimeFormat = "150405"
)
// Carbon defines Carbon structure.
// Carbon defines a Carbon struct.
// 定义 Carbon 结构体
type Carbon struct {
Time time.Time
Loc *time.Location
Lang *Language
Error error
Time time.Time
weekStartsAt time.Weekday
loc *time.Location
lang *Language
Error error
}
// NewCarbon returns a new Carbon instance.
// 初始化 Carbon 结构体
func NewCarbon() Carbon {
return Carbon{Loc: time.Local, Lang: NewLanguage()}
return Carbon{weekStartsAt: time.Sunday, loc: time.Local, lang: NewLanguage()}
}
// Time2Carbon converts time.Time into Carbon.
// Time2Carbon converts time.Time to Carbon.
// 将 time.Time 转换成 Carbon
func Time2Carbon(tt time.Time) Carbon {
c := NewCarbon()
@ -170,24 +171,22 @@ func Time2Carbon(tt time.Time) Carbon {
return c
}
// Carbon2Time converts Carbon into time.Time.
// Carbon2Time converts Carbon to time.Time.
// 将 Carbon 转换成 time.Time
func (c Carbon) Carbon2Time() time.Time {
return c.Time.In(c.Loc)
return c.Time.In(c.loc)
}
// Now returns a Carbon instance for now.
// 当前
func (c Carbon) Now(timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
}
c.Time = time.Now().In(c.Loc)
c.Time = time.Now().In(c.loc)
return c
}
@ -201,17 +200,15 @@ func Now(timezone ...string) Carbon {
// 明天
func (c Carbon) Tomorrow(timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
}
if c.IsZero() {
c.Time = time.Now().In(c.Loc).AddDate(0, 0, 1)
c.Time = time.Now().In(c.loc).AddDate(0, 0, 1)
} else {
c.Time = c.Time.In(c.Loc).AddDate(0, 0, 1)
c.Time = c.Time.In(c.loc).AddDate(0, 0, 1)
}
return c
}
@ -226,17 +223,15 @@ func Tomorrow(timezone ...string) Carbon {
// 昨天
func (c Carbon) Yesterday(timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
}
if c.IsZero() {
c.Time = time.Now().In(c.Loc).AddDate(0, 0, -1)
c.Time = time.Now().In(c.loc).AddDate(0, 0, -1)
} else {
c.Time = c.Time.In(c.Loc).AddDate(0, 0, -1)
c.Time = c.Time.In(c.loc).AddDate(0, 0, -1)
}
return c
}

View File

@ -10,8 +10,8 @@ func (c Carbon) Constellation() string {
if c.IsInvalid() {
return ""
}
if len(c.Lang.resources) == 0 {
c.Lang.SetLocale(defaultLocale)
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
index := -1
switch {
@ -40,7 +40,7 @@ func (c Carbon) Constellation() string {
case c.Month() == 2 && c.Day() >= 19, c.Month() == 3 && c.Day() <= 20:
index = 11 // 双鱼座
}
if constellations, ok := c.Lang.resources["constellations"]; ok {
if constellations, ok := c.lang.resources["constellations"]; ok {
slice := strings.Split(constellations, "|")
if len(slice) == 12 {
return slice[index]
@ -169,7 +169,7 @@ func (c Carbon) IsScorpio() bool {
return false
}
// IsSagittarius whether is Sagittarius
// IsSagittarius whether is Sagittarius.
// 是否是射手座
func (c Carbon) IsSagittarius() bool {
if c.IsInvalid() {

View File

@ -9,9 +9,7 @@ import (
// 从给定的时间戳创建 Carbon 实例,支持秒、毫秒、微秒和纳秒
func (c Carbon) CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
@ -44,14 +42,12 @@ func CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
// 从给定的年月日时分秒创建 Carbon 实例
func (c Carbon) CreateFromDateTime(year int, month int, day int, hour int, minute int, second int, timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
}
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, time.Now().Nanosecond(), c.Loc)
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, time.Now().Nanosecond(), c.loc)
return c
}
@ -65,15 +61,13 @@ func CreateFromDateTime(year int, month int, day int, hour int, minute int, seco
// 从给定的年月日创建 Carbon 实例
func (c Carbon) CreateFromDate(year int, month int, day int, timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
}
hour, minute, second := time.Now().In(c.Loc).Clock()
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, time.Now().Nanosecond(), c.Loc)
hour, minute, second := time.Now().In(c.loc).Clock()
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, time.Now().Nanosecond(), c.loc)
return c
}
@ -87,15 +81,13 @@ func CreateFromDate(year int, month int, day int, timezone ...string) Carbon {
// 从给定的时分秒创建 Carbon 实例
func (c Carbon) CreateFromTime(hour int, minute int, second int, timezone ...string) Carbon {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
}
year, month, day := time.Now().In(c.Loc).Date()
c.Time = time.Date(year, month, day, hour, minute, second, time.Now().Nanosecond(), c.Loc)
year, month, day := time.Now().In(c.loc).Date()
c.Time = time.Date(year, month, day, hour, minute, second, time.Now().Nanosecond(), c.loc)
return c
}

View File

@ -10,7 +10,7 @@ import (
func (c *Carbon) Scan(v interface{}) error {
value, ok := v.(time.Time)
if ok {
*c = Carbon{Time: value, Loc: time.Local}
*c = Carbon{Time: value, loc: time.Local}
return nil
}
return fmt.Errorf("can not convert %v to carbon", v)

View File

@ -195,17 +195,17 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string {
case c.DiffInSecondsWithAbs(end) == 0:
unit = "now"
diff = 0
return c.Lang.translate(unit, diff)
return c.lang.translate(unit, diff)
}
translation := c.Lang.translate(unit, diff)
translation := c.lang.translate(unit, diff)
if c.Lt(end) && len(carbon) == 0 {
return strings.Replace(c.Lang.resources["ago"], "%s", translation, 1)
return strings.Replace(c.lang.resources["ago"], "%s", translation, 1)
}
if c.Lt(end) && len(carbon) > 0 {
return strings.Replace(c.Lang.resources["before"], "%s", translation, 1)
return strings.Replace(c.lang.resources["before"], "%s", translation, 1)
}
if c.Gt(end) && len(carbon) == 0 {
return strings.Replace(c.Lang.resources["from_now"], "%s", translation, 1)
return strings.Replace(c.lang.resources["from_now"], "%s", translation, 1)
}
return strings.Replace(c.Lang.resources["after"], "%s", translation, 1)
return strings.Replace(c.lang.resources["after"], "%s", translation, 1)
}