Skip to content

Commit

Permalink
Dates.Time constructor and UNIX_EPOCH constant (#5)
Browse files Browse the repository at this point in the history
* UNIX_EPOCH constant

* Time constructor

* Fix typo

* Update version and add unit test
  • Loading branch information
dantaras committed Mar 12, 2021
1 parent 06380d3 commit 50ebbfb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,7 +1,7 @@
name = "UnixTimes"
uuid = "ab1a18e7-b408-4913-896c-624bb82ed7f4"
authors = ["Christian Rorvik <christian.rorvik@gmail.com>"]
version = "1.0.0"
version = "1.1.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
5 changes: 5 additions & 0 deletions src/UnixTimes.jl
Expand Up @@ -5,6 +5,8 @@ using TimeZones

export UnixTime
export unix_now
export UNIX_EPOCH


struct UnixTime <: Dates.AbstractDateTime
instant::Dates.UTInstant{Nanosecond}
Expand All @@ -23,6 +25,8 @@ function UnixTime(
convert(UnixTime, DateTime(y, m, d, h, mi, s, ms)) + Nanosecond(us * 1000 + ns)
end

const UNIX_EPOCH = UnixTime(Dates.UTInstant(Nanosecond(0)))

Dates.days(x::UnixTime) = Dates.days(convert(DateTime, x))
Dates.hour(x::UnixTime) = mod(fld(Dates.value(x), 3600_000_000_000), 24)
Dates.minute(x::UnixTime) = mod(fld(Dates.value(x), 60_000_000_000), 60)
Expand All @@ -47,6 +51,7 @@ function Dates.DateTime(x::UnixTime)
end

Dates.Date(x::UnixTime) = Date(DateTime(x))
Dates.Time(x::UnixTime) = Time(Nanosecond(Dates.value(x)))

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

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Expand Up @@ -82,6 +82,7 @@ end
@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 Time(UnixTime(2020, 1, 2, 3, 4, 5, 6, 7, 8)) == Time(3, 4, 5, 6, 7, 8)
@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)
Expand Down

2 comments on commit 50ebbfb

@ancapdev
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/31792

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 50ebbfb459e85737c066d54db9b504b16c5824be
git push origin v1.1.0

Please sign in to comment.