From a4fc126a0823209f2b19f89f5dfd8afb3f2384c2 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Mon, 21 Mar 2022 13:55:18 +0330 Subject: [PATCH] kotlin: Implement AstroTime.addDays --- .../io/github/cosinekitty/astronomy/Main.kt | 23 +++++++++++++++---- .../io/github/cosinekitty/astronomy/Tests.kt | 6 +++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/Main.kt b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/Main.kt index ddafc1f6..512ece8d 100644 --- a/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/Main.kt +++ b/source/kotlin/src/main/kotlin/io/github/cosinekitty/astronomy/Main.kt @@ -219,17 +219,33 @@ class AstroTime { /** * Converts this object to .NET `DateTime` format. * - * @returns a UTC `DateTime` object for this `AstroTime` value. + * @return a UTC `DateTime` object for this `AstroTime` value. */ fun toDate(): Date = Date(origin.time + (ut * MILLIS_PER_DAY).roundToLong()) /** * Converts this `AstroTime` to ISO 8601 format, expressed in UTC with millisecond resolution. * - * @returns: Example: "2019-08-30T17:45:22.763". + * @return Example: "2019-08-30T17:45:22.763". */ override fun toString(): String = dateFormat.format(toDate()) + /** + * Calculates the sum or difference of an #AstroTime with a specified floating point number of days. + * + * Sometimes we need to adjust a given #AstroTime value by a certain amount of time. + * This function adds the given real number of days in `days` to the date and time in this object. + * + * More precisely, the result's Universal Time field `ut` is exactly adjusted by `days` and + * the Terrestrial Time field `tt` is adjusted for the resulting UTC date and time, + * using a best-fit piecewise polynomial model devised by + * [Espenak and Meeus](https://eclipse.gsfc.nasa.gov/SEhelp/deltatpoly2004.html). + * + * @param days A floating point number of days by which to adjust `time`. May be negative, 0, or positive. + * @return A date and time that is conceptually equal to `time + days`. + */ + fun addDays(days: Double): AstroTime = AstroTime(ut + days) + companion object { private val origin = GregorianCalendar(TimeZone.getTimeZone("UTC")).also { it.set(2000, 0, 1, 12, 0, 0) @@ -256,8 +272,7 @@ class AstroTime { * * @param tt The number of days after the J2000 epoch. */ - fun fromTerrestrialTime(tt: Double): AstroTime = - AstroTime(Astronomy.universalTime(tt), tt) + fun fromTerrestrialTime(tt: Double): AstroTime = AstroTime(Astronomy.universalTime(tt), tt) } } diff --git a/source/kotlin/src/test/kotlin/io/github/cosinekitty/astronomy/Tests.kt b/source/kotlin/src/test/kotlin/io/github/cosinekitty/astronomy/Tests.kt index e13d3ae6..0275d9fe 100644 --- a/source/kotlin/src/test/kotlin/io/github/cosinekitty/astronomy/Tests.kt +++ b/source/kotlin/src/test/kotlin/io/github/cosinekitty/astronomy/Tests.kt @@ -27,4 +27,10 @@ class Tests { assertEquals(deltaT, time.ut) assertEquals(time.toString(), expectedToString) } + + @Test + fun `AstroTime should be able to add days`() { + val time = AstroTime(2000, 1, 1, 12, 0, 0) + assertEquals("2000-01-02 12:00:00.0 +0000", time.addDays(1.0).toString()) + } }