Exemple #1
0
GSList *
chat_log_get_previous(const gchar * const login, const gchar * const recipient,
    GSList *history)
{
    GTimeZone *tz = g_time_zone_new_local();

    GDateTime *now = g_date_time_new_now_local();
    GDateTime *log_date = g_date_time_new(tz,
        g_date_time_get_year(session_started),
        g_date_time_get_month(session_started),
        g_date_time_get_day_of_month(session_started),
        g_date_time_get_hour(session_started),
        g_date_time_get_minute(session_started),
        g_date_time_get_second(session_started));

    // get data from all logs from the day the session was started to today
    while (g_date_time_compare(log_date, now) != 1) {
        char *filename = _get_log_filename(recipient, login, log_date, FALSE);

        FILE *logp = fopen(filename, "r");
        char *line;
        if (logp != NULL) {
            GString *gs_header = g_string_new("");
            g_string_append_printf(gs_header, "%d/%d/%d:",
                g_date_time_get_day_of_month(log_date),
                g_date_time_get_month(log_date),
                g_date_time_get_year(log_date));
            char *header = strdup(gs_header->str);
            history = g_slist_append(history, header);
            g_string_free(gs_header, TRUE);

            while ((line = prof_getline(logp)) != NULL) {
                history = g_slist_append(history, line);
            }

            fclose(logp);
        }

        free(filename);
        GDateTime *next = g_date_time_add_days(log_date, 1);
        g_date_time_unref(log_date);
        log_date = g_date_time_ref(next);
    }

    g_time_zone_unref(tz);

    return history;
}
Exemple #2
0
static void today_clicked (GtkWidget *cal)
{
  GDateTime *now = g_date_time_new_now_local ();
  gtk_calendar_select_month ( GTK_CALENDAR(cal), g_date_time_get_month(now)-1, g_date_time_get_year(now) );
  gtk_calendar_select_day ( GTK_CALENDAR(cal), g_date_time_get_day_of_month(now) );
  g_date_time_unref ( now );
}
static guint
calculate_seconds_until_next_minute (void)
{
  guint seconds;
  GTimeSpan diff;
  GDateTime * now;
  GDateTime * next;
  GDateTime * start_of_next;

  now = g_date_time_new_now_local ();
  next = g_date_time_add_minutes (now, 1);
  start_of_next = g_date_time_new_local (g_date_time_get_year (next),
                                         g_date_time_get_month (next),
                                         g_date_time_get_day_of_month (next),
                                         g_date_time_get_hour (next),
                                         g_date_time_get_minute (next),
                                         1);

  diff = g_date_time_difference (start_of_next, now);
  seconds = (diff + (G_TIME_SPAN_SECOND - 1)) / G_TIME_SPAN_SECOND;

  /* cleanup */
  g_date_time_unref (start_of_next);
  g_date_time_unref (next);
  g_date_time_unref (now);

  return seconds;
}
Exemple #4
0
static gboolean
win32_in_dst (GDateTime *date, TIME_ZONE_INFORMATION *tzinfo)
{
    guint year, month, day;
    SYSTEMTIME *std, *dlt;

    if (tzinfo == NULL || tzinfo->StandardDate.wMonth == 0)
      return FALSE;

    year = g_date_time_get_year (date);
    month = g_date_time_get_month (date);
    day = g_date_time_get_day_of_month (date);

    std = &(tzinfo->StandardDate);
    dlt = &(tzinfo->DaylightDate);

    if (std->wMonth < dlt->wMonth)
    {
         if ((month > dlt->wMonth || month < std->wMonth) ||
	     (month == dlt->wMonth && day > dlt->wDay) ||
	     (month == std->wMonth && day < std->wDay))
	     return TRUE;
    }
    else
    {
         if ((month > dlt->wMonth && month < std->wMonth) ||
	     (month == dlt->wMonth && day > dlt->wDay) ||
	     (month == std->wMonth && day < std->wDay))
	     return TRUE;
    }
    return FALSE;
}
Exemple #5
0
static void _tgenmain_log(GLogLevelFlags level, const gchar* fileName, const gint lineNum, const gchar* functionName, const gchar* format, ...) {
    if(level > tgenLogFilterLevel) {
        return;
    }

    va_list vargs;
    va_start(vargs, format);

    gchar* fileStr = fileName ? g_path_get_basename(fileName) : g_strdup("n/a");
    const gchar* functionStr = functionName ? functionName : "n/a";

    GDateTime* dt = g_date_time_new_now_local();
    GString* newformat = g_string_new(NULL);

    g_string_append_printf(newformat, "%04i-%02i-%02i %02i:%02i:%02i %"G_GINT64_FORMAT".%06i [%s] [%s:%i] [%s] %s",
            g_date_time_get_year(dt), g_date_time_get_month(dt), g_date_time_get_day_of_month(dt),
            g_date_time_get_hour(dt), g_date_time_get_minute(dt), g_date_time_get_second(dt),
            g_date_time_to_unix(dt), g_date_time_get_microsecond(dt),
            _tgenmain_logLevelToString(level), fileStr, lineNum, functionName, format);

    gchar* message = g_strdup_vprintf(newformat->str, vargs);
    g_print("%s\n", message);
    g_free(message);

    g_string_free(newformat, TRUE);
    g_date_time_unref(dt);
    g_free(fileStr);

    va_end(vargs);
}
Exemple #6
0
gchar*
balde_datetime_logging(GDateTime *dt)
{
    return g_strdup_printf("%02d/%s/%04d %02d:%02d:%02d",
        g_date_time_get_day_of_month(dt), months[g_date_time_get_month(dt) - 1],
        g_date_time_get_year(dt), g_date_time_get_hour(dt),
        g_date_time_get_minute(dt), g_date_time_get_second(dt));
}
Exemple #7
0
/**
 * gst_date_time_get_month:
 * @datetime: a #GstDateTime
 *
 * Returns the month of this #GstDateTime. January is 1, February is 2, etc..
 * Call gst_date_time_has_month() before, to avoid warnings.
 *
 * Return value: The month of this #GstDateTime
 */
