Skip to content

Commit 8f81a52

Browse files
committed
Eliminate Type Piracy
1 parent b0cd6f3 commit 8f81a52

File tree

1 file changed

+22
-42
lines changed

1 file changed

+22
-42
lines changed

src/datetime_rotation.jl

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,6 @@ function reopen!(drfl::DatetimeRotatingFileLogger)
3939
return nothing
4040
end
4141

42-
# I kind of wish these were defined in Dates
43-
isless(::Type{Millisecond}, ::Type{Millisecond}) = false
44-
isless(::Type{Millisecond}, ::Type{T}) where {T <: Dates.Period} = true
45-
46-
isless(::Type{Second}, ::Type{Millisecond}) = false
47-
isless(::Type{Second}, ::Type{Second}) = false
48-
isless(::Type{Second}, ::Type{T}) where {T <: Dates.Period} = true
49-
50-
isless(::Type{Minute}, ::Type{Millisecond}) = false
51-
isless(::Type{Minute}, ::Type{Second}) = false
52-
isless(::Type{Minute}, ::Type{Minute}) = false
53-
isless(::Type{Minute}, ::Type{T}) where {T <: Dates.Period} = true
54-
55-
isless(::Type{Hour}, ::Type{Day}) = true
56-
isless(::Type{Hour}, ::Type{Month}) = true
57-
isless(::Type{Hour}, ::Type{Year}) = true
58-
isless(::Type{Hour}, ::Type{T}) where {T <: Dates.Period} = false
59-
60-
isless(::Type{Day}, ::Type{Month}) = true
61-
isless(::Type{Day}, ::Type{Year}) = true
62-
isless(::Type{Day}, ::Type{T}) where {T <: Dates.Period} = false
63-
64-
isless(::Type{Month}, ::Type{Year}) = true
65-
isless(::Type{Month}, ::Type{T}) where {T <: Dates.Period} = false
66-
67-
isless(::Type{Year}, ::Type{T}) where {T <: Dates.Period} = false
68-
6942
"""
7043
next_datetime_transition(fmt::DateFormat)
7144
@@ -76,30 +49,37 @@ function next_datetime_transition(fmt::DateFormat)
7649
extract_token(x::Dates.DatePart{T}) where {T} = T
7750
token_timescales = Dict(
7851
# Milliseconds is the smallest timescale
79-
's' => Millisecond,
52+
's' => Millisecond(1),
8053
# Seconds
81-
'S' => Second,
54+
'S' => Second(1),
8255
# Minutes
83-
'M' => Minute,
56+
'M' => Minute(1),
8457
# Hours
85-
'I' => Hour,
86-
'H' => Hour,
58+
'I' => Hour(1),
59+
'H' => Hour(1),
8760
# Days
88-
'd' => Day,
89-
'e' => Day,
90-
'E' => Day,
61+
'd' => Day(1),
62+
'e' => Day(1),
63+
'E' => Day(1),
9164
# Month
92-
'm' => Month,
93-
'u' => Month,
94-
'U' => Month,
65+
'm' => Month(1),
66+
'u' => Month(1),
67+
'U' => Month(1),
9568
# Year
96-
'y' => Year,
97-
'Y' => Year,
69+
'y' => Year(1),
70+
'Y' => Year(1),
9871
)
9972

73+
# Dates for some reason explicitly does not define equality between the smaller
74+
# timescales (Second, Minute, Day, etc..) and the larger, non-constant timescales
75+
# (Month, Year). We do so explicitly here, without committing type piracy:
76+
custom_isless(x, y) = isless(x, y)
77+
custom_isless(x::Union{Millisecond,Second,Minute,Hour,Day}, y::Union{Month, Year}) = true
78+
custom_isless(x::Union{Month, Year}, y::Union{Millisecond,Second,Minute,Hour,Day}) = false
79+
10080
tokens = filter(t -> isa(t, Dates.DatePart), collect(fmt.tokens))
101-
minimum_timescale = minimum(map(t -> token_timescales[extract_token(t)], tokens))
102-
return Dates.ceil(now(), minimum_timescale) - Second(1)
81+
minimum_timescale = first(sort(map(t -> token_timescales[extract_token(t)], tokens), lt=custom_isless))
82+
return ceil(now(), minimum_timescale) - Second(1)
10383
end
10484

10585
calc_logpath(dir, filename_pattern) = joinpath(dir, Dates.format(now(), filename_pattern))

0 commit comments

Comments
 (0)