Repository navigation
Expand file tree
/
Copy pathopenhours.go
More file actions
1405 lines (1298 loc) · 37.9 KB
/
Copy pathopenhours.go
File metadata and controls
1405 lines (1298 loc) · 37.9 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package openhours
import (
"encoding/binary"
"encoding/json"
"errors"
"math/bits"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"unsafe"
)
const (
minutesPerWeek = 10080
hasMonotonic = 1 << 63
nsecShift = 30
nsecMask = (1 << nsecShift) - 1
internalToUnix = -62135596800
// wallToInternal converts the packed wall-clock seconds field (seconds since
// 1885-01-01, used when the monotonic bit is set) into the internal epoch
// (seconds since year 1). It mirrors time's own wallToInternal.
wallToInternal = (1884*365 + 1884/4 - 1884/100 + 1884/400) * 86400
)
type timeRepr struct {
wall uint64
ext int64
loc *time.Location
}
var (
internMu sync.RWMutex
internPool = make(map[string]*OpeningHours)
zeroDuration time.Duration
weekdayToDayIdx = [7]int{6, 0, 1, 2, 3, 4, 5} // Sunday (0) -> 6, Monday (1) -> 0, ...
emptyOH = &OpeningHours{expression: "", windows: nil, plainText: true}
alwaysOpenOH = makeAlwaysOpenOH()
// l1Cache is a small, lock-free one-slot cache of the most recently resolved
// expression, so that hot repeated parses skip the RWMutex and hash lookup.
// It is safe for concurrent use (atomic pointer publication).
l1Cache atomic.Pointer[parseSlot]
// parseCache is a lock-free, direct-mapped cache of resolved expressions.
// It spreads interned lookups over many slots so that a working set of
// distinct-but-recursively-queried expressions (e.g. a cycle of schedule
// strings) resolves without taking internMu or allocating a fresh slot on
// every call. It stores the *same* interned pointer that internPool holds,
// so interning identity is preserved.
parseCache [parseCacheSize]atomic.Pointer[parseSlot]
)
const (
parseCacheSize = 1024 // power of two
parseCacheMask = parseCacheSize - 1
)
// hashString is a cheap, allocation-free hash used only to pick a slot in
// parseCache. Hash quality affects only the hit rate (collisions merely fall
// back to internPool), never correctness, since entries are verified by a full
// string comparison.
//
// For strings of at least 8 bytes it reads only the first and last 8 bytes
// (plus the length), so the cost is constant regardless of expression length —
// a long expression hashes as fast as a short one. Shorter strings fall back to
// a byte-wise accumulation.
func hashString(s string) uint64 {
const (
mul = 0x9e3779b97f4a7c15
mix = 0x9E3779B185EBCA87
)
n := len(s)
if n >= 8 {
p := unsafe.Pointer(unsafe.StringData(s))
a := *(*uint64)(p)
b := *(*uint64)(unsafe.Add(p, n-8))
h := (a*mix ^ bits.RotateLeft64(b, 31) ^ uint64(n)*mul)
h ^= h >> 29
h *= mul
h ^= h >> 32
return h
}
var t uint64
for i := 0; i < n; i++ {
t = t<<8 | uint64(s[i])
}
h := (t ^ uint64(n)) * mul
h ^= h >> 32
return h
}
type parseSlot struct {
expr string
oh *OpeningHours
}
func makeAlwaysOpenOH() *OpeningHours {
oh := &OpeningHours{
expression: "24/7",
windows: []TimeWindow{{Start: 0, End: minutesPerWeek}},
plainText: true,
}
for i := range oh.bitmask {
oh.bitmask[i] = ^uint64(0)
}
return oh
}
// TimeWindow represents a minute interval [Start, End) within a week (0 to 10080 minutes).
// Monday 00:00 is minute 0, and Sunday 24:00 is minute 10080.
type TimeWindow struct {
Start int
End int
}
// OpeningHours is an immutable, interval-math based evaluator for OSM opening_hours expressions.
// It supports overnight shifts, off/closed overrides, 24/7, open-ended ranges, and multi-interval rules.
type OpeningHours struct {
expression string
windows []TimeWindow
bitmask [158]uint64
// winBase is a precomputed search accelerator for the (sorted, disjoint)
// windows slice: winBase[w] is the number of windows whose End is <= w*64,
// i.e. a lower bound on findFirstWindowStartingAtOrAfter(t) for every
// t in [w*64, w*64+63]. It turns the branchy binary search into one table
// load plus at most a couple of comparisons. It is derived data only:
// it is never consulted for the query result, only to skip windows that
// provably cannot be the answer.
winBase [158]uint8
// plainText reports whether expression contains no JSON escape-triggering
// bytes (no backslash and no control character). It lets DecodeJSON skip
// the escape scan when a cached expression already matches the raw JSON
// bytes, which implies the bytes need no unescaping.
plainText bool
}
// Parse parses an OSM opening_hours expression string and returns an *OpeningHours instance.
// If the expression is invalid or empty, it returns a safe instance with empty schedule (IsOpen = false).
// Identical expressions are interned to conserve memory.
func Parse(expression string) *OpeningHours {
if expression == "" {
return emptyOH
}
if expression == "24/7" {
return alwaysOpenOH
}
slot := l1Cache.Load()
if slot != nil {
if unsafe.StringData(slot.expr) == unsafe.StringData(expression) && len(slot.expr) == len(expression) {
return slot.oh
}
if slot.expr == expression {
return slot.oh
}
}
// Second-level direct-mapped cache: absorbs a working set of distinct
// expressions that the one-slot l1Cache cannot hold, without the RWMutex or
// the per-hit parseSlot allocation the internPool path incurs.
cslot := &parseCache[hashString(expression)&parseCacheMask]
if s := cslot.Load(); s != nil && s.expr == expression {
l1Cache.Store(s)
return s.oh
}
return parseSlow(expression, cslot)
}
// parseSlow holds the cold path (intern-pool probes and the actual parse). It
// is a separate function so the large `rulesBuf` scratch array no longer sizes
// Parse's stack frame on the hot cache-hit path.
func parseSlow(expression string, cslot *atomic.Pointer[parseSlot]) *OpeningHours {
internMu.RLock()
cached, ok := internPool[expression]
internMu.RUnlock()
if ok {
ps := &parseSlot{expression, cached}
cslot.Store(ps)
l1Cache.Store(ps)
return cached
}
trimmed := strings.TrimSpace(expression)
internMu.Lock()
defer internMu.Unlock()
if cached, ok := internPool[expression]; ok {
ps := &parseSlot{expression, cached}
cslot.Store(ps)
l1Cache.Store(ps)
return cached
}
var oh *OpeningHours
switch {
case trimmed == "":
oh = emptyOH
case trimmed == "24/7":
oh = alwaysOpenOH
default:
var rulesBuf [8]openingRule
rules := rulesBuf[:0]
remaining := trimmed
for len(remaining) > 0 {
var part string
if idx := strings.IndexByte(remaining, ';'); idx >= 0 {
part = remaining[:idx]
remaining = remaining[idx+1:]
} else {
part = remaining
remaining = ""
}
part = strings.TrimSpace(part)
if part != "" {
rule := parseOpeningRule(part)
if rule.DayMask > 0 && (rule.IsAllDay || rule.hasRanges()) {
rules = append(rules, rule)
}
}
}
windows := bakeRules(rules)
bm := bakeBitmask(windows)
oh = &OpeningHours{
expression: expression,
windows: windows,
bitmask: bm,
winBase: bakeWinBase(windows),
plainText: isPlainJSON(expression),
}
}
internPool[expression] = oh
ps := &parseSlot{expression, oh}
cslot.Store(ps)
l1Cache.Store(ps)
return oh
}
func bakeBitmask(windows []TimeWindow) [158]uint64 {
var bm [158]uint64
for _, w := range windows {
start := w.Start
end := w.End
if start < 0 {
start = 0
}
if end > minutesPerWeek {
end = minutesPerWeek
}
if start >= end {
continue
}
startWord := start >> 6
endWord := (end - 1) >> 6
startBit := start & 63
endBit := (end - 1) & 63
if startWord == endWord {
mask := ((uint64(1) << (endBit - startBit + 1)) - 1) << startBit
bm[startWord] |= mask
} else {
bm[startWord] |= (^uint64(0)) << startBit
for i := startWord + 1; i < endWord; i++ {
bm[i] = ^uint64(0)
}
bm[endWord] |= (^uint64(0)) >> (63 - endBit)
}
}
return bm
}
// bakeWinBase builds the window-search accelerator described on OpeningHours.winBase.
// windows must be sorted by Start (and therefore by End, since they are disjoint).
func bakeWinBase(windows []TimeWindow) [158]uint8 {
var base [158]uint8
j := 0
for w := 0; w < len(base); w++ {
lim := w << 6
for j < len(windows) && windows[j].End <= lim {
j++
}
base[w] = uint8(j)
}
return base
}
// isPlainJSON reports whether s contains neither a backslash nor a control
// character, i.e. whether it would survive a JSON string round-trip verbatim.
// It is evaluated once per unique parsed expression.
func isPlainJSON(s string) bool {
for i := 0; i < len(s); i++ {
c := s[i]
if c == '\\' || c < 0x20 {
return false
}
}
return true
}
// Raw returns the raw expression string.
func (oh *OpeningHours) Raw() string {
if oh == nil {
return ""
}
return oh.expression
}
// String returns the raw expression string.
func (oh *OpeningHours) String() string {
if oh == nil {
return ""
}
return oh.expression
}
// Windows returns a copy of the disjoint time windows (in week minutes) that comprise the schedule.
func (oh *OpeningHours) Windows() []TimeWindow {
if oh == nil || len(oh.windows) == 0 {
return nil
}
return slices.Clone(oh.windows)
}
// IsOpen returns true if the specified time is within opening hours.
func (oh *OpeningHours) IsOpen(t time.Time) bool {
// No empty-window guard: an empty schedule has an all-zero bitmask, so the
// bit test below already returns false.
if oh == nil {
return false
}
tt := (*timeRepr)(unsafe.Pointer(&t))
var weekMin int
if tt.loc == nil || tt.loc == time.UTC {
var unixSec int64
if tt.wall&hasMonotonic != 0 {
unixSec = int64(tt.wall<<1>>(nsecShift+1)) + internalToUnix
} else {
unixSec = tt.ext + internalToUnix
}
if unixSec >= 0 {
// Unsigned division by the constant 60 lowers to a single
// multiply-shift with no sign correction, unlike the signed form.
weekMin = int((uint64(unixSec)/60 + 4320) % minutesPerWeek)
} else {
weekMin = int((unixSec/60 + 4320) % minutesPerWeek)
if weekMin < 0 {
weekMin += minutesPerWeek
}
}
} else {
weekMin = timeToWeekMinuteNonUTC(t)
}
return (oh.bitmask[uint(weekMin)>>6] & (uint64(1) << (uint(weekMin) & 63))) != 0
}
func timeToWeekMinuteNonUTC(t time.Time) int {
hour, min, _ := t.Clock()
day := weekdayToDayIdx[t.Weekday()]
return day*1440 + hour*60 + min
}
// Match returns true if the time t is within opening hours.
func (oh *OpeningHours) Match(t time.Time) bool {
return oh.IsOpen(t)
}
// GetCurrentShiftEnd returns the end time of the current open shift if open at t,
// or nil if currently closed, or nil if open 24/7 (infinite open).
func (oh *OpeningHours) GetCurrentShiftEnd(t time.Time) *time.Time {
if oh == nil || len(oh.windows) == 0 {
return nil
}
if len(oh.windows) == 1 && oh.windows[0].Start == 0 && oh.windows[0].End == minutesPerWeek {
return nil
}
min, subMinute := getWeekMinute(t)
idx := oh.findFirstWindowStartingAtOrAfter(min)
if idx >= len(oh.windows) || oh.windows[idx].Start > min {
return nil
}
w := oh.windows[idx]
diffMin := w.End - min
if idx == len(oh.windows)-1 && w.End == minutesPerWeek && oh.windows[0].Start == 0 {
diffMin = (minutesPerWeek - min) + oh.windows[0].End
}
res := addDuration(t, time.Duration(diffMin)*time.Minute-subMinute)
return &res
}
// GetTimeToOpen returns the duration until the next opening window starting from `from`.
// If currently open, returns 0. If never opens, returns nil.
func (oh *OpeningHours) GetTimeToOpen(from time.Time) *time.Duration {
d, ok := oh.getTimeToOpen(from)
if !ok {
return nil
}
if d == 0 {
return &zeroDuration
}
return &d
}
// TimeToOpen returns the duration until the next opening window starting from `from`,
// and a boolean indicating whether the location ever opens. If currently open, duration is 0.
// This is a zero-allocation value method.
func (oh *OpeningHours) TimeToOpen(from time.Time) (time.Duration, bool) {
return oh.getTimeToOpen(from)
}
func (oh *OpeningHours) getTimeToOpen(from time.Time) (time.Duration, bool) {
if oh == nil || len(oh.windows) == 0 {
return 0, false
}
if len(oh.windows) == 1 && oh.windows[0].Start == 0 && oh.windows[0].End == minutesPerWeek {
return 0, true
}
t, subMinute := getWeekMinute(from)
// winBase turns the window search into one table load plus at most a couple
// of comparisons; on every schedule measured this beats a per-minute query
// table (which added a second dependent load and a lazily-published
// pointer), so the search is the whole hot path.
idx := oh.findFirstWindowStartingAtOrAfter(t)
if idx < len(oh.windows) {
w := oh.windows[idx]
if w.Start <= t {
return 0, true
}
d := time.Duration(w.Start-t)*time.Minute - subMinute
return d, true
}
d := time.Duration((minutesPerWeek-t)+oh.windows[0].Start)*time.Minute - subMinute
return d, true
}
// GetTimeToOpenForDuration returns the wait duration from `from` until an opening window
// that is at least `duration` long begins (or 0 if currently open and remaining continuous slot >= duration).
// Returns nil if no single opening window in the schedule is long enough.
func (oh *OpeningHours) GetTimeToOpenForDuration(from time.Time, duration time.Duration) *time.Duration {
d, ok := oh.getTimeToOpenForDuration(from, duration)
if !ok {
return nil
}
if d == 0 {
return &zeroDuration
}
return &d
}
// TimeToOpenForDuration returns the wait duration from `from` until an opening window
// of at least `duration` begins, and a boolean indicating if such a window exists.
// This is a zero-allocation value method.
func (oh *OpeningHours) TimeToOpenForDuration(from time.Time, duration time.Duration) (time.Duration, bool) {
return oh.getTimeToOpenForDuration(from, duration)
}
func (oh *OpeningHours) getTimeToOpenForDuration(from time.Time, duration time.Duration) (time.Duration, bool) {
if oh == nil || len(oh.windows) == 0 {
return 0, false
}
if duration <= 0 {
return 0, true
}
if duration > time.Duration(minutesPerWeek)*time.Minute {
return 0, false
}
if len(oh.windows) == 1 && oh.windows[0].Start == 0 && oh.windows[0].End == minutesPerWeek {
return 0, true
}
t, subMinute := getWeekMinute(from)
reqMin := int((duration + time.Minute - 1) / time.Minute)
startIdx := oh.findFirstWindowStartingAtOrAfter(t)
// Check windows in current week starting from t
for i := startIdx; i < len(oh.windows); i++ {
w := oh.windows[i]
effectiveEnd := w.End
if i == len(oh.windows)-1 && w.End == minutesPerWeek && oh.windows[0].Start == 0 {
effectiveEnd = minutesPerWeek + oh.windows[0].End
}
if t >= w.Start {
remDur := time.Duration(effectiveEnd-t)*time.Minute - subMinute
if remDur >= duration {
return 0, true
}
} else {
if effectiveEnd-w.Start >= reqMin {
d := time.Duration(w.Start-t)*time.Minute - subMinute
return d, true
}
}
}
// Wrap around to next week. Windows after startIdx already had their total
// duration validated in the first loop above (future-start branch checks
// effectiveEnd-w.Start >= reqMin), so only windows [0..startIdx] need to be
// re-tested here — notably the currently-open window at startIdx, whose total
// length was not validated when only its remaining time was short.
for i := 0; i <= startIdx && i < len(oh.windows); i++ {
w := oh.windows[i]
effectiveEnd := w.End
if i == len(oh.windows)-1 && w.End == minutesPerWeek && oh.windows[0].Start == 0 {
effectiveEnd = minutesPerWeek + oh.windows[0].End
}
if effectiveEnd-w.Start >= reqMin {
d := time.Duration((minutesPerWeek-t)+w.Start)*time.Minute - subMinute
return d, true
}
}
return 0, false
}
// When returns the time `from` + wait duration when `duration` can be continuously serviced during open hours,
// or nil if it never fits.
func (oh *OpeningHours) When(from time.Time, duration time.Duration) *time.Time {
t, ok := oh.WhenTime(from, duration)
if !ok {
return nil
}
return &t
}
// WhenTime returns the time `from` + wait duration when `duration` can be continuously serviced during open hours,
// and a boolean indicating success. It does not allocate memory on the heap.
func (oh *OpeningHours) WhenTime(from time.Time, duration time.Duration) (time.Time, bool) {
d, ok := oh.getTimeToOpenForDuration(from, duration)
if !ok {
return time.Time{}, false
}
if d == 0 {
return from, true
}
return addDuration(from, d), true
}
// NextDur returns whether currently open, and the duration until state changes (shift ends or opens).
func (oh *OpeningHours) NextDur(t time.Time) (bool, time.Duration) {
if oh == nil || len(oh.windows) == 0 {
return false, 0
}
if len(oh.windows) == 1 && oh.windows[0].Start == 0 && oh.windows[0].End == minutesPerWeek {
return true, time.Duration(minutesPerWeek) * time.Minute
}
min, subMinute := getWeekMinute(t)
// See getTimeToOpen: winBase makes the search cheap enough that a
// per-minute query table is a net loss.
idx := oh.findFirstWindowStartingAtOrAfter(min)
if idx < len(oh.windows) {
w := oh.windows[idx]
if w.Start <= min {
// Currently open
diffMin := w.End - min
if idx == len(oh.windows)-1 && w.End == minutesPerWeek && oh.windows[0].Start == 0 {
diffMin = (minutesPerWeek - min) + oh.windows[0].End
}
dur := time.Duration(diffMin)*time.Minute - subMinute
return true, dur
}
// Currently closed, opens at w.Start
dur := time.Duration(w.Start-min)*time.Minute - subMinute
return false, dur
}
// Currently closed, opens at windows[0].Start next week
dur := time.Duration((minutesPerWeek-min)+oh.windows[0].Start)*time.Minute - subMinute
return false, dur
}
// NextDate returns whether currently open, and the time when the status changes.
func (oh *OpeningHours) NextDate(t time.Time) (bool, time.Time) {
isOpen, dur := oh.NextDur(t)
return isOpen, addDuration(t, dur)
}
func (oh *OpeningHours) findFirstWindowStartingAtOrAfter(t int) int {
windows := oh.windows
n := len(windows)
if n == 0 {
return 0
}
// winBase gives a lower bound on the answer for the whole 64-minute word
// containing t, so the scan below only re-examines the few windows whose
// End actually falls inside (or just after) that word.
word := t >> 6
if uint(word) >= uint(len(oh.winBase)) {
if t < 0 {
word = 0
} else {
word = len(oh.winBase) - 1
}
}
i := int(oh.winBase[word])
for ; i < n; i++ {
if windows[i].End > t {
return i
}
}
return n
}
// MarshalJSON serializes OpeningHours as a JSON string with its expression.
func (oh *OpeningHours) MarshalJSON() ([]byte, error) {
if oh == nil {
return []byte("null"), nil
}
return json.Marshal(oh.expression)
}
// UnmarshalJSON deserializes OpeningHours from a JSON string, without going
// through encoding/json's reflection-based dispatch in the decode path.
func (oh *OpeningHours) UnmarshalJSON(data []byte) error {
expr, err := decodeExpression(data)
if err != nil {
return err
}
// Copy the interned instance's immutable fields by value rather than
// assigning *oh = *Parse(expr) so a whole-struct copy never has to be
// reasoned about.
p := Parse(expr)
oh.expression = p.expression
oh.windows = p.windows
oh.bitmask = p.bitmask
oh.winBase = p.winBase
oh.plainText = p.plainText
return nil
}
// DecodeJSON parses a JSON string of an opening_hours expression and returns the
// corresponding *OpeningHours (a shared, interned instance — not a copy). It is a
// dependency-free, reflection-free decode entry point: the common plain-string
// case resolves through the lock-free L1 cache with zero allocations, and escaped
// JSON strings are handled by a small hand-rolled scanner instead of
// encoding/json.
func DecodeJSON(data []byte) (*OpeningHours, error) {
if n := len(data); n >= 2 && data[0] == '"' && data[n-1] == '"' {
inner := data[1 : n-1]
// If a cached expression equals the raw bytes *and* is itself known to
// be escape-free, then those raw bytes contain no backslash and no
// control character: a backslash in the bytes could not equal the
// escape-free expression, so unescaping is provably a no-op and the
// decode result is exactly that cached expression. Checking the cache
// first therefore skips the escape scan on the hot repeated-decode path.
if slot := l1Cache.Load(); slot != nil && slot.oh.plainText && stringEqualsBytes(slot.expr, inner) {
return slot.oh, nil
}
if !hasJSONEscape(inner) {
return Parse(string(inner)), nil
}
}
expr, err := decodeExpression(data)
if err != nil {
return nil, err
}
return Parse(expr), nil
}
// decodeExpression validates that data is a JSON string and returns the decoded
// opening_hours expression, handling the standard JSON escapes. It performs no
// reflection and does not route through encoding/json.
func decodeExpression(data []byte) (string, error) {
n := len(data)
if n < 2 || data[0] != '"' {
return "", errNotJSONString
}
// Fast path: a plain quoted string with no escapes.
inner := data[1:]
for i := 0; i < len(inner); i++ {
c := inner[i]
if c == '\\' || c < 0x20 {
return decodeEscaped(data)
}
if c == '"' {
if i == len(inner)-1 {
return string(data[1 : n-1]), nil
}
return "", errNotJSONString
}
}
return "", errNotJSONString
}
var (
errNotJSONString = errors.New("openhours: invalid character looking for beginning of value")
errInvalidUnicode = errors.New("openhours: invalid unicode escape")
errInvalidEscape = errors.New("openhours: invalid escape")
errInvalidControlChar = errors.New("openhours: invalid control character")
errUnexpectedEOFInString = errors.New("openhours: unexpected end of JSON input")
)
// hasJSONEscape reports whether raw string content contains a backslash or a
// control character, i.e. input that needs JSON unescaping. It scans 8 bytes at
// a time using SWAR tricks (no per-byte branches) and falls back to a scalar
// loop for the tail.
func hasJSONEscape(s []byte) bool {
const (
lo = 0x0101010101010101
hi = 0x8080808080808080
)
i := 0
for ; i+8 <= len(s); i += 8 {
v := binary.LittleEndian.Uint64(s[i : i+8])
// Any byte < 0x20 (control char)?
if (v-0x2020202020202020)&^v&hi != 0 {
return true
}
// Any byte == '\\' (0x5C)?
x := v ^ 0x5C5C5C5C5C5C5C5C
if (x-lo)&^x&hi != 0 {
return true
}
}
for ; i < len(s); i++ {
c := s[i]
if c == '\\' || c < 0x20 {
return true
}
}
return false
}
// stringEqualsBytes compares a string against a byte slice without allocating.
// It only reads the string's backing memory (never writes) and never retains the
// byte slice, so the read-only view is safe.
func stringEqualsBytes(s string, b []byte) bool {
if len(s) != len(b) {
return false
}
if len(b) == 0 {
return true
}
// Compare via a read-only string view of b so the equality test lowers to
// runtime.memequal (SIMD on amd64) instead of a scalar byte loop.
return s == unsafe.String(unsafe.SliceData(b), len(b))
}
// decodeEscaped unescapes a JSON string that contains backslash escapes.
func decodeEscaped(data []byte) (string, error) {
n := len(data)
if n < 2 || data[0] != '"' {
return "", errNotJSONString
}
buf := make([]byte, 0, n-1)
i := 1
for i < n {
c := data[i]
switch {
case c == '"':
return string(buf), nil
case c == '\\':
if i+1 >= n {
return "", errUnexpectedEOFInString
}
e := data[i+1]
switch e {
case '"', '\\', '/':
buf = append(buf, e)
i += 2
case 'b':
buf = append(buf, '\b')
i += 2
case 'f':
buf = append(buf, '\f')
i += 2
case 'n':
buf = append(buf, '\n')
i += 2
case 'r':
buf = append(buf, '\r')
i += 2
case 't':
buf = append(buf, '\t')
i += 2
case 'u':
if i+6 > n {
return "", errUnexpectedEOFInString
}
r, ok := decodeHex4(data[i+2 : i+6])
if !ok {
return "", errInvalidUnicode
}
if r <= 0x7F {
buf = append(buf, byte(r))
} else {
var enc [4]byte
m := utf8.EncodeRune(enc[:], r)
buf = append(buf, enc[:m]...)
}
i += 6
default:
return "", errInvalidEscape
}
case c < 0x20:
return "", errInvalidControlChar
default:
buf = append(buf, c)
i++
}
}
return "", errUnexpectedEOFInString
}
func decodeHex4(b []byte) (rune, bool) {
if len(b) != 4 {
return 0, false
}
var r rune
for _, c := range b {
r <<= 4
switch {
case c >= '0' && c <= '9':
r |= rune(c - '0')
case c >= 'a' && c <= 'f':
r |= rune(c-'a') + 10
case c >= 'A' && c <= 'F':
r |= rune(c-'A') + 10
default:
return 0, false
}
}
// Reject UTF-16 surrogate halves without a pair and out-of-range values.
if r >= 0xD800 && r <= 0xDFFF {
return 0, false
}
if r > 0x10FFFF {
return 0, false
}
return r, true
}
// addDuration is a drop-in replacement for t.Add(d) that operates on the raw
// time representation. It reproduces the standard library's algorithm exactly
// (including its monotonic-clock retention and saturation behaviour) while
// removing the extra call into time.addSec and the 24-byte Time copy that the
// stdlib's Add pays on every NextDate/When result. Instants that carry a
// monotonic clock reading are rare here and take the addDurationMono path.
func addDuration(t time.Time, d time.Duration) time.Time {
tt := (*timeRepr)(unsafe.Pointer(&t))
if tt.wall&hasMonotonic != 0 {
return addDurationMono(t, d)
}
// time.Time.Add, sub-second part: fold d's nanoseconds into wall's nsec field.
dsec := int64(d / 1e9)
nsec := int32(tt.wall&nsecMask) + int32(d%1e9)
if nsec >= 1e9 {
dsec++
nsec -= 1e9
} else if nsec < 0 {
dsec--
nsec += 1e9
}
tt.wall = tt.wall&^nsecMask | uint64(nsec)
// Without a monotonic reading, time.Time.addSec just additively stores into
// ext, and the subsequent monotonic refinement is skipped.
tt.ext = addDurOverflow(tt.ext, dsec)
return t
}
// addDurationMono handles instants that carry a monotonic clock reading.
func addDurationMono(t time.Time, d time.Duration) time.Time {
tt := (*timeRepr)(unsafe.Pointer(&t))
dsec := int64(d / 1e9)
nsec := int32(tt.wall&nsecMask) + int32(d%1e9)
if nsec >= 1e9 {
dsec++
nsec -= 1e9
} else if nsec < 0 {
dsec--
nsec += 1e9
}
tt.wall = tt.wall&^nsecMask | uint64(nsec)
// time.Time.addSec(dsec).
seconds := int64(tt.wall<<1>>(nsecShift+1)) + dsec
if 0 <= seconds && seconds <= 1<<33-1 {
tt.wall = tt.wall&nsecMask | uint64(seconds)<<nsecShift | hasMonotonic
} else {
// stripMono: the packed seconds field overflowed, move to ext.
tt.ext = wallToInternal + int64(tt.wall<<1>>(nsecShift+1))
tt.wall &= nsecMask
tt.ext = addDurOverflow(tt.ext, dsec)
}
if tt.wall&hasMonotonic != 0 {
te := tt.ext + int64(d)
if d < 0 && te > tt.ext || d > 0 && te < tt.ext {
// Monotonic reading out of range; degrade to wall-only.
tt.ext = wallToInternal + int64(tt.wall<<1>>(nsecShift+1))
tt.wall &= nsecMask
} else {
tt.ext = te
}
}
return t
}
// addDurOverflow mirrors time's saturating add of ext and d.
func addDurOverflow(ext, d int64) int64 {
sum := ext + d
if (sum > ext) == (d > 0) {
return sum
}
if d > 0 {
return 1<<63 - 1
}
return -(1<<63 - 1)
}
// getWeekMinute returns the minute within the week (Monday 00:00 == 0) and the
// sub-minute remainder of dt. The UTC fast path keeps the division unsigned,
// which the compiler lowers to a single multiply-shift without sign fix-ups.
func getWeekMinute(dt time.Time) (int, time.Duration) {
tt := (*timeRepr)(unsafe.Pointer(&dt))
if tt.loc == nil || tt.loc == time.UTC {
var unixSec, nsec int64
if tt.wall&hasMonotonic != 0 {
nsec = int64(tt.wall & nsecMask)
unixSec = int64(tt.wall<<1>>(nsecShift+1)) + internalToUnix
} else {
nsec = int64(tt.wall)
unixSec = tt.ext + internalToUnix
}
if unixSec >= 0 {
u := uint64(unixSec)
q := u / 60
return int((q + 4320) % minutesPerWeek), time.Duration(u-q*60)*time.Second + time.Duration(nsec)
}
q := unixSec / 60
weekMin := int((q + 4320) % minutesPerWeek)
if weekMin < 0 {
weekMin += minutesPerWeek
}
return weekMin, time.Duration(unixSec-q*60)*time.Second + time.Duration(nsec)
}
return getWeekMinuteNonUTC(dt)
}
func getWeekMinuteNonUTC(dt time.Time) (int, time.Duration) {
hour, min, sec := dt.Clock()
day := weekdayToDayIdx[dt.Weekday()]
subMinute := time.Duration(sec)*time.Second + time.Duration(dt.Nanosecond())
return day*1440 + hour*60 + min, subMinute
}
type timeRange struct {
StartMin int16
EndMin int16
DayOffset int8
}
type openingRule struct {
DayMask uint8
IsOff bool
IsAllDay bool
NumRanges uint8
TimeRanges [4]timeRange
ExtraRanges []timeRange
}
func (r *openingRule) addRange(tr timeRange) {
if r.NumRanges < 4 {
r.TimeRanges[r.NumRanges] = tr
r.NumRanges++
} else {
r.ExtraRanges = append(r.ExtraRanges, tr)
}
}
func (r *openingRule) hasRanges() bool {
return r.NumRanges > 0 || len(r.ExtraRanges) > 0
}
func hasSuffixFold(s, suffix string) bool {
if len(s) < len(suffix) {
return false
}
s = s[len(s)-len(suffix):]
for i := 0; i < len(suffix); i++ {
c1 := s[i]
c2 := suffix[i]
if c1 != c2 {
if c1 >= 'A' && c1 <= 'Z' {
c1 += 'a' - 'A'
}
if c2 >= 'A' && c2 <= 'Z' {