-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathdatetime.go
722 lines (621 loc) · 15.6 KB
/
datetime.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
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
/*
Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package datetime
import (
"encoding/binary"
"time"
"vitess.io/vitess/go/mysql/decimal"
"vitess.io/vitess/go/vt/vthash"
)
const negMask = uint16(1 << 15)
type Time struct {
hour uint16
minute uint8
second uint8
nanosecond uint32
}
type Date struct {
year uint16
month uint8
day uint8
}
type DateTime struct {
Date Date
Time Time
}
const DefaultPrecision = 6
func (t Time) AppendFormat(b []byte, prec uint8) []byte {
if t.Neg() {
b = append(b, '-')
}
b = appendInt(b, t.Hour(), 2)
b = append(b, ':')
b = appendInt(b, t.Minute(), 2)
b = append(b, ':')
b = appendInt(b, t.Second(), 2)
if prec > 0 {
b = append(b, '.')
b = appendNsec(b, t.Nanosecond(), int(prec))
}
return b
}
func (t Time) Format(prec uint8) []byte {
return t.AppendFormat(make([]byte, 0, 16), prec)
}
func (t Time) FormatInt64() (n int64) {
tr := t.Round(0)
v := int64(tr.Hour())*10000 + int64(tr.Minute())*100 + int64(tr.Second())
if t.Neg() {
return -v
}
return v
}
func (t Time) FormatFloat64() (n float64) {
v := float64(t.Hour())*10000 + float64(t.Minute())*100 + float64(t.Second()) + float64(t.Nanosecond())/1e9
if t.Neg() {
return -v
}
return v
}
func (t Time) FormatDecimal() decimal.Decimal {
v := int64(t.Hour())*10000 + int64(t.Minute())*100 + int64(t.Second())
dec := decimal.NewFromInt(v)
dec = dec.Add(decimal.New(int64(t.Nanosecond()), -9))
if t.Neg() {
dec = dec.Neg()
}
return dec
}
func (t Time) ToDateTime(now time.Time) (out DateTime) {
return NewDateTimeFromStd(t.ToStdTime(now))
}
func (t Time) IsZero() bool {
return t.Hour() == 0 && t.Minute() == 0 && t.Second() == 0 && t.Nanosecond() == 0
}
// RoundForJSON rounds the time to the nearest 32nd hour. This is some really
// weird behavior that MySQL does when it casts a JSON time back to a MySQL
// TIME value. We just mimic the behavior here.
func (t Time) RoundForJSON() Time {
if t.Hour() < 32 {
return t
}
res := t
res.hour = uint16(t.Hour() % 32)
if t.Neg() {
res.hour |= negMask
}
return res
}
func (t Time) Hour() int {
return int(t.hour & ^negMask)
}
func (t Time) Minute() int {
return int(t.minute)
}
func (t Time) Second() int {
return int(t.second)
}
func (t Time) Nanosecond() int {
return int(t.nanosecond)
}
func (t Time) Neg() bool {
return t.hour&negMask != 0
}
func (t Time) Hash(h *vthash.Hasher) {
h.Write16(t.hour)
h.Write8(t.minute)
h.Write8(t.second)
h.Write32(t.nanosecond)
}
func (t Time) Compare(t2 Time) int {
if t.Neg() != t2.Neg() {
if t.Neg() {
return -1
}
return 1
}
// Need to swap if both are negative.
if t.Neg() {
t, t2 = t2, t
}
h1, h2 := t.Hour(), t2.Hour()
if h1 < h2 {
return -1
}
if h1 > h2 {
return 1
}
m1, m2 := t.Minute(), t2.Minute()
if m1 < m2 {
return -1
}
if m1 > m2 {
return 1
}
s1, s2 := t.Second(), t2.Second()
if s1 < s2 {
return -1
}
if s1 > s2 {
return 1
}
ns1, ns2 := t.Nanosecond(), t2.Nanosecond()
if ns1 < ns2 {
return -1
}
if ns1 > ns2 {
return 1
}
return 0
}
var precs = []int{1e9, 1e8, 1e7, 1e6, 1e5, 1e4, 1e3, 1e2, 1e1, 1e0}
func (t Time) Round(p int) (r Time) {
if t.nanosecond == 0 {
return t
}
n := int(t.nanosecond)
prec := precs[p]
s := (n / prec) * prec
l := s + prec
if n-s >= l-n {
n = l
} else {
n = s
}
r = t
if n == 1e9 {
r.second++
n = 0
if r.second == 60 {
r.minute++
r.second = 0
if r.minute == 60 {
r.hour++
r.minute = 0
}
}
}
r.nanosecond = uint32(n)
return r
}
func (d Date) IsZero() bool {
return d.Year() == 0 && d.Month() == 0 && d.Day() == 0
}
func (d Date) Year() int {
return int(d.year)
}
func (d Date) Month() int {
return int(d.month)
}
func (d Date) Day() int {
return int(d.day)
}
func (d Date) Hash(h *vthash.Hasher) {
h.Write16(d.year)
h.Write8(d.month)
h.Write8(d.day)
}
func (d Date) Weekday() time.Weekday {
// Go considers 0000-01-01 day as Saturday, while
// MySQL considers it to be Sunday, now 0000-02-29 exists in
// Go but not in MySQL so it balances out after that
wd := d.ToStdTime(time.Local).Weekday()
if d.Year() == 0 && d.Month() <= 2 {
wd = (wd + 1) % 7
}
return wd
}
func (d Date) Yearday() int {
return d.ToStdTime(time.Local).YearDay()
}
func (d Date) ISOWeek() (int, int) {
return d.ToStdTime(time.Local).ISOWeek()
}
// SundayWeek returns the year and week number of the current
// date, when week numbers are defined by starting on the first
// Sunday of the year.
func (d Date) SundayWeek() (int, int) {
t := d.ToStdTime(time.Local)
// Since the week numbers always start on a Sunday, we can look
// at the week number of Sunday itself. So we shift back to last
// Sunday we saw and compute the week number based on that.
sun := t.AddDate(0, 0, -int(t.Weekday()))
return sun.Year(), (sun.YearDay()-1)/7 + 1
}
// MondayWeek returns the year and week number of the current
// date, when week numbers are defined by starting on the first
// Monday of the year.
func (d Date) MondayWeek() (int, int) {
t := d.ToStdTime(time.Local)
// Since the week numbers always start on a Monday, we can look
// at the week number of Monday itself. So we shift back to last
// Monday we saw and compute the week number based on that.
wd := (t.Weekday() + 6) % 7
mon := t.AddDate(0, 0, -int(wd))
return mon.Year(), (mon.YearDay()-1)/7 + 1
}
// Sunday4DayWeek returns the year and week number of the current
// date, when week numbers are defined by starting on the Sunday
// where week 1 is defined as having at least 4 days in the new
// year.
func (d Date) Sunday4DayWeek() (int, int) {
t := d.ToStdTime(time.Local)
// In this format, the first Wednesday of the year is always
// in the first week. So we can look at the week number of
// Wednesday in the same week. On days before Wednesday, we need
// to move the time forward to Wednesday, on days after we need to
// move it back to Wednesday.
var wed time.Time
switch wd := t.Weekday(); {
case wd == 3:
wed = t
case wd < 3:
wed = t.AddDate(0, 0, int(3-t.Weekday()))
case wd > 3:
wed = t.AddDate(0, 0, -int(t.Weekday()-3))
}
return wed.Year(), (wed.YearDay()-1)/7 + 1
}
const DefaultWeekMode = 0
func (d Date) Week(mode int) int {
switch mode & 7 {
case 0:
year, week := d.SundayWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 1:
year, week := d.ISOWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 2:
_, week := d.SundayWeek()
return week
case 3:
_, week := d.ISOWeek()
return week
case 4:
year, week := d.Sunday4DayWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 5:
year, week := d.MondayWeek()
if year < d.Year() {
return 0
} else if year > d.Year() {
return 53
}
return week
case 6:
_, week := d.Sunday4DayWeek()
return week
case 7:
_, week := d.MondayWeek()
return week
default:
return d.Week(DefaultWeekMode)
}
}
func (d Date) YearWeek(mode int) int {
switch mode {
case 0, 2:
year, week := d.SundayWeek()
return year*100 + week
case 1, 3:
year, week := d.ISOWeek()
return year*100 + week
case 4, 6:
year, week := d.Sunday4DayWeek()
return year*100 + week
case 5, 7:
year, week := d.MondayWeek()
return year*100 + week
default:
return d.YearWeek(DefaultWeekMode)
}
}
func (d Date) Quarter() int {
switch d.Month() {
case 0:
return 0
case 1, 2, 3:
return 1
case 4, 5, 6:
return 2
case 7, 8, 9:
return 3
case 10, 11, 12:
return 4
default:
panic("unreachable")
}
}
func (dt DateTime) IsZero() bool {
return dt.Date.IsZero() && dt.Time.IsZero()
}
func (dt DateTime) Hash(h *vthash.Hasher) {
dt.Date.Hash(h)
dt.Time.Hash(h)
}
func (t Time) ToDuration() time.Duration {
duration := time.Duration(t.Hour())*time.Hour +
time.Duration(t.Minute())*time.Minute +
time.Duration(t.Second())*time.Second +
time.Duration(t.Nanosecond())*time.Nanosecond
if t.Neg() {
return -duration
}
return duration
}
func (t Time) toStdTime(year int, month time.Month, day int, loc *time.Location) (out time.Time) {
hours := t.Hour()
minutes := t.Minute()
secs := t.Second()
nsecs := t.Nanosecond()
if t.Neg() {
hours = -hours
minutes = -minutes
secs = -secs
nsecs = -nsecs
}
return time.Date(year, month, day, hours, minutes, secs, nsecs, loc)
}
func (t Time) ToStdTime(now time.Time) (out time.Time) {
year, month, day := now.Date()
return t.toStdTime(year, month, day, now.Location())
}
func (t Time) AddInterval(itv *Interval, stradd bool) (Time, uint8, bool) {
dt := DateTime{Time: t}
ok := dt.addInterval(itv)
return dt.Time, itv.precision(stradd), ok
}
func (t Time) toDuration() time.Duration {
dur := time.Duration(t.hour)*time.Hour + time.Duration(t.minute)*time.Minute + time.Duration(t.second)*time.Second + time.Duration(t.nanosecond)*time.Nanosecond
if t.Neg() {
return -dur
}
return dur
}
func (t Time) ToSeconds() int64 {
return int64(t.ToDuration().Seconds())
}
func (d Date) ToStdTime(loc *time.Location) (out time.Time) {
return time.Date(d.Year(), time.Month(d.Month()), d.Day(), 0, 0, 0, 0, loc)
}
func (dt DateTime) ToStdTime(now time.Time) time.Time {
zerodate := dt.Date.IsZero()
zerotime := dt.Time.IsZero()
switch {
case zerodate && zerotime:
return time.Time{}
case zerodate:
return dt.Time.ToStdTime(now)
case zerotime:
return dt.Date.ToStdTime(now.Location())
default:
year, month, day := dt.Date.Year(), time.Month(dt.Date.Month()), dt.Date.Day()
return dt.Time.toStdTime(year, month, day, now.Location())
}
}
func (dt DateTime) Format(prec uint8) []byte {
return DateTime_YYYY_MM_DD_hh_mm_ss.Format(dt, prec)
}
func (d Date) Format() []byte {
return Date_YYYY_MM_DD.Format(DateTime{Date: d}, 0)
}
func (d Date) FormatInt64() int64 {
return int64(d.Year())*10000 + int64(d.Month())*100 + int64(d.Day())
}
func (d Date) Compare(d2 Date) int {
y1, y2 := d.Year(), d2.Year()
if y1 < y2 {
return -1
}
if y1 > y2 {
return 1
}
m1, m2 := d.Month(), d2.Month()
if m1 < m2 {
return -1
}
if m1 > m2 {
return 1
}
day1, day2 := d.Day(), d2.Day()
if day1 < day2 {
return -1
}
if day1 > day2 {
return 1
}
return 0
}
func (d Date) AddInterval(itv *Interval) (Date, bool) {
dt := DateTime{Date: d}
ok := dt.addInterval(itv)
return dt.Date, ok
}
func (dt DateTime) FormatInt64() int64 {
d := dt.Round(0)
return d.Date.FormatInt64()*1000000 + d.Time.FormatInt64()
}
func (dt DateTime) FormatFloat64() float64 {
return float64(dt.Date.FormatInt64()*1000000) + dt.Time.FormatFloat64()
}
func (dt DateTime) FormatDecimal() decimal.Decimal {
return decimal.New(dt.Date.FormatInt64(), 6).Add(dt.Time.FormatDecimal())
}
func (dt DateTime) Compare(dt2 DateTime) int {
zerodate1, zerodate2 := dt.Date.IsZero(), dt2.Date.IsZero()
switch {
case zerodate1 && zerodate2:
return dt.Time.Compare(dt2.Time)
case zerodate1 || zerodate2:
// if we're comparing a time to a datetime, we need to normalize them
// both into datetimes; this normalization is not trivial because negative
// times result in a date change, so let the standard library handle this
// Using the current time is OK here since the comparison is relative
now := time.Now()
return dt.ToStdTime(now).Compare(dt2.ToStdTime(now))
}
if cmp := dt.Date.Compare(dt2.Date); cmp != 0 {
return cmp
}
return dt.Time.Compare(dt2.Time)
}
func (dt DateTime) AddInterval(itv *Interval, prec uint8, stradd bool) (DateTime, uint8, bool) {
ok := dt.addInterval(itv)
return dt, max(prec, itv.precision(stradd)), ok
}
func (dt DateTime) Round(p int) (r DateTime) {
if dt.Time.nanosecond == 0 {
return dt
}
n := dt.Time.Nanosecond()
prec := precs[p]
s := (n / prec) * prec
l := s + prec
if n-s >= l-n {
n = l
} else {
n = s
}
r = dt
if n == 1e9 {
r.Time.nanosecond = 0
r.addInterval(&Interval{timeparts: timeparts{sec: 1}, unit: IntervalSecond})
} else {
r.Time.nanosecond = uint32(n)
}
return r
}
func (dt DateTime) toDuration() time.Duration {
dur := dt.Time.toDuration()
if !dt.Date.IsZero() {
dur += time.Duration(dt.Date.Day()-1) * durationPerDay
}
return dur
}
func (dt *DateTime) addInterval(itv *Interval) bool {
switch {
case itv.unit.HasTimeParts():
if !itv.inRange() {
return false
}
dur := dt.toDuration()
dur += itv.toDuration()
days := time.Duration(0)
if !dt.Date.IsZero() {
days = dur / durationPerDay
dur -= days * durationPerDay
if dur < 0 {
dur += durationPerDay
days--
}
}
dt.Time.nanosecond = uint32((dur % time.Second) / time.Nanosecond)
dt.Time.second = uint8((dur % time.Minute) / time.Second)
dt.Time.minute = uint8((dur % time.Hour) / time.Minute)
dt.Time.hour = uint16(dur / time.Hour)
daynum := MysqlDayNumber(dt.Date.Year(), dt.Date.Month(), 1) + int(days)
if daynum < 0 || daynum > maxDay {
return false
}
dt.Date.year, dt.Date.month, dt.Date.day = mysqlDateFromDayNumber(daynum)
return true
case itv.unit.HasDayParts():
daynum := MysqlDayNumber(dt.Date.Year(), dt.Date.Month(), dt.Date.Day())
daynum += itv.day
dt.Date.year, dt.Date.month, dt.Date.day = mysqlDateFromDayNumber(daynum)
return true
case itv.unit.HasMonthParts():
months := dt.Date.Year()*12 + itv.year*12 + (dt.Date.Month() - 1) + itv.month
if months < 0 || months >= 120000 {
return false
}
year := months / 12
month := (months % 12) + 1
dt.Date.year = uint16(year)
dt.Date.month = uint8(month)
// MySQL quirk: if the original date was in a day that the new month
// doesn't have, the date is offset backwards to the last day of
// the new month. This is the opposite to normal date handling where
// we'd offset days into the next month.
if dim := daysIn(time.Month(month), year); dt.Date.Day() > dim {
dt.Date.day = uint8(dim)
}
return true
case itv.unit == IntervalYear:
if itv.year > 10000 {
return false
}
year := dt.Date.Year() + itv.year
dt.Date.year = uint16(year)
// MySQL quirk: if the original date was Feb 29th on a leap year, and
// the resulting year is not a leap year, the date is offset backwards.
// This is the opposite to what normal date handling does.
if dt.Date.Month() == 2 && dt.Date.Day() == 29 && !isLeap(year) {
dt.Date.day = 28
}
return true
default:
panic("unexpected IntervalType")
}
}
func (dt DateTime) WeightString(dst []byte) []byte {
// This logic does the inverse of what we do in the binlog parser for the datetime2 type.
year, month, day := dt.Date.Year(), dt.Date.Month(), dt.Date.Day()
ymd := uint64(year*13+month)<<5 | uint64(day)
hms := uint64(dt.Time.Hour())<<12 | uint64(dt.Time.Minute())<<6 | uint64(dt.Time.Second())
raw := (ymd<<17|hms)<<24 + uint64(dt.Time.Nanosecond()/1000)
if dt.Time.Neg() {
raw = -raw
}
raw = raw ^ (1 << 63)
return binary.BigEndian.AppendUint64(dst, raw)
}
func NewDateFromStd(t time.Time) Date {
year, month, day := t.Date()
return Date{
year: uint16(year),
month: uint8(month),
day: uint8(day),
}
}
func NewTimeFromStd(t time.Time) Time {
hour, min, sec := t.Clock()
nsec := t.Nanosecond()
return Time{
hour: uint16(hour),
minute: uint8(min),
second: uint8(sec),
nanosecond: uint32(nsec),
}
}
func NewDateTimeFromStd(t time.Time) DateTime {
return DateTime{
Date: NewDateFromStd(t),
Time: NewTimeFromStd(t),
}
}