From b444d9d0c4f654509cb373bfe975efaaf5e66af5 Mon Sep 17 00:00:00 2001 From: gouguoyin <245629560@qq.com> Date: Fri, 18 Jun 2021 14:36:41 +0800 Subject: [PATCH] V1.3.9 --- README.en.md | 6 ++++-- README.md | 6 ++++-- carbon.go | 11 +++++------ modifier.go | 15 +++++++++------ modifier_test.go | 43 +++++++++++++++++++++++++------------------ setter.go | 11 ----------- 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/README.en.md b/README.en.md index 320c044..9147fda 100755 --- a/README.en.md +++ b/README.en.md @@ -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 diff --git a/README.md b/README.md index c2a0d7f..5f2345b 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/carbon.go b/carbon.go index f6a9b53..72ce23f 100755 --- a/carbon.go +++ b/carbon.go @@ -139,16 +139,15 @@ const ( // Carbon 定义 Carbon 结构体 type Carbon struct { - Time time.Time - WeekStartDay time.Weekday - Loc *time.Location - Lang *Language - Error error + Time time.Time + Loc *time.Location + Lang *Language + Error error } // 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 diff --git a/modifier.go b/modifier.go index 9e14cf6..4c79557 100755 --- a/modifier.go +++ b/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 } diff --git a/modifier_test.go b/modifier_test.go index c91284a..da36bb2 100755 --- a/modifier_test.go +++ b/modifier_test.go @@ -2,6 +2,7 @@ package carbon import ( "testing" + "time" ) func TestCarbon_StartOfYear(t *testing.T) { @@ -90,19 +91,22 @@ func TestCarbon_EndOfMonth(t *testing.T) { func TestCarbon_StartOfWeek(t *testing.T) { Tests := []struct { - input string // 输入值 - output string // 期望输出值 + 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) @@ -112,19 +116,22 @@ func TestCarbon_StartOfWeek(t *testing.T) { func TestCarbon_EndOfWeek(t *testing.T) { Tests := []struct { - input string // 输入值 - output string // 期望输出值 + 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) diff --git a/setter.go b/setter.go index 3d65470..58dc69c 100755 --- a/setter.go +++ b/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)