Skip to content

Commit 50ebbfb

Browse files
authored
Dates.Time constructor and UNIX_EPOCH constant (#5)
* UNIX_EPOCH constant * Time constructor * Fix typo * Update version and add unit test
1 parent 06380d3 commit 50ebbfb

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "UnixTimes"
22
uuid = "ab1a18e7-b408-4913-896c-624bb82ed7f4"
33
authors = ["Christian Rorvik <[email protected]>"]
4-
version = "1.0.0"
4+
version = "1.1.0"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/UnixTimes.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using TimeZones
55

66
export UnixTime
77
export unix_now
8+
export UNIX_EPOCH
9+
810

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

28+
const UNIX_EPOCH = UnixTime(Dates.UTInstant(Nanosecond(0)))
29+
2630
Dates.days(x::UnixTime) = Dates.days(convert(DateTime, x))
2731
Dates.hour(x::UnixTime) = mod(fld(Dates.value(x), 3600_000_000_000), 24)
2832
Dates.minute(x::UnixTime) = mod(fld(Dates.value(x), 60_000_000_000), 60)
@@ -47,6 +51,7 @@ function Dates.DateTime(x::UnixTime)
4751
end
4852

4953
Dates.Date(x::UnixTime) = Date(DateTime(x))
54+
Dates.Time(x::UnixTime) = Time(Nanosecond(Dates.value(x)))
5055

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

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ end
8282
@test UnixTime(DateTime(2020, 1, 2, 3)) == UnixTime(2020, 1, 2, 3)
8383
@test DateTime(UnixTime(2020, 1, 2, 3)) == DateTime(2020, 1, 2, 3)
8484
@test Date(UnixTime(2020, 1, 2, 3)) == Date(2020, 1, 2)
85+
@test Time(UnixTime(2020, 1, 2, 3, 4, 5, 6, 7, 8)) == Time(3, 4, 5, 6, 7, 8)
8586
@test UnixTime(Date(2020, 1, 2)) == UnixTime(2020, 1, 2)
8687
@test convert(UnixTime, DateTime(2020, 1, 2, 3)) == UnixTime(2020, 1, 2, 3)
8788
@test convert(DateTime, UnixTime(2020, 1, 2, 3)) == DateTime(2020, 1, 2, 3)

0 commit comments

Comments
 (0)