• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37552

pending completion
#37552

push

local

web-flow
Abbreviate varinfo signature and re-order for consistency (#48860)

1 of 1 new or added line in 1 file covered. (100.0%)

72746 of 83846 relevant lines covered (86.76%)

34617131.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

39.66
/stdlib/Dates/src/query.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
# Date Locales
4

5
struct DateLocale
6
    months::Vector{String}
2✔
7
    months_abbr::Vector{String}
8
    days_of_week::Vector{String}
9
    days_of_week_abbr::Vector{String}
10
    month_value::Dict{String, Int64}
11
    month_abbr_value::Dict{String, Int64}
12
    day_of_week_value::Dict{String, Int64}
13
    day_of_week_abbr_value::Dict{String, Int64}
14
end
15

16
function locale_dict(names::Vector{<:AbstractString})
8✔
17
    result = Dict{String, Int}()
8✔
18

19
    # Keep both the common case-sensitive version of the name and an all lowercase
20
    # version for case-insensitive matches. Storing both allows us to avoid using the
21
    # lowercase function during parsing.
22
    for i in 1:length(names)
16✔
23
        name = names[i]
70✔
24
        result[name] = i
70✔
25
        result[lowercase(name)] = i
70✔
26
    end
132✔
27
    return result
8✔
28
end
29

30
"""
31
    DateLocale(["January", "February",...], ["Jan", "Feb",...],
32
               ["Monday", "Tuesday",...], ["Mon", "Tue",...])
33

34
Create a locale for parsing or printing textual month names.
35

36
Arguments:
37

38
- `months::Vector`: 12 month names
39
- `months_abbr::Vector`: 12 abbreviated month names
40
- `days_of_week::Vector`: 7 days of week
41
- `days_of_week_abbr::Vector`: 7 days of week abbreviated
42

43
This object is passed as the last argument to `tryparsenext` and `format`
44
methods defined for each `AbstractDateToken` type.
45
"""
46
function DateLocale(months::Vector, months_abbr::Vector,
2✔
47
                    days_of_week::Vector, days_of_week_abbr::Vector)
48
    DateLocale(
2✔
49
        months, months_abbr, days_of_week, days_of_week_abbr,
50
        locale_dict(months), locale_dict(months_abbr),
51
        locale_dict(days_of_week), locale_dict(days_of_week_abbr),
52
    )
53
end
54

55
const ENGLISH = DateLocale(
56
    ["January", "February", "March", "April", "May", "June",
57
     "July", "August", "September", "October", "November", "December"],
58
    ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
59
     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
60
    ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
61
    ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
62
)
63

64
const LOCALES = Dict{String, DateLocale}("english" => ENGLISH)
65

66
for (fn, field) in zip(
67
    [:dayname_to_value, :dayabbr_to_value, :monthname_to_value, :monthabbr_to_value],
68
    [:day_of_week_value, :day_of_week_abbr_value, :month_value, :month_abbr_value],
69
)
70
    @eval @inline function $fn(word::AbstractString, locale::DateLocale)
32✔
71
        # Maximize performance by attempting to avoid the use of `lowercase` and trying
72
        # a case-sensitive lookup first
73
        value = get(locale.$field, word, 0)
62✔
74
        if value == 0
32✔
75
            value = get(locale.$field, lowercase(word), 0)
2✔
76
        end
77
        value
32✔
78
    end
79
end
80

81
# Date functions
82

83
### Core query functions
84

85
# Monday = 1....Sunday = 7
86
dayofweek(days) = mod1(days, 7)
26✔
87

88
# Number of days in year
89
"""
90
    daysinyear(dt::TimeType) -> Int
91

92
Return 366 if the year of `dt` is a leap year, otherwise return 365.
93

94
# Examples
95
```jldoctest
96
julia> daysinyear(1999)
97
365
98

99
julia> daysinyear(2000)
100
366
101
```
102
"""
103
daysinyear(y) = 365 + isleapyear(y)
×
104

105
# Day of the year
106
const MONTHDAYS = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
107
dayofyear(y, m, d) = MONTHDAYS[m] + d + (m > 2 && isleapyear(y))
×
108

109
### Days of the Week
110
"""
111
    dayofweek(dt::TimeType) -> Int64
112

113
Return the day of the week as an [`Int64`](@ref) with `1 = Monday, 2 = Tuesday, etc.`.
114

115
# Examples
116
```jldoctest
117
julia> dayofweek(Date("2000-01-01"))
118
6
119
```
120
"""
121
dayofweek(dt::TimeType) = dayofweek(days(dt))
26✔
122

123
const Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = 1, 2, 3, 4, 5, 6, 7
124
const Mon, Tue, Wed, Thu, Fri, Sat, Sun = 1, 2, 3, 4, 5, 6, 7
125
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))
126
    short_name = string(short_day)
