Skip to content

Commit

Permalink
Support eps on TimeType types (JuliaLang#31487)
Browse files Browse the repository at this point in the history
* Support eps on TimeType types

* Add news
  • Loading branch information
omus committed Apr 3, 2020
1 parent f47e092 commit 5572795
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ Standard library changes
arrays.

#### Dates
* The `eps` function now accepts `TimeType` types ([#31487]).

#### Statistics

Expand Down
31 changes: 23 additions & 8 deletions stdlib/Dates/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,32 @@ calendar(dt::DateTime) = ISOCalendar
calendar(dt::Date) = ISOCalendar

"""
eps(::DateTime) -> Millisecond
eps(::Date) -> Day
eps(::Time) -> Nanosecond
eps(::Type{DateTime}) -> Millisecond
eps(::Type{Date}) -> Day
eps(::Type{Time}) -> Nanosecond
eps(::TimeType) -> Period
Returns `Millisecond(1)` for `DateTime` values, `Day(1)` for `Date` values, and `Nanosecond(1)` for `Time` values.
Return the smallest unit value supported by the `TimeType`.
# Examples
```jldoctest
julia> eps(DateTime)
1 millisecond
julia> eps(Date)
1 day
julia> eps(Time)
1 nanosecond
```
"""
Base.eps
Base.eps(::Union{Type{DateTime}, Type{Date}, Type{Time}, TimeType})

Base.eps(::Type{DateTime}) = Millisecond(1)
Base.eps(::Type{Date}) = Day(1)
Base.eps(::Type{Time}) = Nanosecond(1)
Base.eps(::T) where T <: TimeType = eps(T)::Period

Base.eps(dt::DateTime) = Millisecond(1)
Base.eps(dt::Date) = Day(1)
Base.eps(t::Time) = Nanosecond(1)

Base.typemax(::Union{DateTime, Type{DateTime}}) = DateTime(146138512, 12, 31, 23, 59, 59)
Base.typemin(::Union{DateTime, Type{DateTime}}) = DateTime(-146138511, 1, 1, 0, 0, 0)
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Dates/test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ c = Dates.Time(0)
@testset "DateTime traits" begin
@test Dates.calendar(a) == Dates.ISOCalendar
@test Dates.calendar(b) == Dates.ISOCalendar
@test eps(DateTime) == Dates.Millisecond(1)
@test eps(Date) == Dates.Day(1)
@test eps(Time) == Dates.Nanosecond(1)
@test eps(a) == Dates.Millisecond(1)
@test eps(b) == Dates.Day(1)
@test eps(c) == Dates.Nanosecond(1)
Expand Down

0 comments on commit 5572795

Please sign in to comment.