-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
query.jl
662 lines (520 loc) · 12.5 KB
/
query.jl
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
# This file is a part of Julia. License is MIT: https://julialang.org/license
# Date Locales
struct DateLocale
months::Vector{String}
months_abbr::Vector{String}
days_of_week::Vector{String}
days_of_week_abbr::Vector{String}
month_value::Dict{String, Int64}
month_abbr_value::Dict{String, Int64}
day_of_week_value::Dict{String, Int64}
day_of_week_abbr_value::Dict{String, Int64}
end
function locale_dict(names::Vector{<:AbstractString})
result = Dict{String, Int}()
# Keep both the common case-sensitive version of the name and an all lowercase
# version for case-insensitive matches. Storing both allows us to avoid using the
# lowercase function during parsing.
for i in 1:length(names)
name = names[i]
result[name] = i
result[lowercase(name)] = i
end
return result
end
"""
DateLocale(["January", "February",...], ["Jan", "Feb",...],
["Monday", "Tuesday",...], ["Mon", "Tue",...])
Create a locale for parsing or printing textual month names.
Arguments:
- `months::Vector`: 12 month names
- `months_abbr::Vector`: 12 abbreviated month names
- `days_of_week::Vector`: 7 days of week
- `days_of_week_abbr::Vector`: 7 days of week abbreviated
This object is passed as the last argument to `tryparsenext` and `format`
methods defined for each `AbstractDateToken` type.
"""
function DateLocale(months::Vector, months_abbr::Vector,
days_of_week::Vector, days_of_week_abbr::Vector)
DateLocale(
months, months_abbr, days_of_week, days_of_week_abbr,
locale_dict(months), locale_dict(months_abbr),
locale_dict(days_of_week), locale_dict(days_of_week_abbr),
)
end
const ENGLISH = DateLocale(
["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"],
["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
)
const LOCALES = Dict{String, DateLocale}("english" => ENGLISH)
for (fn, field) in zip(
[:dayname_to_value, :dayabbr_to_value, :monthname_to_value, :monthabbr_to_value],
[:day_of_week_value, :day_of_week_abbr_value, :month_value, :month_abbr_value],
)
@eval @inline function $fn(word::AbstractString, locale::DateLocale)
# Maximize performance by attempting to avoid the use of `lowercase` and trying
# a case-sensitive lookup first
value = get(locale.$field, word, 0)
if value == 0
value = get(locale.$field, lowercase(word), 0)
end
value
end
end
# Date functions
### Core query functions
# Monday = 1....Sunday = 7
dayofweek(days) = mod1(days, 7)
# Number of days in year
"""
daysinyear(dt::TimeType) -> Int
Return 366 if the year of `dt` is a leap year, otherwise return 365.
# Examples
```jldoctest
julia> Dates.daysinyear(1999)
365
julia> Dates.daysinyear(2000)
366
```
"""
daysinyear(y) = 365 + isleapyear(y)
# Day of the year
const MONTHDAYS = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
dayofyear(y, m, d) = MONTHDAYS[m] + d + (m > 2 && isleapyear(y))
### Days of the Week
"""
dayofweek(dt::TimeType) -> Int64
Return the day of the week as an [`Int64`](@ref) with `1 = Monday, 2 = Tuesday, etc.`.
# Examples
```jldoctest
julia> Dates.dayofweek(Date("2000-01-01"))
6
```
"""
dayofweek(dt::TimeType) = dayofweek(days(dt))
const Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = 1, 2, 3, 4, 5, 6, 7
const Mon, Tue, Wed, Thu, Fri, Sat, Sun = 1, 2, 3, 4, 5, 6, 7
for (ii, day_ind, short_day, long_day) in ((1, "first", :Mon, :Monday), (2, "second", :Tue, :Tuesday), (3, "third", :Wed, :Wednesday), (4, "fourth", :Thu, :Thursday), (5, "fifth", :Fri, :Friday), (6, "sixth", :Sat, :Saturday), (7, "seventh", :Sun, :Sunday))
short_name = string(short_day)
long_name = string(long_day)
name_ind = day_ind
ind_str = string(ii)
@eval begin
@doc """
$($long_name)
$($short_name)
The $($name_ind) day of the week.
# Examples
```jldoctest
julia> $($long_name)
$($ind_str)
julia> $($short_name)
$($ind_str)
```
""" ($long_day, $short_day)
end
end
dayname(day::Integer, locale::DateLocale) = locale.days_of_week[day]
dayabbr(day::Integer, locale::DateLocale) = locale.days_of_week_abbr[day]
dayname(day::Integer; locale::AbstractString="english") = dayname(day, LOCALES[locale])
dayabbr(day::Integer; locale::AbstractString="english") = dayabbr(day, LOCALES[locale])
"""
dayname(dt::TimeType; locale="english") -> String
dayname(day::Integer; locale="english") -> String
Return the full day name corresponding to the day of the week of the `Date` or `DateTime` in
the given `locale`. Also accepts `Integer`.
# Examples
```jldoctest
julia> Dates.dayname(Date("2000-01-01"))
"Saturday"
julia> Dates.dayname(4)
"Thursday"
```
"""
function dayname(dt::TimeType;locale::AbstractString="english")
dayname(dayofweek(dt); locale=locale)
end
"""
dayabbr(dt::TimeType; locale="english") -> String
dayabbr(day::Integer; locale="english") -> String
Return the abbreviated name corresponding to the day of the week of the `Date` or `DateTime`
in the given `locale`. Also accepts `Integer`.
# Examples
```jldoctest
julia> Dates.dayabbr(Date("2000-01-01"))
"Sat"
julia> Dates.dayabbr(3)
"Wed"
```
"""
function dayabbr(dt::TimeType;locale::AbstractString="english")
dayabbr(dayofweek(dt); locale=locale)
end
# Convenience methods for each day
ismonday(dt::TimeType) = dayofweek(dt) == Mon
istuesday(dt::TimeType) = dayofweek(dt) == Tue
iswednesday(dt::TimeType) = dayofweek(dt) == Wed
isthursday(dt::TimeType) = dayofweek(dt) == Thu
isfriday(dt::TimeType) = dayofweek(dt) == Fri
issaturday(dt::TimeType) = dayofweek(dt) == Sat
issunday(dt::TimeType) = dayofweek(dt) == Sun
# i.e. 1st Monday? 2nd Monday? 3rd Wednesday? 5th Sunday?
"""
dayofweekofmonth(dt::TimeType) -> Int
For the day of week of `dt`, return which number it is in `dt`'s month. So if the day of
the week of `dt` is Monday, then `1 = First Monday of the month, 2 = Second Monday of the
month, etc.` In the range 1:5.
# Examples
```jldoctest
julia> Dates.dayofweekofmonth(Date("2000-02-01"))
1
julia> Dates.dayofweekofmonth(Date("2000-02-08"))
2
julia> Dates.dayofweekofmonth(Date("2000-02-15"))
3
```
"""
function dayofweekofmonth(dt::TimeType)
d = day(dt)
return d < 8 ? 1 : d < 15 ? 2 : d < 22 ? 3 : d < 29 ? 4 : 5
end
# Total number of a day of week in the month
# e.g. are there 4 or 5 Mondays in this month?
const TWENTYNINE = BitSet([1, 8, 15, 22, 29])
const THIRTY = BitSet([1, 2, 8, 9, 15, 16, 22, 23, 29, 30])
const THIRTYONE = BitSet([1, 2, 3, 8, 9, 10, 15, 16, 17, 22, 23, 24, 29, 30, 31])
"""
daysofweekinmonth(dt::TimeType) -> Int
For the day of week of `dt`, return the total number of that day of the week in `dt`'s
month. Returns 4 or 5. Useful in temporal expressions for specifying the last day of a week
in a month by including `dayofweekofmonth(dt) == daysofweekinmonth(dt)` in the adjuster
function.
# Examples
```jldoctest
julia> Dates.daysofweekinmonth(Date("2005-01-01"))
5
julia> Dates.daysofweekinmonth(Date("2005-01-04"))
4
```
"""
function daysofweekinmonth(dt::TimeType)
y, m, d = yearmonthday(dt)
ld = daysinmonth(y, m)
return ld == 28 ? 4 : ld == 29 ? ((d in TWENTYNINE) ? 5 : 4) :
ld == 30 ? ((d in THIRTY) ? 5 : 4) :
(d in THIRTYONE) ? 5 : 4
end
### Months
"""
January
The first month of the year.
# Examples
```jldoctest
julia> January
1
```
"""
const January = 1
"""
Jan
Abbreviation for [`January`](@ref).
# Examples
```jldoctest
julia> Jan
1
```
"""
const Jan = 1
"""
February
The second month of the year.
# Examples
```jldoctest
julia> February
2
```
"""
const February = 2
"""
Feb
Abbreviation for [`February`](@ref).
# Examples
```jldoctest
julia> Feb
2
```
"""
const Feb = 2
"""
March
The third month of the year.
# Examples
```jldoctest
julia> March
3
```
"""
const March = 3
"""
Mar
Abbreviation for [`March`](@ref).
# Examples
```jldoctest
julia> Mar
3
```
"""
const Mar = 3
"""
April
The fourth month of the year.
# Examples
```jldoctest
julia> April
4
```
"""
const April = 4
"""
Apr
Abbreviation for [`April`](@ref).
# Examples
```jldoctest
julia> Apr
4
```
"""
const Apr = 4
"""
May
The fifth month of the year.
# Examples
```jldoctest
julia> May
5
```
"""
const May = 5
"""
June
The sixth month of the year.
# Examples
```jldoctest
julia> June
6
```
"""
const June = 6
"""
Jun
Abbreviation for [`June`](@ref).
# Examples
```jldoctest
julia> Jun
6
```
"""
const Jun = 6
"""
July
The seventh month of the year.
# Examples
```jldoctest
julia> July
7
```
"""
const July = 7
"""
Jul
Abbreviation for [`July`](@ref).
# Examples
```jldoctest
julia> Jul
7
```
"""
const Jul = 7
"""
August
The eighth month of the year.
# Examples
```jldoctest
julia> August
8
```
"""
const August = 8
"""
Aug
Abbreviation for [`August`](@ref).
# Examples
```jldoctest
julia> Aug
8
```
"""
const Aug = 8
"""
September
The ninth month of the year.
# Examples
```jldoctest
julia> September
9
```
"""
const September = 9
"""
Sep
Abbreviation for [`September`](@ref).
# Examples
```jldoctest
julia> Sep
9
```
"""
const Sep = 9
"""
October
The tenth month of the year.
# Examples
```jldoctest
julia> October
10
```
"""
const October = 10
"""
Oct
Abbreviation for [`October`](@ref).
# Examples
```jldoctest
julia> Oct
10
```
"""
const Oct = 10
"""
November
The eleventh month of the year.
# Examples
```jldoctest
julia> November
11
```
"""
const November = 11
"""
Nov
Abbreviation for [`November`](@ref).
# Examples
```jldoctest
julia> Nov
11
```
"""
const Nov = 11
"""
December
The last month of the year.
# Examples
```jldoctest
julia> December
12
```
"""
const December = 12
"""
Dec
Abbreviation for [`December`](@ref).
# Examples
```jldoctest
julia> Dec
12
```
"""
const Dec = 12
monthname(month::Integer, locale::DateLocale) = locale.months[month]
monthabbr(month::Integer, locale::DateLocale) = locale.months_abbr[month]
monthname(month::Integer; locale::AbstractString="english") = monthname(month, LOCALES[locale])
monthabbr(month::Integer; locale::AbstractString="english") = monthabbr(month, LOCALES[locale])
"""
monthname(dt::TimeType; locale="english") -> String
monthname(month::Integer, locale="english") -> String
Return the full name of the month of the `Date` or `DateTime` or `Integer` in the given `locale`.
# Examples
```jldoctest
julia> Dates.monthname(Date("2005-01-04"))
"January"
julia> Dates.monthname(2)
"February"
```
"""
function monthname(dt::TimeType; locale::AbstractString="english")
monthname(month(dt); locale=locale)
end
"""
monthabbr(dt::TimeType; locale="english") -> String
monthabbr(month::Integer, locale="english") -> String
Return the abbreviated month name of the `Date` or `DateTime` or `Integer` in the given `locale`.
# Examples
```jldoctest
julia> Dates.monthabbr(Date("2005-01-04"))
"Jan"
julia> monthabbr(2)
"Feb"
```
"""
function monthabbr(dt::TimeType; locale::AbstractString="english")
monthabbr(month(dt); locale=locale)
end
"""
daysinmonth(dt::TimeType) -> Int
Return the number of days in the month of `dt`. Value will be 28, 29, 30, or 31.
# Examples
```jldoctest
julia> Dates.daysinmonth(Date("2000-01"))
31
julia> Dates.daysinmonth(Date("2001-02"))
28
julia> Dates.daysinmonth(Date("2000-02"))
29
```
"""
daysinmonth(dt::TimeType) = ((y, m) = yearmonth(dt); return daysinmonth(y, m))
### Years
"""
isleapyear(dt::TimeType) -> Bool
Return `true` if the year of `dt` is a leap year.
# Examples
```jldoctest
julia> Dates.isleapyear(Date("2004"))
true
julia> Dates.isleapyear(Date("2005"))
false
```
"""
isleapyear(dt::TimeType) = isleapyear(year(dt))
"""
dayofyear(dt::TimeType) -> Int
Return the day of the year for `dt` with January 1st being day 1.
"""
dayofyear(dt::TimeType) = ((y, m, d) = yearmonthday(dt); return dayofyear(y, m, d))
daysinyear(dt::TimeType) = 365 + isleapyear(dt)
### Quarters
"""
quarterofyear(dt::TimeType) -> Int
Return the quarter that `dt` resides in. Range of value is 1:4.
"""
quarterofyear(dt::TimeType) = quarter(dt)
const QUARTERDAYS = (0, 90, 181, 273)
"""
dayofquarter(dt::TimeType) -> Int
Return the day of the current quarter of `dt`. Range of value is 1:92.
"""
dayofquarter(dt::TimeType) = dayofyear(dt) - QUARTERDAYS[quarterofyear(dt)]