forked from venliong/webby
/
time.go
69 lines (56 loc) · 1.29 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
61
62
63
64
65
66
67
68
69
package webby
import (
"time"
)
// Default Time Location
var DefaultTimeLoc *time.Location
// Default Time Format
var DefaultTimeFormat string = "Monday, _2 January 2006, 15:04"
// Set Timezone on global level
func SetTimeZone(zone string) {
var err error
DefaultTimeLoc, err = time.LoadLocation(zone)
Check(err)
}
// Get Current Time
func CurTime() time.Time {
return time.Now()
}
type Time struct {
w *Web
}
func (w *Web) Time() Time {
return Time{w}
}
// Set Timezone on user request level
func (t Time) SetZone(zone string) {
var err error
t.w.TimeLoc, err = time.LoadLocation(zone)
t.w.Check(err)
}
// Get Current Time
func (t Time) Now() time.Time {
return CurTime()
}
func init() {
DefaultTimeLoc, _ = time.LoadLocation("Local")
HtmlFuncBoot.Register(func(w *Web) {
// Convert to Default Timezone.
w.HtmlFunc["time"] = func(clock time.Time) time.Time {
return clock.In(w.TimeLoc)
}
// Convert to Timezone
w.HtmlFunc["timeZone"] = func(zone string, clock time.Time) time.Time {
loc, err := time.LoadLocation(zone)
w.Check(err)
return clock.In(loc)
}
// Format time, leave empty for default
w.HtmlFunc["timeFormat"] = func(format string, clock time.Time) string {
if format == "" {
format = w.TimeFormat
}
return clock.Format(format)
}
})
}