Skip to content

Commit 871fd39

Browse files
committed
address feedback by dropping the generic conversion constructor in favor of explicity copy constructors. Additional test to show the periodicity of StepRangeLen{Time}
1 parent 8531346 commit 871fd39

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

stdlib/Dates/src/conversions.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Convert a `DateTime` to a `Date`. The hour, minute, second, and millisecond part
99
the `DateTime` are truncated, so only the year, month and day parts are used in
1010
construction.
1111
"""
12-
Date(dt::TimeType) = convert(Date, dt)
12+
Date(dt::DateTime) = convert(Date, dt)
1313

1414
"""
15-
DateTime(dt::Date)
15+
DateTime(d::Date)
1616
1717
Convert a `Date` to a `DateTime`. The hour, minute, second, and millisecond parts of
1818
the new `DateTime` are assumed to be zero.
1919
"""
20-
DateTime(dt::TimeType) = convert(DateTime, dt)
20+
DateTime(d::Date) = convert(DateTime, d)
2121

2222
"""
2323
Time(dt::DateTime)

stdlib/Dates/src/types.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,14 @@ function DateTime(dt::Date, t::Time)
417417
return DateTime(y, m, d, hour(t), minute(t), second(t), millisecond(t))
418418
end
419419

420+
# explicit copy constructors
421+
# (to avoid the Fallbacks which attempt to convert single param to Int64)
422+
DateTime(dt::DateTime) = dt
423+
Date(d::Date) = d
424+
Time(t::Time) = t
420425
# Fallback constructors
421426
DateTime(y, m=1, d=1, h=0, mi=0, s=0, ms=0, ampm::AMPM=TWENTYFOURHOUR) = DateTime(Int64(y), Int64(m), Int64(d), Int64(h), Int64(mi), Int64(s), Int64(ms), ampm)
422427
Date(y, m=1, d=1) = Date(Int64(y), Int64(m), Int64(d))
423-
Time(t::Time) = t # avoid the less helpful fallback below.
424428
Time(h, mi=0, s=0, ms=0, us=0, ns=0, ampm::AMPM=TWENTYFOURHOUR) = Time(Int64(h), Int64(mi), Int64(s), Int64(ms), Int64(us), Int64(ns), ampm)
425429

426430
# Traits, Equality

stdlib/Dates/test/ranges.jl

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -534,43 +534,54 @@ let d = Dates.Day(1)
534534
@test (Dates.Date(2000):d:Dates.Date(2001)) - d == (Dates.Date(2000) - d:d:Dates.Date(2001) - d)
535535
end
536536

