feat: add.

This commit is contained in:
tx7do
2023-11-06 14:11:20 +08:00
parent 83bb818ade
commit da82d442cc
20 changed files with 545 additions and 94 deletions

13
timeutil/consts.go Normal file
View File

@@ -0,0 +1,13 @@
package util
import "time"
const (
DateLayout = "2006-01-02"
ClockLayout = "15:04:05"
TimeLayout = DateLayout + " " + ClockLayout
DefaultTimeLocationName = "Asia/Shanghai"
)
var ReferenceTimeValue time.Time = time.Date(2006, 1, 2, 15, 4, 5, 999999999, time.FixedZone("MST", -7*60*60))

61
timeutil/diff.go Normal file
View File

@@ -0,0 +1,61 @@
package util
import (
"math"
"time"
)
// DayDifferenceHours 两天之间相差了多少小时
func DayDifferenceHours(startDate, endDate string) float64 {
startTime, _ := time.Parse(DateLayout, startDate)
endTime, _ := time.Parse(DateLayout, endDate)
duration := endTime.Sub(startTime)
return duration.Hours()
}
// StringDifferenceDays 两天之间相差了多少天
func StringDifferenceDays(startDate, endDate string) int {
hours := DayDifferenceHours(startDate, endDate)
if hours == 0 {
return 0
}
return int(math.Ceil(hours / 24))
}
func DayTimeDifferenceHours(startDate, endDate time.Time) float64 {
startTime := time.Date(startDate.Year(), startDate.Month(), startDate.Day(), 0, 0, 0, 0, time.Local)
endTime := time.Date(endDate.Year(), endDate.Month(), endDate.Day(), 0, 0, 0, 0, time.Local)
duration := endTime.Sub(startTime)
return duration.Hours()
}
// TimeDifferenceDays 两天之间相差了多少天
func TimeDifferenceDays(startDate, endDate time.Time) int {
hours := DayTimeDifferenceHours(startDate, endDate)
if hours == 0 {
return 0
}
return int(math.Ceil(hours / 24))
}
func DaySecondsDifferenceHours(startSecond, endSecond int64) float64 {
startTime := time.Unix(startSecond, 0)
endTime := time.Unix(endSecond, 0)
duration := endTime.Sub(startTime)
return duration.Hours()
}
// SecondsDifferenceDays 两天之间相差了多少天
func SecondsDifferenceDays(startSecond, endSecond int64) int {
hours := DaySecondsDifferenceHours(startSecond, endSecond)
if hours == 0 {
return 0
}
return int(math.Ceil(hours / 24))
}

57
timeutil/diff_test.go Normal file
View File

