Skip to content

Commit 80c861b

Browse files
committed
feat: added the ability to compare UnixTime with DateTime
1 parent 73e00e3 commit 80c861b

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

src/UnixTimes.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ function Base.convert(::Type{UnixTime}, x::DateTime)
7878
UnixTime(Dates.UTInstant(Nanosecond(instant_ns)))
7979
end
8080

81+
# Promotion rules for comparing UnixTime with DateTime
82+
Base.promote_rule(::Type{UnixTime}, ::Type{DateTime}) = UnixTime
83+
8184
function Base.show(io::IO, x::UnixTime)
8285
xdt = convert(DateTime, x)
8386
print(io, Dates.format(xdt, DATEFORMAT))

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,15 @@ end
102102
@test collect(t1:p:t2) isa Vector
103103
end
104104

105+
@testset "comparison with DateTime" begin
106+
ut = UnixTime(2001, 03, 21, 13, 10, 0, 0)
107+
dt = DateTime("2001-03-21T13:10:00")
108+
109+
@test ut == dt
110+
@test ut > DateTime("2001-03-21T13:00:00")
111+
@test ut < DateTime("2001-03-21T13:20:00")
112+
@test DateTime("2001-03-21T13:20:00") > ut
113+
@test UnixTime(2001, 03, 21, 13, 10, 0, 114, 549, 673) > dt
114+
end
115+
105116
end

0 commit comments

Comments
 (0)