Example #1
0
time64
gnc_mktime (struct tm* time)
{
     GDateTime *gdt;
     time64 secs;
     normalize_struct_tm (time);
     gdt = gnc_g_date_time_new_local (time->tm_year + 1900, time->tm_mon,
				      time->tm_mday, time->tm_hour,
				      time->tm_min, (gdouble)(time->tm_sec));
     if (gdt == NULL)
     {
         g_warning("Invalid time passed to gnc_mktime");
         return -1;
     }
     time->tm_mon = time->tm_mon > 0 ? time->tm_mon - 1 : 11;
     // Watch out: struct tm has wday=0..6 with Sunday=0, but GDateTime has wday=1..7 with Sunday=7.
     time->tm_wday = g_date_time_get_day_of_week (gdt) % 7;
     time->tm_yday = g_date_time_get_day_of_year (gdt);
     time->tm_isdst = g_date_time_is_daylight_savings (gdt);

#ifdef HAVE_STRUCT_TM_GMTOFF
     time->tm_gmtoff = g_date_time_get_utc_offset (gdt) / G_TIME_SPAN_SECOND;
#endif

     secs = g_date_time_to_unix (gdt);
     g_date_time_unref (gdt);
     return secs;
}
/*
 * Formulas taken from https://www.esrl.noaa.gov/gmd/grad/solcalc/calcdetails.html
 *
 * The returned values are fractional hours, so 6am would be 6.0 and 4:30pm
 * would be 16.5.
 *
 * The values returned by this function might not make sense for locations near
 * the polar regions. For example, in the north of Lapland there might not be
 * a sunrise at all.
 */
