-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
ctx.go
75 lines (60 loc) · 1.16 KB
/
ctx.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
70
71
72
73
74
75
package dtfmt
import "time"
// ctx stores pre-computed time fields used by the formatter.
type ctx struct {
year int
month time.Month
day int
weekday time.Weekday
yearday int
isoWeek, isoYear int
hour, min, sec int
millis int
buf []byte
}
type ctxConfig struct {
date bool
clock bool
weekday bool
yearday bool
millis bool
iso bool
}
func (c *ctx) initTime(config *ctxConfig, t time.Time) {
if config.date {
c.year, c.month, c.day = t.Date()
}
if config.clock {
c.hour, c.min, c.sec = t.Clock()
}
if config.iso {
c.isoYear, c.isoWeek = t.ISOWeek()
}
if config.millis {
c.millis = t.Nanosecond() / 1000000
}
if config.yearday {
c.yearday = t.YearDay()
}
if config.weekday {
c.weekday = t.Weekday()
}
}
func (c *ctxConfig) enableDate() {
c.date = true
}
func (c *ctxConfig) enableClock() {
c.clock = true
}
func (c *ctxConfig) enableWeekday() {
c.weekday = true
}
func (c *ctxConfig) enableYearday() {
c.yearday = true
}
func (c *ctxConfig) enableISO() {
c.iso = true
}
func isLeap(year int) bool {
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
}