Skip to content

Commit

Permalink
support datetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Sep 30, 2023
1 parent d397fef commit f0f2a43
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
11 changes: 9 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
name = "InverseFunctions"
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
version = "0.1.12"
version = "0.1.13"

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[weakdeps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

[extensions]
DatesExt = "Dates"

[compat]
julia = "1"

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

[targets]
test = ["Documenter"]
test = ["Dates", "Documenter"]
16 changes: 16 additions & 0 deletions ext/DatesExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module DatesExt

using Dates
import InverseFunctions: inverse

inverse(::typeof(Dates.datetime2epochms)) = Dates.epochms2datetime
inverse(::typeof(Dates.epochms2datetime)) = Dates.datetime2epochms
inverse(::typeof(Dates.date2epochdays)) = Dates.epochdays2date
inverse(::typeof(Dates.epochdays2date)) = Dates.date2epochdays

inverse(::typeof(datetime2unix)) = unix2datetime
inverse(::typeof(unix2datetime)) = datetime2unix
inverse(::typeof(datetime2julian)) = julian2datetime
inverse(::typeof(julian2datetime)) = datetime2julian

end
26 changes: 20 additions & 6 deletions test/test_inverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Test
using InverseFunctions
using Dates


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

@static if VERSION >= v"1.6"
_bc_func(f) = Base.Broadcast.BroadcastFunction(f)
else
_bc_func(f) = Base.Fix1(broadcast, f)
end

@testset "inverse" begin
@static if VERSION >= v"1.6"
_bc_func(f) = Base.Broadcast.BroadcastFunction(f)
else
_bc_func(f) = Base.Fix1(broadcast, f)
end

f_without_inverse(x) = 1
@test inverse(f_without_inverse) isa NoInverse
@test_throws ErrorException inverse(f_without_inverse)(42)
Expand All @@ -40,7 +40,9 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
@test @inferred(NoInverse(Complex)) isa NoInverse{Type{Complex}}

InverseFunctions.test_inverse(inverse, log, compare = ===)
end

@testset "maths" begin
InverseFunctions.test_inverse(!, false)

x = rand()
Expand Down Expand Up @@ -122,3 +124,15 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
InverseFunctions.test_inverse(log foo, x)
end
end

VERSION v"1.9" && @testset "dates" begin
InverseFunctions.test_inverse(Dates.date2epochdays, Date(2020, 1, 2); compare = ===)
InverseFunctions.test_inverse(Dates.datetime2epochms, DateTime(2020, 1, 2, 12, 34, 56); compare = ===)
InverseFunctions.test_inverse(Dates.epochdays2date, Int64(1234); compare = ===)
InverseFunctions.test_inverse(Dates.epochms2datetime, Int64(1234567890); compare = ===)

InverseFunctions.test_inverse(datetime2unix, DateTime(2020, 1, 2, 12, 34, 56); compare = ===)
InverseFunctions.test_inverse(unix2datetime, 1234.56; compare = ===)
InverseFunctions.test_inverse(datetime2julian, DateTime(2020, 1, 2, 12, 34, 56); compare = ===)
InverseFunctions.test_inverse(julian2datetime, 1234.56; compare = ===)
end

0 comments on commit f0f2a43

Please sign in to comment.