gboolean
gsd_night_light_get_sunrise_sunset (GDateTime *dt,
                                    gdouble pos_lat, gdouble pos_long,
                                    gdouble *sunrise, gdouble *sunset)
{
        g_autoptr(GDateTime) dt_zero = g_date_time_new_utc (1900, 1, 1, 0, 0, 0);
        GTimeSpan ts = g_date_time_difference (dt, dt_zero);

        g_return_val_if_fail (pos_lat <= 90.f && pos_lat >= -90.f, FALSE);
        g_return_val_if_fail (pos_long <= 180.f && pos_long >= -180.f, FALSE);

        gdouble tz_offset = (gdouble) g_date_time_get_utc_offset (dt) / G_USEC_PER_SEC / 60 / 60; // B5
        gdouble date_as_number = ts / G_USEC_PER_SEC / 24 / 60 / 60 + 2;  // B7
        gdouble time_past_local_midnight = 0;  // E2, unused in this calculation
        gdouble julian_day = date_as_number + 2415018.5 +
                        time_past_local_midnight - tz_offset / 24;
        gdouble julian_century = (julian_day - 2451545) / 36525;
        gdouble geom_mean_long_sun = fmod (280.46646 + julian_century *
                        (36000.76983 + julian_century * 0.0003032), 360); // I2
        gdouble geom_mean_anom_sun = 357.52911 + julian_century *
                        (35999.05029 - 0.0001537 * julian_century);  // J2
        gdouble eccent_earth_orbit = 0.016708634 - julian_century *
                        (0.000042037 + 0.0000001267 * julian_century); // K2
        gdouble sun_eq_of_ctr = sin (deg2rad (geom_mean_anom_sun)) *
                        (1.914602 - julian_century * (0.004817 + 0.000014 * julian_century)) +
                        sin (deg2rad (2 * geom_mean_anom_sun)) * (0.019993 - 0.000101 * julian_century) +
                        sin (deg2rad (3 * geom_mean_anom_sun)) * 0.000289; // L2
        gdouble sun_true_long = geom_mean_long_sun + sun_eq_of_ctr; // M2
        gdouble sun_app_long = sun_true_long - 0.00569 - 0.00478 *
                        sin (deg2rad (125.04 - 1934.136 * julian_century)); // P2
        gdouble mean_obliq_ecliptic = 23 +  (26 +  ((21.448 - julian_century *
                        (46.815 + julian_century * (0.00059 - julian_century * 0.001813)))) / 60) / 60; // Q2
        gdouble obliq_corr = mean_obliq_ecliptic + 0.00256 *
                        cos (deg2rad (125.04 - 1934.136 * julian_century)); // R2
        gdouble sun_declin = rad2deg (asin (sin (deg2rad (obliq_corr)) *
                                            sin (deg2rad (sun_app_long)))); // T2
        gdouble var_y = tan (deg2rad (obliq_corr/2)) * tan (deg2rad (obliq_corr / 2)); // U2
        gdouble eq_of_time = 4 * rad2deg (var_y * sin (2 * deg2rad (geom_mean_long_sun)) -
                        2 * eccent_earth_orbit * sin (deg2rad (geom_mean_anom_sun)) +
                        4 * eccent_earth_orbit * var_y *
                                sin (deg2rad (geom_mean_anom_sun)) *
                                cos (2 * deg2rad (geom_mean_long_sun)) -
                        0.5 * var_y * var_y * sin (4 * deg2rad (geom_mean_long_sun)) -
                        1.25 * eccent_earth_orbit * eccent_earth_orbit *
                                sin (2 * deg2rad (geom_mean_anom_sun))); // V2
        gdouble ha_sunrise = rad2deg (acos (cos (deg2rad (90.833)) / (cos (deg2rad (pos_lat)) *
                        cos (deg2rad (sun_declin))) - tan (deg2rad (pos_lat)) *
                        tan (deg2rad (sun_declin)))); // W2
        gdouble solar_noon =  (720 - 4 * pos_long - eq_of_time + tz_offset * 60) / 1440; // X2
        gdouble sunrise_time = solar_noon - ha_sunrise * 4 / 1440; //  Y2
        gdouble sunset_time = solar_noon + ha_sunrise * 4 / 1440; // Z2

        /* convert to hours */
        if (sunrise != NULL)
                *sunrise = sunrise_time * 24;
        if (sunset != NULL)
                *sunset = sunset_time * 24;
        return TRUE;
}
Example #3
0
/**
 * gst_date_time_get_time_zone_offset:
 * @datetime: a #GstDateTime
 *
 * Retrieves the offset from UTC in hours that the timezone specified
 * by @datetime represents. Timezones ahead (to the east) of UTC have positive
 * values, timezones before (to the west) of UTC have negative values.
 * If @datetime represents UTC time, then the offset is zero.
 *
 * Return value: the offset from UTC in hours
 */
gfloat
gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
{
  g_return_val_if_fail (datetime != NULL, 0.0);
  g_return_val_if_fail (gst_date_time_has_time (datetime), 0.0);

  return (g_date_time_get_utc_offset (datetime->datetime) /
      G_USEC_PER_SEC) / 3600.0;
}
Example #4
0
static gboolean
handle_get_server_time (CockpitManager *object,
                        GDBusMethodInvocation *invocation)
{
  GDateTime *now = g_date_time_new_now_local ();
  cockpit_manager_complete_get_server_time (object, invocation,
                                            g_date_time_to_unix (now),
                                            g_date_time_get_timezone_abbreviation (now),
                                            g_date_time_get_utc_offset (now) / 1.0e6);
  g_date_time_unref (now);
  return TRUE;
}
Example #5
0
/* Linux, Darwin, and MSWindows implementations of this function set the
 * globals timezone and daylight; BSD doesn't have those globals, and
 * Gnucash never uses them, so they're omitted from this
 * implementation. Bug 704185.
 */