gint
gst_date_time_get_month (const GstDateTime * datetime)
{
  g_return_val_if_fail (datetime != NULL, 0);
  g_return_val_if_fail (gst_date_time_has_month (datetime), 0);

  return g_date_time_get_month (datetime->datetime);
}
Exemple #8
0
gchar*
balde_datetime_rfc5322(GDateTime *dt)
{
    // datetime must be utc
    return g_strdup_printf("%s, %02d %s %04d %02d:%02d:%02d GMT",
        days[g_date_time_get_day_of_week(dt) - 1],
        g_date_time_get_day_of_month(dt), months[g_date_time_get_month(dt) - 1],
        g_date_time_get_year(dt), g_date_time_get_hour(dt),
        g_date_time_get_minute(dt), g_date_time_get_second(dt));
}
Exemple #9
0
gint
datetime_compare_date (GDateTime *dt1,
                       GDateTime *dt2)
{
  if (!dt1 && !dt2)
    return 0;
  else if (!dt1)
    return -1;
  else if (!dt2)
    return 1;

  if (g_date_time_get_year (dt1) != g_date_time_get_year (dt2))
    return g_date_time_get_year (dt1) - g_date_time_get_year (dt2);

  if (g_date_time_get_month (dt1) != g_date_time_get_month (dt2))
    return g_date_time_get_month (dt1) - g_date_time_get_month (dt2);

  if (g_date_time_get_day_of_month (dt1) != g_date_time_get_day_of_month (dt2))
    return g_date_time_get_day_of_month (dt1) - g_date_time_get_day_of_month (dt2);

  return 0;
}
void
gcal_time_selector_set_time (GcalTimeSelector *selector,
                             GDateTime        *time)
{
  g_return_if_fail (GCAL_IS_TIME_SELECTOR (selector));

  if (selector->time != time)
    {
      gint hour, minute;

      g_clear_pointer (&selector->time, g_date_time_unref);
      selector->time = g_date_time_new_local (g_date_time_get_year (time),
                                              g_date_time_get_month (time),
                                              g_date_time_get_day_of_month (time),
                                              g_date_time_get_hour (time),
                                              g_date_time_get_minute (time),
                                              0);

      /* Update the spinners */
      g_signal_handlers_block_by_func (selector->hour_adjustment, update_time, selector);
      g_signal_handlers_block_by_func (selector->minute_adjustment, update_time, selector);

      hour = g_date_time_get_hour (time);
      minute = g_date_time_get_minute (time);

      if (!selector->format_24h)
        {
          g_signal_handlers_block_by_func (selector->period_combo, update_time, selector);

          gtk_combo_box_set_active (GTK_COMBO_BOX (selector->period_combo), hour >= 12);
          hour =  hour % 12;
          hour = (hour == 0)? 12 : hour;

          g_signal_handlers_unblock_by_func (selector->period_combo, update_time, selector);
        }

      gtk_adjustment_set_value (selector->hour_adjustment, hour);
      gtk_adjustment_set_value (selector->minute_adjustment, minute);

      update_label (selector);

      g_signal_handlers_unblock_by_func (selector->hour_adjustment, update_time, selector);
      g_signal_handlers_unblock_by_func (selector->minute_adjustment, update_time, selector);

      g_object_notify (G_OBJECT (selector), "time");
    }
}
static void _pcapmain_log(GLogLevelFlags level, const gchar* functionName, const gchar* format, ...) {
    va_list vargs;
    va_start(vargs, format);

    GDateTime* dt = g_date_time_new_now_local();
    GString* newformat = g_string_new(NULL);

    g_string_append_printf(newformat, "%04i-%02i-%02i %02i:%02i:%02i %"G_GINT64_FORMAT".%06i [%s] [%s] %s",
            g_date_time_get_year(dt), g_date_time_get_month(dt), g_date_time_get_day_of_month(dt),
            g_date_time_get_hour(dt), g_date_time_get_minute(dt), g_date_time_get_second(dt),
            g_date_time_to_unix(dt), g_date_time_get_microsecond(dt),
            _pcapmain_logLevelToString(level), functionName, format);
    g_logv(PCAP_LOG_DOMAIN, level, newformat->str, vargs);

    g_string_free(newformat, TRUE);
    g_date_time_unref(dt);

    va_end(vargs);
}
char *
cc_util_get_smart_date (GDateTime *date)
{
        gchar *label;
        GDateTime *today, *local;
        GTimeSpan span;

        /* Set today date */
        local = g_date_time_new_now_local ();
        today = g_date_time_new_local (g_date_time_get_year (local),
                                       g_date_time_get_month (local),
                                       g_date_time_get_day_of_month (local),
                                       0, 0, 0);

        span = g_date_time_difference (today, date);
        if (span <= 0)
          {
            label = g_strdup (_("Today"));
          }
        else if (span <= G_TIME_SPAN_DAY)
          {
            label = g_strdup (_("Yesterday"));
          }
        else
          {
            if (g_date_time_get_year (date) == g_date_time_get_year (today))
              {
                /* Translators: This is a date format string in the style of "Feb 24". */
                label = g_date_time_format (date, _("%b %e"));
              }
            else
              {
                /* Translators: This is a date format string in the style of "Feb 24, 2013". */
                label = g_date_time_format (date, _("%b %e, %Y"));
              }
          }

        g_date_time_unref (local);
        g_date_time_unref (today);

        return label;
}
static gboolean on_visibility_notify(GtkWidget* widget,
                                     GdkEvent* event,
                                     gpointer data)
{
    GDateTime* datetime = g_date_time_new_now_local();
    if(!datetime)
    {
        gtk_menu_item_set_label(GTK_MENU_ITEM(greeter.ui.clock.date_widget), "[date]");
        g_signal_handler_disconnect(greeter.ui.clock.date_widget, visibility_notify_id);
        return TRUE;
    }
    gchar* str = g_date_time_format(datetime, config.clock.date_format);
    gtk_menu_item_set_label(GTK_MENU_ITEM(greeter.ui.clock.date_widget), str);
    gtk_calendar_select_month(GTK_CALENDAR(greeter.ui.clock.calendar_widget),
                              g_date_time_get_month(datetime) - 1,
                              g_date_time_get_year(datetime));
    gtk_calendar_select_day(GTK_CALENDAR(greeter.ui.clock.calendar_widget), g_date_time_get_day_of_month(datetime));
    g_free(str);
    g_date_time_unref(datetime);
    return TRUE;
}
void
um_history_dialog_show (UmHistoryDialog *um,
                        GtkWindow       *parent)
{
        GDateTime *temp, *local;

        /* Set the first day of this week */
        local = g_date_time_new_now_local ();
        temp = g_date_time_new_local (g_date_time_get_year (local),
                                      g_date_time_get_month (local),
                                      g_date_time_get_day_of_month (local),
                                      0, 0, 0);
        um->week = g_date_time_add_days (temp, 1 - g_date_time_get_day_of_week (temp));
        um->current_week = g_date_time_ref (um->week);
        g_date_time_unref (local);
        g_date_time_unref (temp);

        show_week (um);

        gtk_window_set_transient_for (GTK_WINDOW (um->dialog), parent);
        gtk_window_present (GTK_WINDOW (um->dialog));
}
static void
change_time (CcDateTimePanel *panel)
{
  CcDateTimePanelPrivate *priv = panel->priv;
  guint h, m;
  GDateTime *old_date;

  old_date = priv->date;

  h = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (W ("h_spinbutton")));
  m = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (W ("m_spinbutton")));

  if (priv->clock_format == G_DESKTOP_CLOCK_FORMAT_12H)
    {
      gboolean is_pm_time;
      GtkWidget *visible_child;

      visible_child = gtk_stack_get_visible_child (GTK_STACK (priv->am_pm_stack));
      if (visible_child == priv->pm_label)
        is_pm_time = TRUE;
      else
        is_pm_time = FALSE;

      if (h == 12)
        h = 0;
      if (is_pm_time)
        h += 12;
    }

  priv->date = g_date_time_new_local (g_date_time_get_year (old_date),
                                      g_date_time_get_month (old_date),
                                      g_date_time_get_day_of_month (old_date),
                                      h, m,
                                      g_date_time_get_second (old_date));
  g_date_time_unref (old_date);

  update_time (panel);
  queue_set_datetime (panel);
}
Exemple #16
0
icaltimetype*
datetime_to_icaltime (GDateTime *dt)
{
  icaltimetype *idt;

  if (!dt)
    return NULL;

  idt = g_new0 (icaltimetype, 1);

  idt->year = g_date_time_get_year (dt);
  idt->month = g_date_time_get_month (dt);
  idt->day = g_date_time_get_day_of_month (dt);
  idt->hour = g_date_time_get_hour (dt);
  idt->minute = g_date_time_get_minute (dt);
  idt->second = g_date_time_get_seconds (dt);
  idt->is_date = (idt->hour == 0 &&
                  idt->minute == 0 &&
                  idt->second == 0);

  return idt;
}
static void
update_time (GcalTimeSelector *selector)
{
  GDateTime *now, *new_time;
  gint hour, minute;

  /* Retrieve current time */
  hour = (gint) gtk_adjustment_get_value (selector->hour_adjustment);
  minute = (gint) gtk_adjustment_get_value (selector->minute_adjustment);

  if (!selector->format_24h)
    {
      hour = hour % 12;

      if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector->period_combo)) == PM)
        {
          g_signal_handlers_block_by_func (selector->period_combo, update_time, selector);

          gtk_combo_box_set_active (GTK_COMBO_BOX (selector->period_combo), hour >= 12);
          hour += 12;

          g_signal_handlers_unblock_by_func (selector->period_combo, update_time, selector);
        }
    }

  now = g_date_time_new_now_local ();
  new_time = g_date_time_new_local (g_date_time_get_year (now),
                                    g_date_time_get_month (now),
                                    g_date_time_get_day_of_month (now),
                                    hour, minute, 0);

  /* Set the new time */
  gcal_time_selector_set_time (selector, new_time);

  g_clear_pointer (&new_time, g_date_time_unref);
  g_clear_pointer (&now, g_date_time_unref);
}
Exemple #18
0
bool CalendarDatePrivate::isTheSameMonth(GDateTime * date1, GDateTime * date2) {
    gint month1 = g_date_time_get_month(date1);
    gint month2 = g_date_time_get_month(date2);
    return (isTheSameYear(date1, date2) && (month1 == month2));
}
static void
setup_datetime_dialog (CcDateTimePanel *self)
{
  CcDateTimePanelPrivate *priv = self->priv;
  GtkAdjustment *adjustment;
  GdkScreen *screen;
  GtkCssProvider *provider;
  GtkWidget *dialog;
  guint num_days;

  setup_am_pm_button (self);

  /* Big time buttons */
  provider = gtk_css_provider_new ();
  gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (provider),
                                   ".gnome-control-center-datetime-setup-time>spinbutton,\n"
                                   ".gnome-control-center-datetime-setup-time>label {\n"
                                   "    font-size: 250%;\n"
                                   "}\n"
                                   ".gnome-control-center-datetime-setup-time>spinbutton>entry {\n"
                                   "    padding: 8px 13px;\n"
                                   "}", -1, NULL);
  screen = gdk_screen_get_default ();
  gtk_style_context_add_provider_for_screen (screen,
                                             GTK_STYLE_PROVIDER (provider),
                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  g_object_unref (provider);

  dialog = W ("datetime-dialog");
  g_signal_connect (dialog, "delete-event",
                    G_CALLBACK (gtk_widget_hide_on_delete), NULL);

  /* Force the direction for the time, so that the time
   * is presented correctly for RTL languages */
  gtk_widget_set_direction (W ("time_grid"), GTK_TEXT_DIR_LTR);

  /* Month */
  gtk_combo_box_set_active (GTK_COMBO_BOX (W ("month-combobox")),
                            g_date_time_get_month (priv->date) - 1);
  g_signal_connect (G_OBJECT (W("month-combobox")), "changed",
                    G_CALLBACK (month_year_changed), self);

  /* Day */
  num_days = g_date_get_days_in_month (g_date_time_get_month (priv->date),
                                       g_date_time_get_year (priv->date));
  adjustment = (GtkAdjustment*) gtk_adjustment_new (g_date_time_get_day_of_month (priv->date), 1,
                                                    num_days + 1, 1, 10, 1);
  gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (W ("day-spinbutton")),
                                  adjustment);
  g_signal_connect (G_OBJECT (W ("day-spinbutton")), "value-changed",
                    G_CALLBACK (day_changed), self);

  /* Year */
  adjustment = (GtkAdjustment*) gtk_adjustment_new (g_date_time_get_year (priv->date),
                                                    1, G_MAXDOUBLE, 1,
                                                    10, 1);
  gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (W ("year-spinbutton")),
                                  adjustment);
  g_signal_connect (G_OBJECT (W ("year-spinbutton")), "value-changed",
                    G_CALLBACK (month_year_changed), self);

  /* Hours and minutes */
  g_signal_connect (W ("h_spinbutton"), "output",
                    G_CALLBACK (format_hours_combobox), self);
  g_signal_connect (W ("m_spinbutton"), "output",
                    G_CALLBACK (format_minutes_combobox), self);

  gtk_spin_button_set_increments (GTK_SPIN_BUTTON (W ("h_spinbutton")), 1, 0);
  gtk_spin_button_set_increments (GTK_SPIN_BUTTON (W ("m_spinbutton")), 1, 0);

  gtk_spin_button_set_range (GTK_SPIN_BUTTON (W ("h_spinbutton")), 0, 23);
  gtk_spin_button_set_range (GTK_SPIN_BUTTON (W ("m_spinbutton")), 0, 59);

  g_signal_connect_swapped (W ("h_spinbutton"), "value-changed",
                            G_CALLBACK (change_time), self);
  g_signal_connect_swapped (W ("m_spinbutton"), "value-changed",
                            G_CALLBACK (change_time), self);
}
Exemple #20
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;
}
Exemple #21
0
gint
gst_date_time_get_month (const GstDateTime * datetime)
{
  return g_date_time_get_month (datetime->datetime);
}
static gint
g_calendar_gregorian_real_get_month (GCalendar *calendar, /* IN */
                                     GDateTime *datetime) /* IN */
{
  return g_date_time_get_month (datetime);
}
static void
set_time (GstRTSPTime * time, GstRTSPTime2 * time2, GstRTSPRangeUnit unit,
    GstClockTime clock_time)
{
  memset (time, 0, sizeof (GstRTSPTime));
  memset (time2, 0, sizeof (GstRTSPTime2));

  if (clock_time == GST_CLOCK_TIME_NONE) {
    time->type = GST_RTSP_TIME_END;
    return;
  }

  switch (unit) {
    case GST_RTSP_RANGE_SMPTE:
    case GST_RTSP_RANGE_SMPTE_30_DROP:
    {
      time->seconds = (guint64) (clock_time / GST_SECOND);
      time2->frames = 30003 * (clock_time % GST_SECOND) /
          (gdouble) (1001 * GST_SECOND);
      time->type = GST_RTSP_TIME_FRAMES;
      g_assert (time2->frames < 30);
      break;
    }
    case GST_RTSP_RANGE_SMPTE_25:
    {
      time->seconds = (guint64) (clock_time / GST_SECOND);
      time2->frames = (25 * (clock_time % GST_SECOND)) / (gdouble) GST_SECOND;
      time->type = GST_RTSP_TIME_FRAMES;
      g_assert (time2->frames < 25);
      break;
    }
    case GST_RTSP_RANGE_NPT:
    {
      time->seconds = (gdouble) clock_time / (gdouble) GST_SECOND;
      time->type = GST_RTSP_TIME_SECONDS;
      break;
    }
    case GST_RTSP_RANGE_CLOCK:
    {
      GDateTime *bt, *datetime;
      GstClockTime subsecond = clock_time % GST_SECOND;

      bt = g_date_time_new_utc (1900, 1, 1, 0, 0, 0.0);
      datetime = g_date_time_add_seconds (bt, clock_time / GST_SECOND);

      time2->year = g_date_time_get_year (datetime);
      time2->month = g_date_time_get_month (datetime);
      time2->day = g_date_time_get_day_of_month (datetime);

      time->seconds = g_date_time_get_hour (datetime) * 60 * 60;
      time->seconds += g_date_time_get_minute (datetime) * 60;
      time->seconds += g_date_time_get_seconds (datetime);
      time->seconds += (gdouble) subsecond / (gdouble) GST_SECOND;
      time->type = GST_RTSP_TIME_UTC;

      g_date_time_unref (bt);
      g_date_time_unref (datetime);
      break;
    }
  }

  if (time->seconds < 0.000000001)
    time->seconds = 0;
  if (time2->frames < 0.000000001)
    time2->frames = 0;
}
static void
cc_date_time_panel_init (CcDateTimePanel *self)
{
  CcDateTimePanelPrivate *priv;
  gchar *objects[] = { "datetime-panel", "region-liststore", "city-liststore",
      "month-liststore", "city-modelfilter", "city-modelsort", NULL };
  char *buttons[] = { "hour_up_button", "hour_down_button", "min_up_button",
          "min_down_button", "ampm_up_button", "ampm_down_button" };
  GtkWidget *widget;
  GtkAdjustment *adjustment;
  GError *err = NULL;
  GtkTreeModelFilter *city_modelfilter;
  GtkTreeModelSort *city_modelsort;
  guint i, num_days;
  gboolean using_ntp;
  gboolean can_use_ntp;
  int ret;
  DateEndianess endianess;
  GError *error;

  priv = self->priv = DATE_TIME_PANEL_PRIVATE (self);

  priv->cancellable = g_cancellable_new ();
  error = NULL;
  priv->dtm = date_time_mechanism_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                          G_DBUS_PROXY_FLAGS_NONE,
                                                          "org.gnome.SettingsDaemon.DateTimeMechanism",
                                                          "/",
                                                          priv->cancellable,
                                                          &error);
  if (priv->dtm == NULL) {
        g_warning ("could not get proxy for DateTimeMechanism: %s", error->message);
        g_error_free (error);
  }

  priv->builder = gtk_builder_new ();

  ret = gtk_builder_add_objects_from_file (priv->builder, DATADIR"/datetime.ui",
                                           objects, &err);

  if (ret == 0)
    {
      g_warning ("Could not load ui: %s", err ? err->message : "No reason");
      if (err)
        g_error_free (err);
      return;
    }

  /* set up network time button */
  error = NULL;
  using_ntp = can_use_ntp = FALSE;
  if (!date_time_mechanism_call_get_using_ntp_sync (priv->dtm,
                                                    &can_use_ntp,
                                                    &using_ntp,
                                                    priv->cancellable,
                                                    &error))
    {
      g_warning ("Failed to get using ntp: %s", error->message);
      g_error_free (error);
    }

  gtk_switch_set_active (GTK_SWITCH (W("network_time_switch")), using_ntp);
  update_widget_state_for_ntp (self, using_ntp);
  g_signal_connect (W("network_time_switch"), "notify::active",
                    G_CALLBACK (change_ntp), self);

  /* set up time editing widgets */
  for (i = 0; i < G_N_ELEMENTS (buttons); i++)
    {
      g_signal_connect (W(buttons[i]), "clicked",
                        G_CALLBACK (change_time), self);
    }

  /* set up date editing widgets */
  priv->date = g_date_time_new_now_local ();
  endianess = date_endian_get_default (FALSE);
  reorder_date_widget (endianess, priv);

  /* Force the direction for the time, so that the time
   * is presented correctly for RTL languages */
  gtk_widget_set_direction (W("table2"), GTK_TEXT_DIR_LTR);

  gtk_combo_box_set_active (GTK_COMBO_BOX (W ("month-combobox")),
                            g_date_time_get_month (priv->date) - 1);
  g_signal_connect (G_OBJECT (W("month-combobox")), "changed",
                    G_CALLBACK (month_year_changed), self);

  num_days = g_date_get_days_in_month (g_date_time_get_month (priv->date),
                                       g_date_time_get_year (priv->date));
  adjustment = (GtkAdjustment*) gtk_adjustment_new (g_date_time_get_day_of_month (priv->date), 1,
                                                    num_days + 1, 1, 10, 1);
  gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (W ("day-spinbutton")),
                                  adjustment);
  g_signal_connect (G_OBJECT (W("day-spinbutton")), "value-changed",
                    G_CALLBACK (day_changed), self);

  adjustment = (GtkAdjustment*) gtk_adjustment_new (g_date_time_get_year (priv->date),
                                                    G_MINDOUBLE, G_MAXDOUBLE, 1,
                                                    10, 1);
  gtk_spin_button_set_adjustment (GTK_SPIN_BUTTON (W ("year-spinbutton")),
                                  adjustment);
  g_signal_connect (G_OBJECT (W("year-spinbutton")), "value-changed",
                    G_CALLBACK (month_year_changed), self);

  /* set up timezone map */
  priv->map = widget = (GtkWidget *) cc_timezone_map_new ();
  gtk_widget_show (widget);

  gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (priv->builder,
                                                            "aspectmap")),
                     widget);

  gtk_container_add (GTK_CONTAINER (self),
                     GTK_WIDGET (gtk_builder_get_object (priv->builder,
                                                         "datetime-panel")));


  /* setup the time itself */
  priv->clock_tracker = g_object_new (GNOME_TYPE_WALL_CLOCK, NULL);
  g_signal_connect (priv->clock_tracker, "notify::clock", G_CALLBACK (on_clock_changed), self);

  priv->settings = g_settings_new (CLOCK_SCHEMA);
  clock_settings_changed_cb (priv->settings, CLOCK_FORMAT_KEY, self);
  g_signal_connect (priv->settings, "changed::" CLOCK_FORMAT_KEY,
                    G_CALLBACK (clock_settings_changed_cb), self);

  g_signal_connect (W("24h_button"), "notify::active",
                    G_CALLBACK (change_clock_settings), self);

  update_time (self);

  priv->locations = (GtkTreeModel*) gtk_builder_get_object (priv->builder,
                                                            "region-liststore");

  load_regions_model (GTK_LIST_STORE (priv->locations),
                      GTK_LIST_STORE (gtk_builder_get_object (priv->builder,
                                                              "city-liststore")));

  city_modelfilter = GTK_TREE_MODEL_FILTER (gtk_builder_get_object (priv->builder, "city-modelfilter"));

  widget = (GtkWidget*) gtk_builder_get_object (priv->builder,
                                                "region_combobox");
  city_modelsort = GTK_TREE_MODEL_SORT (gtk_builder_get_object (priv->builder, "city-modelsort"));
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (city_modelsort), CITY_COL_CITY_TRANSLATED,
                                        GTK_SORT_ASCENDING);

  gtk_tree_model_filter_set_visible_func (city_modelfilter,
                                          (GtkTreeModelFilterVisibleFunc) city_model_filter_func,
                                          widget,
                                          NULL);

  /* After the initial setup, so we can be sure that
   * the model is filled up */
  date_time_mechanism_call_get_timezone (priv->dtm,
                                         priv->cancellable,
                                         get_timezone_cb,
                                         self);

  /* add the lock button */
  priv->permission = polkit_permission_new_sync ("org.gnome.settingsdaemon.datetimemechanism.configure", NULL, NULL, NULL);
  if (priv->permission == NULL)
    {
      g_warning ("Your system does not have the '%s' PolicyKit files installed. Please check your installation",
                 "org.gnome.settingsdaemon.datetimemechanism.configure");
      return;
    }

  g_signal_connect (priv->permission, "notify",
                    G_CALLBACK (on_permission_changed), self);
  on_permission_changed (priv->permission, NULL, self);
}
/**
 * vik_datetime_edit_dialog:
 * @parent:         The parent window
 * @title:          The title to use for the dialog
 * @initial_time:   The inital date/time to be shown
 * @tz:             The #GTimeZone this dialog will operate in
 *
 * Returns: A time selected by the user via this dialog
 *          Even though a time of zero is notionally valid - consider it unlikely to be actually wanted!
 *          Thus if the time is zero then the dialog was cancelled or somehow an invalid date was encountered.
 */
