Skip to content

Commit 778d1ad

Browse files
committed
Add equality methods for ITime
Adds Base.(==) between ITime and DateTime and between ITime and a number, which is assumed to represent seconds. Throw error when comparing ITime to Number for == Apply formatting
1 parent d01995c commit 778d1ad

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/ITime.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ function Base.:*(a::AbstractFloat, t::ITime)
279279
end
280280
end
281281

282+
Base.:(==)(t1::ITime, t2::Number) = error("Cannot compare ITime with a Number")
283+
Base.:(==)(t1::Number, t2::ITime) = t2 == t1
284+
285+
"""
286+
Base.:(==)(t1::ITime, t2::Dates.DateTime)
287+
288+
Converts `t1` to a `DateTime` and checks for equality with `t2`. `t1` must have an `epoch`
289+
`t1==t2` ⟺ `t2==t1`
290+
"""
291+
Base.:(==)(t1::ITime, t2::Dates.DateTime) = date(t1) == t2
292+
Base.:(==)(t1::Dates.DateTime, t2::ITime) = t2 == t1
293+
282294
"""
283295
Base.:(:)(start::ITime, step::ITime, stop::ITime)
284296

src/OnlineLogging.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ function report_walltime(wt, integrator)
148148

149149
wall_time_ave_per_step = wt.∑Δt_wall[] / n_steps
150150
wall_time_ave_per_step_str = _time_and_units_str(wall_time_ave_per_step)
151-
percent_complete = round((t - t_start) / (t_end - t_start) * 100; digits = 1)
151+
percent_complete =
152+
round((t - t_start) / (t_end - t_start) * 100; digits = 1)
152153
n_steps_remaining = n_steps_total - n_steps
153154
wall_time_remaining = wall_time_ave_per_step * n_steps_remaining
154155
wall_time_remaining_str = _time_and_units_str(wall_time_remaining)

test/itime.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,20 @@ using Test, Dates
215215
@test t6 % t7 == ITime(1, period = Second(2))
216216
end
217217

218+
@testset "Base.:(==)" begin
219+
@test ITime(0; period = Day(1), epoch = DateTime(2010)) ==
220+
DateTime(2010)
221+
@test DateTime(2011) ==
222+
ITime(365; period = Day(1), epoch = DateTime(2010))
223+
@test ITime(365; period = Day(1), epoch = DateTime(2010)) !=
224+
DateTime(2010)
225+
@test_throws "Cannot compare ITime with a Number" ITime(
226+
1;
227+
period = Day(1),
228+
) == 60 * 60 * 24
229+
@test_throws "Cannot compare ITime with a Number" Inf == ITime(1.0;)
230+
end
231+
218232
@testset "iszero" begin
219233
t1 = ITime(0, period = Hour(1))
220234
t2 = ITime(1, period = Second(1))

0 commit comments

Comments
 (0)