forked from dagedaniao/AutoDnsPodScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
60 lines (54 loc) · 1.35 KB
/
time.go
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
package main
import (
"time"
"errors"
"log"
)
func IsHourOk(hour int) bool {
return hour >= 0 && hour <= 24
}
func IsMinOk(min int) bool {
return min >= 0 && min < 60
}
func IsSecOk(sec int) bool {
return sec >= 0 && sec < 60
}
func GetDurtionTime(hour, min, sec int) (duration time.Duration, err error) {
if !IsHourOk(hour) || !IsMinOk(min) || !IsSecOk(sec) {
err = errors.New("time value is invalid!")
log.Println("H:M:S", hour, min, sec)
return
}
const baseTimeFormat = "2006-01-02 15:04:05"
t := time.Now()
year, month, day := t.Year(), t.Month(), t.Day()
if hour == 24 || hour == 0 {
hour = 0
day += 1
}
tm1 := time.Date(year, month, day, hour, min, sec, 0, t.Location())
duration = tm1.Sub(t)
log.Println("Now: Target, durtionTime!", t, tm1, duration)
return
}
func IsScaleIn(start, end TimeFormat) (ok bool, err error) {
startD, err := GetDurtionTime(start.Hour, start.Min, start.Sec)
if err != nil {
log.Println("IsScale: startTime Err!", start, err)
return
}
endD, err := GetDurtionTime(end.Hour, end.Min, end.Sec)
if err != nil {
log.Println("IsScale: endTime Err!", end, err)
return
}
if startD < 0 && endD < 0 {
err = errors.New("Need tomorrow to scale!")
return
}
if (endD < 0 && startD > 0) || (endD >=0 && startD >= 0 && endD > startD) {
ok = true
return
}
return
}