-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
calends.go
369 lines (309 loc) · 10.2 KB
/
calends.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
// Package calends is a library for handling dates and times across arbitrary
// calendar systems.
/*
Dates and times are converted to "TAI64NARUX instants", values that
unambiguously encode moments over 146 billion years into the past or future, in
increments as small as 10**-45 seconds (internally called a "xindectosecond", but
there's no actual prefix for units this small, even though the Planck Time - the
smallest meaningful period of time in quantum physics - is 54 of them).
Calculations and comparisons are done using these instants, to maintain the
highest accuracy possible while working with such values. A single Calends value
can hold either a single instant, or a time span between two of them; the
duration of such a value is automatically calculated during object creation, for
easy use elsewhere. When you need a version of the instant itself (either of
them, for a span) that you can use elsewhere, it is converted back to a
date/time string in whatever calendar system you need at the time.
Several calendar systems are supported officially, and the library is easily
extended to support others without having to modify the core in any way.
*/
package calends
import (
"encoding/json"
"fmt"
"io"
"math/big"
"strings"
"github.com/danhunsaker/calends/calendars"
"github.com/go-errors/errors"
)
// The Calends type is the core of the library, and the primary interface with
// it.
type Calends struct {
startTime calendars.TAI64NARUXTime
duration *big.Float
endTime calendars.TAI64NARUXTime
}
// Version of the library
var Version = "0.1.0"
// Create is the mechanism for constructing new Calends objects.
/*
It is preferred over using `make`, `new`, or `Calends{}` directly. It takes a
date/time value, a calendar name, and a format string, and returns a Calends
object representing the instant that date/time value took place. It also returns
an error object, if something goes wrong.
If the calendar passed is the empty string (`""`), Calends will use the
`"unix"` calendar automatically. If the format is the empty string, Calends
will use a default format provided by the calendar system itself.
The date/time value can be one of many types, and the exact list of types
supported varies by calendar system. At a minimum, string values should always
be supported. The documentation for each calendar system should provide more
detail on what other types are supported, and what ways the values can be
presented with each.
In any case, the date/time value can either be a single interface{} value, or a
map[string]interface{} containing two or three of them. The valid map keys are
'start', 'end', and 'duration'. Any combination of two will create the Calends
object with the associated time span. If all three are provided, the 'duration'
is ignored, and recalculated from the 'start' and 'end' values exclusively.
*/
func Create(stamp interface{}, calendar, format string) (instance Calends, err error) {
instance.duration = big.NewFloat(0.)
if stamp == nil {
return
}
if calendar == "" {
calendar = "unix"
}
if !calendars.Registered(calendar) {
err = errors.Wrap(calendars.ErrUnknownCalendar(calendar), 1)
return
}
if format == "" {
format = calendars.DefaultFormat(calendar)
}
switch stamp := stamp.(type) {
default:
var internal calendars.TAI64NARUXTime
internal, err = calendars.ToInternal(calendar, stamp, format)
instance = Calends{
startTime: internal,
duration: big.NewFloat(0.),
endTime: internal,
}
case map[string]interface{}:
instance, err = retrieveInstance(calendar, stamp, format)
}
return
}
func retrieveInstance(calendar string, stamp map[string]interface{}, format string) (instance Calends, err error) {
start, hasStart, e := retrieveStart(calendar, stamp, format)
if e != nil {
err = e
return
}
end, hasEnd, e := retrieveEnd(calendar, stamp, format)
if e != nil {
err = e
return
}
duration, hasDuration, e := retrieveDuration(calendar, stamp, format)
if e != nil {
err = e
return
}
if hasStart && hasEnd {
instance = Calends{
startTime: start,
duration: end.Sub(start).Float(),
endTime: end,
}
} else if hasStart && hasDuration {
instance = Calends{
startTime: start,
duration: &duration,
endTime: start.Add(calendars.TAI64NARUXTimeFromFloat(duration)),
}
} else if hasEnd && hasDuration {
instance = Calends{
startTime: end.Sub(calendars.TAI64NARUXTimeFromFloat(duration)),
duration: &duration,
endTime: end,
}
} else {
var internal calendars.TAI64NARUXTime
internal, err = calendars.ToInternal(calendar, stamp, format)
instance = Calends{
startTime: internal,
duration: big.NewFloat(0.),
endTime: internal,
}
}
return
}
func retrieveStart(calendar string, stamp map[string]interface{}, format string) (start calendars.TAI64NARUXTime, hasStart bool, err error) {
var rawStart interface{}
rawStart, hasStart = stamp["start"]
if hasStart {
start, err = calendars.ToInternal(calendar, rawStart, format)
}
return
}
func retrieveEnd(calendar string, stamp map[string]interface{}, format string) (end calendars.TAI64NARUXTime, hasEnd bool, err error) {
var rawEnd interface{}
rawEnd, hasEnd = stamp["end"]
if hasEnd {
end, err = calendars.ToInternal(calendar, rawEnd, format)
}
return
}
func retrieveDuration(calendar string, stamp map[string]interface{}, format string) (duration big.Float, hasDuration bool, err error) {
var rawDuration interface{}
rawDuration, hasDuration = stamp["duration"]
if hasDuration {
switch rawDuration := rawDuration.(type) {
case []byte:
var tmp *big.Float
tmp, _, err = big.ParseFloat(string(rawDuration), 10, 176, big.ToNearestAway)
if err == nil {
duration = *tmp
} else if err.Error() == "EOF" {
duration = *big.NewFloat(0)
err = nil
}
case string:
var tmp *big.Float
tmp, _, err = big.ParseFloat(rawDuration, 10, 176, big.ToNearestAway)
if err == nil {
duration = *tmp
} else if err.Error() == "EOF" {
duration = *big.NewFloat(0)
err = nil
}
case int:
duration = *big.NewFloat(float64(rawDuration))
case float64:
duration = *big.NewFloat(rawDuration)
case *big.Float:
tmp := rawDuration
duration = *tmp
case big.Float:
duration = rawDuration
default:
err = errors.New("Invalid Duration Type")
}
}
return
}
// Date is used to retrieve the value of an instant in a given calendar system.
func (c Calends) Date(calendar, format string) (string, error) {
if calendar == "" {
calendar = "unix"
}
if !calendars.Registered(calendar) {
err := errors.Wrap(calendars.ErrUnknownCalendar(calendar), 1)
return "", err
}
if format == "" {
format = calendars.DefaultFormat(calendar)
}
return calendars.FromInternal(calendar, c.startTime, format)
}
// Duration retrieves the number of seconds between the start and end instants.
func (c Calends) Duration() *big.Float {
return c.duration
}
// EndDate retrieves the value of the end instant in a given calendar system.
func (c Calends) EndDate(calendar, format string) (string, error) {
if calendar == "" {
calendar = "unix"
}
if !calendars.Registered(calendar) {
err := errors.Wrap(calendars.ErrUnknownCalendar(calendar), 1)
return "", err
}
if format == "" {
format = calendars.DefaultFormat(calendar)
}
return calendars.FromInternal(calendar, c.endTime, format)
}
// String implements the fmt.Stringer interface.
func (c Calends) String() string {
if tmp, _ := c.duration.Int64(); tmp == 0 {
out, _ := c.startTime.MarshalText()
return string(out)
}
start, _ := c.startTime.MarshalText()
end, _ := c.endTime.MarshalText()
return fmt.Sprintf("from %s to %s", start, end)
}
// MarshalText implements the encoding.TextMarshaler interface.
func (c Calends) MarshalText() ([]byte, error) {
if tmp, _ := c.duration.Int64(); tmp == 0 {
return c.startTime.MarshalText()
}
start, err := c.startTime.MarshalText()
if err != nil {
return []byte{}, err
}
end, err := c.endTime.MarshalText()
if err != nil {
return []byte{}, err
}
return []byte(fmt.Sprintf("%s::%s", start, end)), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (c *Calends) UnmarshalText(text []byte) error {
var startTime, endTime calendars.TAI64NARUXTime
var start, end string
n, err := fmt.Sscanf(string(text), "%56s::%56s", &start, &end)
if err != nil && err.Error() != io.EOF.Error() && err.Error() != "input does not match format" && err.Error() != "unexpected EOF" {
return err
}
if n < 2 {
end = start
}
startTime.UnmarshalText([]byte(start))
endTime.UnmarshalText([]byte(end))
tmp, err := Create(map[string]interface{}{
"start": startTime,
"end": endTime,
}, "tai64", "tai64narux")
*c = tmp
return err
}
// MarshalJSON implements the encoding/json.Marshaler interface.
func (c Calends) MarshalJSON() ([]byte, error) {
if tmp, _ := c.duration.Int64(); tmp == 0 {
tmp, err := c.startTime.MarshalText()
return append(append([]byte{'"'}, tmp...), '"'), err
}
start, err := c.startTime.MarshalText()
if err != nil {
return []byte{}, err
}
end, err := c.endTime.MarshalText()
if err != nil {
return []byte{}, err
}
return []byte(fmt.Sprintf(`{"start":"%s","end":"%s"}`, start, end)), nil
}
// UnmarshalJSON implements the encoding/json.Unmarshaler interface.
func (c *Calends) UnmarshalJSON(text []byte) error {
var startTime, endTime calendars.TAI64NARUXTime
parsed := make(map[string]string)
err := json.Unmarshal(text, &parsed)
if err == nil {
err = startTime.UnmarshalText([]byte(parsed["start"]))
if err != nil {
return errors.New("JSON decode failure while parsing start time [" + parsed["start"] + "]: " + err.Error())
}
err = endTime.UnmarshalText([]byte(parsed["end"]))
if err != nil {
return errors.New("JSON decode failure while parsing end time [" + parsed["end"] + "]: " + err.Error())
}
} else {
err = startTime.UnmarshalText([]byte(strings.Trim(string(text), `"`)))
if err != nil {
return errors.New("JSON decode failure while parsing time [" + strings.Trim(string(text), `"`) + "]: " + err.Error())
}
endTime = startTime
}
temp, err := Create(map[string]interface{}{
"start": startTime,
"end": endTime,
}, "tai64", "")
*c = temp
if err != nil {
err = errors.New("JSON decode failure while setting values: " + err.Error())
}
return err
}