Skip to content

Commit

Permalink
Merge pull request #23 from JuliaSpaceMissionDesign/dev
Browse files Browse the repository at this point in the history
v1.3.0
  • Loading branch information
andreapasquale94 authored Jun 4, 2024
2 parents 1e57a27 + 75abe71 commit 944f79e
Show file tree
Hide file tree
Showing 17 changed files with 282 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
fail-fast: false
matrix:
version:
- '1.8'
- '1.9'
- '1.10'
- '1'
os:
- ubuntu-latest
Expand Down
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name = "Tempo"
uuid = "c33777b2-e695-4fae-9135-aeae8855dd81"
authors = ["JSMD Team"]
version = "1.2.0"
version = "1.3.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
FunctionWrappersWrappers = "77dc65aa-8811-40c2-897b-53d922fa7daf"
JSMDInterfaces = "6b30ee2f-618e-4a15-bf4e-7df7b496e609"
JSMDUtils = "67801824-9821-48b9-a814-9bfb19231086"
Expand Down
2 changes: 2 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ makedocs(;
"Low-level API" => "lapi.md"
],
],
clean=true,
checkdocs=:none
)

deploydocs(; repo="github.com/JuliaSpaceMissionDesign/Tempo.jl", branch="gh-pages")
5 changes: 5 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ find_dayinyear

## Epochs

### Duration
```@docs
Duration
```

