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

Converting Dates.FixedPeriod to Quantity{...,𝐓} #329

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.2.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

Expand Down
3 changes: 3 additions & 0 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import Base: getindex, eltype, step, last, first, frexp
import Base: Integer, Rational, typemin, typemax
import Base: steprange_last, unsigned

import Dates: Dates

import LinearAlgebra: Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal
import LinearAlgebra: istril, istriu, norm
import Random
Expand Down Expand Up @@ -64,5 +66,6 @@ include("fastmath.jl")
include("logarithm.jl")
include("complex.jl")
include("pkgdefaults.jl")
include("dates.jl")

end
29 changes: 29 additions & 0 deletions src/dates.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


# Dates.FixedPeriod == Union{Day, Hour, Microsecond, Millisecond, Minute, Nanosecond, Second, Week}
Quantity(x::Dates.Nanosecond) = x.value * u"ns"
Quantity(x::Dates.Microsecond) = x.value * u"μs"
Quantity(x::Dates.Millisecond) = x.value * u"ms"
Quantity(x::Dates.Second) = x.value * u"s"
Quantity(x::Dates.Minute) = x.value * u"minute"
Quantity(x::Dates.Hour) = x.value * u"hr"
Quantity(x::Dates.Day) = x.value * u"d"
Quantity(x::Dates.Week) = x.value * u"wk"

uconvert(a::Units, x::Dates.FixedPeriod) = uconvert(a, Quantity(x))

Base.promote_rule(::Type{Quantity{T,D,U}}, ::Type{<:Dates.FixedPeriod}) where {T,D,U} =
Quantity{promote_type(T,Int64),D,U}

convert(type::Type{<:Quantity}, x::Dates.FixedPeriod) = convert(type, Quantity(x))

Base.inv(x::Dates.FixedPeriod) = inv(Quantity(x))

convert(::Type{Dates.Nanosecond}, x::Quantity) = Dates.Nanosecond(ustrip(uconvert(u"ns", x)))
convert(::Type{Dates.Microsecond}, x::Quantity) = Dates.Microsecond(ustrip(uconvert(u"μs", x)))
convert(::Type{Dates.Millisecond}, x::Quantity) = Dates.Millisecond(ustrip(uconvert(u"ms", x)))
convert(::Type{Dates.Second}, x::Quantity) = Dates.Second(ustrip(uconvert(u"s", x)))
convert(::Type{Dates.Minute}, x::Quantity) = Dates.Minute(ustrip(uconvert(u"minute", x)))
convert(::Type{Dates.Hour}, x::Quantity) = Dates.Hour(ustrip(uconvert(u"hr", x)))
convert(::Type{Dates.Day}, x::Quantity) = Dates.Day(ustrip(uconvert(u"d", x)))
convert(::Type{Dates.Week}, x::Quantity) = Dates.Week(ustrip(uconvert(u"wk", x)))
19 changes: 18 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Unitful
using Test, LinearAlgebra, Random, ConstructionBase
using Test, LinearAlgebra, Random, Dates, ConstructionBase
import Unitful: DimensionError, AffineError
import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel
import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity
Expand Down Expand Up @@ -187,6 +187,23 @@ end
@test 1u"rpm" == 2π/minute
end
end
@testset "> Date ↔ unitful conversion" begin
uconvert(u"s", Nanosecond(1)) === (1//10^9)u"s"
uconvert(u"s", Microsecond(1)) === (1//10^6)u"s"
uconvert(u"s", Millisecond(1)) === (1//1000)u"s"
uconvert(u"s", Second(1)) === 1u"s"
uconvert(u"s", Minute(1)) === 60u"s"
uconvert(u"s", Hour(1)) === 3600u"s"
uconvert(u"hr", Day(1)) === 24u"hr"
uconvert(u"hr", Week(1)) === 7*24u"hr"

convert(typeof(2u"s"), Second(3)) === 3u"s"
promote(4u"s", Second(5)) === (4u"s", 5u"s")

convert(Second, 1u"hr") === Second(3600)
convert(Minute, 1u"hr") === Minute(60)
convert(Day, 2u"wk") === Day(14)
end
end

@testset "Temperature and affine quantities" begin
Expand Down