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

JuliaLang / julia / #37527

pending completion
#37527

push

local

web-flow
make `IRShow.method_name` inferrable (#49607)

18 of 18 new or added lines in 3 files covered. (100.0%)

68710 of 81829 relevant lines covered (83.97%)

33068903.12 hits per line

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

93.44
/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
91,197✔
6

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

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

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

22
# TimeType-Year arithmetic
23
function (+)(dt::DateTime, y::Year)
29✔
24
    oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m)
87✔
25
    return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
29✔
26
end
27
function (+)(dt::Date,y::Year)
39✔
28
    oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m)
117✔
29
    return Date(ny, m, d <= ld ? d : ld)
39✔
30
end
31
function (-)(dt::DateTime,y::Year)
7✔
32
    oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m)
21✔
33
    return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
7✔
34
end
35
function (-)(dt::Date,y::Year)
29✔
36
    oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m)
87✔
37
    return Date(ny, m, d <= ld ? d : ld)
29✔
38
end
39

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

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

54
function (+)(dt::Date, z::Month)
680✔
55
    y,m,d = yearmonthday(dt)
680✔
56
    ny = yearwrap(y, m, value(z))
680✔
57
    mm = monthwrap(m, value(z)); ld = daysinmonth(ny, mm)
2,040✔
58
    return Date(ny, mm, d <= ld ? d : ld)
680✔
59
end
60
function (-)(dt::DateTime, z::Month)
10✔
61
    y,m,d = yearmonthday(dt)
10✔
62
    ny = yearwrap(y, m, -value(z))
10✔
63
    mm = monthwrap(m, -value(z)); ld = daysinmonth(ny, mm)
30✔
64
    return DateTime(ny, mm, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
10✔
65
end
66
function (-)(dt::Date, z::Month)
40✔
67
    y,m,d = yearmonthday(dt)
40✔
68
    ny = yearwrap(y, m, -value(z))
40✔
69
    mm = monthwrap(m, -value(z)); ld = daysinmonth(ny, mm)
120✔
70
    return Date(ny, mm, d <= ld ? d : ld)
40✔
71
end
72

73
(+)(x::Date, y::Quarter) = x + Month(y)
3✔
74
(-)(x::Date, y::Quarter) = x - Month(y)
1✔
75
(+)(x::DateTime, y::Quarter) = x + Month(y)
2✔
76
(-)(x::DateTime, y::Quarter) = x - Month(y)
×
77
(+)(x::Date, y::Week) = return Date(UTD(value(x) + 7 * value(y)))
754✔
78
(-)(x::Date, y::Week) = return Date(UTD(value(x) - 7 * value(y)))
4✔
79
(+)(x::Date, y::Day)  = return Date(UTD(value(x) + value(y)))
43,430✔
80
(-)(x::Date, y::Day)  = return Date(UTD(value(x) - value(y)))
197✔
81
(+)(x::DateTime, y::Period) = return DateTime(UTM(value(x) + toms(y)))
139,357✔
82
(-)(x::DateTime, y::Period) = return DateTime(UTM(value(x) - toms(y)))
80✔
83
(+)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) + tons(y)))
36,692✔
84
(-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y)))
64✔
85
(+)(y::Period, x::TimeType) = x + y
9✔
86

87
# Missing support
88
(+)(x::AbstractTime, y::Missing) = missing
12✔
89
(+)(x::Missing, y::AbstractTime) = missing
12✔
90
(-)(x::AbstractTime, y::Missing) = missing
12✔
91
(-)(x::Missing, y::AbstractTime) = missing
12✔
92

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

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