### Types
```@docs
Epoch
Expand Down
1 change: 0 additions & 1 deletion docs/src/lapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Tempo.utc2tai
```@docs
Tempo.Leapseconds
Tempo.LEAPSECONDS
Tempo.get_leapseconds
Tempo.leapseconds
```

Expand Down
17 changes: 13 additions & 4 deletions src/Tempo.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module Tempo

# TODO: remove this dependency - could be handled by Tempo itself?
using Dates: DateTime as DatesDateTime, datetime2julian, now

using FunctionWrappersWrappers: FunctionWrappersWrapper,
FunctionWrappers.FunctionWrapper

Expand Down Expand Up @@ -30,17 +27,29 @@ using SMDGraphs:

import SMDGraphs: get_node_id

export DJ2000, DMJD, DJM0

include("constants.jl")
include("errors.jl")
include("convert.jl")
include("parse.jl")

include("leapseconds.jl")
include("offset.jl")

export TIMESCALES, @timescale, add_timescale!,
TimeSystem, timescale_alias, timescale_name, timescale_id
include("scales.jl")

export Date, Time,
year, month, day, find_dayinyear,
j2000, j2000s,j2000c, hour, minute, second, DateTime
include("datetime.jl")
include("origin.jl")

export Duration, value
include("duration.jl")

export Epoch, j2000, j2000s, j2000c, doy, timescale, value
include("epoch.jl")

# Package precompilation routines
Expand Down
2 changes: 0 additions & 2 deletions src/constants.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

export DJ2000, DMJD, DJM0

const DAY2SEC = 86400
const YEAR2SEC = 60 * 60 * 24 * 365.25
const CENTURY2SEC = 60 * 60 * 24 * 365.25 * 100
Expand Down
15 changes: 0 additions & 15 deletions src/datetime.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@

export Date,
Time,
year,
month,
day,
find_dayinyear,
j2000,
j2000s,
j2000c,
hour,
minute,
second,
DateTime


# -------------------------------------
# DATE
# -------------------------------------
Expand Down
79 changes: 79 additions & 0 deletions src/duration.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

"""
Duration{T}
A `Duration` represents a period of time, split into an integer number of seconds and a
fractional part.
### Fields
- `seconds`: The integer number of seconds.
- `fraction`: The fractional part of the duration, where `T` is a subtype of `Number`.
"""
struct Duration{T}
seconds::Int
fraction::T
end

function Duration(seconds::T) where {T<:Number}
i, f = divrem(seconds, 1)
return Duration{T}(i, f)
end

function Duration(sec::Int, frac::T) where {T<:Number}
return Duration{T}(sec, frac)
end

value(d::Duration{T}) where T = d.seconds + d.fraction

function Base.isless(d::Duration{T}, q::Number) where T
return value(d) < q
end

function Base.isless(d::Duration{T1}, d2::Duration{T2}) where {T1, T2}
return value(d) < value(d2)
end

function fmasf(a, b, mul)
amulb = fma(mul, a, b)
i, f = divrem(amulb, 1)
return i, f
end

function Base.:-(d1::Duration, d2::Duration)
s1, f1 = d1.seconds, d1.fraction
s2, f2 = d2.seconds, d2.fraction
ds, df = divrem(f1 - f2, 1)
sec = s1 - s2 + ds
if df < 0
sec -= 1
df += 1
end
return Duration(convert(Int, sec), df)
end

function Base.:+(d1::Duration, d2::Duration)
s1, f1 = d1.seconds, d1.fraction
s2, f2 = d2.seconds, d2.fraction
s, f = fmasf(f1, f2, 1)
return Duration(convert(Int, s1 + s2 + s), f)
end

function Base.:+(d::Duration, x::Number)
es, ef = d.seconds, d.fraction
xs, xf = divrem(x, 1)
s, f = fmasf(ef, xf, 1)
return Duration(convert(Int, es + xs + s), f)
end

function Base.:-(d::Duration, x::Number)
es, ef = d.seconds, d.fraction
xs, xf = divrem(x, 1)
ds, df = divrem(ef - xf, 1)
sec = es - xs + ds
if df < 0
sec -= 1
df += 1
end
return Duration(convert(Int, sec), df)
end

65 changes: 45 additions & 20 deletions src/epoch.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export Epoch, j2000, j2000s, j2000c, second, timescale, value

# This is the default timescale used by Epochs
const DEFAULT_EPOCH_TIMESCALE = :TDB

Expand Down Expand Up @@ -68,13 +66,12 @@ julia> Epoch("12.0 TT")
"""
struct Epoch{S,T}
scale::S
seconds::Int
fraction::T
dur::Duration{T}
end

function Epoch{S}(seconds::Number) where {S<:AbstractTimeScale}
sec, frac = divrem(seconds, 1)
return Epoch{S, typeof(frac)}(S(), Int(sec), frac)
return Epoch{S, typeof(frac)}(S(), Duration(Int(sec), frac))
end

Epoch(sec::Number, ::S) where {S<:AbstractTimeScale} = Epoch{S}(sec)
Expand All @@ -89,8 +86,10 @@ Epoch{S,T}(e::Epoch{S,T}) where {S,T} = e
Epoch{S,T}(e::Epoch{S,N}) where {S, N, T} = Epoch{S}(T(j2000s(e)))

# Construct an epoch from an ISO string and a scale
function Epoch(s::AbstractString, scale::AbstractTimeScale)
function Epoch(s::AbstractString, scale::S) where {S <: AbstractTimeScale}
y, m, d, H, M, sec, sf = parse_iso(s)

# TODO: the precision of this could be improved
_, jd2 = calhms2jd(y, m, d, H, M, sec + sf)
return Epoch(jd2 * 86400, scale)
end
Expand Down Expand Up @@ -140,22 +139,22 @@ function Epoch(s::AbstractString)
if length(sub) == 2 # check for timescale
scale = eval(Symbol(sub[2]))
end
return Epoch(parse(Float64, sub[1]) * 86400.0, scale)
return Epoch(parse(Float64, sub[1]) * DAY2SEC, scale)
end

"""
timescale(ep::Epoch)
timescale(e::Epoch)
Epoch timescale.
"""
timescale(ep::Epoch) = ep.scale
timescale(e::Epoch) = e.scale

"""
value(ep::Epoch)
value(e::Epoch)
Full `Epoch` value.
"""
@inline value(ep::Epoch) = ep.seconds + ep.fraction
@inline value(e::Epoch) = value(e.dur)

"""
j2000(e::Epoch)
Expand All @@ -178,29 +177,57 @@ Convert `Epoch` in Julian Date centuries since [`J2000`](@ref).
"""
j2000c(e::Epoch) = value(e) / CENTURY2SEC

"""
doy(e::Epoch)
Find day of year.
"""
function doy(e::Epoch)
Y, M, D, _ = jd2cal(DJ2000, j2000(e))
return find_dayinyear(M, D, isleapyear(Y))
end

function Base.show(io::IO, ep::Epoch)
return print(io, DateTime(ep), " ", timescale(ep))
end

# ----
# Operations

Base.:-(e1::Epoch{S}, e2::Epoch{S}) where S = value(e1) - value(e2)
function Base.:-(e1::Epoch{S}, e2::Epoch{S}) where S
return e1.dur - e2.dur
end
function Base.:-(::Epoch{S1}, ::Epoch{S2}) where {S1, S2}
throw(ErrorException("only epochs defined in the same timescale can be subtracted."))
end

Base.:+(e::Epoch, x::Number) = Epoch(value(e) + x, timescale(e))
Base.:-(e::Epoch, x::Number) = Epoch(value(e) - x, timescale(e))
function Base.:+(e::Epoch{S, N}, x::Number) where {S, N}
return Epoch{S, N}(timescale(e), e.dur + x)
end

function (::Base.Colon)(start::Epoch, step::AbstractFloat, stop::Epoch)
function Base.:-(e::Epoch{S, N}, x::Number) where {S, N}
return Epoch{S, N}(timescale(e), e.dur - x)
end

function Base.:+(e::Epoch{S, N}, d::Duration{<:Number}) where {S, N}
return Epoch{S, N}(timescale(e), e.dur + d)
end

function Base.:-(e::Epoch{S, N}, d::Duration{<:Number}) where {S, N}
return Epoch{S, N}(timescale(e), e.dur - d)
end

function (::Base.Colon)(start::Epoch, step::Number, stop::Epoch)
step = start < stop ? step : -step
return StepRangeLen(start, step, floor(Int64, (stop - start) / step) + 1)
diff = value(stop - start)
return StepRangeLen(start, step, floor(Int, diff / step) + 1)
end

(::Base.Colon)(start::Epoch, stop::Epoch) = (:)(start, 86400, stop)
function (::Base.Colon)(start::Epoch, step::Duration, stop::Epoch)
return (:)(start, value(step), stop)
end

Base.isless(e1::Epoch{S}, e2::Epoch{S}) where {S} = value(e1) < value(e2)
Base.isless(e1::Epoch{S}, e2::Epoch{S}) where {S} = e1.dur < e2.dur

function Base.isapprox(e1::Epoch{S}, e2::Epoch{S}; kwargs...) where {S}
return isapprox(value(e1), value(e2); kwargs...)
Expand Down Expand Up @@ -241,11 +268,9 @@ end
Construct a `DateTime` object from an [`Epoch`](@ref).
"""
function DateTime(ep::Epoch)

y, m, d, H, M, S = jd2calhms(DJ2000, j2000(ep))
s, f = divrem(S, 1)

return DateTime(y, m, d, H, M, convert(Int, s), f)

end

Loading

2 comments on commit 944f79e

@andreapasquale94
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

  • Removed automatic leapseconds parsing
  • Higher precision arithmetic using Duration
  • Improved tests

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108528

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.0 -m "<description of version>" 944f79e51f3b74c7f90dc3476b5ad2fbbfdb29a6
git push origin v1.3.0

Please sign in to comment.