Skip to content

Commit 43fd0df

Browse files
authored
support datetimes (#38)
1 parent b4d786d commit 43fd0df

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

Diff for: Project.toml

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
name = "InverseFunctions"
22
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
3-
version = "0.1.12"
3+
version = "0.1.13"
44

55
[deps]
6+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
67
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
78

9+
[weakdeps]
10+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
11+
12+
[extensions]
13+
DatesExt = "Dates"
14+
815
[compat]
916
julia = "1"
1017

1118
[extras]
19+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1220
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
1321

1422
[targets]
15-
test = ["Documenter"]
23+
test = ["Dates", "Documenter"]

Diff for: ext/DatesExt.jl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module DatesExt
2+
3+
using Dates
4+
import InverseFunctions: inverse
5+
6+
inverse(::typeof(Dates.datetime2epochms)) = Dates.epochms2datetime
7+
inverse(::typeof(Dates.epochms2datetime)) = Dates.datetime2epochms
8+
inverse(::typeof(Dates.date2epochdays)) = Dates.epochdays2date
9+
inverse(::typeof(Dates.epochdays2date)) = Dates.date2epochdays
10+
11+
inverse(::typeof(datetime2unix)) = unix2datetime
12+
inverse(::typeof(unix2datetime)) = datetime2unix
13+
inverse(::typeof(datetime2julian)) = julian2datetime
14+
inverse(::typeof(julian2datetime)) = datetime2julian
15+
16+
end

Diff for: src/InverseFunctions.jl

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ include("inverse.jl")
1313
include("setinverse.jl")
1414
include("test.jl")
1515

16+
@static if !isdefined(Base, :get_extension)
17+
include("../ext/DatesExt.jl")
18+
end
19+
1620
end # module

Diff for: test/test_inverse.jl

+20-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Test
44
using InverseFunctions
5+
using Dates
56

67

78
foo(x) = inv(exp(-x) + 1)
@@ -18,14 +19,13 @@ end
1819
(f::Bar)(x) = f.A * x
1920
InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
2021

22+
@static if VERSION >= v"1.6"
23+
_bc_func(f) = Base.Broadcast.BroadcastFunction(f)
24+
else
25+
_bc_func(f) = Base.Fix1(broadcast, f)
26+
end
2127

2228
@testset "inverse" begin
23-
@static if VERSION >= v"1.6"
24-
_bc_func(f) = Base.Broadcast.BroadcastFunction(f)
25-
else
26-
_bc_func(f) = Base.Fix1(broadcast, f)
27-
end
28-
2929
f_without_inverse(x) = 1
3030
@test inverse(f_without_inverse) isa NoInverse
3131
@test_throws ErrorException inverse(f_without_inverse)(42)
@@ -40,7 +40,9 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
4040
@test @inferred(NoInverse(Complex)) isa NoInverse{Type{Complex}}
4141

4242
InverseFunctions.test_inverse(inverse, log, compare = ===)
43+
end
4344

45+
@testset "maths" begin
4446
InverseFunctions.test_inverse(!, false)
4547

4648
x = rand()
@@ -122,3 +124,15 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
122124
InverseFunctions.test_inverse(log foo, x)
123125
end
124126
end
127+
128+
@testset "dates" begin
129+
InverseFunctions.test_inverse(Dates.date2epochdays, Date(2020, 1, 2); compare = ===)
130+
InverseFunctions.test_inverse(Dates.datetime2epochms, DateTime(2020, 1, 2, 12, 34, 56); compare = ===)
131+
InverseFunctions.test_inverse(Dates.epochdays2date, Int64(1234); compare = ===)
132+
InverseFunctions.test_inverse(Dates.epochms2datetime, Int64(1234567890); compare = ===)
133+
134+
InverseFunctions.test_inverse(datetime2unix, DateTime(2020, 1, 2, 12, 34, 56); compare = ===)
135+
InverseFunctions.test_inverse(unix2datetime, 1234.56; compare = ===)
136+
InverseFunctions.test_inverse(datetime2julian, DateTime(2020, 1, 2, 12, 34, 56); compare = ===)
137+
InverseFunctions.test_inverse(julian2datetime, 1234.56; compare = ===)
138+
end

0 commit comments

Comments
 (0)