yinbangzhong
2024-07-29 9f78e3b126b15a9b331c3a1a318da1ceea30114c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package timex
 
import (
    "speechAnalysis/constvar"
    "time"
)
 
func StringToTime(timeStr string) (time.Time, error) {
    return time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
}
 
func StringToTime2(timeStr string) (time.Time, error) {
    return time.ParseInLocation("2006-01-02", timeStr, time.Local)
}
 
func StringToTime3(timeStr string) (time.Time, error) {
    return time.ParseInLocation("2006-01-02 15:04", timeStr, time.Local)
}
 
func StringToClock(timeStr string) (time.Time, error) {
    return time.ParseInLocation("15:04", timeStr, time.Local)
}
 
func TimeToString(time time.Time) string {
    return time.Format("2006-01-02 15:04:05")
}
 
func TimeToString2(time time.Time) string {
    return time.Format("2006-01-02")
}
 
func TimeToString3(time time.Time) string {
    return time.Format("15:04")
}
 
func UnixTimeToString(_t int64) string {
    return time.Unix(_t, 0).In(time.Local).Format("2006-01-02 15:04:05")
}
func UnixTimeToDate(_t int64) string {
    return time.Unix(_t, 0).In(time.Local).Format("2006-01-02")
}
 
func DayStartTime(_t int64) int64 {
    l := time.Unix(_t, 0).In(time.Local)
    k := time.Date(l.Year(), l.Month(), l.Day(), 0, 0, 0, 0, time.Local).Unix()
    return k
}
 
func DayStartTimeDateStr(_t int64) string {
    l := time.Unix(_t, 0).In(time.Local)
    k := time.Date(l.Year(), l.Month(), l.Day(), 0, 0, 0, 0, time.Local)
    return TimeToString2(k)
}
 
const timeLayout = "2006-01-02 15:04:05"
 
func GetCurrentTime() string {
    return time.Now().Format(timeLayout)
}
 
func NextDateTimestamp(base time.Time, unit constvar.InspectCycleUnit, cycle int) time.Time {
    var t time.Time
    switch unit {
    case constvar.InspectCycleUnitWeek:
        t = base.AddDate(0, 0, cycle*7)
    case constvar.InspectCycleUnitMonth:
        t = base.AddDate(0, cycle, 0)
    case constvar.InspectCycleUnitDay:
        t = base.AddDate(0, 0, cycle)
    }
    return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
 
// Cycle2Seconds 周期换算成秒数
func Cycle2Seconds(unit constvar.InspectCycleUnit, cycle int) int {
    var s int
    switch unit {
    case constvar.InspectCycleUnitWeek:
        s = cycle * 86400 * 7
    case constvar.InspectCycleUnitMonth:
        s = cycle * 86400 * 30
    case constvar.InspectCycleUnitDay:
        s = cycle * 86400
    }
    return s
}