struct tm*
gnc_localtime_r (const time64 *secs, struct tm* time)
{
     guint index = 0;
     GDateTime *gdt = gnc_g_date_time_new_from_unix_local (*secs);
     g_return_val_if_fail (gdt != NULL, NULL);

     gnc_g_date_time_fill_struct_tm (gdt, time);
     if (g_date_time_is_daylight_savings (gdt))
     {
	  index = 1;
          time->tm_isdst = 1;
     }

#ifdef HAVE_STRUCT_TM_GMTOFF
     time->tm_gmtoff = g_date_time_get_utc_offset (gdt) / G_TIME_SPAN_SECOND;
#endif

     g_date_time_unref (gdt);
     return time;
}
Example #6
0
BerValue *
gda_ldap_attr_g_value_to_value (LdapConnectionData *cdata, const GValue *cvalue)
{
	BerValue *bv;

	if (!cvalue)
		return NULL;

	bv = g_new (struct berval, 1);

	if (G_VALUE_TYPE (cvalue) == G_TYPE_STRING) {
		const gchar *cstr;
		cstr = g_value_get_string (cvalue);
		bv->bv_val = g_strdup (cstr);
		bv->bv_len = strlen (cstr);
	}
	else if (G_VALUE_TYPE (cvalue) == G_TYPE_DATE_TIME) {
		GDateTime *ts;
		gchar *str;
		ts = g_value_get_boxed (cvalue);
		if (g_date_time_get_second (ts) == (gint) g_date_time_get_seconds (ts)) {
			if (g_date_time_get_utc_offset (ts) == 0)
				str = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d",
															 g_date_time_get_year (ts),
															 g_date_time_get_month (ts),
															 g_date_time_get_day_of_month (ts),
															 g_date_time_get_hour (ts),
															 g_date_time_get_minute (ts),
															 g_date_time_get_second (ts));
			else {
				str = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d",
															 g_date_time_get_year (ts),
															 g_date_time_get_month (ts),
															 g_date_time_get_day_of_month (ts),
															 g_date_time_get_hour (ts),
															 g_date_time_get_minute (ts),
															 g_date_time_get_second (ts));
				TO_IMPLEMENT;
			}
		}
		else {
			if (g_date_time_get_utc_offset (ts) == 0)
				str = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d,%lu",
															 g_date_time_get_year (ts),
															 g_date_time_get_month (ts),
															 g_date_time_get_day_of_month (ts),
															 g_date_time_get_hour (ts),
															 g_date_time_get_minute (ts),
															 g_date_time_get_second (ts),
															 (gulong) ((g_date_time_get_seconds (ts) - g_date_time_get_second (ts)) * 1000000.0));
			else {
				str = g_strdup_printf ("%04d-%02d-%02dT%02d:%02d:%02d,%lu",
															 g_date_time_get_year (ts),
															 g_date_time_get_month (ts),
															 g_date_time_get_day_of_month (ts),
															 g_date_time_get_hour (ts),
															 g_date_time_get_minute (ts),
															 g_date_time_get_second (ts),
															 (gulong) ((g_date_time_get_seconds (ts) - g_date_time_get_second (ts)) * 1000000.0));
				TO_IMPLEMENT;
			}
		}
		bv->bv_val = str;
		bv->bv_len = strlen (str);
	}
	else if (G_VALUE_TYPE (cvalue) == G_TYPE_DATE) {
		GDate *date;
		gchar *str;
		date = (GDate*) g_value_get_boxed (cvalue);
		str = g_strdup_printf ("%04d-%02d-%02d", g_date_get_year (date), g_date_get_month (date),
				       g_date_get_day (date));
		bv->bv_val = str;
		bv->bv_len = strlen (str);
	}
	else if (G_VALUE_TYPE (cvalue) == GDA_TYPE_NULL) {
		bv->bv_val = NULL;
		bv->bv_len = 0;
	}
	else if (G_VALUE_TYPE (cvalue) == GDA_TYPE_BINARY) {
		TO_IMPLEMENT;
	}
	else if (G_VALUE_TYPE (cvalue) == GDA_TYPE_BLOB) {
		TO_IMPLEMENT;
	}
	else {
		gchar *str;
		str = gda_value_stringify (cvalue);
		bv->bv_val = str;
		bv->bv_len = strlen (str);
	}
	return bv;
}
Example #7
0
gfloat
gst_date_time_get_time_zone_offset (const GstDateTime * datetime)
{
  return (g_date_time_get_utc_offset (datetime->datetime) /
      G_USEC_PER_SEC) / 3600.0;
}