Skip to content

Commit

Permalink
Hook into the Dates parsing machinery
Browse files Browse the repository at this point in the history
  • Loading branch information
helgee committed Aug 9, 2018
1 parent 710614d commit 8d8f653
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/AstroTime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module AstroTime
using EarthOrientation
using Reexport

import Dates

export @timescale

include("TimeScales.jl")
Expand All @@ -15,6 +17,24 @@ include("Epochs.jl")
@reexport using .AstroDates
@reexport using .Epochs

function __init__()
for scale in TimeScales.ACRONYMS
epoch = Symbol(scale, "Epoch")
@eval begin
Dates.CONVERSION_TRANSLATIONS[$epoch] = (
Dates.Year,
Dates.Month,
Dates.Day,
Dates.Hour,
Dates.Minute,
Dates.Second,
Dates.Millisecond,
)
Dates.default_format(::Type{$epoch}) = Dates.ISODateTimeFormat
end
end
end

"""
@timescale scale
Expand Down
9 changes: 7 additions & 2 deletions src/Epochs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ export Epoch,
JULIAN_EPOCH, J2000_EPOCH, MODIFIED_JULIAN_EPOCH,
FIFTIES_EPOCH, GALILEO_EPOCH, GPS_EPOCH, CCSDS_EPOCH

struct Epoch{S, T}
struct Epoch{S, T} <: Dates.AbstractDateTime
epoch::Int64
offset::T
Epoch{S}(epoch::Int64, offset::T) where {S, T} = new{S::TimeScale, T}(epoch, offset)
end

for scale in TimeScales.acronyms
for scale in TimeScales.ACRONYMS
epoch = Symbol(scale, "Epoch")
@eval begin
const $epoch = Epoch{$scale}
Expand Down Expand Up @@ -80,6 +80,11 @@ function Epoch{S}(year::Int, month::Int, day::Int, hour::Int=0,
Epoch{S}(Date(year, month, day), Time(hour, minute, second))
end

function Epoch{S}(year::Int, month::Int, day::Int, hour::Int,
minute::Int, second::Int, milliseconds::Int) where S
Epoch{S}(Date(year, month, day), Time(hour, minute, second + 1e-3milliseconds))
end

function Epoch{S2}(ep::Epoch{S1}) where {S1, S2}
Δt = tai_offset(S2, ep) - tai_offset(S1, ep)
Epoch{S2}(ep.epoch, ep.offset, Δt)
Expand Down
6 changes: 3 additions & 3 deletions src/TimeScales.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following timescales are defined:
"""
abstract type TimeScale end

const scales = (
const SCALES = (
:CoordinatedUniversalTime,
:UniversalTime,
:InternationalAtomicTime,
Expand All @@ -26,7 +26,7 @@ const scales = (
:BarycentricDynamicalTime,
)

const acronyms = (
const ACRONYMS = (
:UTC,
:UT1,
:TAI,
Expand All @@ -36,7 +36,7 @@ const acronyms = (
:TDB,
)

for (acronym, scale) in zip(acronyms, scales)
for (acronym, scale) in zip(ACRONYMS, SCALES)
name = String(acronym)
@eval begin
struct $scale <: TimeScale end
Expand Down

0 comments on commit 8d8f653

Please sign in to comment.