mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
V1.3.9
This commit is contained in:
parent
c6260490df
commit
b444d9d0c4
@ -157,9 +157,11 @@ carbon.Parse("2020-08-05 13:14:15").StartOfMonth().ToStartTimeString() // 2020-0
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToDateTimeString() // 2020-08-31 23:59:59
|
||||
|
||||
// Start of the week
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek().ToDateTimeString() // 2020-08-03 00:00:00
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek(time.Sunday).ToDateTimeString() // 2020-08-02 00:00:00
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek(time.Monday).ToDateTimeString() // 2020-08-03 00:00:00
|
||||
// End of the week
|
||||
carbon.Parse("2020-08-05 13:14:15").LastOfWeek().ToDateTimeString() // 2020-08-09 23:59:59
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfWeek(time.Sunday).ToDateTimeString() // 2020-08-08 23:59:59
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfWeek(time.Monday).ToDateTimeString() // 2020-08-09 23:59:59
|
||||
|
||||
// Start of the day
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfDay().ToDateTimeString() // 2020-08-05 00:00:00
|
||||
|
@ -156,9 +156,11 @@ carbon.Parse("2020-08-05 13:14:15").StartOfMonth().ToDateTimeString() // 2020-08
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToDateTimeString() // 2020-08-31 23:59:59
|
||||
|
||||
// 本周开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek().ToDateTimeString() // 2020-08-03 00:00:00
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek(time.Sunday).ToDateTimeString() // 2020-08-02 00:00:00
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek(time.Monday).ToDateTimeString() // 2020-08-03 00:00:00
|
||||
// 本周结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfWeek().ToDateTimeString() // 2020-08-09 23:59:59
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfWeek(time.Sunday).ToDateTimeString() // 2020-08-08 23:59:59
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfWeek(time.Monday).ToDateTimeString() // 2020-08-09 23:59:59
|
||||
|
||||
// 本日开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfDay().ToDateTimeString() // 2020-08-05 00:00:00
|
||||
|
@ -140,7 +140,6 @@ const (
|
||||
// Carbon 定义 Carbon 结构体
|
||||
type Carbon struct {
|
||||
Time time.Time
|
||||
WeekStartDay time.Weekday
|
||||
Loc *time.Location
|
||||
Lang *Language
|
||||
Error error
|
||||
@ -148,7 +147,7 @@ type Carbon struct {
|
||||
|
||||
// NewCarbon 新建 Carbon
|
||||
func NewCarbon() Carbon {
|
||||
return Carbon{WeekStartDay: time.Sunday, Loc: time.Local, Lang: NewLanguage()}
|
||||
return Carbon{Loc: time.Local, Lang: NewLanguage()}
|
||||
}
|
||||
|
||||
// Time2Carbon 将 time.Time 转换成 Carbon
|
||||
|
15
modifier.go
15
modifier.go
@ -30,12 +30,12 @@ func (c Carbon) EndOfMonth() Carbon {
|
||||
}
|
||||
|
||||
// StartOfWeek 本周开始时间
|
||||
func (c Carbon) StartOfWeek() Carbon {
|
||||
func (c Carbon) StartOfWeek(weekStartDay time.Weekday) Carbon {
|
||||
weekDay := c.Time.In(c.Loc).Weekday()
|
||||
if weekDay == c.WeekStartDay {
|
||||
if weekDay == weekStartDay {
|
||||
return c.StartOfDay()
|
||||
}
|
||||
days := int(weekDay) - int(c.WeekStartDay)
|
||||
days := int(weekDay) - int(weekStartDay)
|
||||
if weekDay == time.Sunday {
|
||||
days = 6
|
||||
}
|
||||
@ -43,12 +43,15 @@ func (c Carbon) StartOfWeek() Carbon {
|
||||
}
|
||||
|
||||
// EndOfWeek 本周结束时间
|
||||
func (c Carbon) EndOfWeek() Carbon {
|
||||
func (c Carbon) EndOfWeek(weekStartDay time.Weekday) Carbon {
|
||||
weekDay := c.Time.In(c.Loc).Weekday()
|
||||
if weekDay == 1-c.WeekStartDay {
|
||||
if weekStartDay == 0 && weekDay == 6 {
|
||||
return c.EndOfDay()
|
||||
}
|
||||
days := 6 - int(weekDay) + int(c.WeekStartDay)
|
||||
if weekStartDay == 1 && weekDay == 0 {
|
||||
return c.EndOfDay()
|
||||
}
|
||||
days := 6 - int(weekDay) + int(weekStartDay)
|
||||
if weekDay == time.Sunday {
|
||||
days = 6
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package carbon
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCarbon_StartOfYear(t *testing.T) {
|
||||
@ -91,18 +92,21 @@ func TestCarbon_EndOfMonth(t *testing.T) {
|
||||
func TestCarbon_StartOfWeek(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
week time.Weekday // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-01-01 00:00:00", "2019-12-29 00:00:00"},
|
||||
{"2020-01-31 23:59:59", "2020-01-26 00:00:00"},
|
||||
{"2020-02-01 13:14:15", "2020-01-26 00:00:00"},
|
||||
{"2020-02-28", "2020-02-23 00:00:00"},
|
||||
{"2020-02-29", "2020-02-23 00:00:00"},
|
||||
{"2020-10-12", "2020-10-11 00:00:00"},
|
||||
{"2021-06-13", time.Sunday, "2021-06-13 00:00:00"},
|
||||
{"2021-06-14", time.Sunday, "2021-06-13 00:00:00"},
|
||||
{"2021-06-18", time.Sunday, "2021-06-13 00:00:00"},
|
||||
{"2021-06-13", time.Monday, "2021-06-07 00:00:00"},
|
||||
{"2021-06-14", time.Monday, "2021-06-14 00:00:00"},
|
||||
{"2021-06-18", time.Monday, "2021-06-14 00:00:00"},
|
||||
{"2021-06-19", time.Monday, "2021-06-14 00:00:00"},
|
||||
{"2021-06-20", time.Monday, "2021-06-14 00:00:00"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).StartOfWeek().ToDateTimeString()
|
||||
output := Parse(v.input).StartOfWeek(v.week).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -113,18 +117,21 @@ func TestCarbon_StartOfWeek(t *testing.T) {
|
||||
func TestCarbon_EndOfWeek(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
week time.Weekday // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-01-01 00:00:00", "2020-01-04 23:59:59"},
|
||||
{"2020-01-31 23:59:59", "2020-02-01 23:59:59"},
|
||||
{"2020-02-01 13:14:15", "2020-02-01 23:59:59"},
|
||||
{"2020-02-28", "2020-02-29 23:59:59"},
|
||||
{"2020-02-29", "2020-02-29 23:59:59"},
|
||||
{"2020-10-04", "2020-10-10 23:59:59"},
|
||||
{"2021-06-13", time.Sunday, "2021-06-19 23:59:59"},
|
||||
{"2021-06-14", time.Sunday, "2021-06-19 23:59:59"},
|
||||
{"2021-06-18", time.Sunday, "2021-06-19 23:59:59"},
|
||||
{"2021-06-13", time.Monday, "2021-06-13 23:59:59"},
|
||||
{"2021-06-14", time.Monday, "2021-06-20 23:59:59"},
|
||||
{"2021-06-18", time.Monday, "2021-06-20 23:59:59"},
|
||||
{"2021-06-19", time.Monday, "2021-06-20 23:59:59"},
|
||||
{"2021-06-20", time.Monday, "2021-06-20 23:59:59"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).EndOfWeek().ToDateTimeString()
|
||||
output := Parse(v.input).EndOfWeek(v.week).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Errorf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
|
11
setter.go
11
setter.go
@ -49,17 +49,6 @@ func SetLocale(locale string) Carbon {
|
||||
return c
|
||||
}
|
||||
|
||||
// SetWeekStartDay 设置一周开始时间
|
||||
func (c Carbon) SetWeekStartDay(wd time.Weekday) Carbon {
|
||||
c.WeekStartDay = wd
|
||||
return c
|
||||
}
|
||||
|
||||
// SetWeekStartDay 设置一周开始时间
|
||||
func SetWeekStartDay(wd time.Weekday) Carbon {
|
||||
return NewCarbon().SetWeekStartDay(wd)
|
||||
}
|
||||
|
||||
// SetYear 设置年
|
||||
func (c Carbon) SetYear(year int) Carbon {
|
||||
c.Time = time.Date(year, time.Month(c.Month()), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Loc)
|
||||
|
Loading…
Reference in New Issue
Block a user