Skip to content

Commit 8b0ca1c

Browse files
timholytkelman
authored andcommitted
Deprecate Dates<-->Real conversions, one(Dates), and require step in ranges (#19920)
Fixes #19896
1 parent b45f443 commit 8b0ca1c

File tree

7 files changed

+50
-87
lines changed

7 files changed

+50
-87
lines changed

base/dates/conversions.jl

+4-24
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,10 @@ Base.convert(::Type{DateTime}, dt::Date) = DateTime(UTM(value(dt)*86400000))
3131
Base.convert(::Type{Date}, dt::DateTime) = Date(UTD(days(dt)))
3232
Base.convert(::Type{Time}, dt::DateTime) = Time(Nanosecond((value(dt) % 86400000) * 1000000))
3333

34-
"""
35-
convert{T<:Real}(::Type{T}, dt::DateTime) -> T
36-
Converts a DateTime value `dt` to a number of type `T`. The returned value corresponds to the number of Rata Die milliseconds since epoch.
37-
See `convert(DateTime, x::Real)` for inverse.
38-
"""
39-
Base.convert{R<:Real}(::Type{R},x::DateTime) = convert(R,value(x))
40-
"""
41-
convert{T<:Real}(::Type{T}, dt::Date) -> T
42-
Converts a Date value `dt` to a number of type `T`. The returned value corresponds to the number of Rata Die days since epoch.
43-
See `convert(Date, x::Real)` for inverse.
44-
"""
45-
Base.convert{R<:Real}(::Type{R},x::Date) = convert(R,value(x))
46-
"""
47-
convert{T<:Real}(::Type{DateTime}, x::T) -> DateTime
48-
Converts a number of type `T` to a DateTime. `x` should be the number of Rata Die milliseconds since epoch.
49-
See `convert(Int64,dt::DateTime)` for inverse.
50-
"""
51-
Base.convert{R<:Real}(::Type{DateTime}, x::R) = DateTime(UTM(x))
52-
"""
53-
convert{T<:Real}(::Type{Date}, x::T) -> Date
54-
Converts a number of type `T` to a Date. `x` should be the number of Rata Die days since epoch.
55-
See `convert(Int64,dt::Date)` for inverse.
56-
"""
57-
Base.convert{R<:Real}(::Type{Date}, x::R) = Date(UTD(x))
34+
Base.convert(::Type{DateTime},x::Millisecond) = DateTime(Dates.UTInstant(x)) # Converts Rata Die milliseconds to a DateTime
35+
Base.convert(::Type{Millisecond},dt::DateTime) = Millisecond(value(dt)) # Converts DateTime to Rata Die milliseconds
36+
Base.convert(::Type{Date},x::Day) = Date(Dates.UTInstant(x)) # Converts Rata Die days to a Date
37+
Base.convert(::Type{Day},dt::Date) = Day(value(dt)) # Converts Date to Rata Die days
5838

5939
### External Conversions
6040
const UNIXEPOCH = value(DateTime(1970)) #Rata Die milliseconds for 1970-01-01T00:00:00

base/dates/periods.jl

+5-9
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,26 @@ for period in (:Year, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond
3737
""" $period(v)
3838
end
3939
end
40-
# Now we're safe to define Period-Number conversions
41-
# Anything an Int64 can convert to, a Period can convert to
42-
Base.convert{T<:Number}(::Type{T},x::Period) = convert(T,value(x))
43-
Base.convert{T<:Period}(::Type{T},x::Real) = T(x)
4440

4541
#Print/show/traits
4642
Base.string{P<:Period}(x::P) = string(value(x),_units(x))
4743
Base.show(io::IO,x::Period) = print(io,string(x))
4844
Base.zero{P<:Period}(::Union{Type{P},P}) = P(0)
49-
Base.one{P<:Period}(::Union{Type{P},P}) = P(1)
45+
Base.one{P<:Period}(::Union{Type{P},P}) = 1 # see #16116
5046
Base.typemin{P<:Period}(::Type{P}) = P(typemin(Int64))
5147
Base.typemax{P<:Period}(::Type{P}) = P(typemax(Int64))
5248

5349
# Default values (as used by TimeTypes)
5450
"""
5551
default(p::Period) -> Period
5652
57-
Returns a sensible "default" value for the input Period by returning `one(p)` for Year,
58-
Month, and Day, and `zero(p)` for Hour, Minute, Second, and Millisecond.
53+
Returns a sensible "default" value for the input Period by returning `T(1)` for Year,
54+
Month, and Day, and `T(0)` for Hour, Minute, Second, and Millisecond.
5955
"""
6056
function default end
6157

62-
default{T<:DatePeriod}(p::Union{T,Type{T}}) = one(p)
63-
default{T<:TimePeriod}(p::Union{T,Type{T}}) = zero(p)
58+
default{T<:DatePeriod}(p::Union{T,Type{T}}) = T(1)
59+
default{T<:TimePeriod}(p::Union{T,Type{T}}) = T(0)
6460

6561
(-){P<:Period}(x::P) = P(-value(x))
6662
Base.isless{P<:Period}(x::P,y::P) = isless(value(x),value(y))

base/dates/ranges.jl

+11-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44

55
# Override default step; otherwise it would be Millisecond(1)
66
Base.colon{T<:DateTime}(start::T, stop::T) = StepRange(start, Day(1), stop)
7-
Base.colon{T<:Time}(start::T, stop::T) = StepRange(start, Second(1), stop)
7+
Base.colon{T<:Date}(start::T, stop::T) = StepRange(start, Day(1), stop)
8+
Base.colon{T<:Time}(start::T, stop::T) = StepRange(start, Second(1), stop)
9+
10+
Base.range(start::DateTime, len::Integer) = range(start, Day(1), len)
11+
Base.range(start::Date, len::Integer) = range(start, Day(1), len)
12+
13+
(::Type{StepRange{T,R}}){T<:Dates.DatePeriod,R<:Real}(start, step, stop) =
14+
throw(ArgumentError("must specify step as a Period when constructing Dates ranges"))
815

916
# Given a start and end date, how many steps/periods are in between
10-
guess(a::DateTime,b::DateTime,c) = floor(Int64,(Int128(b) - Int128(a)) / toms(c))
11-
guess(a::Date,b::Date,c) = Int64(div(Int64(b - a), days(c)))
12-
len(a::Time,b::Time,c) = Int64(div(Int64(b - a), tons(c)))
17+
guess(a::DateTime,b::DateTime,c) = floor(Int64,(Int128(value(b)) - Int128(value(a)))/toms(c))
18+
guess(a::Date,b::Date,c) = Int64(div(value(b - a),days(c)))
19+
len(a::Time,b::Time,c) = Int64(div(value(b - a), tons(c)))
1320
function len(a,b,c)
1421
lo, hi, st = min(a,b), max(a,b), abs(c)
1522
i = guess(a,b,c)-1

base/deprecated.jl

+13
Original file line numberDiff line numberDiff line change
@@ -1773,4 +1773,17 @@ end)
17731773

17741774
@deprecate EachLine(stream, ondone) EachLine(stream, ondone=ondone)
17751775

1776+
# These conversions should not be defined, see #19896
1777+
@deprecate convert{T<:Number}(::Type{T}, x::Dates.Period) convert(T, Dates.value(x))
1778+
@deprecate convert{T<:Dates.Period}(::Type{T}, x::Real) T(x)
1779+
@deprecate convert{R<:Real}(::Type{R}, x::Dates.DateTime) R(Dates.value(x))
1780+
@deprecate convert{R<:Real}(::Type{R}, x::Dates.Date) R(Dates.value(x))
1781+
@deprecate convert(::Type{Dates.DateTime}, x::Real) Dates.DateTime(Dates.Millisecond(x))
1782+
@deprecate convert(::Type{Dates.Date}, x::Real) Dates.Date(Dates.Day(x))
1783+
1784+
function colon{T<:Dates.Period}(start::T, stop::T)
1785+
depwarn("$start:$stop is deprecated, use $start:$T(1):$stop instead.", :colon)
1786+
colon(start, T(1), stop)
1787+
end
1788+
17761789
# End deprecations scheduled for 0.6

test/dates/conversions.jl

+15-17
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ let t = Dates.Period[Dates.Week(2), Dates.Day(14), Dates.Hour(14*24), Dates.Minu
6161
Pi = typeof(t[i])
6262
for j = 1:length(t)
6363
@test t[i] == t[j]
64-
@test Int(convert(Pi,t[j])) == Int(t[i])
6564
end
6665
for j = i+1:length(t)
6766
Pj = typeof(t[j])
68-
tj1 = t[j] + one(Pj)
67+
tj1 = t[j] + Pj(1)
6968
@test t[i] < tj1
7069
@test_throws InexactError Pi(tj1)
7170
@test_throws InexactError Pj(Pi(typemax(Int64)))
@@ -74,8 +73,7 @@ let t = Dates.Period[Dates.Week(2), Dates.Day(14), Dates.Hour(14*24), Dates.Minu
7473
end
7574
end
7675
@test Dates.Year(3) == Dates.Month(36)
77-
@test Int(convert(Dates.Month, Dates.Year(3))) == 36
78-
@test Int(convert(Dates.Year, Dates.Month(36))) == 3
76+
@test_throws ErrorException Int(Dates.Month(36)) # eventually change to MethodError
7977
@test Dates.Year(3) < Dates.Month(37)
8078
@test_throws InexactError convert(Dates.Year, Dates.Month(37))
8179
@test_throws InexactError Dates.Month(Dates.Year(typemax(Int64)))
@@ -89,20 +87,20 @@ let dt = DateTime(1915,1,1,12)
8987
@test Dates.julian2datetime(julian) == dt
9088
end
9189

92-
# Conversions to/from numbers
90+
# "Conversions" to/from numbers
9391
a = Dates.DateTime(2000)
9492
b = Dates.Date(2000)
95-
@test convert(Real,b) == 730120
96-
@test convert(Float64,b) == 730120.0
97-
@test convert(Int32,b) == 730120
98-
@test convert(Real,a) == 63082368000000
99-
@test convert(Float64,a) == 63082368000000.0
100-
@test convert(Int64,a) == 63082368000000
101-
@test convert(DateTime,63082368000000) == a
102-
@test convert(DateTime,63082368000000.0) == a
103-
@test convert(Date,730120) == b
104-
@test convert(Date,730120.0) == b
105-
@test convert(Date,Int32(730120)) == b
93+
@test Dates.value(b) == 730120
94+
@test Dates.value(a) == 63082368000000
95+
@test convert(Dates.DateTime, Dates.Millisecond(63082368000000)) == a
96+
@test convert(Dates.Millisecond, a) == Dates.Millisecond(63082368000000)
97+
@test Dates.DateTime(Dates.UTM(63082368000000)) == a
98+
@test Dates.DateTime(Dates.UTM(63082368000000.0)) == a
99+
@test convert(Dates.Date, Dates.Day(730120)) == b
100+
@test convert(Dates.Day, b) == Dates.Day(730120)
101+
@test Dates.Date(Dates.UTD(730120)) == b
102+
@test Dates.Date(Dates.UTD(730120.0)) == b
103+
@test Dates.Date(Dates.UTD(Int32(730120))) == b
106104

107105
dt = Dates.DateTime(2000,1,1,23,59,59,50)
108106
t = Dates.Time(dt)
@@ -111,4 +109,4 @@ t = Dates.Time(dt)
111109
@test Dates.second(t) == 59
112110
@test Dates.millisecond(t) == 50
113111
@test Dates.microsecond(t) == 0
114-
@test Dates.nanosecond(t) == 0
112+
@test Dates.nanosecond(t) == 0

test/dates/periods.jl

-31
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,6 @@ ns = Dates.Nanosecond(1)
6161
@test Dates.Millisecond(ms) == ms
6262
@test Dates.Microsecond(us) == us
6363
@test Dates.Nanosecond(ns) == ns
64-
@test typeof(Int8(y)) <: Int8
65-
@test typeof(UInt8(y)) <: UInt8
66-
@test typeof(Int16(y)) <: Int16
67-
@test typeof(UInt16(y)) <: UInt16
68-
@test typeof(Int32(y)) <: Int32
69-
@test typeof(UInt32(y)) <: UInt32
70-
@test typeof(Int64(y)) <: Int64
71-
@test typeof(UInt64(y)) <: UInt64
72-
@test typeof(Int128(y)) <: Int128
73-
@test typeof(UInt128(y)) <: UInt128
74-
@test typeof(convert(BigInt,y)) <: BigInt
75-
@test typeof(convert(BigFloat,y)) <: BigFloat
76-
@test typeof(convert(Complex,y)) <: Complex
77-
@test typeof(convert(Rational,y)) <: Rational
78-
@test typeof(Float16(y)) <: Float16
79-
@test typeof(Float32(y)) <: Float32
80-
@test typeof(Float64(y)) <: Float64
8164
@test Dates.Year(convert(Int8,1)) == y
8265
@test Dates.Year(convert(UInt8,1)) == y
8366
@test Dates.Year(convert(Int16,1)) == y
@@ -227,20 +210,6 @@ test = ((((((((dt + y) - m) + w) - d) + h) - mi) + s) - ms)
227210
@test zero(Dates.Second(10)) == Dates.Second(0)
228211
@test zero(Dates.Millisecond) == Dates.Millisecond(0)
229212
@test zero(Dates.Millisecond(10)) == Dates.Millisecond(0)
230-
@test one(Dates.Year) == Dates.Year(1)
231-
@test one(Dates.Year(10)) == Dates.Year(1)
232-
@test one(Dates.Month) == Dates.Month(1)
233-
@test one(Dates.Month(10)) == Dates.Month(1)
234-
@test one(Dates.Day) == Dates.Day(1)
235-
@test one(Dates.Day(10)) == Dates.Day(1)
236-
@test one(Dates.Hour) == Dates.Hour(1)
237-
@test one(Dates.Hour(10)) == Dates.Hour(1)
238-
@test one(Dates.Minute) == Dates.Minute(1)
239-
@test one(Dates.Minute(10)) == Dates.Minute(1)
240-
@test one(Dates.Second) == Dates.Second(1)
241-
@test one(Dates.Second(10)) == Dates.Second(1)
242-
@test one(Dates.Millisecond) == Dates.Millisecond(1)
243-
@test one(Dates.Millisecond(10)) == Dates.Millisecond(1)
244213
@test Dates.Year(-1) < Dates.Year(1)
245214
@test !(Dates.Year(-1) > Dates.Year(1))
246215
@test Dates.Year(1) == Dates.Year(1)

test/dates/ranges.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,10 @@ let n=100000
478478
return b
479479
end
480480

481-
@test length(Dates.Year(1):Dates.Year(10)) == 10
481+
@test length(Dates.Year(1):Dates.Year(1):Dates.Year(10)) == 10
482482
@test length(Dates.Year(10):Dates.Year(-1):Dates.Year(1)) == 10
483483
@test length(Dates.Year(10):Dates.Year(-2):Dates.Year(1)) == 5
484-
@test_throws OverflowError length(typemin(Dates.Year):typemax(Dates.Year))
484+
@test_throws OverflowError length(typemin(Dates.Year):Dates.Year(1):typemax(Dates.Year))
485485
@test_throws MethodError Dates.Date(0):Dates.DateTime(2000)
486486
@test_throws MethodError Dates.Date(0):Dates.Year(10)
487487
@test length(range(Dates.Date(2000),366)) == 366

0 commit comments

Comments
 (0)