Skip to content

Commit

Permalink
Merge branch 'master' into fix/to-week-string
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin authored Nov 30, 2023
2 parents 85ff137 + 731fdcb commit 760c884
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 79 deletions.
2 changes: 1 addition & 1 deletion carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Version current version
// 当前版本号
const Version = "2.2.13"
const Version = "2.2.14"

// timezone constants
// 时区常量
Expand Down
10 changes: 5 additions & 5 deletions creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func CreateFromStdTime(tt time.Time) Carbon {
// 从给定的秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
Expand All @@ -36,7 +36,7 @@ func CreateFromTimestamp(timestamp int64, timezone ...string) Carbon {
// 从给定的毫秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
Expand All @@ -55,7 +55,7 @@ func CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon {
// 从给定的微秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
Expand All @@ -74,7 +74,7 @@ func CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon {
// 从给定的纳秒级时间戳创建 Carbon 实例
func (c Carbon) CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
Expand Down Expand Up @@ -254,7 +254,7 @@ func CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string
// 从给定的年、月、日、时、分、秒、纳秒创建 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[len(timezone)-1])
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil {
return c
Expand Down
20 changes: 10 additions & 10 deletions difference.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c Carbon) DiffInYears(carbon ...Carbon) int64 {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}

dy, dm, dd := end.Year()-start.Year(), end.Month()-start.Month(), end.Day()-start.Day()
Expand All @@ -40,7 +40,7 @@ func (c Carbon) DiffInMonths(carbon ...Carbon) int64 {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
startYear, startMonth, startDay := c.Date()
endYear, endMonth, endDay := end.Date()
Expand Down Expand Up @@ -75,7 +75,7 @@ func (c Carbon) DiffInWeeks(carbon ...Carbon) int64 {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (7 * 24 * 3600))))
}
Expand All @@ -94,7 +94,7 @@ func (c Carbon) DiffInDays(carbon ...Carbon) int64 {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (24 * 3600))))
}
Expand All @@ -113,7 +113,7 @@ func (c Carbon) DiffInHours(carbon ...Carbon) int64 {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
return c.DiffInSeconds(end) / SecondsPerHour
}
Expand All @@ -132,7 +132,7 @@ func (c Carbon) DiffInMinutes(carbon ...Carbon) int64 {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
return c.DiffInSeconds(end) / SecondsPerMinute
}
Expand All @@ -151,7 +151,7 @@ func (c Carbon) DiffInSeconds(carbon ...Carbon) int64 {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
return end.Timestamp() - c.Timestamp()
}
Expand All @@ -170,7 +170,7 @@ func (c Carbon) DiffInString(carbon ...Carbon) string {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
if c.Error != nil || end.Error != nil {
return ""
Expand All @@ -187,7 +187,7 @@ func (c Carbon) DiffAbsInString(carbon ...Carbon) string {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
if c.Error != nil || end.Error != nil {
return ""
Expand All @@ -204,7 +204,7 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string {
end = CreateFromTimestampNano(c.testNow, c.Location())
}
if len(carbon) > 0 {
end = carbon[len(carbon)-1]
end = carbon[0]
}
if c.Error != nil || end.Error != nil {
return ""
Expand Down
10 changes: 6 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ var layouts = []string{
"2006-01-02 15:04:05PM MST", "2006-01-02 15:04:05.999999999PM MST", "2006-1-2 15:4:5PM MST", "2006-1-2 15:4:5.999999999PM MST",
"2006-01-02 15:04:05 PM MST", "2006-01-02 15:04:05.999999999 PM MST", "2006-1-2 15:4:5 PM MST", "2006-1-2 15:4:5.999999999 PM MST",
"1/2/2006", "1/2/2006 15", "1/2/2006 15:4", "1/2/2006 15:4:5", "1/2/2006 15:4:5.999999999",
"2006-1-2 15:4:5 -0700 MST", "2006-1-2 15:4:5.999999999 -0700 MST",
"2006-1-2T15:4:5Z07", "2006-1-2T15:4:5.999999999Z07",
"2006-1-2T15:4:5Z07:00", "2006-1-2T15:4:5.999999999Z07:00",
"2006-1-2T15:4:5-07:00", "2006-1-2T15:4:5.999999999-07:00",
"2006-1-2 15:4:5 -0700 MST", "2006-1-2 15:4:5.999999999 -0700 MST", "2006-1-2 15:04:05 -0700 MST", "2006-1-2 15:04:05.999999999 -0700 MST",
"2006-01-02T15:04:05", "2006-01-02T15:04:05.999999999", "2006-1-2T3:4:5", "2006-1-2T3:4:5.999999999",
"2006-01-02T15:04:05Z07", "2006-01-02T15:04:05.999999999Z07", "2006-1-2T15:4:5Z07", "2006-1-2T15:4:5.999999999Z07",
"2006-01-02T15:04:05Z07:00", "2006-01-02T15:04:05.999999999Z07:00", "2006-1-2T15:4:5Z07:00", "2006-1-2T15:4:5.999999999Z07:00",
"2006-01-02T15:04:05-07:00", "2006-01-02T15:04:05.999999999-07:00", "2006-1-2T15:4:5-07:00", "2006-1-2T15:4:5.999999999-07:00",
"2006-01-02T15:04:05-0700", "2006-01-02T15:04:05.999999999-0700", "2006-1-2T3:4:5-0700", "2006-1-2T3:4:5.999999999-0700",
"20060102150405-07:00", "20060102150405.999999999-07:00",
"20060102150405Z07", "20060102150405.999999999Z07",
"20060102150405Z07:00", "20060102150405.999999999Z07:00",
Expand Down
Loading

0 comments on commit 760c884

Please sign in to comment.