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
|
// Copyright 2020 The Libc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package libc // import "modernc.org/libc"
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
"modernc.org/libc/errno"
"modernc.org/libc/signal"
"modernc.org/libc/sys/types"
)
const (
allocatorPageOverhead = 4 * unsafe.Sizeof(int(0))
stackHeaderSize = unsafe.Sizeof(stackHeader{})
stackSegmentSize = 1<<12 - allocatorPageOverhead
uintptrSize = unsafe.Sizeof(uintptr(0))
)
var (
Covered = map[uintptr]struct{}{}
CoveredC = map[string]struct{}{}
fToken uintptr
tid int32
atExit []func()
atExitMu sync.Mutex
signals [signal.NSIG]uintptr
signalsMu sync.Mutex
objectMu sync.Mutex
objects = map[uintptr]interface{}{}
tlsBalance int32
_ = origin
_ = trc
)
func init() {
if n := stackHeaderSize; n%16 != 0 {
panic(fmt.Errorf("internal error: stackHeaderSize %v == %v (mod 16)", n, n%16))
}
}
func origin(skip int) string {
pc, fn, fl, _ := runtime.Caller(skip)
f := runtime.FuncForPC(pc)
var fns string
if f != nil {
fns = f.Name()
if x := strings.LastIndex(fns, "."); x > 0 {
fns = fns[x+1:]
}
}
return fmt.Sprintf("%s:%d:%s", filepath.Base(fn), fl, fns)
}
func trc(s string, args ...interface{}) string { //TODO-
switch {
case s == "":
s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
default:
s = fmt.Sprintf(s, args...)
}
r := fmt.Sprintf("%s: TRC %s", origin(2), s)
fmt.Fprintf(os.Stdout, "%s\n", r)
os.Stdout.Sync()
return r
}
func todo(s string, args ...interface{}) string { //TODO-
switch {
case s == "":
s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
default:
s = fmt.Sprintf(s, args...)
}
r := fmt.Sprintf("%s: TODOTODO %s", origin(2), s) //TODOOK
if dmesgs {
dmesg("%s", r)
}
fmt.Fprintf(os.Stdout, "%s\n", r)
fmt.Fprintf(os.Stdout, "%s\n", debug.Stack()) //TODO-
os.Stdout.Sync()
os.Exit(1)
panic("unrechable")
}
var coverPCs [1]uintptr //TODO not concurrent safe
func Cover() {
runtime.Callers(2, coverPCs[:])
Covered[coverPCs[0]] = struct{}{}
}
func CoverReport(w io.Writer) error {
var a []string
pcs := make([]uintptr, 1)
for pc := range Covered {
pcs[0] = pc
frame, _ := runtime.CallersFrames(pcs).Next()
a = append(a, fmt.Sprintf("%s:%07d:%s", filepath.Base(frame.File), frame.Line, frame.Func.Name()))
}
sort.Strings(a)
_, err := fmt.Fprintf(w, "%s\n", strings.Join(a, "\n"))
return err
}
func CoverC(s string) {
CoveredC[s] = struct{}{}
}
func CoverCReport(w io.Writer) error {
var a []string
for k := range CoveredC {
a = append(a, k)
}
sort.Strings(a)
_, err := fmt.Fprintf(w, "%s\n", strings.Join(a, "\n"))
return err
}
func token() uintptr { return atomic.AddUintptr(&fToken, 1) }
func addObject(o interface{}) uintptr {
t := token()
objectMu.Lock()
objects[t] = o
objectMu.Unlock()
return t
}
func getObject(t uintptr) interface{} {
objectMu.Lock()
o := objects[t]
if o == nil {
panic(todo("", t))
}
objectMu.Unlock()
return o
}
func removeObject(t uintptr) {
objectMu.Lock()
if _, ok := objects[t]; !ok {
panic(todo(""))
}
delete(objects, t)
objectMu.Unlock()
}
func (t *TLS) setErrno(err interface{}) {
if memgrind {
if atomic.SwapInt32(&t.reentryGuard, 1) != 0 {
panic(todo("concurrent use of TLS instance %p", t))
}
defer func() {
if atomic.SwapInt32(&t.reentryGuard, 0) != 1 {
panic(todo("concurrent use of TLS instance %p", t))
}
}()
}
// if dmesgs {
// dmesg("%v: %T(%v)\n%s", origin(1), err, err, debug.Stack())
// }
again:
switch x := err.(type) {
case int:
*(*int32)(unsafe.Pointer(t.errnop)) = int32(x)
case int32:
*(*int32)(unsafe.Pointer(t.errnop)) = x
case *os.PathError:
err = x.Err
goto again
case syscall.Errno:
*(*int32)(unsafe.Pointer(t.errnop)) = int32(x)
case *os.SyscallError:
err = x.Err
goto again
default:
panic(todo("%T", x))
}
}
// Close frees the resources of t.
func (t *TLS) Close() {
t.Free(int(unsafe.Sizeof(int32(0))))
if memgrind {
if t.stackHeaderBalance != 0 {
panic(todo("non zero stack header balance: %d", t.stackHeaderBalance))
}
atomic.AddInt32(&tlsBalance, -1)
}
}
// Alloc allocates n bytes of thread-local storage. It must be paired with a
// call to t.Free(n), using the same n. The order matters. This is ok:
//
// t.Alloc(11)
// t.Alloc(22)
// t.Free(22)
// t.Free(11)
//
// This is not correct:
//
// t.Alloc(11)
// t.Alloc(22)
// t.Free(11)
// t.Free(22)
func (t *TLS) Alloc(n int) (r uintptr) {
if memgrind {
if atomic.SwapInt32(&t.reentryGuard, 1) != 0 {
panic(todo("concurrent use of TLS instance %p", t))
}
defer func() {
if atomic.SwapInt32(&t.reentryGuard, 0) != 1 {
panic(todo("concurrent use of TLS instance %p", t))
}
}()
}
n += 15
n &^= 15
if t.stack.free >= n {
r = t.stack.sp
t.stack.free -= n
t.stack.sp += uintptr(n)
return r
}
//if we have a next stack
if nstack := t.stack.next; nstack != 0 {
if (*stackHeader)(unsafe.Pointer(nstack)).free >= n {
*(*stackHeader)(unsafe.Pointer(t.stack.page)) = t.stack
t.stack = *(*stackHeader)(unsafe.Pointer(nstack))
r = t.stack.sp
t.stack.free -= n
t.stack.sp += uintptr(n)
return r
}
nstack := *(*stackHeader)(unsafe.Pointer(t.stack.next))
for ; ; nstack = *(*stackHeader)(unsafe.Pointer(nstack.next)) {
if memgrind {
if atomic.AddInt32(&t.stackHeaderBalance, -1) < 0 {
panic(todo("negative stack header balance"))
}
}
Xfree(t, nstack.page)
if nstack.next == 0 {
break
}
}
t.stack.next = 0
}
if t.stack.page != 0 {
*(*stackHeader)(unsafe.Pointer(t.stack.page)) = t.stack
}
rq := n + int(stackHeaderSize)
if rq%int(stackSegmentSize) != 0 {
rq -= rq % int(stackSegmentSize)
rq += int(stackSegmentSize)
}
t.stack.free = rq - int(stackHeaderSize)
t.stack.prev = t.stack.page
rq += 15
rq &^= 15
t.stack.page = Xmalloc(t, types.Size_t(rq))
if t.stack.page == 0 {
panic("OOM")
}
if memgrind {
atomic.AddInt32(&t.stackHeaderBalance, 1)
}
t.stack.sp = t.stack.page + stackHeaderSize
r = t.stack.sp
t.stack.free -= n
t.stack.sp += uintptr(n)
if t.stack.prev != 0 {
(*stackHeader)(unsafe.Pointer(t.stack.prev)).next = t.stack.page
}
return r
}
//this declares how many stack frames are kept alive before being freed
const stackFrameKeepalive = 2
// Free deallocates n bytes of thread-local storage. See TLS.Alloc for details
// on correct usage.
func (t *TLS) Free(n int) {
if memgrind {
if atomic.SwapInt32(&t.reentryGuard, 1) != 0 {
panic(todo("concurrent use of TLS instance %p", t))
}
defer func() {
if atomic.SwapInt32(&t.reentryGuard, 0) != 1 {
panic(todo("concurrent use of TLS instance %p", t))
}
}()
}
n += 15
n &^= 15
t.stack.free += n
t.stack.sp -= uintptr(n)
if t.stack.sp != t.stack.page+stackHeaderSize {
return
}
nstack := t.stack
//if we are the first one, just free all of them
if t.stack.prev == 0 {
for ; ; nstack = *(*stackHeader)(unsafe.Pointer(nstack.next)) {
if memgrind {
if atomic.AddInt32(&t.stackHeaderBalance, -1) < 0 {
panic(todo("negative stack header balance"))
}
}
Xfree(t, nstack.page)
if nstack.next == 0 {
break
}
}
t.stack = stackHeader{}
return
}
//look if we are in the last n stackframes (n=stackFrameKeepalive)
//if we find something just return and set the current stack pointer to the previous one
for i := 0; i < stackFrameKeepalive; i++ {
if nstack.next == 0 {
*((*stackHeader)(unsafe.Pointer(t.stack.page))) = t.stack
t.stack = *(*stackHeader)(unsafe.Pointer(t.stack.prev))
return
}
nstack = *(*stackHeader)(unsafe.Pointer(nstack.next))
}
//else only free the last
if memgrind {
if atomic.AddInt32(&t.stackHeaderBalance, -1) < 0 {
panic(todo("negative stack header balance"))
}
}
Xfree(t, nstack.page)
(*stackHeader)(unsafe.Pointer(nstack.prev)).next = 0
*(*stackHeader)(unsafe.Pointer(t.stack.page)) = t.stack
t.stack = *(*stackHeader)(unsafe.Pointer(t.stack.prev))
}
type stackHeader struct {
free int // bytes left in page
page uintptr // stack page
prev uintptr // prev stack page = prev stack header
next uintptr // next stack page = next stack header
sp uintptr // next allocation address
_ stackHeaderPadding
}
func cString(t *TLS, s string) uintptr { //TODO-
n := len(s)
p := Xmalloc(t, types.Size_t(n)+1)
if p == 0 {
panic("OOM")
}
copy((*RawMem)(unsafe.Pointer(p))[:n:n], s)
*(*byte)(unsafe.Pointer(p + uintptr(n))) = 0
return p
}
// VaList fills a varargs list at p with args and returns p. The list must
// have been allocated by caller and it must not be in Go managed memory, ie.
// it must be pinned. Caller is responsible for freeing the list.
//
// Individual arguments must be one of int, uint, int32, uint32, int64, uint64,
// float64, uintptr or Intptr. Other types will panic.
//
// This function supports code generated by ccgo/v3. For manually constructed
// var args it's recommended to use the NewVaList function instead.
//
// Note: The C translated to Go varargs ABI alignment for all types is 8 on all
// architectures.
func VaList(p uintptr, args ...interface{}) (r uintptr) {
if p&7 != 0 {
panic("internal error")
}
r = p
for _, v := range args {
switch x := v.(type) {
case int:
*(*int64)(unsafe.Pointer(p)) = int64(x)
case int32:
*(*int64)(unsafe.Pointer(p)) = int64(x)
case int64:
*(*int64)(unsafe.Pointer(p)) = x
case uint:
*(*uint64)(unsafe.Pointer(p)) = uint64(x)
case uint16:
*(*uint64)(unsafe.Pointer(p)) = uint64(x)
case uint32:
*(*uint64)(unsafe.Pointer(p)) = uint64(x)
case uint64:
*(*uint64)(unsafe.Pointer(p)) = x
case float64:
*(*float64)(unsafe.Pointer(p)) = x
case uintptr:
*(*uintptr)(unsafe.Pointer(p)) = x
default:
panic(todo("invalid VaList argument type: %T", x))
}
p += 8
}
return r
}
// NewVaListN returns a newly allocated va_list for n items. The caller of
// NewVaListN is responsible for freeing the va_list.
func NewVaListN(n int) (va_list uintptr) {
return Xmalloc(nil, types.Size_t(8*n))
}
// NewVaList is like VaList but automatically allocates the correct amount of
// memory for all of the items in args.
//
// The va_list return value is used to pass the constructed var args to var
// args accepting functions. The caller of NewVaList is responsible for freeing
// the va_list.
func NewVaList(args ...interface{}) (va_list uintptr) {
return VaList(NewVaListN(len(args)), args...)
}
func VaInt32(app *uintptr) int32 {
ap := *(*uintptr)(unsafe.Pointer(app))
if ap == 0 {
return 0
}
ap = roundup(ap, 8)
v := int32(*(*int64)(unsafe.Pointer(ap)))
ap += 8
*(*uintptr)(unsafe.Pointer(app)) = ap
return v
}
func VaUint32(app *uintptr) uint32 {
ap := *(*uintptr)(unsafe.Pointer(app))
if ap == 0 {
return 0
}
ap = roundup(ap, 8)
v := uint32(*(*uint64)(unsafe.Pointer(ap)))
ap += 8
*(*uintptr)(unsafe.Pointer(app)) = ap
return v
}
func VaInt64(app *uintptr) int64 {
ap := *(*uintptr)(unsafe.Pointer(app))
if ap == 0 {
return 0
}
ap = roundup(ap, 8)
v := *(*int64)(unsafe.Pointer(ap))
ap += 8
*(*uintptr)(unsafe.Pointer(app)) = ap
return v
}
func VaUint64(app *uintptr) uint64 {
ap := *(*uintptr)(unsafe.Pointer(app))
if ap == 0 {
return 0
}
ap = roundup(ap, 8)
v := *(*uint64)(unsafe.Pointer(ap))
ap += 8
*(*uintptr)(unsafe.Pointer(app)) = ap
return v
}
func VaFloat32(app *uintptr) float32 {
ap := *(*uintptr)(unsafe.Pointer(app))
if ap == 0 {
return 0
}
ap = roundup(ap, 8)
v := *(*float64)(unsafe.Pointer(ap))
ap += 8
*(*uintptr)(unsafe.Pointer(app)) = ap
return float32(v)
}
func VaFloat64(app *uintptr) float64 {
ap := *(*uintptr)(unsafe.Pointer(app))
if ap == 0 {
return 0
}
ap = roundup(ap, 8)
v := *(*float64)(unsafe.Pointer(ap))
ap += 8
*(*uintptr)(unsafe.Pointer(app)) = ap
return v
}
func VaUintptr(app *uintptr) uintptr {
ap := *(*uintptr)(unsafe.Pointer(app))
if ap == 0 {
return 0
}
ap = roundup(ap, 8)
v := *(*uintptr)(unsafe.Pointer(ap))
ap += 8
*(*uintptr)(unsafe.Pointer(app)) = ap
return v
}
func roundup(n, to uintptr) uintptr {
if r := n % to; r != 0 {
return n + to - r
}
return n
}
func GoString(s uintptr) string {
if s == 0 {
return ""
}
var buf []byte
for {
b := *(*byte)(unsafe.Pointer(s))
if b == 0 {
return string(buf)
}
buf = append(buf, b)
s++
}
}
// GoBytes returns a byte slice from a C char* having length len bytes.
func GoBytes(s uintptr, len int) []byte {
if len == 0 {
return nil
}
return (*RawMem)(unsafe.Pointer(s))[:len:len]
}
func Bool32(b bool) int32 {
if b {
return 1
}
return 0
}
func Bool64(b bool) int64 {
if b {
return 1
}
return 0
}
type sorter struct {
len int
base uintptr
sz uintptr
f func(*TLS, uintptr, uintptr) int32
t *TLS
}
func (s *sorter) Len() int { return s.len }
func (s *sorter) Less(i, j int) bool {
return s.f(s.t, s.base+uintptr(i)*s.sz, s.base+uintptr(j)*s.sz) < 0
}
func (s *sorter) Swap(i, j int) {
p := uintptr(s.base + uintptr(i)*s.sz)
q := uintptr(s.base + uintptr(j)*s.sz)
for i := 0; i < int(s.sz); i++ {
*(*byte)(unsafe.Pointer(p)), *(*byte)(unsafe.Pointer(q)) = *(*byte)(unsafe.Pointer(q)), *(*byte)(unsafe.Pointer(p))
p++
q++
}
}
func CString(s string) (uintptr, error) {
n := len(s)
p := Xmalloc(nil, types.Size_t(n)+1)
if p == 0 {
return 0, fmt.Errorf("CString: cannot allocate %d bytes", n+1)
}
copy((*RawMem)(unsafe.Pointer(p))[:n:n], s)
*(*byte)(unsafe.Pointer(p + uintptr(n))) = 0
return p, nil
}
func GetEnviron() (r []string) {
for p := Environ(); ; p += unsafe.Sizeof(p) {
q := *(*uintptr)(unsafe.Pointer(p))
if q == 0 {
return r
}
r = append(r, GoString(q))
}
}
func strToUint64(t *TLS, s uintptr, base int32) (seenDigits, neg bool, next uintptr, n uint64, err int32) {
var c byte
out:
for {
c = *(*byte)(unsafe.Pointer(s))
switch c {
case ' ', '\t', '\n', '\r', '\v', '\f':
s++
case '+':
s++
break out
case '-':
s++
neg = true
break out
default:
break out
}
}
for {
c = *(*byte)(unsafe.Pointer(s))
var digit uint64
switch base {
case 10:
switch {
case c >= '0' && c <= '9':
seenDigits = true
digit = uint64(c) - '0'
default:
return seenDigits, neg, s, n, 0
}
case 16:
if c >= 'A' && c <= 'F' {
c = c + ('a' - 'A')
}
switch {
case c >= '0' && c <= '9':
seenDigits = true
digit = uint64(c) - '0'
case c >= 'a' && c <= 'f':
seenDigits = true
digit = uint64(c) - 'a' + 10
default:
return seenDigits, neg, s, n, 0
}
default:
panic(todo("", base))
}
n0 := n
n = uint64(base)*n + digit
if n < n0 { // overflow
return seenDigits, neg, s, n0, errno.ERANGE
}
s++
}
}
func strToFloatt64(t *TLS, s uintptr, bits int) (n float64, errno int32) {
var b []byte
var neg bool
defer func() {
var err error
if n, err = strconv.ParseFloat(string(b), bits); err != nil {
panic(todo(""))
}
if neg {
n = -n
}
}()
var c byte
out:
for {
c = *(*byte)(unsafe.Pointer(s))
switch c {
case ' ', '\t', '\n', '\r', '\v', '\f':
s++
case '+':
s++
break out
case '-':
s++
neg = true
break out
default:
break out
}
}
for {
c = *(*byte)(unsafe.Pointer(s))
switch {
case c >= '0' && c <= '9':
b = append(b, c)
case c == '.':
b = append(b, c)
s++
for {
c = *(*byte)(unsafe.Pointer(s))
switch {
case c >= '0' && c <= '9':
b = append(b, c)
case c == 'e' || c == 'E':
b = append(b, c)
s++
for {
c = *(*byte)(unsafe.Pointer(s))
switch {
case c == '+' || c == '-':
b = append(b, c)
s++
for {
c = *(*byte)(unsafe.Pointer(s))
switch {
case c >= '0' && c <= '9':
b = append(b, c)
default:
return
}
s++
}
default:
panic(todo("%q %q", b, string(c)))
}
}
default:
return
}
s++
}
default:
panic(todo("%q %q", b, string(c)))
}
s++
}
}
func parseZone(s string) (name string, off int) {
_, name, off, _ = parseZoneOffset(s, false)
return name, off
}
func parseZoneOffset(s string, offOpt bool) (string, string, int, bool) {
s0 := s
name := s
for len(s) != 0 {
switch c := s[0]; {
case c >= 'A' && c <= 'Z', c >= 'a' && c <= 'z', c == '_', c == '/':
s = s[1:]
default:
name = name[:len(name)-len(s)]
if len(name) < 3 {
panic(todo("%q", s0))
}
if offOpt {
if len(s) == 0 {
return "", name, 0, false
}
if c := s[0]; (c < '0' || c > '9') && c != '+' && c != '-' {
return s, name, 0, false
}
}
s, off := parseOffset(s)
return s, name, off, true
}
}
return "", s0, 0, true
}
// [+|-]hh[:mm[:ss]]
func parseOffset(s string) (string, int) {
if len(s) == 0 {
panic(todo(""))
}
k := 1
switch s[0] {
case '+':
// nop
s = s[1:]
case '-':
k = -1
s = s[1:]
}
s, hh, ok := parseUint(s)
if !ok {
panic(todo(""))
}
n := hh * 3600
if len(s) == 0 || s[0] != ':' {
return s, k * n
}
s = s[1:] // ':'
if len(s) == 0 {
panic(todo(""))
}
s, mm, ok := parseUint(s)
if !ok {
panic(todo(""))
}
n += mm * 60
if len(s) == 0 || s[0] != ':' {
return s, k * n
}
s = s[1:] // ':'
if len(s) == 0 {
panic(todo(""))
}
s, ss, _ := parseUint(s)
return s, k * (n + ss)
}
func parseUint(s string) (string, int, bool) {
var ok bool
var r int
for len(s) != 0 {
switch c := s[0]; {
case c >= '0' && c <= '9':
ok = true
r0 := r
r = 10*r + int(c) - '0'
if r < r0 {
panic(todo(""))
}
s = s[1:]
default:
return s, r, ok
}
}
return s, r, ok
}
// https://stackoverflow.com/a/53052382
//
// isTimeDST returns true if time t occurs within daylight saving time
// for its time zone.
func isTimeDST(t time.Time) bool {
// If the most recent (within the last year) clock change
// was forward then assume the change was from std to dst.
hh, mm, _ := t.UTC().Clock()
tClock := hh*60 + mm
for m := -1; m > -12; m-- {
// assume dst lasts for at least one month
hh, mm, _ := t.AddDate(0, m, 0).UTC().Clock()
clock := hh*60 + mm
if clock != tClock {
return clock > tClock
}
}
// assume no dst
return false
}
|