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

JuliaLang / julia / #37616

10 Sep 2023 01:51AM UTC coverage: 86.489% (+0.3%) from 86.196%
#37616

push

local

web-flow
Make _global_logstate a typed global (#51257)

73902 of 85447 relevant lines covered (86.49%)

13068259.04 hits per line

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

93.55
/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
130,724✔
6

7
# TimeType arithmetic
8
(+)(x::TimeType) = x
×
9
(-)(x::T, y::T) where {T<:TimeType} = x.instant - y.instant
167,339✔
10
(-)(x::TimeType, y::TimeType) = -(promote(x, y)...)
1✔
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)
5✔
21
(+)(t::Time, dt::Date) = DateTime(dt, t)
1✔
22

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

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

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

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

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

94
# AbstractArray{TimeType}, AbstractArray{TimeType}
95
(-)(x::OrdinalRange{T}, y::OrdinalRange{T}) where {T<:TimeType} = Vector(x) - Vector(y)
2✔
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)
94✔
100
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