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

JuliaLang / julia / #37666

04 Nov 2023 02:27AM UTC coverage: 87.924% (+0.09%) from 87.831%
#37666

push

local

web-flow
Simplify, 16bit PDP-11 isn't going to be supported (#45763)

PDP_ENDIAN isn't used.

Co-authored-by: Viral B. Shah <ViralBShah@users.noreply.github.com>

74550 of 84789 relevant lines covered (87.92%)

15319904.67 hits per line

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

93.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
1,099,239✔
6

7
# TimeType arithmetic
8
(+)(x::TimeType) = x
×
9
(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant
1,003,807✔
10
(-)(x::T, y::T) where {T<:AbstractDateTime} = x.instant - y.instant
132,439✔
11
(-)(x::AbstractDateTime, y::AbstractDateTime) = -(promote(x, y)...)
×
12

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

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

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

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

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

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

75
(+)(x::Date, y::Quarter) = x + Month(y)
151,530✔
76
(-)(x::Date, y::Quarter) = x - Month(y)
11✔
77
(+)(x::DateTime, y::Quarter) = x + Month(y)
135,467✔
78
(-)(x::DateTime, y::Quarter) = x - Month(y)
10✔
79
(+)(x::Date, y::Week) = return Date(UTD(value(x) + 7 * value(y)))
71,537✔
80
(-)(x::Date, y::Week) = return Date(UTD(value(x) - 7 * value(y)))
14✔
81
(+)(x::Date, y::Day)  = return Date(UTD(value(x) + value(y)))
261,035✔
82
(-)(x::Date, y::Day)  = return Date(UTD(value(x) - value(y)))
220,726✔
83
(+)(x::DateTime, y::Period) = return DateTime(UTM(value(x) + toms(y)))
526,525,087✔
84
(-)(x::DateTime, y::Period) = return DateTime(UTM(value(x) - toms(y)))
20,912✔
85
(+)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) + tons(y)))
37,063✔
86
(-)(x::Time, y::TimePeriod) = return Time(Nanosecond(value(x) - tons(y)))
122✔
87
(+)(y::Period, x::TimeType) = x + y
11✔
88

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

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

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

© 2026 Coveralls, Inc