kotlin: Implement AstroTime.addDays

This commit is contained in:
Ebrahim Byagowi
2022-03-21 13:55:18 +03:30
parent adf7b7a991
commit a4fc126a08
2 changed files with 25 additions and 4 deletions

View File

@@ -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)
}
}

View File

@@ -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())
}
}