537-
# Time ranges
538-
dr = Dates.Time(23, 1, 1):Dates.Second(1):Dates.Time(23, 2, 1)
539-
dr1 = Dates.Time(23, 1, 1):Dates.Second(1):Dates.Time(23, 1, 1)
540-
dr2 = Dates.Time(23, 1, 1):Dates.Second(1):Dates.Time(22, 2, 1) # empty range
541-
dr3 = Dates.Time(23, 1, 1):Dates.Minute(-1):Dates.Time(22, 1, 1) # negative step
542-
dr4 = range(Dates.Time(0), step=Dates.Hour(1), length=23) # by step, length
543-
544-
# Big ranges
545-
dr8 = typemin(Dates.Time):Dates.Second(1):typemax(Dates.Time)
546-
dr9 = typemin(Dates.Time):Dates.Nanosecond(1):typemax(Dates.Time)
547-
# Other steps
548-
dr10 = typemax(Dates.Time):Dates.Microsecond(-1):typemin(Dates.Time)
549-
dr11 = typemin(Dates.Time):Dates.Millisecond(1):typemax(Dates.Time)
550-
dr12 = typemin(Dates.Time):Dates.Minute(1):typemax(Dates.Time)
551-
dr13 = typemin(Dates.Time):Dates.Hour(1):typemax(Dates.Time)
552-
dr14 = typemin(Dates.Time):Dates.Millisecond(10):typemax(Dates.Time)
553-
dr15 = typemin(Dates.Time):Dates.Minute(100):typemax(Dates.Time)
554-
dr16 = typemin(Dates.Time):Dates.Hour(1000):typemax(Dates.Time)
555-
dr17 = typemax(Dates.Time):Dates.Millisecond(-10000):typemin(Dates.Time)
556-
dr18 = typemax(Dates.Time):Dates.Minute(-100):typemin(Dates.Time)
557-
dr19 = typemax(Dates.Time):Dates.Hour(-10):typemin(Dates.Time)
558-
dr20 = typemin(Dates.Time):Dates.Microsecond(2):typemax(Dates.Time)
559-
560-
drs = Any[dr, dr1, dr2, dr3, dr4, dr8, dr9, dr10,
561-
dr11, dr12, dr13, dr14, dr15, dr16, dr17, dr18, dr19, dr20]
537+
# small Time ranges
538+
small_ranges_with = Any[
539+
Dates.Time(23, 1, 1):Dates.Second(1):Dates.Time(23, 2, 1),
540+
Dates.Time(23, 1, 1):Dates.Second(1):Dates.Time(23, 1, 1),
541+
Dates.Time(23, 1, 1):Dates.Minute(-1):Dates.Time(22, 1, 1) # negative step
542+
]
543+
empty_range = Dates.Time(23, 1, 1):Dates.Second(1):Dates.Time(22, 2, 1)
544+
small_ranges_without = Any[
545+
empty_range,
546+
range(Dates.Time(0), step=Dates.Hour(1), length=23) # by step, length
547+
]
548+
small_ranges = Any[small_ranges_with; small_ranges_without]
549+
550+
# Big ranges of various steps, increasing and decreasing
551+
big_ranges = Any[
552+
[typemin(Dates.Time):step:typemax(Dates.Time) for step in [
553+
Dates.Second(1)
554+
Dates.Nanosecond(1)
555+
Dates.Microsecond(2)
556+
Dates.Millisecond(1)
557+
Dates.Minute(1)
558+
Dates.Hour(1)
559+
Dates.Millisecond(10)
560+
Dates.Minute(100)
561+
Dates.Hour(1000)
562+
]]
563+
[typemax(Dates.Time):step:typemin(Dates.Time) for step in [
564+
Dates.Microsecond(-1)
565+
Dates.Millisecond(-10000)
566+
Dates.Minute(-100)
567+
Dates.Hour(-10)
568+
]]
569+
]
570+
571+
572+
drs = Any[small_ranges; big_ranges]
562573

563574
@test map(length, drs) == map(x->size(x)[1], drs)
564-
@test all(x->findall(in(x), x) == [1:length(x);], drs[1:4])
565-
@test isempty(dr2)
575+
@test all(x->findall(in(x), x) == [1:length(x);], small_ranges)
576+
@test isempty(empty_range)
566577
@test all(x->reverse(x) == last(x): - step(x):first(x), drs)
567-
@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), drs[4:end])
568-
@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)), drs[4:end])
569-
@test_throws MethodError dr .+ 1
578+
@test all(x->minimum(x) == (step(x) < zero(step(x)) ? last(x) : first(x)), big_ranges)
579+
@test all(x->maximum(x) == (step(x) < zero(step(x)) ? first(x) : last(x)), big_ranges)
580+
@test_throws MethodError drs[1] .+ 1
570581

571582
a = Dates.Time(23, 1, 1)
572-
@test map(x->a in x, drs[1:4]) == [true, true, false, true]
573-
@test a in dr
583+
@test all(x->a in x, small_ranges_with)
584+
@test all(x->!(a in x), small_ranges_without)
574585

575586
@test all(x->sort(x) == (step(x) < zero(step(x)) ? reverse(x) : x), drs)
576587
@test all(x->step(x) < zero(step(x)) ? issorted(reverse(x)) : issorted(x), drs)
@@ -613,4 +624,12 @@ end
613624
@test_throws OverflowError StepRange(dmin, Day(1), dmax)
614625
end
615626

627+
@testset "StepRangeLen{Time} periodicity, indexing" begin
628+
r = range(Time(0), step = Hour(9), length = 5)
629+
@test length(r) == 5
630+
@test r[begin:end] == [Time(0), Time(9), Time(18), Time(3), Time(12)]
631+
end
632+
633+
634+
616635
end # RangesTest module

0 commit comments

Comments
 (0)