forked from yyoshiki41/go-radiko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
48 lines (39 loc) · 1020 Bytes
/
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
package util
import "time"
const (
dateLayout = "20060102"
datetimeLayout = "20060102150405"
// Always use Asia/Tokyo timezone.
tz = "Asia/Tokyo"
)
var location *time.Location
func init() {
var err error
location, err = time.LoadLocation(tz)
if err != nil {
// panic happens in init function.
panic(err)
}
}
// Date returns a textual representation of the time value
// formatted in dateLayout.
func Date(t time.Time) string {
localTime := t.In(location)
return localTime.Format(dateLayout)
}
// Datetime returns a textual representation of the time value
// formatted in datetimeLayout.
func Datetime(t time.Time) string {
localTime := t.In(location)
return localTime.Format(datetimeLayout)
}
// ProgramsDate returns a textual representation of the time value
// formatted in dateLayout.
func ProgramsDate(t time.Time) string {
localTime := t.In(location)
h := localTime.Hour()
if h >= 0 && h <= 4 {
localTime = localTime.Add(-24 * time.Hour)
}
return localTime.Format(dateLayout)
}