From 07a351c37378cf84914ec1738b844d0fdaf47bd8 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 10 Jun 2014 18:06:12 +0200 Subject: [PATCH] Add Universal Time calculation functions This is required for sidereal time calculations later. get/set Julian day functions have been renamed to have an _et suffix. Meanwhile, the old gswe_timestamp_get_julian_day() and gswe_timestamp_set_julian_day() functions have been deprecated. --- docs/reference/swe-glib/swe-glib-sections.txt | 4 + src/gswe-timestamp.c | 157 ++++++++++++++++-- src/gswe-timestamp.h | 11 ++ 3 files changed, 159 insertions(+), 13 deletions(-) diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index 21c4c40..f3b5a8e 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -264,6 +264,10 @@ gswe_timestamp_set_gregorian_timezone gswe_timestamp_get_gregorian_timezone gswe_timestamp_set_julian_day gswe_timestamp_get_julian_day +gswe_timestamp_set_julian_day_et +gswe_timestamp_get_julian_day_et +gswe_timestamp_set_julian_day_ut +gswe_timestamp_get_julian_day_ut GSWE_IS_TIMESTAMP GSWE_IS_TIMESTAMP_CLASS diff --git a/src/gswe-timestamp.c b/src/gswe-timestamp.c index 33129b5..2afb3e2 100644 --- a/src/gswe-timestamp.c +++ b/src/gswe-timestamp.c @@ -55,6 +55,7 @@ struct _GsweTimestampPrivate { gdouble gregorian_timezone_offset; gdouble julian_day; + gdouble julian_day_ut; }; enum { @@ -813,29 +814,23 @@ gswe_timestamp_calculate_julian(GsweTimestamp *timestamp, GError **err) g_set_error(err, GSWE_ERROR, GSWE_ERROR_SWE_FATAL, "Swiss Ephemeris error: %s", serr); } else { timestamp->priv->julian_day = dret[0]; + timestamp->priv->julian_day_ut = dret[1]; timestamp->priv->valid_dates |= VALID_JULIAN_DAY; } } /** * gswe_timestamp_set_julian_day: - * @timestamp: A GsweTimestamp + * @timestamp: a #GsweTimestamp * @julian_day: The Julian day number, with hours included as fractions * - * Sets the Julian day value of the timestamp. The Gregorian date will be - * calculated as requested. + * Deprecated: This function is deprecated in version 2.1. In newly written + * code use gswe_timestamp_set_julian_day_et() */ void gswe_timestamp_set_julian_day(GsweTimestamp *timestamp, gdouble julian_day) { - timestamp->priv->julian_day = julian_day; - timestamp->priv->valid_dates = VALID_JULIAN_DAY; - - if (timestamp->priv->instant_recalc == TRUE) { - gswe_timestamp_calculate_all(timestamp, NULL); - } - - gswe_timestamp_emit_changed(timestamp); + gswe_timestamp_set_julian_day_et(timestamp, julian_day, NULL); } /** @@ -843,17 +838,153 @@ gswe_timestamp_set_julian_day(GsweTimestamp *timestamp, gdouble julian_day) * @timestamp: a GsweTimestamp * @err: a #GError * - * Gets the Julian day value of @timestamp. @err is populated if a calculations - * error arises. + * Deprecated: This function is deprecated in version 2.1. In newly written + * code use gswe_timestamp_get_julian_day_et() */ gdouble gswe_timestamp_get_julian_day(GsweTimestamp *timestamp, GError **err) +{ + return gswe_timestamp_get_julian_day_et(timestamp, err); +} + +/** + * gswe_timestamp_set_julian_day_et: + * @timestamp: A GsweTimestamp + * @julian_day: The Julian day number, with hours included as fractions + * @err: a #GError + * + * Sets the Julian day value of the timestamp. The Gregorian date will be + * calculated as requested. Given Julian day must be given in Ephemeris Time + * (ET). See gswe_timestamp_set_julian_day_ut() for Universal Time (UT) + * version. + * + * Since: 2.1 + */ +void +gswe_timestamp_set_julian_day_et(GsweTimestamp *timestamp, gdouble julian_day, GError **err) +{ + GError *err_local = NULL; + + timestamp->priv->julian_day = julian_day; + timestamp->priv->valid_dates = VALID_JULIAN_DAY; + + // to get exact UT value, we must calculate gregorian date, and + // reverse-engineer the UT value from that. This is an overkill, but libswe + // doesn’t provide an API for that. + gswe_timestamp_calculate_gregorian(timestamp, &err_local); + + if (err_local) { + if (err) { + *err = err_local; + } + + return; + } + + timestamp->priv->valid_dates = VALID_GREGORIAN; + gswe_timestamp_calculate_julian(timestamp, &err_local); + + if (err_local) { + if (err) { + *err = err_local; + } + + return; + } + + if (timestamp->priv->instant_recalc == TRUE) { + gswe_timestamp_calculate_all(timestamp, err); + } + + gswe_timestamp_emit_changed(timestamp); +} + +/** + * gswe_timestamp_get_julian_day_et: + * @timestamp: a GsweTimestamp + * @err: a #GError + * + * Gets the Julian day value of @timestamp, Ephemeris Time (ET). For Universal + * Time (UT), see gswe_timestamp_get_julian_day_ut(). @err is populated if the + * calculations raises an error. + * + * Since: 2.1 + */ +gdouble +gswe_timestamp_get_julian_day_et(GsweTimestamp *timestamp, GError **err) { gswe_timestamp_calculate_julian(timestamp, err); return timestamp->priv->julian_day; } +/** + * gswe_timestamp_set_julian_day_ut: + * @timestamp: a GsweTimestamp + * @julian_day: The Julian day number in Universal Time + * @err: a #GError + * + * Sets the Julian day value of the timestamp. The Julian day is considered to + * be in Universal Time (UT). For Ephemeris time, see + * gswe_timestamp_set_julian_day(). Should an error occur, @err is populated + * with the error details. + */ +void +gswe_timestamp_set_julian_day_ut(GsweTimestamp *timestamp, gdouble julian_day, GError **err) +{ + GError *err_local = NULL; + + timestamp->priv->julian_day_ut = julian_day; + timestamp->priv->valid_dates = VALID_JULIAN_DAY; + + // to get exact ET value, we must calculate gregorian date, and + // reverse-engineer the ET value from that. This is an overkill, but libswe + // doesn’t provide an API for that. + gswe_timestamp_calculate_gregorian(timestamp, err); + + if (err_local) { + if (err) { + *err = err_local; + } + + return; + } + + timestamp->priv->valid_dates = VALID_GREGORIAN; + gswe_timestamp_calculate_julian(timestamp, err); + + if (err_local) { + if (err) { + *err = err_local; + } + + return; + } + + if (timestamp->priv->instant_recalc == TRUE) { + gswe_timestamp_calculate_all(timestamp, err); + } + + gswe_timestamp_emit_changed(timestamp); +} + +/** + * gswe_timestamp_get_julian_day_ut: + * @timestamp: a #GsweTimestamp + * @err: a #GError + * + * Gets the Julian day value of @timestamp, Universal Time (UT). For Ephemeris + * Time (ET) see gswe_timestamp_get_julian_day_et(). @err is populated if the + * calculations raise an error. + */ +gdouble +gswe_timestamp_get_julian_day_ut(GsweTimestamp *timestamp, GError **err) +{ + gswe_timestamp_calculate_julian(timestamp, err); + + return timestamp->priv->julian_day_ut; +} + /** * gswe_timestamp_new: * diff --git a/src/gswe-timestamp.h b/src/gswe-timestamp.h index 157049b..513950c 100644 --- a/src/gswe-timestamp.h +++ b/src/gswe-timestamp.h @@ -106,8 +106,19 @@ void gswe_timestamp_set_gregorian_microsecond(GsweTimestamp *timestamp, gint gre gint gswe_timestamp_get_gregorian_microsecond(GsweTimestamp *timestamp, GError **err); void gswe_timestamp_set_gregorian_timezone(GsweTimestamp *timestamp, gdouble gregorian_timezone_offset, GError **err); gdouble gswe_timestamp_get_gregorian_timezone(GsweTimestamp *timestamp); + +#ifndef GSWE_DISABLE_DEPRECATED +G_DEPRECATED_FOR(gswe_timestamp_set_julian_day_et) void gswe_timestamp_set_julian_day(GsweTimestamp *timestamp, gdouble julian_day); + +G_DEPRECATED_FOR(gswe_timestamp_get_julian_day_et) gdouble gswe_timestamp_get_julian_day(GsweTimestamp *timestamp, GError **err); +#endif + +void gswe_timestamp_set_julian_day_et(GsweTimestamp *timestamp, gdouble julian_day, GError **err); +gdouble gswe_timestamp_get_julian_day_et(GsweTimestamp *timestamp, GError **err); +void gswe_timestamp_set_julian_day_ut(GsweTimestamp *timestamp, gdouble julian_day, GError **err); +gdouble gswe_timestamp_get_julian_day_ut(GsweTimestamp *timestamp, GError **err); #endif /* __SWE_GLIB_GSWE_TIMESTAMP_H__ */