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

Improve TimeTick conversions #5

Closed
wants to merge 4 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "TimeArrays"
uuid = "058eeebf-2231-41de-8410-62311c15f51e"
version = "1.0.1"
version = "1.1.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
26 changes: 21 additions & 5 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ struct TimeTick{T<:TimeLike,V<:Any} <: AbstractTick{T,V}
end

function TimeTick{T,V}(x::Tuple{TimeLike,Any}) where {T<:TimeLike,V}
return new{T,V}(x...)
return new{T,V}(x[1], x[2])
end

function TimeTick{T,V}(x::NamedTuple{names,<:Tuple{TimeLike,Any}} where {names}) where {T<:TimeLike,V}
return new{T,V}(x...)
return new{T,V}(x[1], x[2])
end

function TimeTick{T,V}(x::Pair{<:TimeLike,<:Any}) where {T<:TimeLike,V}
Expand Down Expand Up @@ -86,7 +86,7 @@ TimeTick(2024-01-01T00:00:00, 100)
```
"""
function TimeTick(x::Tuple{T,V}) where {T<:TimeLike,V}
return TimeTick{T,V}(x...)
return TimeTick{T,V}(x[1], x[2])
end

"""
Expand All @@ -107,7 +107,7 @@ TimeTick(2024-01-01T00:00:00, 100)
```
"""
function TimeTick(x::NamedTuple{names,Tuple{T,V}}) where {names,T<:TimeLike,V}
return TimeTick{T,V}(x...)
return TimeTick{T,V}(x[1], x[2])
end

"""
Expand Down Expand Up @@ -168,6 +168,22 @@ function Base.convert(::Type{TimeTick{T,V}}, value::Pair{TimeLike,Any}) where {T
return TimeTick{T,V}(value)
end

function Base.convert(::Type{Tuple}, timetick::TimeTick)
return Tuple(timetick)
end

function Base.convert(::Type{Tuple{T,V}}, timetick::TimeTick) where {T<:TimeLike,V}
return Tuple{T,V}(timetick)
end

function Base.convert(::Type{Pair}, timetick::TimeTick)
return Pair(x.first, x.second)
end

function Base.convert(::Type{Pair{T,V}}, timetick::TimeTick) where {T<:TimeLike,V}
return Pair{T,V}(x.first, x.second)
end

function Base.getindex(t::TimeTick, i::Integer)
i == 1 && return ta_timestamp(t)
i == 2 && return ta_value(t)
Expand Down Expand Up @@ -203,7 +219,7 @@ end

Supertype for `TimeArray{T,V}` with timestamps of type `T` and values of type `V`.
"""
abstract type AbstractTimeArray{T,V} <: AbstractVector{TimeTick{T,V}} end
abstract type AbstractTimeArray{T,V} <: AbstractVector{AbstractTick{T,V}} end

"""
TimeArray{T,V} <: AbstractTimeArray{T,V}
Expand Down