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

JuliaLang / julia / #37550

pending completion
#37550

push

local

web-flow
Extend comparison lifting to `Core.ifelse` (#49882)

This change extends our existing transformation for:
   φ(a,b) === Const(c)   =>   φ(a === c, b === c)

to perform the analogous transformation for `Core.ifelse`:
   Core.ifelse(cond, a, b) === Const(c)
  =>
   Core.ifelse(cond, a === c, b === c)

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

72705 of 83986 relevant lines covered (86.57%)

20561515.97 hits per line

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

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

3
# Instant arithmetic
4
(+)(x::Instant) = x
×
5
(-)(x::T, y::T) where {T<:Instant} = x.periods - y.periods
906,627✔
6

7
# TimeType arithmetic
8
(+)(x::TimeType) = x
×
9
(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant
906,899✔
10
(-)(x::TimeType, y::TimeType) = -(promote(x, y)...)
×
11

12
# Date-Time arithmetic
13
"""
14
    dt::Date + t::Time -> DateTime
15

16
The addition of a `Date` with a `Time` produces a `DateTime`. The hour, minute, second, and millisecond parts of
17
the `Time` are used along with the year, month, and day of the `Date` to create the new `DateTime`.
18
Non-zero microseconds or nanoseconds in the `Time` type will result in an `InexactError` being thrown.
19
"""
20
(+)(dt::Date, t::Time) = DateTime(dt ,t)
×
21
(+)(t::Time, dt::Date) = DateTime(dt, t)
×
22

23
# TimeType-Year arithmetic
24
function (+)(dt::DateTime, y::Year)
145,058✔
25
    oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m)
435,174✔
26
    return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
145,058✔
27
end
28
function (+)(dt::Date,y::Year)
883,170✔
29
    oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m)
2,649,510✔
30
    return Date(ny, m, d <= ld ? d : ld)
883,170✔
31
end
32
function (-)(dt::DateTime,y::Year)
10✔
33
    oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m)
30✔
34
    return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
10✔
35
end
36
function (-)(dt::Date,y::Year)
10✔
37
    oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m)
30✔
38
    return Date(ny, m, d <= ld ? d : ld)
10✔
39
end
40

41
# TimeType-Month arithmetic
42
# monthwrap adds two months with wraparound behavior (i.e. 12 + 1 == 1)
43
monthwrap(m1, m2) = (v = mod1(m1 + m2, 12); return v < 0 ? 12 + v : v)
3,586,222✔
44
# yearwrap takes a starting year/month and a month to add and returns
45
# the resulting year with wraparound behavior (i.e. 2000-12 + 1 == 2001)
46
yearwrap(y, m1, m2) = y + fld(m1 + m2 - 1, 12)
1,202,074✔
47

48
function (+)(dt::DateTime, z::Month)
185,798✔
49
    y,m,d = yearmonthday(dt)
185,798✔
50
    ny = yearwrap(y, m, value(z))
185,798✔
51
    mm = monthwrap(m, value(z)); ld = daysinmonth(ny, mm)
537,394✔
52
    return DateTime(ny, mm, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
185,798✔
53
end
54

55
function (+)(dt::Date, z::Month)
1,016,231✔
56
    y,m,d = yearmonthday(dt)
1,016,231✔
57
    ny = yearwrap(y, m, value(z))
1,016,231✔
58
    mm = monthwrap(m, value(z)); ld = daysinmonth(ny, mm)
3,048,693✔
59
    return Date(ny, mm, d <= ld ? d : ld)
1,016,231✔
60
end
61
function (-)(dt::DateTime, z::Month)
24✔
62
    y,m,d = yearmonthday(dt)
24✔
63
    ny = yearwrap(y, m, -value(z))
24✔
64
    mm = monthwrap(m, -value(z)); ld = daysinmonth(ny, mm)
72✔
65
    return DateTime(ny, mm, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
24✔
66
end
67
function (-)(dt::Date, z::Month)
21✔
68
    y,m,d = yearmonthday(dt)
21✔
69
    ny = yearwrap(y, m, -value(z))
21✔
70
    mm = monthwrap(m, -value(z)); ld = daysinmonth(ny, mm)
63✔
71
    return Date(ny, mm, d <= ld ? d : ld)
21✔
72
end
73

74
(+)(x::Date, y::Quarter) = x + Month(y)
101,958✔
75
(-)(x::Date, y::Quarter) = x - Month(y)
10✔
76
(+)(x::DateTime, y::Quarter) = x + Month(y)
101,905✔
77
(-)(x::DateTime, y::Quarter) = x - Month(y)
10✔
78
(+)(x::Date, y::Week) = return Date(UTD(value(x) + 7 * value(y)))
54,139✔
79
(-)(x::Date, y::Week) = return Date(UTD(value(x) - 7 * value(y)))
10✔
80
(+)(x::Date, y::Day)  = return Date(UTD(value(x) + value(y)))
177,653✔
81
(-)(x::Date, y::Day)  = return Date(UTD(value(x) - value(y)))
220,646✔
82
(+)(x::DateTime, y::Period) = return DateTime(UTM(value(x) + toms(y)))
526,169,993✔
83
(-)(x::DateTime, y::Period) = return DateTime(UTM(value(x) - toms(y)))
20,858✔
84
(+)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) + tons(y)))
36,984✔
85
(-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y)))
62✔
86
(+)(y::Period, x::TimeType) = x + y
2✔
87

88
# Missing support
89
(+)(x::AbstractTime, y::Missing) = missing
×
90
(+)(x::Missing, y::AbstractTime) = missing
×
91
(-)(x::AbstractTime, y::Missing) = missing
×
92
(-)(x::Missing, y::AbstractTime) = missing
×
93

94
# AbstractArray{TimeType}, AbstractArray{TimeType}
95
(-)(x::OrdinalRange{T}, y::OrdinalRange{T}) where {T<:TimeType} = Vector(x) - Vector(y)
×
96
(-)(x::AbstractRange{T}, y::AbstractRange{T}) where {T<:TimeType} = Vector(x) - Vector(y)
×
97

98
# Allow dates, times, and time zones to broadcast as unwrapped scalars
99
Base.Broadcast.broadcastable(x::AbstractTime) = Ref(x)
34✔
100
Base.Broadcast.broadcastable(x::TimeZone) = Ref(x)
×
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

© 2025 Coveralls, Inc