Skip to content

Commit fd29411

Browse files
committed
Fix type instability in julian_period
1 parent 35c196b commit fd29411

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/Epochs.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ function Epoch{S}(jd1::T, jd2::T=zero(T), args...; origin=:j2000) where {S, T<:P
154154
end
155155

156156
"""
157-
julian_period(ep::Epoch; origin=:j2000, scale=timescale(ep), unit=days, raw=false)
157+
julian_period([T,] ep::Epoch; origin=:j2000, scale=timescale(ep), unit=days)
158158
159159
Return the period since Julian Epoch `origin` within the time scale `scale` expressed in
160-
`unit` for a given epoch `ep`. If `raw` is `true`, the raw value is returned instead of a
161-
[`Period`](@ref) object.
160+
`unit` for a given epoch `ep`. The result is a [`Period`](@ref) object by default.
161+
If the type argument `T` is present, the result is converted to `T` instead.
162162
163163
### Example ###
164164
@@ -172,11 +172,11 @@ julia> julian_period(ep; scale=TAI)
172172
julia> julian_period(ep; unit=years)
173173
18.100929728496464 years
174174
175-
julia> julian_period(ep; raw=true)
175+
julia> julian_period(Float64, ep)
176176
6611.364583333333
177177
```
178178
"""
179-
function julian_period(ep::Epoch; origin=:j2000, scale=timescale(ep), unit=days, raw=false)
179+
function julian_period(ep::Epoch; origin=:j2000, scale=timescale(ep), unit=days)
180180
ep1 = Epoch(ep, scale)
181181
jd1 = unit(ep1.second * seconds)
182182
jd2 = unit(ep1.fraction * seconds)
@@ -191,8 +191,12 @@ function julian_period(ep::Epoch; origin=:j2000, scale=timescale(ep), unit=days,
191191
throw(ArgumentError("Unknown Julian epoch: $origin"))
192192
end
193193

194-
jd = jd2 + jd1
195-
return ifelse(raw, value(jd), jd)
194+
return jd2 + jd1
195+
end
196+
197+
function julian_period(::Type{T}, ep::Epoch; kwargs...) where T
198+
jd = julian_period(ep; kwargs...)
199+
return T(value(jd))
196200
end
197201

198202
"""

test/epochs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ end
151151
@test ep == UTCEpoch(2000, 1, 1, 12)
152152
@test julian_period(ep) == 0.0days
153153
@test julian_period(ep; scale=TAI, unit=seconds) == 32.0seconds
154-
@test julian_period(ep; raw=true) == 0.0
154+
@test julian_period(Float64, ep) == 0.0
155155
@test j2000(ep) == jd
156156
jd = 86400.0seconds
157157
ep = UTCEpoch(jd)

0 commit comments

Comments
 (0)