/
dates.jl
188 lines (151 loc) · 7.28 KB
/
dates.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Conversion from and to types from the `Dates` stdlib
# Dates.FixedPeriod
for (period, unit) = ((Dates.Week, wk), (Dates.Day, d), (Dates.Hour, hr),
(Dates.Minute, minute), (Dates.Second, s), (Dates.Millisecond, ms),
(Dates.Microsecond, μs), (Dates.Nanosecond, ns))
@eval unit(::Type{$period}) = $unit
@eval (::Type{$period})(x::AbstractQuantity) = $period(ustrip(unit($period), x))
end
dimension(p::Dates.FixedPeriod) = dimension(typeof(p))
dimension(::Type{<:Dates.FixedPeriod}) = 𝐓
"""
unit(x::Dates.FixedPeriod)
unit(x::Type{<:Dates.FixedPeriod})
Return the units that correspond to a particular period.
# Examples
```julia
julia> unit(Second(15)) == u"s"
true
julia> unit(Hour) == u"hr"
true
```
"""
unit(p::Dates.FixedPeriod) = unit(typeof(p))
numtype(x::Dates.FixedPeriod) = numtype(typeof(x))
numtype(::Type{T}) where {T<:Dates.FixedPeriod} = Int64
quantitytype(::Type{T}) where {T<:Dates.FixedPeriod} =
Quantity{numtype(T),dimension(T),typeof(unit(T))}
ustrip(p::Dates.FixedPeriod) = Dates.value(p)
"""
Quantity(period::Dates.FixedPeriod)
Create a `Quantity` that corresponds to the given `period`. The numerical value of the
resulting `Quantity` is of type `Int64`.
# Example
```jldoctest
julia> using Dates: Second
julia> Quantity(Second(5))
5 s
```
"""
Quantity(period::Dates.FixedPeriod) = Quantity(ustrip(period), unit(period))
uconvert(u::Units, period::Dates.FixedPeriod) = uconvert(u, Quantity(period))
(T::Type{<:AbstractQuantity})(period::Dates.FixedPeriod) = T(Quantity(period))
convert(T::Type{<:AbstractQuantity}, period::Dates.FixedPeriod) = T(period)
convert(T::Type{<:Dates.FixedPeriod}, x::AbstractQuantity) = T(x)
round(T::Type{<:Dates.FixedPeriod}, x::AbstractQuantity, r::RoundingMode=RoundNearest) =
T(round(numtype(T), ustrip(unit(T), x), r))
round(u::Units, period::Dates.FixedPeriod, r::RoundingMode=RoundNearest; kwargs...) =
round(u, Quantity(period), r; kwargs...)
round(T::Type{<:Number}, u::Units, period::Dates.FixedPeriod, r::RoundingMode=RoundNearest;
kwargs...) = round(T, u, Quantity(period), r; kwargs...)
round(T::Type{<:AbstractQuantity}, period::Dates.FixedPeriod, r::RoundingMode=RoundNearest;
kwargs...) = round(T, Quantity(period), r; kwargs...)
for (f, r) in ((:floor,:RoundDown), (:ceil,:RoundUp), (:trunc,:RoundToZero))
@eval $f(T::Type{<:Dates.FixedPeriod}, x::AbstractQuantity) = round(T, x, $r)
@eval $f(u::Units, period::Dates.FixedPeriod; kwargs...) =
round(u, period, $r; kwargs...)
@eval $f(T::Type{<:Number}, u::Units, period::Dates.FixedPeriod; kwargs...) =
round(T, u, period, $r; kwargs...)
@eval $f(T::Type{<:AbstractQuantity}, period::Dates.FixedPeriod; kwargs...) =
round(T, period, $r; kwargs...)
end
for op = (:+, :-, :*, :/, ://, :fld, :cld, :mod, :rem, :atan,
:(==), :isequal, :<, :isless, :≤)
@eval $op(x::Dates.FixedPeriod, y::AbstractQuantity) = $op(Quantity(x), y)
@eval $op(x::AbstractQuantity, y::Dates.FixedPeriod) = $op(x, Quantity(y))
end
for op = (:*, :/, ://)
@eval $op(x::Dates.FixedPeriod, y::Units) = $op(Quantity(x), y)
@eval $op(x::Units, y::Dates.FixedPeriod) = $op(x, Quantity(y))
end
div(x::Dates.FixedPeriod, y::AbstractQuantity, r...) = div(Quantity(x), y, r...)
div(x::AbstractQuantity, y::Dates.FixedPeriod, r...) = div(x, Quantity(y), r...)
isapprox(x::Dates.FixedPeriod, y::AbstractQuantity; kwargs...) =
isapprox(Quantity(x), y; kwargs...)
isapprox(x::AbstractQuantity, y::Dates.FixedPeriod; kwargs...) =
isapprox(x, Quantity(y); kwargs...)
function isapprox(x::AbstractArray{<:AbstractQuantity}, y::AbstractArray{T};
kwargs...) where {T<:Dates.Period}
if isconcretetype(T)
y′ = reinterpret(quantitytype(T), y)
else
y′ = Quantity.(y)
end
isapprox(x, y′; kwargs...)
end
isapprox(x::AbstractArray{<:Dates.FixedPeriod}, y::AbstractArray{<:AbstractQuantity};
kwargs...) = isapprox(y, x; kwargs...)
Base.promote_rule(::Type{Quantity{T,𝐓,U}}, ::Type{S}) where {T,U,S<:Dates.FixedPeriod} =
promote_type(Quantity{T,𝐓,U}, quantitytype(S))
# Dates.CompoundPeriod
dimension(p::Dates.CompoundPeriod) = dimension(typeof(p))
dimension(::Type{<:Dates.CompoundPeriod}) = 𝐓
uconvert(u::Units, period::Dates.CompoundPeriod) =
Quantity{promote_type(Int64,typeof(convfact(u,ns))),dimension(u),typeof(u)}(period)
try_uconvert(u::Units, period::Dates.CompoundPeriod) = nothing
function try_uconvert(u::TimeUnits, period::Dates.CompoundPeriod)
T = Quantity{promote_type(Int64,typeof(convfact(u,ns))),dimension(u),typeof(u)}
val = zero(T)
for p in period.periods
p isa Dates.FixedPeriod || return nothing
val += T(p)
end
val
end
(T::Type{<:AbstractQuantity})(period::Dates.CompoundPeriod) =
mapreduce(T, +, period.periods, init=zero(T))
convert(T::Type{<:AbstractQuantity}, period::Dates.CompoundPeriod) = T(period)
round(u::Units, period::Dates.CompoundPeriod, r::RoundingMode=RoundNearest; kwargs...) =
round(u, uconvert(u, period), r; kwargs...)
round(T::Type{<:Number}, u::Units, period::Dates.CompoundPeriod,
r::RoundingMode=RoundNearest; kwargs...) =
round(T, u, uconvert(u, period), r; kwargs...)
round(T::Type{<:AbstractQuantity}, period::Dates.CompoundPeriod,
r::RoundingMode=RoundNearest; kwargs...) =
round(T, T(period), r; kwargs...)
for (f, r) in ((:floor,:RoundDown), (:ceil,:RoundUp), (:trunc,:RoundToZero))
@eval $f(u::Units, period::Dates.CompoundPeriod; kwargs...) =
round(u, period, $r; kwargs...)
@eval $f(T::Type{<:Number}, u::Units, period::Dates.CompoundPeriod; kwargs...) =
round(T, u, period, $r; kwargs...)
@eval $f(T::Type{<:AbstractQuantity}, period::Dates.CompoundPeriod; kwargs...) =
round(T, period, $r; kwargs...)
end
for op = (:fld, :cld, :atan, :<, :isless, :≤)
@eval $op(x::Dates.CompoundPeriod, y::AbstractQuantity) = $op(uconvert(unit(y),x), y)
@eval $op(x::AbstractQuantity, y::Dates.CompoundPeriod) = $op(x, uconvert(unit(x),y))
end
div(x::Dates.CompoundPeriod, y::AbstractQuantity, r...) = div(uconvert(unit(y),x), y, r...)
div(x::AbstractQuantity, y::Dates.CompoundPeriod, r...) = div(x, uconvert(unit(x),y), r...)
mod(x::Dates.CompoundPeriod, y::AbstractQuantity) = mod(uconvert(unit(y),x), y)
rem(x::Dates.CompoundPeriod, y::AbstractQuantity) = rem(uconvert(unit(y),x), y)
for op = (:(==), :isequal)
@eval $op(x::Dates.CompoundPeriod, y::AbstractQuantity{T,𝐓,U}) where {T,U} =
$op(try_uconvert(U(), x), y)
@eval $op(x::AbstractQuantity{T,𝐓,U}, y::Dates.CompoundPeriod) where {T,U} =
$op(x, try_uconvert(U(), y))
end
isapprox(x::Dates.CompoundPeriod, y::AbstractQuantity; kwargs...) =
dimension(y) === 𝐓 ? isapprox(uconvert(unit(y), x), y; kwargs...) : false
isapprox(x::AbstractQuantity, y::Dates.CompoundPeriod; kwargs...) =
dimension(x) === 𝐓 ? isapprox(x, uconvert(unit(x), y); kwargs...) : false
function isapprox(x::AbstractArray{<:AbstractQuantity},
y::AbstractArray{Dates.CompoundPeriod}; kwargs...)
if dimension(eltype(x)) === 𝐓
isapprox(x, uconvert.(unit(eltype(x)), y); kwargs...)
else
false
end
end
isapprox(x::AbstractArray{Dates.CompoundPeriod}, y::AbstractArray{<:AbstractQuantity};
kwargs...) = isapprox(y, x; kwargs...)