mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 10:12:29 +08:00
datetime: optimized func calls (#7)
Replace time parsing calls with less expensive operations
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
package datetime
|
package datetime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -56,24 +56,17 @@ func init() {
|
|||||||
|
|
||||||
// AddMinute add or sub minute to the time
|
// AddMinute add or sub minute to the time
|
||||||
func AddMinute(t time.Time, minute int64) time.Time {
|
func AddMinute(t time.Time, minute int64) time.Time {
|
||||||
s := strconv.FormatInt(minute, 10)
|
return t.Add(time.Minute * time.Duration(minute))
|
||||||
m, _ := time.ParseDuration(s + "m")
|
|
||||||
return t.Add(m)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddHour add or sub hour to the time
|
// AddHour add or sub hour to the time
|
||||||
func AddHour(t time.Time, hour int64) time.Time {
|
func AddHour(t time.Time, hour int64) time.Time {
|
||||||
s := strconv.FormatInt(hour, 10)
|
return t.Add(time.Hour * time.Duration(hour))
|
||||||
h, _ := time.ParseDuration(s + "h")
|
|
||||||
return t.Add(h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddDay add or sub day to the time
|
// AddDay add or sub day to the time
|
||||||
func AddDay(t time.Time, day int64) time.Time {
|
func AddDay(t time.Time, day int64) time.Time {
|
||||||
dayHours := day * 24
|
return t.Add(24 * time.Hour * time.Duration(day))
|
||||||
d := strconv.FormatInt(dayHours, 10)
|
|
||||||
h, _ := time.ParseDuration(d + "h")
|
|
||||||
return t.Add(h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNowDate return format yyyy-mm-dd of current date
|
// GetNowDate return format yyyy-mm-dd of current date
|
||||||
@@ -109,7 +102,11 @@ func FormatTimeToStr(t time.Time, format string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FormatStrToTime convert string to time
|
// FormatStrToTime convert string to time
|
||||||
func FormatStrToTime(str, format string) time.Time {
|
func FormatStrToTime(str, format string) (time.Time, error) {
|
||||||
t, _ := time.Parse(timeFormat[format], str)
|
v, ok := timeFormat[format]
|
||||||
return t
|
if !ok {
|
||||||
|
return time.Time{}, fmt.Errorf("format %s not found", format)
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Parse(v, str)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,7 +135,10 @@ func TestFormatStrToTime(t *testing.T) {
|
|||||||
"2021/01"}
|
"2021/01"}
|
||||||
|
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
res := FormatStrToTime(datetimeStr[i], cases[i])
|
res, err := FormatStrToTime(datetimeStr[i], cases[i])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
expected, _ := time.Parse(formats[i], datetimeStr[i])
|
expected, _ := time.Parse(formats[i], datetimeStr[i])
|
||||||
if res != expected {
|
if res != expected {
|
||||||
utils.LogFailedTestInfo(t, "FormatTimeToStr", cases[i], expected, res)
|
utils.LogFailedTestInfo(t, "FormatTimeToStr", cases[i], expected, res)
|
||||||
|
|||||||
Reference in New Issue
Block a user