Commit 60a78f8eefd8893e151e34cf976f22981d91ec85

Authored by 王家文
1 parent efcb0240
Exists in master

feat✨:时间公共库

Showing 2 changed files with 39 additions and 96 deletions   Show diff stats
util/ztime/same.go 0 → 100644
@@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
  1 +package ztime
  2 +
  3 +import "time"
  4 +
  5 +// SameMonth
  6 +// @Description: 判断同一月
  7 +// @param dt1
  8 +// @param dt2
  9 +// @return bool
  10 +func SameMonth(dt1, dt2 time.Time) bool {
  11 + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month()
  12 +}
  13 +
  14 +// SameDay
  15 +// @Description: 判断同一天
  16 +// @param dt1
  17 +// @param dt2
  18 +// @return bool
  19 +func SameDay(dt1, dt2 time.Time) bool {
  20 + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day()
  21 +}
  22 +
  23 +// SameHour
  24 +// @Description: 判断同一小时
  25 +// @param dt1
  26 +// @param dt2
  27 +// @return bool
  28 +func SameHour(dt1, dt2 time.Time) bool {
  29 + return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() && dt1.Hour() == dt2.Hour()
  30 +}
util/ztime/time.go
@@ -18,112 +18,25 @@ func UnixMilli() int64 { @@ -18,112 +18,25 @@ func UnixMilli() int64 {
18 return time.Now().UnixMilli() 18 return time.Now().UnixMilli()
19 } 19 }
20 20
21 -// TimeToDateString  
22 -// @Description: 时间转日期字符串  
23 -// @param dt  
24 -// @return string  
25 -func TimeToDateString(dt time.Time) string {  
26 - return dt.Format(DtFormatDate)  
27 -}  
28 -  
29 -// TimeToTimeString  
30 -// @Description: 时间转时间字符串  
31 -// @param dt  
32 -// @return string  
33 -func TimeToTimeString(dt time.Time) string {  
34 - return dt.Format(DtFormatTime)  
35 -}  
36 -  
37 -// TimeToMonthString  
38 -// @Description: 时间转月份字符串  
39 -// @param dt  
40 -// @return string  
41 -func TimeToMonthString(dt time.Time) string {  
42 - return dt.Format(DtFormatMonth)  
43 -}  
44 -  
45 -// TimeToMonth  
46 -// @Description: 时间转月份字符串  
47 -// @param dt  
48 -// @return string  
49 -func TimeToMonth(dt time.Time) int {  
50 - return dt.Year()*100 + int(dt.Month())  
51 -}  
52 -  
53 -// DateStringToTimeE  
54 -// @Description: 日期字符串转时间  
55 -// @param dt  
56 -// @return time.Time  
57 -// @return error  
58 -func DateStringToTimeE(dt string) (time.Time, error) {  
59 - return time.Parse(DtFormatDate, dt)  
60 -}  
61 -  
62 -// DateStringToTime  
63 -// @Description: 时间字符串转时间  
64 -// @param dt  
65 -// @return time.Time  
66 -// @return error  
67 -func DateStringToTime(dt string) time.Time {  
68 - if t, err := time.Parse(DtFormatDate, dt); err == nil { 21 +// ToTime 标准字符串转UTC时间 "2006-01-02 15:04:05"
  22 +func ToTime(dt string) time.Time {
  23 + if t, err := time.Parse(time.DateTime, dt); err == nil {
69 return t 24 return t
70 } else { 25 } else {
71 return time.Now() 26 return time.Now()
72 } 27 }
73 } 28 }
74 29
75 -// TimeStringToTimeE  
76 -// @Description: 时间字符串转时间  
77 -// @param dt  
78 -// @return time.Time  
79 -// @return error  
80 -func TimeStringToTimeE(dt string) (time.Time, error) {  
81 - return time.Parse(DtFormatTime, dt)  
82 -}  
83 -  
84 -// TimeStringToTime  
85 -// @Description: 时间字符串转时间  
86 -// @param dt  
87 -// @return time.Time  
88 -// @return error  
89 -func TimeStringToTime(dt string) time.Time {  
90 - if t, err := time.Parse(DtFormatTime, dt); err == nil { 30 +// ToTimeLocal 标准字符串转本地时间 "2006-01-02 15:04:05"
  31 +func ToTimeLocal(dt string) time.Time {
  32 + if t, err := time.ParseInLocation(time.DateTime, dt, time.Local); err == nil {
91 return t 33 return t
92 } else { 34 } else {
93 return time.Now() 35 return time.Now()
94 } 36 }
95 } 37 }
96 38
97 -// TimeTextInt64 把时间转换成数字字符串 比如 20240510113130  
98 -func TimeTextInt64(dt time.Time) int64 {  
99 - date := int64(dt.Year())*1_0000 + int64(dt.Month())*100 + int64(dt.Day())  
100 - t := int64(dt.Hour())*1_0000 + int64(dt.Minute())*100 + int64(dt.Second())  
101 - return date*1_000000 + t  
102 -}  
103 -  
104 -// SameMonth  
105 -// @Description: 判断同一月  
106 -// @param dt1  
107 -// @param dt2  
108 -// @return bool  
109 -func SameMonth(dt1, dt2 time.Time) bool {  
110 - return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month()  
111 -}  
112 -  
113 -// SameDay  
114 -// @Description: 判断同一天  
115 -// @param dt1  
116 -// @param dt2  
117 -// @return bool  
118 -func SameDay(dt1, dt2 time.Time) bool {  
119 - return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day()  
120 -}  
121 -  
122 -// SameHour  
123 -// @Description: 判断同一小时  
124 -// @param dt1  
125 -// @param dt2  
126 -// @return bool  
127 -func SameHour(dt1, dt2 time.Time) bool {  
128 - return dt1.Year() == dt2.Year() && dt1.Month() == dt2.Month() && dt1.Day() == dt2.Day() && dt1.Hour() == dt2.Hour() 39 +// ToString 时间转标准字符串 "2006-01-02 15:04:05"
  40 +func ToString(dt time.Time) string {
  41 + return dt.Format(time.DateTime)
129 } 42 }