From 208ff9e749a72742eccf90f4ccfb8d4a0f863021 Mon Sep 17 00:00:00 2001 From: Prakhar Srivastava Date: Wed, 6 Jun 2018 06:54:49 +0530 Subject: [PATCH 1/2] cal2jd --- src/conversions.jl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/conversions.jl b/src/conversions.jl index fcdd609..50c9312 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -491,6 +491,37 @@ julia> AstroTime.Epochs.tcbtdb(tcb.jd1, tcb.jd2) end +function cal2jd(iy, im, id) + EYEAR_ALLOWED = -4799 + MON_LENGTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + + if iy < EYEAR_ALLOWED + throw(ArugumentError("Year is outside of the representable range (< $EYEAR_ALLOWED)")) + end + + if im < 1 || im > 12 + throw(ArugumentError("Month is outside of the range (1,12)")) + end + + ly = ((im == 2 ) && !(iy % 4) && (iy % 100 || !(iy % 400))) #check if leap year + + if ((id < 1) || (id > MON_LENGTH[im] + ly))) + throw(ArugumentError("Day is outside of permissible range (1, $(MON_LENGTH[im]))")) + end + + my = (im - 14)/12 + iypmy = iy + my + jd = MJD + jd1 = ((1461 * (iypmy + 4800))/ 4) + + (367 * Int(floor(im - 2 - 12 * my))) / 12 + - (3 * ((iypmy + 4900) / 100)) / 4 + + Int(floor(id - 2432076)) + + jd, jd1 +end + + + # TAI <-> UTC @transform UTC TAI ep begin jd1, jd2 = julian1(ep), julian2(ep) From e92386c174206a0f06c1393b4b7f15c07cc9143f Mon Sep 17 00:00:00 2001 From: Prakhar Srivastava Date: Wed, 6 Jun 2018 08:03:26 +0530 Subject: [PATCH 2/2] test added --- src/conversions.jl | 18 +++++++++--------- test/runtests.jl | 3 +++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/conversions.jl b/src/conversions.jl index 50c9312..4ac333e 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -503,19 +503,19 @@ function cal2jd(iy, im, id) throw(ArugumentError("Month is outside of the range (1,12)")) end - ly = ((im == 2 ) && !(iy % 4) && (iy % 100 || !(iy % 400))) #check if leap year + ly = ((im == 2 ) && !Bool(iy % 4!=0) && (Bool(iy % 100!=0) || !Bool(iy % 400!=0)))? 1:0 #check if leap year - if ((id < 1) || (id > MON_LENGTH[im] + ly))) - throw(ArugumentError("Day is outside of permissible range (1, $(MON_LENGTH[im]))")) + if ((id < 1) || (id > (MON_LENGTH[im] + ly))) + println("Day is outside of permissible range (1, $(MON_LENGTH[im]))") end - my = (im - 14)/12 - iypmy = iy + my + my = (im - 14) ÷ 12 + iypmy = trunc(Int,(iy + my)) jd = MJD - jd1 = ((1461 * (iypmy + 4800))/ 4) - + (367 * Int(floor(im - 2 - 12 * my))) / 12 - - (3 * ((iypmy + 4900) / 100)) / 4 - + Int(floor(id - 2432076)) + jd1 = float((((1461 * (iypmy + 4800)) ÷ 4) + + (367 * trunc(Int,(im - 2 - 12 * my))) ÷ 12 + - (3 * ((iypmy + 4900) ÷ 100)) ÷ 4 + + trunc(Int,id) - 2432076)) jd, jd1 end diff --git a/test/runtests.jl b/test/runtests.jl index 01db5ad..da32e06 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -202,6 +202,9 @@ AstroTime.update() for jd in 2414105.0:10.0:2488985.0 @test Epochs.diff_tdb_tt(jd, 0.5) ≈ Epochs.diff_tdb_tt(jd,0.5,0.0,0.0,0.0,0.0) atol=40e-6 end + + @test Epochs.cal2jd(2000, 1, 1) == ERFA.cal2jd(2000, 1, 1) + @test Epochs.cal2jd(2016, 2, 29) == ERFA.cal2jd(2016, 2, 29) end @testset "Leap Seconds" begin @test leapseconds(TTEpoch(1959,1,1)) == 0