127
    long_name = string(long_day)
128
    name_ind = day_ind
129
    ind_str = string(ii)
130
    @eval begin
131
        @doc """
132
        $($long_name)
133
        $($short_name)
134

135
        The $($name_ind) day of the week.
136

137
        # Examples
138
        ```jldoctest
139
        julia> $($long_name)
140
        $($ind_str)
141

142
        julia> $($short_name)
143
        $($ind_str)
144
        ```
145
        """ ($long_day, $short_day)
146
   end
147
end
148
dayname(day::Integer, locale::DateLocale) = locale.days_of_week[day]
2✔
149
dayabbr(day::Integer, locale::DateLocale) = locale.days_of_week_abbr[day]
17✔
150
dayname(day::Integer; locale::AbstractString="english") = dayname(day, LOCALES[locale])
×
151
dayabbr(day::Integer; locale::AbstractString="english") = dayabbr(day, LOCALES[locale])
×
152

153
"""
154
    dayname(dt::TimeType; locale="english") -> String
155
    dayname(day::Integer; locale="english") -> String
156

157
Return the full day name corresponding to the day of the week of the `Date` or `DateTime` in
158
the given `locale`. Also accepts `Integer`.
159

160
# Examples
161
```jldoctest
162
julia> dayname(Date("2000-01-01"))
163
"Saturday"
164

165
julia> dayname(4)
166
"Thursday"
167
```
168
"""
169
function dayname(dt::TimeType;locale::AbstractString="english")
×
170
    dayname(dayofweek(dt); locale=locale)
×
171
end
172

173
"""
174
    dayabbr(dt::TimeType; locale="english") -> String
175
    dayabbr(day::Integer; locale="english") -> String
176

177
Return the abbreviated name corresponding to the day of the week of the `Date` or `DateTime`
178
in the given `locale`. Also accepts `Integer`.
179

180
# Examples
181
```jldoctest
182
julia> dayabbr(Date("2000-01-01"))
183
"Sat"
184

185
julia> dayabbr(3)
186
"Wed"
187
```
188
"""
189
function dayabbr(dt::TimeType;locale::AbstractString="english")
×
190
    dayabbr(dayofweek(dt); locale=locale)
×
191
end
192

193
# Convenience methods for each day
194
ismonday(dt::TimeType) = dayofweek(dt) == Mon
7✔
195
istuesday(dt::TimeType) = dayofweek(dt) == Tue
×
196
iswednesday(dt::TimeType) = dayofweek(dt) == Wed
×
197
isthursday(dt::TimeType) = dayofweek(dt) == Thu
×
198
isfriday(dt::TimeType) = dayofweek(dt) == Fri
×
199
issaturday(dt::TimeType) = dayofweek(dt) == Sat
×
200
issunday(dt::TimeType) = dayofweek(dt) == Sun
×
201

202
# i.e. 1st Monday? 2nd Monday? 3rd Wednesday? 5th Sunday?
203
"""
204
    dayofweekofmonth(dt::TimeType) -> Int
205

206
For the day of week of `dt`, return which number it is in `dt`'s month. So if the day of
207
the week of `dt` is Monday, then `1 = First Monday of the month, 2 = Second Monday of the
208
month, etc.` In the range 1:5.
209

210
# Examples
211
```jldoctest
212
julia> dayofweekofmonth(Date("2000-02-01"))
213
1
214

215
julia> dayofweekofmonth(Date("2000-02-08"))
216
2
217

218
julia> dayofweekofmonth(Date("2000-02-15"))
219
3
220
```
221
"""
222
function dayofweekofmonth(dt::TimeType)
×
223
    d = day(dt)
×
224
    return d < 8 ? 1 : d < 15 ? 2 : d < 22 ? 3 : d < 29 ? 4 : 5
×
225
end
226

227
# Total number of a day of week in the month
228
# e.g. are there 4 or 5 Mondays in this month?
229
const TWENTYNINE = BitSet([1, 8, 15, 22, 29])
230
const THIRTY = BitSet([1, 2, 8, 9, 15, 16, 22, 23, 29, 30])
231
const THIRTYONE = BitSet([1, 2, 3, 8, 9, 10, 15, 16, 17, 22, 23, 24, 29, 30, 31])
232

