-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathzone.go
99 lines (88 loc) · 2.4 KB
/
zone.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* This file is part of tz.
*
* tz is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* tz is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with tz. If not, see <https://www.gnu.org/licenses/>.
**/
package main
import (
"fmt"
"time"
)
var now = time.Now()
var name, _ = now.Zone()
var DefaultZones = []*Zone{
{
Loc: now.Location(),
Name: "Local",
DbName: name,
},
{
Loc: time.UTC,
Name: "UTC",
DbName: "UTC",
},
}
// EmojiClocks maps hour to the corresponding clock emoji
var EmojiClocks = map[int]string{
0: "🕛",
1: "🕐",
2: "🕑",
3: "🕒",
4: "🕓",
5: "🕔",
6: "🕕",
7: "🕖",
8: "🕗",
9: "🕘",
10: "🕙",
11: "🕙",
}
// Zone stores the name of a time zone
type Zone struct {
Loc *time.Location
DbName string // Name in tzdata
Name string // Preferred name (user-provided, or else DbName by default)
}
func (z Zone) String() string {
return z.Name
}
func (z Zone) VerboseString(t time.Time) string {
return fmt.Sprintf("(%s) %s", z.Abbreviation(t), z.Name)
}
// Abbreviated short name for the zone (e.g. acronym "ABC" if available, or else a number like "-3").
// It depends dynamically on the daylight saving policy in the zone at time `t`.
func (z Zone) Abbreviation(t time.Time) string {
shortName, _ := z.currentTime(t).Zone()
return shortName
}
// ClockEmoji returns the corresponding emoji clock for a given hour
func (z Zone) ClockEmoji(t time.Time) string {
h := ((z.currentTime(t).Hour() % 12) + 12) % 12
return EmojiClocks[h]
}
// ShortDT returns the current time in short format.
func (z Zone) ShortDT(t time.Time) string {
return z.currentTime(t).Format("3:04PM, Mon Jan 02, 2006")
}
// ShortMT returns the current military time in short format.
func (z Zone) ShortMT(t time.Time) string {
return z.currentTime(t).Format("15:04, Mon Jan 02, 2006")
}
func (z Zone) currentTime(t time.Time) time.Time {
zName, _ := t.Zone()
if z.DbName != zName {
return t.In(z.Loc)
}
return t
}