feat: time utils.
This commit is contained in:
175
time/range.go
Normal file
175
time/range.go
Normal 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
time/range_test.go
Normal file
36
time/range_test.go
Normal 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())
|
||||
}
|
||||
79
time/trans.go
Normal file
79
time/trans.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/tx7do/kratos-utils/trans"
|
||||
)
|
||||
|
||||
const (
|
||||
TimeLayout = "2006-01-02 15:04:05"
|
||||
DateLayout = "2006-01-02"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
theTime, err := time.ParseInLocation(TimeLayout, *str, loc)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &theTime
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
theTime, err := time.ParseInLocation(DateLayout, *str, loc)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &theTime
|
||||
}
|
||||
|
||||
// TimeToDateString 时间 -> 日期字符串
|
||||
func TimeToDateString(tm *time.Time) *string {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
return trans.String(tm.Format(DateLayout))
|
||||
}
|
||||
35
time/trans_test.go
Normal file
35
time/trans_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tx7do/kratos-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())))
|
||||
}
|
||||
139
time/util.go
139
time/util.go
@@ -1,139 +0,0 @@
|
||||
package util
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
TimeLayout = "2006-01-02 15:04:05"
|
||||
DateLayout = "2006-01-02"
|
||||
)
|
||||
|
||||
// UnixMilliToStringPtr 毫秒时间戳转字符串
|
||||
func UnixMilliToStringPtr(tm *int64) *string {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
str := time.UnixMilli(*tm).Format(TimeLayout)
|
||||
return &str
|
||||
}
|
||||
|
||||
// StringTimeToTime 字符串转时间
|
||||
func StringTimeToTime(tm *string) *time.Time {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
theTime, err := time.ParseInLocation(TimeLayout, *tm, loc)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &theTime
|
||||
}
|
||||
|
||||
// StringDateToTime 字符串转时间
|
||||
func StringDateToTime(tm *string) *time.Time {
|
||||
if tm == nil {
|
||||
return nil
|
||||
}
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
theTime, err := time.ParseInLocation(DateLayout, *tm, loc)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &theTime
|
||||
}
|
||||
|
||||
// StringToUnixMilliInt64Ptr 字符串转毫秒时间戳
|
||||
func StringToUnixMilliInt64Ptr(tm *string) *int64 {
|
||||
theTime := StringTimeToTime(tm)
|
||||
if theTime == nil {
|
||||
return nil
|
||||
}
|
||||
unixTime := theTime.UnixMilli()
|
||||
return &unixTime
|
||||
}
|
||||
|
||||
// GetCurrentDayTime 获取今天的时间
|
||||
func GetCurrentDayTime() (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
|
||||
}
|
||||
|
||||
// GetCurrentMonthTime 获取本月的时间
|
||||
func GetCurrentMonthTime() (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
|
||||
}
|
||||
|
||||
// GetCurrentYearTime 获取今年的时间
|
||||
func GetCurrentYearTime() (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
|
||||
}
|
||||
|
||||
// GetCurrentDayDateString 获取今天的日期字符串
|
||||
func GetCurrentDayDateString() (string, string) {
|
||||
firstDay, lastDay := GetCurrentDayTime()
|
||||
|
||||
startDate := firstDay.Format(DateLayout)
|
||||
endDate := lastDay.Format(DateLayout)
|
||||
|
||||
return startDate, endDate
|
||||
}
|
||||
|
||||
// GetCurrentMonthDateString 获取本月的日期字符串
|
||||
func GetCurrentMonthDateString() (string, string) {
|
||||
firstDay, lastDay := GetCurrentMonthTime()
|
||||
|
||||
startDate := firstDay.Format(DateLayout)
|
||||
endDate := lastDay.Format(DateLayout)
|
||||
|
||||
return startDate, endDate
|
||||
}
|
||||
|
||||
// GetCurrentYearDateString 获取今年的日期字符串
|
||||
func GetCurrentYearDateString() (string, string) {
|
||||
firstDay, lastDay := GetCurrentYearTime()
|
||||
|
||||
startDate := firstDay.Format(DateLayout)
|
||||
endDate := lastDay.Format(DateLayout)
|
||||
|
||||
return startDate, endDate
|
||||
}
|
||||
|
||||
// GetCurrentDayTimeString 获取今天的时间字符串
|
||||
func GetCurrentDayTimeString() (string, string) {
|
||||
firstDay, lastDay := GetCurrentDayTime()
|
||||
|
||||
startDate := firstDay.Format(TimeLayout)
|
||||
endDate := lastDay.Format(TimeLayout)
|
||||
|
||||
return startDate, endDate
|
||||
}
|
||||
|
||||
// GetCurrentMonthTimeString 获取本月的时间字符串
|
||||
func GetCurrentMonthTimeString() (string, string) {
|
||||
firstDay, lastDay := GetCurrentMonthTime()
|
||||
|
||||
startDate := firstDay.Format(TimeLayout)
|
||||
endDate := lastDay.Format(TimeLayout)
|
||||
|
||||
return startDate, endDate
|
||||
}
|
||||
|
||||
// GetCurrentYearTimeString 获取今年的时间字符串
|
||||
func GetCurrentYearTimeString() (string, string) {
|
||||
firstDay, lastDay := GetCurrentYearTime()
|
||||
|
||||
startDate := firstDay.Format(TimeLayout)
|
||||
endDate := lastDay.Format(TimeLayout)
|
||||
|
||||
return startDate, endDate
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tx7do/kratos-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(*StringToUnixMilliInt64Ptr(trans.String("2023-03-01 00:00:00")))
|
||||
|
||||
fmt.Println(*StringTimeToTime(trans.String("2023-03-01 00:00:00")))
|
||||
fmt.Println(*StringDateToTime(trans.String("2023-03-01")))
|
||||
}
|
||||
|
||||
func TestGetCurrentDateString(t *testing.T) {
|
||||
fmt.Println(GetCurrentDayDateString())
|
||||
fmt.Println(GetCurrentMonthDateString())
|
||||
fmt.Println(GetCurrentYearDateString())
|
||||
}
|
||||
|
||||
func TestGetCurrentTime(t *testing.T) {
|
||||
fmt.Println(GetCurrentDayTime())
|
||||
fmt.Println(GetCurrentMonthTime())
|
||||
fmt.Println(GetCurrentYearTime())
|
||||
}
|
||||
|
||||
func TestGetCurrentTimeString(t *testing.T) {
|
||||
fmt.Println(GetCurrentDayTimeString())
|
||||
fmt.Println(GetCurrentMonthTimeString())
|
||||
fmt.Println(GetCurrentYearTimeString())
|
||||
}
|
||||
Reference in New Issue
Block a user