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

JuliaLang / julia / #37657

20 Oct 2023 05:31AM UTC coverage: 87.929% (+0.6%) from 87.296%
#37657

push

local

web-flow
Fix reference in manual (#51741)

73811 of 83944 relevant lines covered (87.93%)

12565063.6 hits per line

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

81.25
/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
1,008,273✔
6

7
# TimeType arithmetic
8
(+)(x::TimeType) = x
×
9
(-)(x::Date, y::Date) = x.instant - y.instant
966,736✔
10
(-)(x::Time, y::Time) = x.instant - y.instant
37,000✔
11
(-)(x::DateTime, y::DateTime) = x.instant - y.instant
41,537✔
12
(-)(x::AbstractDateTime, y::AbstractDateTime) = -(promote(x, y)...)
×
13

14
# Date-Time arithmetic
15
"""
16
    dt::Date + t::Time -> DateTime
17

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

25
# TimeType-Year arithmetic
26
function (+)(dt::DateTime, y::Year)
169,605✔
27
    oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m)
508,815✔
28
    return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
169,605✔
29
end
30
function (+)(dt::Date,y::Year)
919,729✔
31
    oy, m, d = yearmonthday(dt); ny = oy + value(y); ld = daysinmonth(ny, m)
2,759,358✔
32
    return Date(ny, m, d <= ld ? d : ld)
919,729✔
33
end
34
function (-)(dt::DateTime,y::Year)
10✔
35
    oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m)
30✔
36
    return DateTime(ny, m, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
10✔
37
end
38
function (-)(dt::Date,y::Year)
10✔
39
    oy, m, d = yearmonthday(dt); ny = oy - value(y); ld = daysinmonth(ny, m)
30✔
40
    return Date(ny, m, d <= ld ? d : ld)
10✔
41
end
42

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

50
function (+)(dt::DateTime, z::Month)
223,586✔
51
    y,m,d = yearmonthday(dt)
223,586✔
52
    ny = yearwrap(y, m, value(z))
223,586✔
53
    mm = monthwrap(m, value(z)); ld = daysinmonth(ny, mm)
650,789✔
54
    return DateTime(ny, mm, d <= ld ? d : ld, hour(dt), minute(dt), second(dt), millisecond(dt))
223,586✔
55
end
56

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

76
(+)(x::Date, y::Quarter) = x + Month(y)
151,527✔
77
(-)(x::Date, y::Quarter) = x - Month(y)
10✔
78
(+)(x::DateTime, y::Quarter) = x + Month(y)
135,465✔
79
(-)(x::DateTime, y::Quarter) = x - Month(y)
10✔
80
(+)(x::Date, y::Week) = return Date(UTD(value(x) + 7 * value(y)))
71,519✔
81
(-)(x::Date, y::Week) = return Date(UTD(value(x) - 7 * value(y)))
10✔
82
(+)(x::Date, y::Day)  = return Date(UTD(value(x) + value(y)))
260,921✔
83
(-)(x::Date, y::Day)  = return Date(UTD(value(x) - value(y)))
220,675✔
84
(+)(x::DateTime, y::Period) = return DateTime(UTM(value(x) + toms(y)))
526,206,948✔
85
(-)(x::DateTime, y::Period) = return DateTime(UTM(value(x) - toms(y)))
20,858✔
86
(+)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) + tons(y)))
36,984✔
87
(-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y)))
62✔
88
(+)(y::Period, x::TimeType) = x + y
2✔
89

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

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

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

© 2026 Coveralls, Inc