Skip to content

Commit

Permalink
Merge c9379b3 into d82fc87
Browse files Browse the repository at this point in the history
  • Loading branch information
helgee committed Aug 6, 2018
2 parents d82fc87 + c9379b3 commit 295d3d0
Show file tree
Hide file tree
Showing 18 changed files with 681 additions and 758 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ dist: trusty
language: julia
os:
- linux
# - osx
- osx
julia:
- 0.6
- 0.7
- nightly
matrix:
allow_failures:
Expand Down
12 changes: 7 additions & 5 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
julia 0.6
ItemGraphs 0.2.0
MuladdMacro 0.0.2
julia 0.7-beta
EarthOrientation 0.2.0
OptionalData 0.1.0
RemoteFiles 0.1.0
# ItemGraphs 0.2.0
LeapSeconds
MuladdMacro 0.0.2
# OptionalData 0.1.0
Reexport 0.1.0
# RemoteFiles 0.1.0
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
matrix:
Expand Down
203 changes: 203 additions & 0 deletions src/AstroDates.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
module AstroDates

using ..TimeScales: TimeScale

export Date, Time, DateTime,
year, month, day, j2000day, calendar,
hour, minute, second

abstract type Calendar end

struct ProlepticJulianCalendar <: Calendar end

struct JulianCalendar <: Calendar end

struct GregorianCalendar <: Calendar end

function findyear(::ProlepticJulianCalendar, j2000day)
-Int((-Int64(4) * j2000day - Int64(2920488)) ÷ Int64(1461))
end

function last_j2000_dayofyear(::ProlepticJulianCalendar, year)
365 * year + (year + 1) ÷ 4 - 730123
end

isleap(::ProlepticJulianCalendar, year) = (year % 4) == 0

function findyear(::JulianCalendar, j2000day)
-Int((-Int64(4) * j2000day - Int64(2921948)) ÷ Int64(1461))
end

function last_j2000_dayofyear(::JulianCalendar, year)
365 * year + year ÷ 4 - 730122
end

isleap(::JulianCalendar, year) = (year % 4) == 0

function findyear(::GregorianCalendar, j2000day)
year = Int((Int64(400) * j2000day + Int64(292194288)) ÷ Int64(146097))

# The previous estimate is one unit too high in some rare cases
# (240 days in the 400 years gregorian cycle, about 0.16%)
if j2000day <= last_j2000_dayofyear(GregorianCalendar(), year - 1)
year -= 1
end

year
end

function last_j2000_dayofyear(::GregorianCalendar, year)
365 * year + year ÷ 4 - year ÷ 100 + year ÷ 400 - 730120
end

function isleap(::GregorianCalendar, year)
year % 4 == 0 && (year % 400 == 0 || year % 100 != 0)
end

const PREVIOUS_MONTH_END_DAY_LEAP = (
0,
31,
60,
91,
121,
152,
182,
213,
244,
274,
305,
335,
)

const PREVIOUS_MONTH_END_DAY = (
0,
31,
59,
90,
120,
151,
181,
212,
243,
273,
304,
334,
)

function findmonth(dayinyear, isleap)
offset = isleap ? 313 : 323
dayinyear < 32 ? 1 : (10 * dayinyear + offset) ÷ 306
end

function findday(dayinyear, month, isleap)
previous_days = isleap ? PREVIOUS_MONTH_END_DAY_LEAP : PREVIOUS_MONTH_END_DAY
dayinyear - previous_days[month]
end

function finddayinyear(month, day, isleap)
previous_days = isleap ? PREVIOUS_MONTH_END_DAY_LEAP : PREVIOUS_MONTH_END_DAY
day + previous_days[month]
end

function getcalendar(year, month, day)
calendar = GregorianCalendar()
if year < 1583
if year < 1
calendar = ProlepticJulianCalendar()
elseif year < 1582 || month < 10 || (month < 11 && day < 5)
calendar = JulianCalendar()
end
end
calendar
end

struct Date{C}
year::Int
month::Int
day::Int
Date{C}(year, month, day) where {C} = new{C::Calendar}(year, month, day)
end

function Date(offset)
calendar = GregorianCalendar()
if offset < -152384
if offset > -730122
calendar = JulianCalendar()
else
calendar = ProlepticJulianCalendar()
end
end

year = findyear(calendar, offset)
dayinyear = offset - last_j2000_dayofyear(calendar, year - 1)

month = findmonth(dayinyear, isleap(calendar, year))
day = findday(dayinyear, month, isleap(calendar, year))

Date{calendar}(year, month, day)
end

function Date(year, month, day)
check = Date(j2000day(year, month, day))
if check.year != year || check.month != month || check.day != day
throw(ArgumentError("Invalid date."))
end
Date{calendar(check)}(year, month, day)
end

year(s::Date) = s.year
month(s::Date) = s.month
day(s::Date) = s.day
calendar(s::Date{C}) where {C} = C

function j2000day(calendar::Calendar, year, month, day)
last_j2000_dayofyear(calendar, year - 1) + finddayinyear(month, day, isleap(calendar, year))
end

function j2000day(year, month, day)
calendar = getcalendar(year, month, day)
j2000day(calendar, year, month, day)
end

j2000day(s::Date{C}) where {C} = j2000day(C, year(s), month(s), day(s))

const JULIAN_EPOCH = Date(-4712, 1, 1)
const MODIFIED_JULIAN_EPOCH = Date(1858, 11, 17)
const FIFTIES_EPOCH = Date(1950, 1, 1)
const CCSDS_EPOCH = Date(1958, 1, 1)
const GALILEO_EPOCH = Date(1999, 8, 22)
const GPS_EPOCH = Date(1980, 1, 6)
const J2000_EPOCH = Date(2000, 1, 1)

struct Time
hour::Int
minute::Int
second::Float64

function Time(hour, minute, second)
if hour < 0 || hour > 23
throw(ArgumentError("`hour` must be an integer between 0 and 23."))
elseif minute < 0 || minute > 59
throw(ArgumentError("`minute` must be an integer between 0 and 59."))
elseif second < 0 || second >= 61.0
throw(ArgumentError("`minute` must be a float between 0 and 60."))
end

new(hour, minute, second)
end
end

const H00 = Time(0, 0, 0.0)
const H12 = Time(12, 0, 0.0)

hour(s::Time) = s.hour
minute(s::Time) = s.minute
second(s::Time) = s.second

struct DateTime{C}
date::Date{C}
time::Time
end

end

15 changes: 2 additions & 13 deletions src/AstroTime.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
__precompile__()

module AstroTime

using EarthOrientation
using Reexport
import RemoteFiles: path, isfile

export @timescale

include("utils.jl")

include("LeapSeconds.jl")
include("TimeScales.jl")
include("Periods.jl")
include("AstroDates.jl")
include("Epochs.jl")

@reexport using .TimeScales
@reexport using .Periods
@reexport using .LeapSeconds
@reexport using .AstroDates
@reexport using .Epochs

function __init__()
isfile(LSK_FILE) && push!(LSK_DATA, path(LSK_FILE))
end

"""
@timescale scale
Expand Down Expand Up @@ -54,8 +45,6 @@ end

function update()
EarthOrientation.update()
download(LSK_FILE)
push!(LSK_DATA, path(LSK_FILE))
nothing
end

Expand Down

0 comments on commit 295d3d0

Please sign in to comment.