233
"""
234
    daysofweekinmonth(dt::TimeType) -> Int
235

236
For the day of week of `dt`, return the total number of that day of the week in `dt`'s
237
month. Returns 4 or 5. Useful in temporal expressions for specifying the last day of a week
238
in a month by including `dayofweekofmonth(dt) == daysofweekinmonth(dt)` in the adjuster
239
function.
240

241
# Examples
242
```jldoctest
243
julia> daysofweekinmonth(Date("2005-01-01"))
244
5
245

246
julia> daysofweekinmonth(Date("2005-01-04"))
247
4
248
```
249
"""
250
function daysofweekinmonth(dt::TimeType)
×
251
    y, m, d = yearmonthday(dt)
×
252
    ld = daysinmonth(y, m)
×
253
    return ld == 28 ? 4 : ld == 29 ? ((d in TWENTYNINE) ? 5 : 4) :
×
254
           ld == 30 ? ((d in THIRTY) ? 5 : 4) :
255
           (d in THIRTYONE) ? 5 : 4
256
end
257

258
### Months
259
"""
260
    January
261

262
The first month of the year.
263

264
# Examples
265
```jldoctest
266
julia> January
267
1
268
```
269
"""
270
const January = 1
271

272
"""
273
    Jan
274

275
Abbreviation for [`January`](@ref).
276

277
# Examples
278
```jldoctest
279
julia> Jan
280
1
281
```
282
"""
283
const Jan = 1
284

285
"""
286
    February
287

288
The second month of the year.
289

290
# Examples
291
```jldoctest
292
julia> February
293
2
294
```
295
"""
296
const February = 2
297

298
"""
299
    Feb
300

301
Abbreviation for [`February`](@ref).
302

303
# Examples
304
```jldoctest
305
julia> Feb
306
2
307
```
308
"""
309
const Feb = 2
310

311
"""
312
    March
313

314
The third month of the year.
315

316
# Examples
317
```jldoctest
318
julia> March
319
3
320
```
321
"""
322
const March = 3
323

324
"""
325
    Mar
326

327
Abbreviation for [`March`](@ref).
328

329
# Examples
330
```jldoctest
331
julia> Mar
332
3
333
```
334
"""
335
const Mar = 3
336

337
"""
338
    April
339

340
The fourth month of the year.
341

342
# Examples
343
```jldoctest
344
julia> April
345
4
346
```
347
"""
348
const April = 4
349

350
"""
351
    Apr
352

353
Abbreviation for [`April`](@ref).
354

355
# Examples
356
```jldoctest
357
julia> Apr
358
4
359
```
360
"""
361
const Apr = 4
362

363
"""
364
    May
365

366
The fifth month of the year.
367

368
# Examples
369
```jldoctest
370
julia> May
371
5
372
```
373
"""
374
const May = 5
375

376
"""
377
    June
378

379
The sixth month of the year.
380

381
# Examples
382
```jldoctest
383
julia> June
384
6
385
```
386
"""
387
const June = 6
388

389
"""
390
    Jun
391

392
Abbreviation for [`June`](@ref).
393

394
# Examples
395
```jldoctest
396
julia> Jun
397
6
398
```
399
"""
400
const Jun = 6
401

402
"""
403
    July
404

405
The seventh month of the year.
406

407
# Examples
408
```jldoctest
409
julia> July
410
7
411
```
412
"""
413
const July = 7
414

415
"""
416
    Jul
417

418
Abbreviation for [`July`](@ref).
419

420
# Examples
421
```jldoctest
422
julia> Jul
423
7
424
```
425
"""
426
const Jul = 7
427

428
"""
429
    August
430

431
The eighth month of the year.
432

433
# Examples
434
```jldoctest
435
julia> August
436
8
437
```
438
"""
439
const August = 8
440

441
"""
442
    Aug
443

444
Abbreviation for [`August`](@ref).
445

446
# Examples
447
```jldoctest
448
julia> Aug
449
8
450
```
451
"""
452
const Aug = 8
453

454
"""
455
    September
456

457
The ninth month of the year.
458

459
# Examples
460
```jldoctest
461
julia> September
462
9
463
```
464
"""
465
const September = 9
466

467
"""
468
    Sep
469

470
Abbreviation for [`September`](@ref).
471

472
# Examples
473
```jldoctest
474
julia> Sep
475
9
476
```
477
"""
478
const Sep = 9
479

480
"""
481
    October
482

483
The tenth month of the year.
484

485
# Examples
486
```jldoctest
487
julia> October
488
10
489
```
490
"""
491
const October = 10
492

493
"""
494
    Oct
495

496
Abbreviation for [`October`](@ref).
497

498
# Examples
499
```jldoctest
500
julia> Oct
501
10
502
```
503
"""
504
const Oct = 10
505

