-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_helper.go
49 lines (39 loc) · 1.03 KB
/
time_helper.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
package goutils
import (
"fmt"
"strconv"
"time"
)
// Compare to the offical time.Parse, this ignore the error message and
// return a blank time.Time{} object when value has something wrong
func ParseTime(layout, value string) time.Time {
if value == "" {
return time.Time{}
}
r, err := time.Parse(layout, value)
if err != nil {
PrintStackAndError(err)
return time.Time{}
}
return r
}
// Compare to the offical time.Format, it will return blank string when
// the time is zero, rather than return "0001-01-01 00:00"
func FormatTime(theTime time.Time, layout string) (r string) {
if theTime.IsZero() {
return ""
}
return theTime.Format(layout)
}
// Millisecond e.g. 1445485125599
func MillisecondToTime(ms string) (theTime time.Time, err error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if HasErrorAndPrintStack(err) {
return
}
theTime = time.Unix(0, msInt*int64(time.Millisecond))
return
}
func TimeToMillisecond(theTime time.Time) string {
return fmt.Sprintf("%d", theTime.UnixNano()/int64(time.Millisecond))
}