static void
change_time (GtkButton       *button,
             CcDateTimePanel *panel)
{
  CcDateTimePanelPrivate *priv = panel->priv;
  const gchar *widget_name;
  gint direction;
  GDateTime *old_date;

  old_date = priv->date;

  widget_name = gtk_buildable_get_name (GTK_BUILDABLE (button));

  if (strstr (widget_name, "up"))
    direction = 1;
  else
    direction = -1;

  if (widget_name[0] == 'h')
    {
      priv->date = g_date_time_add_hours (old_date, direction);
    }
  else if (widget_name[0] == 'm')
    {
      priv->date = g_date_time_add_minutes (old_date, direction);
    }
  else
    {
      int hour;
      hour = g_date_time_get_hour (old_date);
      if (hour >= 12)
        priv->date = g_date_time_add_hours (old_date, -12);
      else
        priv->date = g_date_time_add_hours (old_date, 12);
    }
  g_date_time_unref (old_date);

  update_time (panel);
  queue_set_datetime (panel);
}
/**
 * gst_video_time_code_to_date_time:
 * @tc: #GstVideoTimeCode to convert
 *
 * The @tc.config->latest_daily_jam is required to be non-NULL.
 *
 * Returns: the #GDateTime representation of @tc.
 *
 * Since: 1.10
 */
GDateTime *
gst_video_time_code_to_date_time (const GstVideoTimeCode * tc)
{
  GDateTime *ret;
  GDateTime *ret2;
  gdouble add_us;

  g_return_val_if_fail (gst_video_time_code_is_valid (tc), NULL);
  g_return_val_if_fail (tc->config.latest_daily_jam != NULL, NULL);

  ret = g_date_time_ref (tc->config.latest_daily_jam);

  if (ret == NULL) {
    gchar *tc_str = gst_video_time_code_to_string (tc);
    GST_WARNING
        ("Asked to convert time code %s to GDateTime, but its latest daily jam is NULL",
        tc_str);
    g_free (tc_str);
    return NULL;
  }

  if (tc->config.fps_n == 0 && tc->config.fps_d == 1) {
    gchar *tc_str = gst_video_time_code_to_string (tc);
    GST_WARNING
        ("Asked to convert time code %s to GDateTime, but its framerate is unknown",
        tc_str);
    g_free (tc_str);
    return NULL;
  }

  gst_util_fraction_to_double (tc->frames * tc->config.fps_d, tc->config.fps_n,
      &add_us);
  if ((tc->config.flags & GST_VIDEO_TIME_CODE_FLAGS_INTERLACED)
      && tc->field_count == 1) {
    gdouble sub_us;

    gst_util_fraction_to_double (tc->config.fps_d, 2 * tc->config.fps_n,
        &sub_us);
    add_us -= sub_us;
  }

  ret2 = g_date_time_add_seconds (ret, add_us + tc->seconds);
  g_date_time_unref (ret);
  ret = g_date_time_add_minutes (ret2, tc->minutes);
  g_date_time_unref (ret2);
  ret2 = g_date_time_add_hours (ret, tc->hours);
  g_date_time_unref (ret);

  return ret2;
}
void TestShouldRenew() {
  GDateTime *now = g_date_time_new_now_utc();
  GDateTime *one_hour_ago = g_date_time_add_hours(now, -1);
  GDateTime *over_1w_ago = g_date_time_add_weeks(one_hour_ago, -1);
  CachePolicy *policy = CachePolicyNew();
  CacheEntry *entry = CacheEntryNew();

  CachePolicySetValue(policy, "renew", "1w", NULL);

  entry->last_verified = one_hour_ago;
  g_assert(!CachePolicyShouldRenew(policy, entry, now));

  entry->last_verified = over_1w_ago;
  g_assert(CachePolicyShouldRenew(policy, entry, now));

  entry->last_verified = NULL;
  CacheEntryUnref(entry);
  CachePolicyUnref(policy);
  g_date_time_unref(now);
  g_date_time_unref(one_hour_ago);
  g_date_time_unref(over_1w_ago);
}
void TestCheckEntry() {
  GDateTime *now = g_date_time_new_now_utc();
  GDateTime *one_hour_ago = g_date_time_add_hours(now, -1);
  GDateTime *over_1w_ago = g_date_time_add_weeks(one_hour_ago, -1);
  GDateTime *over_2w_ago = g_date_time_add_weeks(one_hour_ago, -2);
  GDateTime *over_3w_ago = g_date_time_add_weeks(one_hour_ago, -3);

  CachePolicy *policy = CachePolicyNew();
  CacheEntry *entry = CacheEntryNew();
  GError *error = NULL;

  // Any entry works with an empty policy.
  entry->tries = 4;
  entry->last_verified = over_3w_ago;
  entry->last_used = over_3w_ago;
  g_assert(CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_no_error(error);

  // Try against a policy with all fields set from now on.
  CachePolicySetValue(policy, "tries", "3", NULL);
  CachePolicySetValue(policy, "refresh", "1w", NULL);
  CachePolicySetValue(policy, "renew", "2w", NULL);
  CachePolicySetValue(policy, "expire", "3w", NULL);

  entry->tries = 3;
  entry->last_verified = over_2w_ago;
  entry->last_used = one_hour_ago;
  g_assert(CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_no_error(error);

  entry->last_verified = over_2w_ago;
  entry->last_used = over_1w_ago;
  g_assert(!CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_error(error, CACHE_POLICY_ERROR, CACHE_POLICY_ENTRY_EXPIRED_ERROR);
  g_error_free(error);
  error = NULL;

  entry->last_verified = over_3w_ago;
  entry->last_used = one_hour_ago;
  g_assert(!CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_error(error, CACHE_POLICY_ERROR, CACHE_POLICY_ENTRY_EXPIRED_ERROR);
  g_error_free(error);
  error = NULL;

  entry->tries = 4;
  entry->last_verified = one_hour_ago;
  entry->last_used = one_hour_ago;
  g_assert(!CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_error(error, CACHE_POLICY_ERROR,
                 CACHE_POLICY_MAX_TRIES_EXCEEDED_ERROR);
  g_error_free(error);
  error = NULL;

  entry->last_verified = NULL;
  entry->last_used = NULL;
  CacheEntryUnref(entry);
  CachePolicyUnref(policy);
  g_date_time_unref(now);
  g_date_time_unref(one_hour_ago);
  g_date_time_unref(over_1w_ago);
  g_date_time_unref(over_2w_ago);
  g_date_time_unref(over_3w_ago);
}