Skip to content

Commit

Permalink
Conversion cleanup and ZonedDateTime support. Closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ancapdev committed Dec 8, 2020
1 parent 21a1b99 commit 83739c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
name = "UnixTimes"
uuid = "ab1a18e7-b408-4913-896c-624bb82ed7f4"
authors = ["Christian Rorvik <christian.rorvik@gmail.com>"]
version = "0.4.0"
version = "1.0.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
TimeZones = "f269a46b-ccf7-5d73-abea-4c690281aa53"

[compat]
julia = "1"
TimeZones = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
16 changes: 13 additions & 3 deletions src/UnixTimes.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module UnixTimes

using Dates
using TimeZones

export UnixTime
export unix_now
Expand Down Expand Up @@ -40,18 +41,27 @@ function Base.:+(x::UnixTime, p::Union{Month, Year})
convert(UnixTime, convert(DateTime, x) + p) + Nanosecond(trunc_ns)
end

function Base.convert(::Type{DateTime}, x::UnixTime)
function Dates.DateTime(x::UnixTime)
instant_ms = Dates.UNIXEPOCH + div(x.instant.periods.value, 1_000_000)
DateTime(Dates.UTM(instant_ms))
end

Base.convert(::Type{Date}, x::UnixTime) = Date(DateTime(x))
Dates.Date(x::UnixTime) = Date(DateTime(x))

function Base.convert(::Type{UnixTime}, x::DateTime)
Base.convert(::Type{DateTime}, x::UnixTime) = DateTime(x)

function UnixTime(x::DateTime)
instant_ns = (Dates.value(x) - Dates.UNIXEPOCH) * 1_000_000
UnixTime(Dates.UTInstant(Nanosecond(instant_ns)))
end

UnixTime(x::Date) = UnixTime(DateTime(x))

Base.convert(::Type{UnixTime}, x::DateTime) = UnixTime(x)

UnixTime(x::ZonedDateTime) = UnixTime(DateTime(x, UTC))
ZonedDateTime(x::UnixTime, tz::TimeZone) = ZonedDateTime(DateTime(x), tz; from_utc = true)

function Base.show(io::IO, x::UnixTime)
xdt = convert(DateTime, x)
print(io, Dates.format(xdt, dateformat"yyyy-mm-ddTHH:MM:SS.sss"))
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnixTimes
using Dates
using TimeZones
using Test

@testset "UnixTime" begin
Expand Down Expand Up @@ -77,4 +78,15 @@ end
@test t2 > t1
end

@testset "conversion" begin
@test UnixTime(DateTime(2020, 1, 2, 3)) == UnixTime(2020, 1, 2, 3)
@test DateTime(UnixTime(2020, 1, 2, 3)) == DateTime(2020, 1, 2, 3)
@test Date(UnixTime(2020, 1, 2, 3)) == Date(2020, 1, 2)
@test UnixTime(Date(2020, 1, 2)) == UnixTime(2020, 1, 2)
@test convert(UnixTime, DateTime(2020, 1, 2, 3)) == UnixTime(2020, 1, 2, 3)
@test convert(DateTime, UnixTime(2020, 1, 2, 3)) == DateTime(2020, 1, 2, 3)
@test UnixTime(ZonedDateTime(2020, 1, 2, 3, tz"UTC-4")) == UnixTime(2020, 1, 2, 7)
@test ZonedDateTime(UnixTime(2020, 1, 2, 7), tz"UTC-4") == ZonedDateTime(2020, 1, 2, 3, tz"UTC-4")
end

end

0 comments on commit 83739c0

Please sign in to comment.