time_t vik_datetime_edit_dialog ( GtkWindow *parent, const gchar *title, time_t initial_time, GTimeZone *tz )
{
	g_return_val_if_fail ( tz, 0 );

	GtkWidget *dialog = gtk_dialog_new_with_buttons ( title,
	                                                  parent,
	                                                  GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
	                                                  GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
	                                                  GTK_STOCK_OK,     GTK_RESPONSE_ACCEPT,
	                                                  NULL );

	gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
	GtkWidget *response_w = NULL;
#if GTK_CHECK_VERSION (2, 20, 0)
	response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
#endif

	GtkWidget *label;
	GtkWidget *cal = gtk_calendar_new ();

	// Set according to the given date/time + timezone for display
	GDateTime *gdt_in = g_date_time_new_from_unix_utc ( (gint64)initial_time );
	GDateTime *gdt_tz = g_date_time_to_timezone ( gdt_in, tz );
	g_date_time_unref ( gdt_in );

	gtk_calendar_select_month ( GTK_CALENDAR(cal), g_date_time_get_month(gdt_tz)-1, g_date_time_get_year (gdt_tz) );
	gtk_calendar_select_day ( GTK_CALENDAR(cal), g_date_time_get_day_of_month(gdt_tz) );

	GtkWidget *hbox_time = gtk_hbox_new ( FALSE, 1 );

	label = gtk_label_new ( g_date_time_get_timezone_abbreviation(gdt_tz) );
	gtk_box_pack_start ( GTK_BOX(hbox_time), label, FALSE, FALSE, 5 );

	GtkWidget *sb_hours = gtk_spin_button_new_with_range ( 0.0, 23.0, 1.0 );
	gtk_box_pack_start ( GTK_BOX(hbox_time), sb_hours, FALSE, FALSE, 0 );
	gtk_spin_button_set_value ( GTK_SPIN_BUTTON(sb_hours), g_date_time_get_hour(gdt_tz) );
	g_signal_connect ( sb_hours, "output", G_CALLBACK(on_output), NULL );

	label = gtk_label_new ( ":" );
	gtk_box_pack_start ( GTK_BOX(hbox_time), label, FALSE, FALSE, 0 );

	GtkWidget *sb_minutes = gtk_spin_button_new_with_range ( 0.0, 59.0, 1.0 );
	gtk_box_pack_start ( GTK_BOX(hbox_time), sb_minutes, FALSE, FALSE, 0);
	gtk_spin_button_set_value ( GTK_SPIN_BUTTON(sb_minutes), g_date_time_get_minute(gdt_tz) );
	g_signal_connect ( sb_minutes, "output", G_CALLBACK(on_output), NULL );

	label = gtk_label_new ( ":" );
	gtk_box_pack_start(GTK_BOX(hbox_time), label, FALSE, FALSE, 0);

	GtkWidget *sb_seconds = gtk_spin_button_new_with_range ( 0.0, 59.0, 1.0 );
	gtk_box_pack_start ( GTK_BOX(hbox_time), sb_seconds, FALSE, FALSE, 0 );
	gtk_spin_button_set_value ( GTK_SPIN_BUTTON(sb_seconds), g_date_time_get_second(gdt_tz) );
	g_signal_connect ( sb_seconds, "output", G_CALLBACK(on_output), NULL );

	gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), cal, FALSE, FALSE, 0 );
	gtk_box_pack_start ( GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), hbox_time, FALSE, FALSE, 5 );

	if ( response_w )
		gtk_widget_grab_focus ( response_w );

	g_date_time_unref ( gdt_tz );

	gtk_widget_show_all ( dialog );
	if ( gtk_dialog_run ( GTK_DIALOG(dialog) ) != GTK_RESPONSE_ACCEPT ) {
		gtk_widget_destroy ( dialog );
		return 0;
	}

	// Read values
	guint year = 0;
	guint month = 0;
	guint day = 0;
	guint hours = 0;
	guint minutes = 0;
	guint seconds = 0;

	gtk_calendar_get_date ( GTK_CALENDAR(cal), &year, &month, &day );
	hours = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sb_hours) );
	minutes = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sb_minutes) );
	seconds = gtk_spin_button_get_value_as_int ( GTK_SPIN_BUTTON(sb_seconds) );

	gtk_widget_destroy(dialog);

	time_t ans = initial_time;
	GDateTime *gdt_ans = g_date_time_new ( tz, year, month+1, day, hours, minutes, (gdouble)seconds );
	if ( gdt_ans ) {
		ans = g_date_time_to_unix ( gdt_ans );
		g_date_time_unref ( gdt_ans );
	}

	return ans;
}