-
Notifications
You must be signed in to change notification settings - Fork 191
/
util.go
56 lines (43 loc) · 1.83 KB
/
util.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
package timex
import (
"time"
"github.com/gookit/goutil/basefn"
)
// NowUnix is short of time.Now().Unix()
func NowUnix() int64 { return time.Now().Unix() }
// NowDate quick get current date string. if template is empty, will use DefaultTemplate.
func NowDate(template ...string) string {
return FormatByTpl(time.Now(), basefn.FirstOr(template, DefaultTemplate))
}
// Format convert time to string use default layout
func Format(t time.Time) string { return t.Format(DefaultLayout) }
// FormatBy given default layout
func FormatBy(t time.Time, layout string) string { return t.Format(layout) }
// Date format time by given date template. see ToLayout() for template parse.
func Date(t time.Time, template ...string) string { return Datetime(t, template...) }
// Datetime convert time to string use template. see ToLayout() for template parse.
func Datetime(t time.Time, template ...string) string {
return FormatByTpl(t, basefn.FirstOr(template, DefaultTemplate))
}
// DateFormat format time by given date template. see ToLayout()
func DateFormat(t time.Time, template string) string {
return FormatByTpl(t, template)
}
// FormatByTpl format time by given date template. see ToLayout()
func FormatByTpl(t time.Time, template string) string {
return t.Format(ToLayout(template))
}
// FormatUnix time seconds use default layout
func FormatUnix(sec int64, layout ...string) string {
return time.Unix(sec, 0).Format(basefn.FirstOr(layout, DefaultLayout))
}
// FormatUnixBy format time seconds use given layout
func FormatUnixBy(sec int64, layout string) string {
return time.Unix(sec, 0).Format(layout)
}
// FormatUnixByTpl format time seconds use given date template.
// see ToLayout()
func FormatUnixByTpl(sec int64, template ...string) string {
layout := ToLayout(basefn.FirstOr(template, DefaultTemplate))
return time.Unix(sec, 0).Format(layout)
}