506
"""
507
    November
508

509
The eleventh month of the year.
510

511
# Examples
512
```jldoctest
513
julia> November
514
11
515
```
516
"""
517
const November = 11
518

519
"""
520
    Nov
521

522
Abbreviation for [`November`](@ref).
523

524
# Examples
525
```jldoctest
526
julia> Nov
527
11
528
```
529
"""
530
const Nov = 11
531

532
"""
533
    December
534

535
The last month of the year.
536

537
# Examples
538
```jldoctest
539
julia> December
540
12
541
```
542
"""
543
const December = 12
544

545
"""
546
    Dec
547

548
Abbreviation for [`December`](@ref).
549

550
# Examples
551
```jldoctest
552
julia> Dec
553
12
554
```
555
"""
556
const Dec = 12
557

558
monthname(month::Integer, locale::DateLocale) = locale.months[month]
2✔
559
monthabbr(month::Integer, locale::DateLocale) = locale.months_abbr[month]
29✔
560
monthname(month::Integer; locale::AbstractString="english") = monthname(month, LOCALES[locale])
×
561
monthabbr(month::Integer; locale::AbstractString="english") = monthabbr(month, LOCALES[locale])
×
562

563
"""
564
    monthname(dt::TimeType; locale="english") -> String
565
    monthname(month::Integer, locale="english") -> String
566

567

568
Return the full name of the month of the `Date` or `DateTime` or `Integer` in the given `locale`.
569

570
# Examples
571
```jldoctest
572
julia> monthname(Date("2005-01-04"))
573
"January"
574

575
julia> monthname(2)
576
"February"
577
```
578
"""
579
function monthname(dt::TimeType; locale::AbstractString="english")
×
580
    monthname(month(dt); locale=locale)
×
581
end
582

583
"""
584
    monthabbr(dt::TimeType; locale="english") -> String
585
    monthabbr(month::Integer, locale="english") -> String
586

587
Return the abbreviated month name of the `Date` or `DateTime` or `Integer` in the given `locale`.
588

589
# Examples
590
```jldoctest
591
julia> monthabbr(Date("2005-01-04"))
592
"Jan"
593

594
julia> monthabbr(2)
595
"Feb"
596
```
597
"""
598
function monthabbr(dt::TimeType; locale::AbstractString="english")
×
599
    monthabbr(month(dt); locale=locale)
×
600
end
601

602
"""
603
    daysinmonth(dt::TimeType) -> Int
604

605
Return the number of days in the month of `dt`. Value will be 28, 29, 30, or 31.
606

607
# Examples
608
```jldoctest
609
julia> daysinmonth(Date("2000-01"))
610
31
611

612
julia> daysinmonth(Date("2001-02"))
613
28
614

615
julia> daysinmonth(Date("2000-02"))
616
29
617
```
618
"""
619
daysinmonth(dt::TimeType) = ((y, m) = yearmonth(dt); return daysinmonth(y, m))
×
620

621
### Years
622
"""
623
    isleapyear(dt::TimeType) -> Bool
624

625
Return `true` if the year of `dt` is a leap year.
626

627
# Examples
628
```jldoctest
629
julia> isleapyear(Date("2004"))
630
true
631

632
julia> isleapyear(Date("2005"))
633
false
634
```
635
"""
636
isleapyear(dt::TimeType) = isleapyear(year(dt))
×
637

638
"""
639
    dayofyear(dt::TimeType) -> Int
640

641
Return the day of the year for `dt` with January 1st being day 1.
642
"""
643
dayofyear(dt::TimeType) = ((y, m, d) = yearmonthday(dt); return dayofyear(y, m, d))
×
644

645
daysinyear(dt::TimeType) = 365 + isleapyear(dt)
×
646

647
### Quarters
648
"""
649
    quarterofyear(dt::TimeType) -> Int
650

651
Return the quarter that `dt` resides in. Range of value is 1:4.
652
"""
653
quarterofyear(dt::TimeType) = quarter(dt)
×
654

655
const QUARTERDAYS = (0, 31, 59, 0, 30, 61, 0, 31, 62, 0, 31, 61)
656

657
"""
658
    dayofquarter(dt::TimeType) -> Int
659

660
Return the day of the current quarter of `dt`. Range of value is 1:92.
661
"""
662
function dayofquarter(dt::TimeType)
×
663
    (y, m, d) = yearmonthday(dt)
×
664
    return QUARTERDAYS[m] + d + (m == 3 && isleapyear(y))
×
665
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc