Skip to content

Commit

Permalink
fix issue #2244 (#2257)
Browse files Browse the repository at this point in the history
* fix issue #2244

* ut update for package gtime

* golangci updates
  • Loading branch information
gqcn authored Nov 3, 2022
1 parent c4a5b8c commit a1b9eca
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
23 changes: 0 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
42 changes: 26 additions & 16 deletions os/gtime/gtime_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
56 changes: 48 additions & 8 deletions os/gtime/gtime_z_unit_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

Expand All @@ -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")
})
}

Expand All @@ -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")
})
}

Expand All @@ -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")
})
}

Expand All @@ -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")
})
}

Expand All @@ -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")
})
}

Expand All @@ -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")
})
}

Expand All @@ -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")
})
}

Expand Down

0 comments on commit a1b9eca

Please sign in to comment.