Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support datetimes #38

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"]
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
4 changes: 4 additions & 0 deletions src/InverseFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
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"
oschulz marked this conversation as resolved.
Show resolved Hide resolved
_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

@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
aplavin marked this conversation as resolved.
Show resolved Hide resolved