@@ -0,0 +1,57 @@
package util
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func parseDate(str string) time.Time {
t, _ := time.Parse(DateLayout, str)
return t
}
func toSecond(str string) int64 {
t, _ := time.Parse(DateLayout, str)
return t.Unix()
}
func TestDifferenceDays(t *testing.T) {
assert.Equal(t, StringDifferenceDays("2017-09-01", "2017-09-01"), 0)
assert.Equal(t, StringDifferenceDays("2017-09-01", "2017-09-02"), 1)
assert.Equal(t, StringDifferenceDays("2017-09-01", "2017-09-03"), 2)
assert.Equal(t, StringDifferenceDays("2017-09-01", "2017-09-04"), 3)
assert.Equal(t, StringDifferenceDays("2017-09-01", "2018-03-11"), 191)
assert.True(t, (StringDifferenceDays("2017-09-01", "2017-09-01")) == 0)
assert.True(t, (StringDifferenceDays("2017-09-01", "2017-09-02"))%1 == 0)
assert.True(t, (StringDifferenceDays("2017-09-01", "2017-09-03"))%2 == 0)
}
func TestTimeDifferenceDays(t *testing.T) {
assert.Equal(t, TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2017-09-01")), 0)
assert.Equal(t, TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2017-09-02")), 1)
assert.Equal(t, TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2017-09-03")), 2)
assert.Equal(t, TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2017-09-04")), 3)
assert.Equal(t, TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2018-03-11")), 191)
assert.True(t, (TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2017-09-01"))) == 0)
assert.True(t, (TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2017-09-02")))%1 == 0)
assert.True(t, (TimeDifferenceDays(parseDate("2017-09-01"), parseDate("2017-09-03")))%2 == 0)
}
func TestSecondsDifferenceDays(t *testing.T) {
assert.Equal(t, SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2017-09-01")), 0)
assert.Equal(t, SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2017-09-02")), 1)
assert.Equal(t, SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2017-09-03")), 2)
assert.Equal(t, SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2017-09-04")), 3)
assert.Equal(t, SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2018-03-11")), 191)
assert.True(t, (SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2017-09-01"))) == 0)
assert.True(t, (SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2017-09-02")))%1 == 0)
assert.True(t, (SecondsDifferenceDays(toSecond("2017-09-01"), toSecond("2017-09-03")))%2 == 0)
}

45
timeutil/format.go Normal file
View File

@@ -0,0 +1,45 @@
package util
import (
"fmt"
"strings"
"time"
)
// ReferenceTime Return the standard Golang reference time (2006-01-02T15:04:05.999999999Z07:00)
func ReferenceTime() time.Time {
return ReferenceTimeValue
}
// FormatTimer Formats the given duration in a colon-separated timer format in the form
// [HH:]MM:SS.
func FormatTimer(d time.Duration) string {
h, m, s := DurationHMS(d)
out := fmt.Sprintf("%02d:%02d:%02d", h, m, s)
out = strings.TrimPrefix(out, `00:`)
out = strings.TrimPrefix(out, `0`)
return out
}
// FormatTimerf Formats the given duration using the given format string. The string follows
// the same formatting rules as described in the fmt package, and will receive
// three integer arguments: hours, minutes, and seconds.
func FormatTimerf(format string, d time.Duration) string {
h, m, s := DurationHMS(d)
out := fmt.Sprintf(format, h, m, s)
return out
}
// DurationHMS Extracts the hours, minutes, and seconds from the given duration.
func DurationHMS(d time.Duration) (int, int, int) {
d = d.Round(time.Second)
h := d / time.Hour
d -= h * time.Hour
m := d / time.Minute
d -= m * time.Minute
s := d / time.Second
return int(h), int(m), int(s)
}

175
timeutil/range.go Normal file
View File

@@ -0,0 +1,175 @@
package util
import "time"
// GetYesterdayRangeTime 获取区间时间 - 昨天
func GetYesterdayRangeTime() (time.Time, time.Time) {
now := time.Now().AddDate(0, 0, -1)
startDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
endDate := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
return startDate, endDate
}
// GetTodayRangeTime 获取区间时间 - 今天
func GetTodayRangeTime() (time.Time, time.Time) {
now := time.Now()
startDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
endDate := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
return startDate, endDate
}
// GetLastMonthRangeTime 获取区间时间 - 上个月
func GetLastMonthRangeTime() (time.Time, time.Time) {
now := time.Now().AddDate(0, -1, 0)
firstDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
lastDay := firstDay.AddDate(0, 1, -1)
endDate := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), 23, 59, 59, 0, now.Location())
return firstDay, endDate
}
// GetCurrentMonthRangeTime 获取区间时间 - 本月
func GetCurrentMonthRangeTime() (time.Time, time.Time) {
now := time.Now()
firstDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
lastDay := firstDay.AddDate(0, 1, -1)
endDate := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), 23, 59, 59, 0, now.Location())
return firstDay, endDate
}
// GetCurrentYearRangeTime 获取区间时间 - 今年
func GetCurrentYearRangeTime() (time.Time, time.Time) {
now := time.Now()
firstDay := time.Date(now.Year(), time.January, 1, 0, 0, 0, 0, now.Location())
lastDay := firstDay.AddDate(1, 0, -1)
endDate := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), 23, 59, 59, 0, now.Location())
return firstDay, endDate
}
// GetLastYearRangeTime 获取区间时间 - 去年
func GetLastYearRangeTime() (time.Time, time.Time) {
now := time.Now().AddDate(-1, 0, 0)
firstDay := time.Date(now.Year(), time.January, 1, 0, 0, 0, 0, now.Location())
lastDay := firstDay.AddDate(1, 0, -1)
endDate := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), 23, 59, 59, 0, now.Location())
return firstDay, endDate
}
// GetTodayRangeDateString 获取区间时间内的日期字符串比如2023-05-23 2023-05-23 - 今天
func GetTodayRangeDateString() (string, string) {
firstDay, lastDay := GetTodayRangeTime()
startDate := firstDay.Format(DateLayout)
endDate := lastDay.Format(DateLayout)
return startDate, endDate
}
// GetYesterdayRangeDateString 获取区间时间内的日期字符串比如2023-05-23 2023-05-23 - 昨天
func GetYesterdayRangeDateString() (string, string) {
firstDay, lastDay := GetYesterdayRangeTime()
startDate := firstDay.Format(DateLayout)
endDate := lastDay.Format(DateLayout)
return startDate, endDate
}
// GetCurrentMonthRangeDateString 获取区间时间内的日期字符串比如2023-05-23 2023-05-23 - 本月
func GetCurrentMonthRangeDateString() (string, string) {
firstDay, lastDay := GetCurrentMonthRangeTime()
startDate := firstDay.Format(DateLayout)
endDate := lastDay.Format(DateLayout)
return startDate, endDate
}
// GetLastMonthRangeDateString 获取区间时间内的日期字符串比如2023-05-23 2023-05-23 - 上个月
func GetLastMonthRangeDateString() (string, string) {
firstDay, lastDay := GetLastMonthRangeTime()
startDate := firstDay.Format(DateLayout)
endDate := lastDay.Format(DateLayout)
return startDate, endDate
}
// GetCurrentYearRangeDateString 获取区间时间内的日期字符串比如2023-05-23 2023-05-23 - 今年
func GetCurrentYearRangeDateString() (string, string) {
firstDay, lastDay := GetCurrentYearRangeTime()
startDate := firstDay.Format(DateLayout)
endDate := lastDay.Format(DateLayout)
return startDate, endDate
}
// GetLastYearRangeDateString 获取区间时间内的日期字符串比如2023-05-23 2023-05-23 - 去年
func GetLastYearRangeDateString() (string, string) {
firstDay, lastDay := GetLastYearRangeTime()
startDate := firstDay.Format(DateLayout)
endDate := lastDay.Format(DateLayout)
return startDate, endDate
}
// GetYesterdayRangeTimeString 获取区间时间内的时间字符串比如2023-05-23 00:00:00 2023-05-23 23:59:59 - 昨天
func GetYesterdayRangeTimeString() (string, string) {
firstDay, lastDay := GetYesterdayRangeTime()
startDate := firstDay.Format(TimeLayout)
endDate := lastDay.Format(TimeLayout)
return startDate, endDate
}
// GetTodayRangeTimeString 获取区间时间内的时间字符串比如2023-05-23 00:00:00 2023-05-23 23:59:59 - 今天
func GetTodayRangeTimeString() (string, string) {
firstDay, lastDay := GetTodayRangeTime()
startDate := firstDay.Format(TimeLayout)
endDate := lastDay.Format(TimeLayout)
return startDate, endDate
}
// GetLastMonthRangeTimeString 获取区间时间内的时间字符串比如2023-05-23 00:00:00 2023-05-23 23:59:59 - 上个月
func GetLastMonthRangeTimeString() (string, string) {
firstDay, lastDay := GetLastMonthRangeTime()
startDate := firstDay.Format(TimeLayout)
endDate := lastDay.Format(TimeLayout)
return startDate, endDate
}
// GetCurrentMonthRangeTimeString 获取区间时间内的时间字符串比如2023-05-23 00:00:00 2023-05-23 23:59:59 - 本月
func GetCurrentMonthRangeTimeString() (string, string) {
firstDay, lastDay := GetCurrentMonthRangeTime()
startDate := firstDay.Format(TimeLayout)
endDate := lastDay.Format(TimeLayout)
return startDate, endDate
}
// GetLastYearRangeTimeString 获取区间时间内的时间字符串比如2023-05-23 00:00:00 2023-05-23 23:59:59 - 去年
func GetLastYearRangeTimeString() (string, string) {
firstDay, lastDay := GetLastYearRangeTime()
startDate := firstDay.Format(TimeLayout)
endDate := lastDay.Format(TimeLayout)
return startDate, endDate
}
// GetCurrentYearRangeTimeString 获取区间时间内的时间字符串比如2023-05-23 00:00:00 2023-05-23 23:59:59 - 今年
func GetCurrentYearRangeTimeString() (string, string) {
firstDay, lastDay := GetCurrentYearRangeTime()
startDate := firstDay.Format(TimeLayout)
endDate := lastDay.Format(TimeLayout)
return startDate, endDate
}

36
timeutil/range_test.go Normal file
View File

@@ -0,0 +1,36 @@
package util
import (
"fmt"
"testing"
)
func TestGetCurrentTimeRangeDateString(t *testing.T) {
fmt.Println(GetTodayRangeDateString())
fmt.Println(GetCurrentMonthRangeDateString())
fmt.Println(GetCurrentYearRangeDateString())
fmt.Println(GetYesterdayRangeDateString())
fmt.Println(GetLastMonthRangeDateString())
fmt.Println(GetLastYearRangeDateString())
}
func TestGetCurrentRangeTime(t *testing.T) {
fmt.Println(GetTodayRangeTime())
fmt.Println(GetCurrentMonthRangeTime())
fmt.Println(GetCurrentYearRangeTime())
fmt.Println(GetYesterdayRangeTime())
fmt.Println(GetLastMonthRangeTime())
fmt.Println(GetLastYearRangeTime())
}
func TestGetCurrentTimeRangeTimeString(t *testing.T) {
fmt.Println(GetTodayRangeTimeString())
fmt.Println(GetCurrentMonthRangeTimeString())
fmt.Println(GetCurrentYearRangeTimeString())
fmt.Println(GetYesterdayRangeTimeString())
fmt.Println(GetLastMonthRangeTimeString())
fmt.Println(GetLastYearRangeTimeString())
}

116
timeutil/trans.go Normal file
View File

@@ -0,0 +1,116 @@
package util
import (
"time"
"github.com/tx7do/go-utils/trans"
)
var DefaultTimeLocation *time.Location
func RefreshDefaultTimeLocation(name string) {
DefaultTimeLocation, _ = time.LoadLocation(name)
}
// UnixMilliToStringPtr 毫秒时间戳 -> 字符串
func UnixMilliToStringPtr(tm *int64) *string {
if tm == nil {
return nil
}
str := time.UnixMilli(*tm).Format(TimeLayout)
return &str
}
// StringToUnixMilliInt64Ptr 字符串 -> 毫秒时间戳
func StringToUnixMilliInt64Ptr(tm *string) *int64 {
theTime := StringTimeToTime(tm)
if theTime == nil {
return nil
}
unixTime := theTime.UnixMilli()
return &unixTime
}
// StringTimeToTime 时间字符串 -> 时间
func StringTimeToTime(str *string) *time.Time {
if str == nil {
return nil
}
if len(*str) == 0 {
return nil
}
if DefaultTimeLocation == nil {
RefreshDefaultTimeLocation(DefaultTimeLocationName)
}
var err error
var theTime time.Time
theTime, err = time.ParseInLocation(TimeLayout, *str, DefaultTimeLocation)
if err == nil {
return &theTime
}
theTime, err = time.ParseInLocation(DateLayout, *str, DefaultTimeLocation)
if err == nil {
return &theTime
}
theTime, err = time.ParseInLocation(ClockLayout, *str, DefaultTimeLocation)
if err == nil {
return &theTime
}
return nil
}
// TimeToTimeString 时间 -> 时间字符串
func TimeToTimeString(tm *time.Time) *string {
if tm == nil {
return nil
}
return trans.String(tm.Format(TimeLayout))
}
// StringDateToTime 字符串 -> 时间
func StringDateToTime(str *string) *time.Time {
if str == nil {
return nil
}
if len(*str) == 0 {
return nil
}
if DefaultTimeLocation == nil {
RefreshDefaultTimeLocation(DefaultTimeLocationName)
}
var err error
var theTime time.Time
theTime, err = time.ParseInLocation(TimeLayout, *str, DefaultTimeLocation)
if err == nil {
return &theTime
}
theTime, err = time.ParseInLocation(DateLayout, *str, DefaultTimeLocation)
if err == nil {
return &theTime
}
theTime, err = time.ParseInLocation(ClockLayout, *str, DefaultTimeLocation)
if err == nil {
return &theTime
}
return nil
}
// TimeToDateString 时间 -> 日期字符串
func TimeToDateString(tm *time.Time) *string {
if tm == nil {
return nil
}
return trans.String(tm.Format(DateLayout))
}

35
timeutil/trans_test.go Normal file
View File

@@ -0,0 +1,35 @@
package util
import (
"fmt"
"testing"
"time"
"github.com/tx7do/go-utils/trans"
)
func TestUnixMilliToStringPtr(t *testing.T) {
now := time.Now().UnixMilli()
str := UnixMilliToStringPtr(&now)
fmt.Println(now)
fmt.Println(*str)
fmt.Println(*UnixMilliToStringPtr(trans.Int64(1677135885288)))
fmt.Println(*UnixMilliToStringPtr(trans.Int64(1677647430853)))
fmt.Println(*UnixMilliToStringPtr(trans.Int64(1677647946234)))
fmt.Println(*UnixMilliToStringPtr(trans.Int64(1678245350773)))
fmt.Println("START: ", *StringToUnixMilliInt64Ptr(trans.String("2023-03-09 00:00:00")))
fmt.Println("END: ", *StringToUnixMilliInt64Ptr(trans.String("2023-03-09 23:59:59")))
fmt.Println(StringTimeToTime(trans.String("2023-03-01 00:00:00")))
fmt.Println(*StringDateToTime(trans.String("2023-03-01")))
fmt.Println(StringTimeToTime(trans.String("2023-03-08 00:00:00")).UnixMilli())
fmt.Println(StringDateToTime(trans.String("2023-03-07")).UnixMilli())
}
func TestTimeToDateString(t *testing.T) {
fmt.Println(*TimeToTimeString(trans.Time(time.Now())))
fmt.Println(*TimeToDateString(trans.Time(time.Now())))
}