From 43fd0df2b7771bf20080e0c1c32a2e1d245125c0 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Mon, 18 Mar 2024 11:29:48 +0100 Subject: [PATCH] support datetimes (#38) --- Project.toml | 12 ++++++++++-- ext/DatesExt.jl | 16 ++++++++++++++++ src/InverseFunctions.jl | 4 ++++ test/test_inverse.jl | 26 ++++++++++++++++++++------ 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 ext/DatesExt.jl diff --git a/Project.toml b/Project.toml index 6d6c83e..c007d0a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,15 +1,23 @@ name = "InverseFunctions" uuid = "3587e190-3f89-42d0-90ee-14403ec27112" -version = "0.1.12" +version = "0.1.13" [deps] +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" 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"] diff --git a/ext/DatesExt.jl b/ext/DatesExt.jl new file mode 100644 index 0000000..38a153f --- /dev/null +++ b/ext/DatesExt.jl @@ -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 diff --git a/src/InverseFunctions.jl b/src/InverseFunctions.jl index 0c5438b..3710fbe 100644 --- a/src/InverseFunctions.jl +++ b/src/InverseFunctions.jl @@ -13,4 +13,8 @@ include("inverse.jl") include("setinverse.jl") include("test.jl") +@static if !isdefined(Base, :get_extension) + include("../ext/DatesExt.jl") +end + end # module diff --git a/test/test_inverse.jl b/test/test_inverse.jl index 8d00796..5798127 100644 --- a/test/test_inverse.jl +++ b/test/test_inverse.jl @@ -2,6 +2,7 @@ using Test using InverseFunctions +using Dates foo(x) = inv(exp(-x) + 1) @@ -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) @@ -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() @@ -122,3 +124,15 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A)) InverseFunctions.test_inverse(log ∘ foo, x) end end + +@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