diff --git a/docs/reference/swe-glib/swe-glib-sections.txt b/docs/reference/swe-glib/swe-glib-sections.txt index f83aca8..2558ec6 100644 --- a/docs/reference/swe-glib/swe-glib-sections.txt +++ b/docs/reference/swe-glib/swe-glib-sections.txt @@ -249,6 +249,7 @@ GsweTimestampClass gswe_timestamp_new gswe_timestamp_new_from_julian_day gswe_timestamp_new_from_gregorian_full +gswe_timestamp_new_from_now_local gswe_timestamp_set_gregorian_full gswe_timestamp_set_instant_recalc gswe_timestamp_get_instant_recalc @@ -268,6 +269,7 @@ gswe_timestamp_set_gregorian_microsecond gswe_timestamp_get_gregorian_microsecond gswe_timestamp_set_gregorian_timezone gswe_timestamp_get_gregorian_timezone +gswe_timestamp_set_now_local gswe_timestamp_set_julian_day gswe_timestamp_get_julian_day gswe_timestamp_set_julian_day_et diff --git a/src/gswe-timestamp.c b/src/gswe-timestamp.c index 5c6c5d7..90ceb6a 100644 --- a/src/gswe-timestamp.c +++ b/src/gswe-timestamp.c @@ -1595,3 +1595,69 @@ gswe_timestamp_new_from_julian_day(gdouble julian_day) return timestamp; } +/** + * gswe_timestamp_set_now_local: + * @timestamp: the #GsweTimestamp to operate on + * @err: a #GError to store errors + * + * Sets the date and time in @timestamp to the current time in the local time + * zone. This also changes the timezone to the local time zone. + */ +void +gswe_timestamp_set_now_local(GsweTimestamp *timestamp, + GError **err) +{ + GDateTime *datetime; + gint year, + month, + day, + hour, + minute, + seconds, + microsec; + gdouble timezone; + + datetime = g_date_time_new_now_local(); + year = g_date_time_get_year(datetime); + month = g_date_time_get_month(datetime); + day = g_date_time_get_day_of_month(datetime); + hour = g_date_time_get_hour(datetime); + minute = g_date_time_get_minute(datetime); + seconds = g_date_time_get_seconds(datetime); + microsec = g_date_time_get_microsecond(datetime); + timezone = (gdouble)g_date_time_get_utc_offset(datetime) / 3600.0; + g_date_time_unref(datetime); + + gswe_timestamp_set_gregorian_full( + timestamp, + year, month, day, + hour, minute, seconds, microsec, + timezone, + err + ); +} + +/** + * gswe_timestamp_new_from_now_local: + * + * Create a new #GsweTimestamp initialised with the current local date and time. + * + * Returns: (transfer full): a new #GsweTimestamp object, or NULL upon an error. + */ +GsweTimestamp * +gswe_timestamp_new_from_now_local(void) +{ + GsweTimestamp *ret; + GError *err = NULL; + + ret = gswe_timestamp_new(); + gswe_timestamp_set_now_local(ret, &err); + + if (err) { + g_clear_error(&err); + + return NULL; + } + + return ret; +} diff --git a/src/gswe-timestamp.h b/src/gswe-timestamp.h index 8261160..f68ebc6 100644 --- a/src/gswe-timestamp.h +++ b/src/gswe-timestamp.h @@ -204,5 +204,10 @@ gdouble gswe_timestamp_get_julian_day_ut(GsweTimestamp *timestamp, gdouble gswe_timestamp_get_sidereal_time(GsweTimestamp *timestamp, GError **err); +void gswe_timestamp_set_now_local(GsweTimestamp *timestamp, + GError **err); + +GsweTimestamp *gswe_timestamp_new_from_now_local(void); + #endif /* __SWE_GLIB_GSWE_TIMESTAMP_H__ */