From a1b9eca7b6d8807ed967d8af92667dbe1ba07a77 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 3 Nov 2022 21:17:28 +0800 Subject: [PATCH] fix issue #2244 (#2257) * fix issue #2244 * ut update for package gtime * golangci updates --- .golangci.yml | 23 ------------ os/gtime/gtime_time.go | 42 +++++++++++++--------- os/gtime/gtime_z_unit_time_test.go | 56 +++++++++++++++++++++++++----- 3 files changed, 74 insertions(+), 47 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 158f4e0c8..d77dc6583 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -40,7 +40,6 @@ linters: - errcheck # Errcheck is a program for checking for unchecked errors in go programs. - errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. - funlen # Tool for detection of long functions - - gci # Gci controls golang package import order and makes it always deterministic. - goconst # Finds repeated strings that could be replaced by a constant - gocritic # Provides diagnostics that check for bugs, performance and style issues. - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification @@ -282,25 +281,3 @@ linters-settings: # - pattern: 'a[b:len(a)]' # replacement: 'a[b:]' - #https://golangci-lint.run/usage/linters/#gci - gci: - # DEPRECATED: use `sections` and `prefix(github.com/org/project)` instead. - local-prefixes: - # Section configuration to compare against. - # Section names are case-insensitive and may contain parameters in (). - # The default order of sections is `standard > default > custom > blank > dot`, - # If `custom-order` is `true`, it follows the order of `sections` option. - # Default: ["standard", "default"] - sections: - - standard # Standard section: captures all standard packages. - - default # Default section: contains all imports that could not be matched to another section type. - - prefix(github.com/gogf/gf) # Custom section: groups all imports with the specified Prefix. - - blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled. - - dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled. - # Skip generated files. - # Default: true - skip-generated: false - # Enable custom order of sections. - # If `true`, make the section order the same as the order of `sections`. - # Default: false - custom-order: true \ No newline at end of file diff --git a/os/gtime/gtime_time.go b/os/gtime/gtime_time.go index 1ed85100d..e66967bc7 100644 --- a/os/gtime/gtime_time.go +++ b/os/gtime/gtime_time.go @@ -421,47 +421,57 @@ func (t *Time) StartOfYear() *Time { return newTime } +// getPrecisionDelta returns the precision parameter for time calculation depending on `withNanoPrecision` option. +func getPrecisionDelta(withNanoPrecision ...bool) time.Duration { + if len(withNanoPrecision) > 0 && withNanoPrecision[0] { + return time.Nanosecond + } + return time.Second +} + // EndOfMinute clones and returns a new time of which the seconds is set to 59. -func (t *Time) EndOfMinute() *Time { - return t.StartOfMinute().Add(time.Minute - time.Nanosecond) +func (t *Time) EndOfMinute(withNanoPrecision ...bool) *Time { + return t.StartOfMinute().Add(time.Minute - getPrecisionDelta(withNanoPrecision...)) } // EndOfHour clones and returns a new time of which the minutes and seconds are both set to 59. -func (t *Time) EndOfHour() *Time { - return t.StartOfHour().Add(time.Hour - time.Nanosecond) +func (t *Time) EndOfHour(withNanoPrecision ...bool) *Time { + return t.StartOfHour().Add(time.Hour - getPrecisionDelta(withNanoPrecision...)) } // EndOfDay clones and returns a new time which is the end of day the and its time is set to 23:59:59. -func (t *Time) EndOfDay() *Time { +func (t *Time) EndOfDay(withNanoPrecision ...bool) *Time { y, m, d := t.Date() newTime := t.Clone() - newTime.Time = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), newTime.Time.Location()) + newTime.Time = time.Date( + y, m, d, 23, 59, 59, int(time.Second-getPrecisionDelta(withNanoPrecision...)), newTime.Time.Location(), + ) return newTime } // EndOfWeek clones and returns a new time which is the end of week and its time is set to 23:59:59. -func (t *Time) EndOfWeek() *Time { - return t.StartOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond) +func (t *Time) EndOfWeek(withNanoPrecision ...bool) *Time { + return t.StartOfWeek().AddDate(0, 0, 7).Add(-getPrecisionDelta(withNanoPrecision...)) } // EndOfMonth clones and returns a new time which is the end of the month and its time is set to 23:59:59. -func (t *Time) EndOfMonth() *Time { - return t.StartOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond) +func (t *Time) EndOfMonth(withNanoPrecision ...bool) *Time { + return t.StartOfMonth().AddDate(0, 1, 0).Add(-getPrecisionDelta(withNanoPrecision...)) } // EndOfQuarter clones and returns a new time which is end of the quarter and its time is set to 23:59:59. -func (t *Time) EndOfQuarter() *Time { - return t.StartOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond) +func (t *Time) EndOfQuarter(withNanoPrecision ...bool) *Time { + return t.StartOfQuarter().AddDate(0, 3, 0).Add(-getPrecisionDelta(withNanoPrecision...)) } // EndOfHalf clones and returns a new time which is the end of the half year and its time is set to 23:59:59. -func (t *Time) EndOfHalf() *Time { - return t.StartOfHalf().AddDate(0, 6, 0).Add(-time.Nanosecond) +func (t *Time) EndOfHalf(withNanoPrecision ...bool) *Time { + return t.StartOfHalf().AddDate(0, 6, 0).Add(-getPrecisionDelta(withNanoPrecision...)) } // EndOfYear clones and returns a new time which is the end of the year and its time is set to 23:59:59. -func (t *Time) EndOfYear() *Time { - return t.StartOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond) +func (t *Time) EndOfYear(withNanoPrecision ...bool) *Time { + return t.StartOfYear().AddDate(1, 0, 0).Add(-getPrecisionDelta(withNanoPrecision...)) } // MarshalJSON implements the interface MarshalJSON for json.Marshal. diff --git a/os/gtime/gtime_z_unit_time_test.go b/os/gtime/gtime_z_unit_time_test.go index 9527b71e5..45bd12c71 100644 --- a/os/gtime/gtime_z_unit_time_test.go +++ b/os/gtime/gtime_z_unit_time_test.go @@ -276,7 +276,12 @@ func Test_EndOfMinute(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfMinute() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:24:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") + timeTemp1 := timeTemp.EndOfMinute(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.999") }) } @@ -292,7 +297,12 @@ func Test_EndOfHour(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfHour() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:59:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") + timeTemp1 := timeTemp.EndOfHour(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.999") }) } @@ -308,7 +318,12 @@ func Test_EndOfDay(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfDay() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 23:59:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") + timeTemp1 := timeTemp.EndOfDay(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999") }) } @@ -324,7 +339,12 @@ func Test_EndOfWeek(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfWeek() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 23:59:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") + timeTemp1 := timeTemp.EndOfWeek(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999") }) } @@ -340,7 +360,12 @@ func Test_EndOfMonth(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfMonth() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") + timeTemp1 := timeTemp.EndOfMonth(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) } @@ -356,7 +381,12 @@ func Test_EndOfQuarter(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfQuarter() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") + timeTemp1 := timeTemp.EndOfQuarter(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) } @@ -372,7 +402,12 @@ func Test_EndOfHalf(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfHalf() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") + timeTemp1 := timeTemp.EndOfHalf(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) } @@ -388,7 +423,12 @@ func Test_EndOfYear(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfYear() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-31 23:59:59") + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + }) + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") + timeTemp1 := timeTemp.EndOfYear(true) + t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) }