-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
376 lines (332 loc) · 10.1 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package internationalcolortime
import (
"fmt"
"time"
)
type Color int
const hourNanoseconds = 3600000000000
const secondNanoseconds = 1000000000
const ICTStandardShortName = "PNK:04:05.999999999"
const ICTStandardLongName = "Pink:04:05.999999999"
const (
_ = iota
stdLongColor
stdShortColor
)
// Colors are arranged according to the order they run in UTC.
const (
Red Color = iota
Brick
Orange
Tangerine
Mustard
Yellow
Pear
Sage
Mint
Lime
Green
Pine
Grey
Aqua
Teal
Denim
Blue
Navy
Indigo
Purple
Lavender
Maroon
Pink
Rose
Count
)
var colorToString = []string{
Red: "Red",
Brick: "Brick",
Orange: "Orange",
Tangerine: "Tangerine",
Mustard: "Mustard",
Yellow: "Yellow",
Pear: "Pear",
Sage: "Sage",
Mint: "Mint",
Lime: "Lime",
Green: "Green",
Pine: "Pine",
Grey: "Grey",
Aqua: "Aqua",
Teal: "Teal",
Denim: "Denim",
Blue: "Blue",
Navy: "Navy",
Indigo: "Indigo",
Purple: "Purple",
Lavender: "Lavender",
Maroon: "Maroon",
Pink: "Pink",
Rose: "Rose",
}
var colorToStringShort = []string{
Red: "RED",
Brick: "BRK",
Orange: "ORG",
Tangerine: "TNG",
Mustard: "MRD",
Yellow: "YLW",
Pear: "PER",
Sage: "SAG",
Mint: "MNT",
Lime: "LIM",
Green: "GRN",
Pine: "PIN",
Grey: "GRY",
Aqua: "AQA",
Teal: "TEL",
Denim: "DNM",
Blue: "BLU",
Navy: "NVY",
Indigo: "ING",
Purple: "PPL",
Lavender: "LVR",
Maroon: "MRN",
Pink: "PNK",
Rose: "ROS",
}
var stringToColor = make(map[string]Color, len(colorToString))
var stringShortToColor = make(map[string]Color, len(colorToStringShort))
func init() {
//create stringToColor map
for k, v := range colorToString {
stringToColor[v] = Color(k)
}
//create stringShortToColor map
for k, v := range colorToStringShort {
stringShortToColor[v] = Color(k)
}
}
// InternationalColorTime represents a time on the International Color Time clock. An ICT value does not contain any
// date information, only time, and as such there are no equivalent After() or Before() functions like there are in the
// standard time package, since they cannot be calculated with certainty.
type InternationalColorTime struct {
hour Color
nanos int64 //nanoseconds past the hour
}
// TimeToICT will convert a time t into an InternationalColorTime.
func TimeToICT(t time.Time) InternationalColorTime {
return ColorTime(Color(t.In(time.UTC).Hour()), t.Minute(), t.Second(), t.Nanosecond())
}
// ColorTime will return the color time struct for a given hour, minute, sec, and nanosecond.
func ColorTime(hour Color, min, sec, nsec int) InternationalColorTime {
return InternationalColorTime{}.add(int(hour), min, sec, nsec)
}
// Now returns the current color time.
func Now() InternationalColorTime {
return TimeToICT(time.Now())
}
// Parse a InternationalColorTime from value using a given layout string. This follows the rules laid out by the time.Time
// library. If the specific reference time is
// Mon Jan 2 15:04:05 MST 2006
// then the corresponding InternationalColorTime reference time is
// PNK:04:05.999999999
// where PNK is the 3-letter code for your given ColorHour.
//func Parse(layout, value string) (InternationalColorTime, error) {
//
//}
// String implements the fmt.Stringer interface.
func (c Color) String() string {
if c >= Count || c < Red {
return ""
}
return colorToString[c]
}
// IsZero returns true if this is a default, uninitialized time.
//func (i InternationalColorTime) IsZero() bool {
//
//}
//
// In returns a time.Time with the equivalent timezone time as denoted by location. Since ICT has no date component,
// the local date value is used. For a more specific translocation, use InDate()
func (i InternationalColorTime) In(location *time.Location) time.Time {
t := time.Now()
return i.InDate(location, t.Year(), t.Month(), t.Day())
}
// InDate returns a time.Time with the equivalent timezone time as denoted by location, and the accompanying date as
// specified by the year/month/day params.
// todo maybe the date info should be the date as it refers to the UTC date, then translate that backwards/forwards as needed as well
func (i InternationalColorTime) InDate(location *time.Location, year int, month time.Month, day int) time.Time {
_, offset := time.Date(0, 0, 0, 0, 0, 0, 0, location).Zone()
//calculate how far from utc to move the time
//we know that ICT is a UTC time, so we just tick the nanos in a direction
numNanoSeconds := offset * secondNanoseconds
newi := i.add(0, 0, 0, numNanoSeconds)
fmt.Println(i.Hour())
t := time.Date(year, month, day, int(newi.Hour()), newi.Minute(), newi.Second(), newi.Nanosecond(), location)
return t
}
// add an amount of duration onto a color time
func (i InternationalColorTime) add(hour, min, sec, nsec int) InternationalColorTime {
ns := int64(nsec+(sec*1000000000)+(min*60*1000000000)) + i.nanos
nsHourIncrs := int(ns / hourNanoseconds)
var newHour = (int(i.hour) + hour + int(Count)) % int(Count)
if nsHourIncrs != 0 {
ns -= int64(nsHourIncrs * hourNanoseconds)
newHour = (newHour + nsHourIncrs) % (int(Count))
}
//check for negative ns
if ns < 0 {
ns += hourNanoseconds
//loop hour back around
newHour = ((newHour - 1) + (int(Count))) % (int(Count))
}
return InternationalColorTime{
hour: Color(newHour),
nanos: ns,
}
}
// Equal will compare two InternationalColorTimes to see if they are the same.
//func (i InternationalColorTime) Equal(i2 *InternationalColorTime) bool {
//
//}
//
// Add will tick i forward by duration.
func (i InternationalColorTime) Add(duration time.Duration) InternationalColorTime {
ns := duration.Nanoseconds()
return i.add(0, 0, 0, int(ns))
}
// Truncate returns the result of rounding t down to a multiple of d (since the zero time). Any value of d that is
// greater than one hour is reduced to one hour.
func (i InternationalColorTime) Truncate(d time.Duration) InternationalColorTime {
if d <= 0 {
return i
}
dur := time.Duration(i.nanos)
dur = dur - dur%d
i.nanos = int64(dur)
return i
}
// String returns the time formatted using the format string
// PNK:04:05.999999999
// which is the InternationalColorTime equivalent of the default time format found in the standard "time"
// package, 15:04:05.999999 -0700 MST.
func (i InternationalColorTime) String() string {
return i.Format(ICTStandardShortName)
}
// Hour returns the hour portion of an InternationalColorTime.
func (i InternationalColorTime) Hour() Color {
return i.hour
}
// Minute returns the minute portion of an InternationalColorTime.
func (i InternationalColorTime) Minute() int {
return int((i.nanos / 6e10) % 60)
}
// Second returns the second portion of an InternationalColorTime.
func (i InternationalColorTime) Second() int {
return int((i.nanos / 1e9) % 60)
}
// Nanosecond returns teh nanosecond portion of an InternationalColorTime.
func (i InternationalColorTime) Nanosecond() int {
return int(i.nanos % 1e9)
}
// Round i to the nearest interval expressed by d.
// A d = time.Hour will round to 0
func (i InternationalColorTime) Round(d time.Duration) InternationalColorTime {
if d <= 0 {
return i
}
dur := time.Duration(i.nanos)
i.nanos = int64(dur.Round(d))
return i
}
// Format i in the given format string. The formatting directive should include placeholder values from the
// InternationalColorTime reference string
// PNK:04:05.999999999
func (i InternationalColorTime) Format(layout string) string {
//let time handle most of the heavy lifting for us
layout = i.In(time.Local).Format(layout)
fmt.Println(layout)
outStr := ""
for layout != "" {
prefix, std, suffix := nextStdChunk(layout)
if prefix != "" {
outStr += prefix
}
if std == 0 {
break
}
layout = suffix
switch std {
case stdLongColor:
outStr += colorToString[i.hour]
case stdShortColor:
outStr += colorToStringShort[i.hour]
}
}
return outStr
}
func nextStdChunk(layout string) (prefix string, std int, suffix string) {
for i := 0; i < len(layout); i++ {
switch c := int(layout[i]); c {
case 'P': // PNK, Pink
if len(layout) > i+3 && layout[i:i+3] == "PNK" {
return layout[0:i], stdShortColor, layout[i+3:]
}
if len(layout) > i+4 && layout[i:i+4] == "Pink" {
return layout[0:i], stdLongColor, layout[i+4:]
}
}
}
return layout, 0, ""
}
// Clock returns the hour, minute, and second components of an InternationalColorTime.
func (i InternationalColorTime) Clock() (hour Color, min, sec int) {
return i.hour, i.Minute(), i.Second()
}
// GobEncode implements the encoding.GobEncoder interface.
//func (i InternationalColorTime) GobEncode() ([]byte, error) {
//
//}
//
// GobDecode implements the encoding.GobDecoder interface.
//func (i InternationalColorTime) GobDecode(data []byte) error {
//
//}
//
// Local will return the time.Time object that corresponds to the InternationalColorTime equivalent for your local system
// time zone. Since InternationalColorTime structs do not contain date components, the local date will be used.
//func (i InternationalColorTime) Local() time.Time {
//
//}
//
//// MarshalBinary implements the encoding.BinaryMarshaler interface.
//func (i InternationalColorTime) MarshalBinary() ([]byte, error) {
//
//}
//
//// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
//func (i *InternationalColorTime) UnmarshalBinary(data []byte) error {
//
//}
//
//// MarshalJSON implements the json.Marshaler interface. The time is a quoted string in RFC 3339 format, with sub-second
//// precision added if present.
//func (i InternationalColorTime) MarshalJSON() ([]byte, error) {
//
//}
//
//// UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339
//// format.
//func (i *InternationalColorTime) UnmarshalJSON(data []byte) error {
//
//}
//
//// MarshalText implements the encoding.TextMarshaler interface. The time is formatted in RFC 3339 format, with
//// sub-second precision added if present.
//func (i InternationalColorTime) MarshalText() ([]byte, error) {
//
//}
//
//// UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.
//func (i *InternationalColorTime) UnmarshalText(data []byte) error {
//
//}