Example #1
0
static GDateTime*
gnc_g_date_time_new_local (gint year, gint month, gint day, gint hour, gint minute, gdouble seconds)
{
#ifndef G_OS_WIN32
    return g_date_time_new_local (year, month, day, hour, minute, seconds);
#else
    GTimeZone *tz = gnc_g_time_zone_new_local();
    GDateTime *gdt = g_date_time_new (tz, year, month, day,
				      hour, minute, seconds);
    if (!gdt)
	return gdt;
    tz = gnc_g_time_zone_adjust_for_dst (tz, gdt);
    g_date_time_unref (gdt);
/* g_date_time_new truncates nanoseconds to microseconds. Sometimes in
 * converting (particularly when parsing from a string) the
 * nanoseconds will have lost 1/2 a femtosecond or so. Adding 1/2 a
 * nano second ensures that the truncation doesn't lose a micorsecond
 * in translation.
 */
    seconds += 5e-10;
    gdt =  g_date_time_new (tz, year, month, day, hour, minute, seconds);
    g_time_zone_unref (tz);
    return gdt;
#endif
}
gint
main (gint argc,
      gchar *argv[])
{

  // if final value remains 0 the test is passed otherwise return a -1: 
  gint retVal = 0;

  GTimeZone *tz = (GTimeZone *)g_time_zone_new_local();  
  //gint64 year, month, day;

  GDateTime *date00 = (GDateTime *)g_date_time_new(tz, 2015, 10, 1, 0, 0, 0);
  GDateTime *date01 = (GDateTime *)g_date_time_new(tz, 2015, 10, 1, 0, 0, 0);
  GDateTime *date02 = (GDateTime *)g_date_time_new(tz, 2015, 10, 1, 0, 0, 0);

  GDateTime *dt_1 =  str_to_gdatetime(tz, "20151001", "%4d%2d%2d");
  if (g_date_time_compare(date00, dt_1) != 0) {
    g_printf("%s was improperly parsed as a GDateTime!", "20151001");
    retVal = -1;
  }
  g_date_time_unref(dt_1);

  GDateTime *dt_2 = str_to_gdatetime(tz, "2015/10/01", "%d/%d/%d");
  if (g_date_time_compare(date01, dt_2) != 0) {
    g_printf("%s was improperly parsed as a GDateTime!", "2015/10/01");
    retVal = -1;
  }
  g_date_time_unref(dt_2);

  GDateTime *dt_3 = str_to_gdatetime(tz, "2015-10-01", "%d-%d-%d");
  if (g_date_time_compare(date02, dt_3) != 0) {
    g_printf("%s was improperly parsed as a GDateTime!", "2015-10-01");
    retVal = -1;
  }
  g_date_time_unref(dt_3);
  
  if (retVal == 0) {
    g_printf("All test strings were properly parsed as GDateTimes in check_str_to_gdatetime.");
  }

  // clean everything up:
  g_date_time_unref(date00);
  g_date_time_unref(date01);
  g_date_time_unref(date02);

  g_time_zone_unref(tz);

  return retVal;

}
Example #3
0
void
print_days_until_birthday (void)
{


  /*Calculate how many days there are between now and my
    birthday. (If you get something out of this glib-cookbook then
    feel free to send me a present after that many days go by. ;) */

  GTimeZone *tz;
  GDateTime *now;
  GDateTime *bday;
  GTimeSpan diff;

  tz = (GTimeZone *)g_time_zone_new_local();  
  now = (GDateTime *)g_date_time_new_now(tz);

  bday = (GDateTime *)g_date_time_new(tz, g_date_time_get_year(now) + 1, 6, 25, 0, 0, 0);

  diff = g_date_time_difference(bday, now);
  
  g_print("There are %ld days until Jamil's birthday.\n",
	  diff / G_TIME_SPAN_DAY);
  
  g_date_time_unref(now);
  g_date_time_unref(bday);
  g_time_zone_unref(tz);

}
Example #4
0
static time_t
date_to_time_t (const xmlChar *str, const char * tzid)
{
    struct tm time = { 0 };
    GTimeZone *tz;
    GDateTime *dt;
    time_t rval;
    char *after;

    after = strptime ((const char*) str, "%Y-%m-%dT%T", &time);
    if (after == NULL) {
	g_warning ("Cannot parse date string \"%s\"", str);
	return 0;
    }

    if (*after == 'Z')
	tzid = "UTC";

    tz = g_time_zone_new (tzid);
    dt = g_date_time_new (tz,
			  time.tm_year + 1900,
			  time.tm_mon + 1,
			  time.tm_mday,
			  time.tm_hour,
			  time.tm_min,
			  time.tm_sec);

    rval = g_date_time_to_unix (dt);

    g_time_zone_unref (tz);
    g_date_time_unref (dt);

    return rval;
}
Example #5
0
static GDateTime *parse_datetime(const char *str)
{
	if(str == NULL) return NULL;

	GDateTime *ret = NULL;
	char **bits = g_strsplit(str, " ", -1);
	int num_bits = g_strv_length(bits);
	if(num_bits < 6) {
		g_debug("num_bits for `%s' isn't valid; format unknown", str);
		goto fail;
	}

	/* #0 is day-of-week in english. that's skipped. */
	int month = parse_month(bits[1]);
	if(month <= 0) goto fail;
	int day = atoi(bits[2]);
	if(day == 0) goto fail;
	int hour, min, sec;
	if(sscanf(bits[3], "%d:%d:%d", &hour, &min, &sec) != 3) goto fail;
	int year = atoi(bits[5]);
	if(year < 0) goto fail;
	GTimeZone *tz = g_time_zone_new(bits[4]);
	if(tz == NULL) goto fail;

	ret = g_date_time_new(tz, year, month, day, hour, min, sec);
	g_time_zone_unref(tz);
	if(ret == NULL) {
		g_debug("couldn't make a new timestamp");
	}

fail:
	g_strfreev(bits);
	return ret;
}
Example #6
0
struct GDateTime *voy_cookie_parse_expires_date(char const *date_string) {
  struct tm tm;
  strptime(date_string, VOY_COOKIE_TIME_FORMAT, &tm);
  struct GDateTime *date_time = g_date_time_new(g_time_zone_new("Z"), tm.tm_year + 1900,
    tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  return date_time;
}
DateTime::DateTime(GTimeZone* gtz, int year, int month, int day, int hour, int minute, double seconds)
{
    g_return_if_fail(gtz!=nullptr);

    auto gdt = g_date_time_new(gtz, year, month, day, hour, minute, seconds);
    reset(gtz, gdt);
    g_date_time_unref(gdt);
}
Example #8
0
void dt_image_add_time_offset(const int imgid, const long int offset)
{
  const dt_image_t *cimg = dt_image_cache_read_get(darktable.image_cache, imgid);
  if (!cimg)
    return;

  // get the datetime_taken and calculate the new time
  gint  year;
  gint  month;
  gint  day;
  gint  hour;
  gint  minute;
  gint  seconds;

  if (sscanf(cimg->exif_datetime_taken, "%d:%d:%d %d:%d:%d",
             (int*)&year, (int*)&month, (int*)&day,
             (int*)&hour,(int*)&minute,(int*)&seconds) != 6)
  {
    fprintf(stderr,"broken exif time in db, '%s', imgid %d\n", cimg->exif_datetime_taken, imgid);
    dt_image_cache_read_release(darktable.image_cache, cimg);
    return;
  }

  GTimeZone *tz = g_time_zone_new_utc();
  GDateTime *datetime_original = g_date_time_new(tz, year, month, day, hour, minute, seconds);
  g_time_zone_unref(tz);
  if(!datetime_original)
  {
    dt_image_cache_read_release(darktable.image_cache, cimg);
    return;
  }

  // let's add our offset
  GDateTime *datetime_new = g_date_time_add_seconds(datetime_original, offset);
  g_date_time_unref(datetime_original);

  if(!datetime_new)
  {
    dt_image_cache_read_release(darktable.image_cache, cimg);
    return;
  }

  gchar *datetime = g_date_time_format(datetime_new, "%Y:%m:%d %H:%M:%S");
  g_date_time_unref(datetime_new);

  // update exif_datetime_taken in img
  if(datetime)
  {
    dt_image_t *img = dt_image_cache_write_get(darktable.image_cache, cimg);
    g_strlcpy(img->exif_datetime_taken, datetime, 20);
    dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_SAFE);
  }

  dt_image_cache_read_release(darktable.image_cache, cimg);
  g_free(datetime);
}
Example #9
0
/* Take a string in the format yyyymmdd, yyyy-mm-dd, or yyyy/mm/dd and
   create a GDateTime.  format must be one of "%4d%2d%2d", "%d/%d/%d",
   or "%d-%d-%d". */
GDateTime *str_to_gdatetime(GTimeZone *tz, gchar *datestr, gchar* format)
{

  gint year;
  gint month;
  gint day;

  sscanf(datestr, format, &year, &month, &day);

  return (GDateTime *)g_date_time_new(tz, year, month, day, 0, 0, 0);

}
Example #10
0
void
print_dates_sorted (void)
{

  /* Choose five dates randomly from a time interval, sort them, and
     send to stdio. */

  GTimeZone *tz;
  GPtrArray *dates;
  GDateTime *dt;
  gchar *str;
  gint i;

  tz = (GTimeZone *)g_time_zone_new_local();
  
  dates = g_ptr_array_new();
  g_ptr_array_set_free_func(dates, (GDestroyNotify)g_date_time_unref);
  
  /*
   * Insert some random dates into the array.
   */
  for (i = 0; i < 5; i++) {
    dt = (GDateTime *)g_date_time_new(tz,
				      g_random_int_range(1900, 2020),
				      g_random_int_range(1, 12),
				      g_random_int_range(1, 28),
				      0, 0, 0);
    g_ptr_array_add(dates, dt);
  }

	/*
	 * Sort dates.  Remember that GPtrArray returns a pointer to the
	 * pointer type in sorting methods so they need to be dereferenced.
	 */
	g_ptr_array_sort(dates, gdt_sorter);

	/*
	 * Print out the dates
	 */
	g_print("Dates sorted in order:\n");
	for (i = 0; i < dates->len; i++) {
		dt = g_ptr_array_index(dates, i);
		str = g_date_time_format(dt, "%b %d, %Y");
		g_print("  %s\n", str);
		g_free(str);
	}

	g_ptr_array_unref(dates);
	g_time_zone_unref(tz);

}
Example #11
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;
}
Example #12
0
GDateTime*
icaltime_to_datetime (const icaltimetype  *date)
{
  GDateTime *dt;
  GTimeZone *tz;

  tz = date->zone ? g_time_zone_new (icaltime_get_tzid (*date)) : g_time_zone_new_utc ();
  dt = g_date_time_new (tz,
                        date->year,
                        date->month,
                        date->day,
                        date->is_date ? 0 : date->hour,
                        date->is_date ? 0 : date->minute,
                        date->is_date ? 0 : date->second);

  g_clear_pointer (&tz, g_time_zone_unref);

  return dt;
}
Example #13
0
GstDateTime *
gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
    gint minute, gdouble seconds)
{
  gchar buf[6];
  GTimeZone *tz;
  GDateTime *dt;
  gint tzhour, tzminute;

  tzhour = (gint) ABS (tzoffset);
  tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60);

  g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour,
      tzminute);

  tz = g_time_zone_new (buf);

  dt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
  g_time_zone_unref (tz);
  return gst_date_time_new_from_gdatetime (dt);
}
Example #14
0
/**
 * gst_date_time_new:
 * @tzoffset: Offset from UTC in hours.
 * @year: the gregorian year
 * @month: the gregorian month
 * @day: the day of the gregorian month
 * @hour: the hour of the day
 * @minute: the minute of the hour
 * @seconds: the second of the minute
 *
 * Creates a new #GstDateTime using the date and times in the gregorian calendar
 * in the supplied timezone.
 *
 * @year should be from 1 to 9999, @month should be from 1 to 12, @day from
 * 1 to 31, @hour from 0 to 23, @minutes and @seconds from 0 to 59.
 *
 * Note that @tzoffset is a float and was chosen so for being able to handle
 * some fractional timezones, while it still keeps the readability of
 * representing it in hours for most timezones.
 *
 * If value is -1 then all over value will be ignored. For example
 * if @month == -1, then #GstDateTime will created only for @year. If
 * @day == -1, then #GstDateTime will created for @year and @month and
 * so on.
 *
 * Free-function: gst_date_time_unref
 *
 * Return value: (transfer full) (nullable): the newly created #GstDateTime
 */
GstDateTime *
gst_date_time_new (gfloat tzoffset, gint year, gint month, gint day, gint hour,
    gint minute, gdouble seconds)
{
  GstDateTimeFields fields;
  gchar buf[6];
  GTimeZone *tz;
  GDateTime *dt;
  GstDateTime *datetime;
  gint tzhour, tzminute;

  g_return_val_if_fail (year > 0 && year <= 9999, NULL);
  g_return_val_if_fail ((month > 0 && month <= 12) || month == -1, NULL);
  g_return_val_if_fail ((day > 0 && day <= 31) || day == -1, NULL);
  g_return_val_if_fail ((hour >= 0 && hour < 24) || hour == -1, NULL);
  g_return_val_if_fail ((minute >= 0 && minute < 60) || minute == -1, NULL);
  g_return_val_if_fail ((seconds >= 0 && seconds < 60) || seconds == -1, NULL);
  g_return_val_if_fail (tzoffset >= -12.0 && tzoffset <= 12.0, NULL);
  g_return_val_if_fail ((hour >= 0 && minute >= 0) ||
      (hour == -1 && minute == -1 && seconds == -1 && tzoffset == 0.0), NULL);

  tzhour = (gint) ABS (tzoffset);
  tzminute = (gint) ((ABS (tzoffset) - tzhour) * 60);

  g_snprintf (buf, 6, "%c%02d%02d", tzoffset >= 0 ? '+' : '-', tzhour,
      tzminute);

  tz = g_time_zone_new (buf);

  fields = gst_date_time_check_fields (&year, &month, &day,
      &hour, &minute, &seconds);

  dt = g_date_time_new (tz, year, month, day, hour, minute, seconds);
  g_time_zone_unref (tz);

  datetime = gst_date_time_new_from_g_date_time (dt);
  datetime->fields = fields;

  return datetime;
}
Example #15
0
Timespec
gnc_iso8601_to_timespec_gmt(const char *str)
{
    Timespec time = { 0L, 0L };
    GDateTime *gdt;
    gint hour = 0, minute = 0, day = 0, month = 0, year = 0;
    gchar zone[12];
    gdouble second = 0.0;
    gint fields;

    memset (zone, 0, sizeof (zone));

    if (!str)
	return time;

    fields = sscanf (str, ISO_DATE_FORMAT, &year, &month,
			  &day, &hour, &minute, &second, zone);
    if (fields < 1)
	return time;
    else if (fields > 6 && strlen (zone) > 0) /* Date string included a timezone */
    {
	GTimeZone *tz = g_time_zone_new (zone);
        time64 secs;
	second += 5e-10;
	gdt = g_date_time_new (tz, year, month, day, hour, minute, second);
        secs = g_date_time_to_unix (gdt);
	g_time_zone_unref (tz);
    }
    else /* No zone info, assume UTC */
    {
	second += 5e-10;
	gdt = g_date_time_new_utc (year, month, day, hour, minute, second);
    }

    time.tv_sec = g_date_time_to_unix (gdt);
    time.tv_nsec = g_date_time_get_microsecond (gdt) * 1000;
    g_date_time_unref (gdt);
    return time;
}
/**
 * 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;
}
Example #17
0
int32_t dt_control_gpx_apply_job_run(dt_job_t *job)
{
  dt_control_image_enumerator_t *t1 = (dt_control_image_enumerator_t *)job->param;
  GList *t = t1->index;
  struct dt_gpx_t *gpx = NULL;
  uint32_t cntr = 0;
  const dt_control_gpx_apply_t *d = t1->data;
  const gchar *filename = d->filename;
  const gchar *tz = d->tz;

  /* do we have any selected images */
  if (!t)
    goto bail_out;

  /* try parse the gpx data */
  gpx = dt_gpx_new(filename);
  if (!gpx)
  {
    dt_control_log(_("failed to parse gpx file"));
    goto bail_out;
  }

  GTimeZone *tz_camera = (tz == NULL)?g_time_zone_new_utc():g_time_zone_new(tz);
  if(!tz_camera)
    goto bail_out;
  GTimeZone *tz_utc = g_time_zone_new_utc();

  /* go thru each selected image and lookup location in gpx */
  do
  {
    GTimeVal timestamp;
    GDateTime *exif_time, *utc_time;
    gdouble lon,lat;
    uint32_t imgid = (long int)t->data;

    /* get image */
    const dt_image_t *cimg = dt_image_cache_read_get(darktable.image_cache, imgid);
    if (!cimg)
      continue;

    /* convert exif datetime
       TODO: exiv2 dates should be iso8601 and we are probably doing some ugly
       convertion before inserting into database.
     */
    gint year;
    gint month;
    gint day;
    gint hour;
    gint minute;
    gint  seconds;

    if (sscanf(cimg->exif_datetime_taken, "%d:%d:%d %d:%d:%d",
               (int*)&year, (int*)&month, (int*)&day,
               (int*)&hour,(int*)&minute,(int*)&seconds) != 6)
    {
      fprintf(stderr,"broken exif time in db, '%s'\n", cimg->exif_datetime_taken);
      dt_image_cache_read_release(darktable.image_cache, cimg);
      continue;
    }

    /* release the lock */
    dt_image_cache_read_release(darktable.image_cache, cimg);

    exif_time = g_date_time_new(tz_camera, year, month, day, hour, minute, seconds);
    if(!exif_time)
      continue;
    utc_time = g_date_time_to_timezone(exif_time, tz_utc);
    g_date_time_unref(exif_time);
    if(!utc_time)
      continue;
    gboolean res = g_date_time_to_timeval(utc_time, &timestamp);
    g_date_time_unref(utc_time);
    if(!res)
      continue;

    /* only update image location if time is within gpx tack range */
    if(dt_gpx_get_location(gpx, &timestamp, &lon, &lat))
    {
      dt_image_set_location(imgid, lon, lat);
      cntr++;
    }

  }
  while((t = g_list_next(t)) != NULL);

  dt_control_log(_("applied matched gpx location onto %d image(s)"), cntr);

  g_time_zone_unref(tz_camera);
  g_time_zone_unref(tz_utc);
  dt_gpx_destroy(gpx);
  g_free(d->filename);
  g_free(d->tz);
  g_free(t1->data);
  return 0;

bail_out:
  if (gpx)
    dt_gpx_destroy(gpx);

  g_free(d->filename);
  g_free(d->tz);
  g_free(t1->data);
  return 1;
}
int main() {

  // if final value remains 0 the test is passed otherwise return a -1: 
  gint retVal = 0;

  /************************************************/
  /* Create three time series for use in the tests: */
  /************************************************/

  // use the same time zone for all dates:
  GTimeZone *tz = (GTimeZone *)g_time_zone_new_local();
  
  //first time series:
  GSList* first_ts = NULL, *first_dates = NULL, *first_values = NULL;

  first_dates = g_slist_append(first_dates, g_date_time_new(tz, 2015, 10, 1, 1, 0, 0.0));
  first_dates = g_slist_append(first_dates, g_date_time_new(tz, 2015, 10, 1, 5, 0, 0.0));
  first_dates = g_slist_append(first_dates, g_date_time_new(tz, 2015, 10, 1, 6, 0, 0.0));

  // GSLists can only hold pointers to data so we need to init the
  // gfloats and then store their addresses in the GSList:
  gdouble p1 = 1.0;
  gdouble p2 = 1.0;
  gdouble p3 = 1.0;
  
  first_values = g_slist_append(first_values, &p1);
  first_values = g_slist_append(first_values, &p2);
  first_values = g_slist_append(first_values, &p3);

  first_ts = ts_gdt_double(first_dates, first_values);

  // second time series using different dates:
  GSList* second_ts = NULL, *second_dates = NULL, *second_values = NULL;

  second_dates = g_slist_append(second_dates, g_date_time_new(tz, 2015, 10, 1, 2, 0, 0.0));
  second_dates = g_slist_append(second_dates, g_date_time_new(tz, 2015, 10, 1, 5, 0, 0.0));
  second_dates = g_slist_append(second_dates, g_date_time_new(tz, 2015, 10, 1, 10, 0, 0.0));
  
  // GSLists can only hold pointers to data so we need to init the
  // gfloats and then store their addresses in the GSList:
  gdouble q1 = 1.0;
  gdouble q2 = 1.0;
  gdouble q3 = 1.0;
  
  second_values = g_slist_append(second_values, &q1);
  second_values = g_slist_append(second_values, &q2);
  second_values = g_slist_append(second_values, &q3);

  second_ts = ts_gdt_double(second_dates, second_values);

  // third time series using still different dates:
  GSList* third_ts = NULL, *third_dates = NULL, *third_values = NULL;

  third_dates = g_slist_append(third_dates, g_date_time_new(tz, 2015, 10, 1, 2, 0, 0.0));
  third_dates = g_slist_append(third_dates, g_date_time_new(tz, 2015, 10, 1, 5, 0, 0.0));
  third_dates = g_slist_append(third_dates, g_date_time_new(tz, 2015, 10, 1, 9, 0, 0.0));
  // GSLists can only hold pointers to data so we need to init the
  // gfloats and then store their addresses in the GSList:
  gdouble r1 = 1.0;
  gdouble r2 = 1.0;
  gdouble r3 = 1.0;
  
  third_values = g_slist_append(third_values, &r1);
  third_values = g_slist_append(third_values, &r2);
  third_values = g_slist_append(third_values, &r3);

  third_ts = ts_gdt_double(third_dates, third_values);

  // clean up that timezone that we used to create all the data:
  g_time_zone_unref(tz);
  
  /********************************************/
  /* Now let's look at our three time series: */
  /********************************************/
  g_printf("first ts: \n");
  ts_pretty_print(first_ts);
  g_printf("second ts: \n");
  ts_pretty_print(second_ts);
  g_printf("third ts: \n");
  ts_pretty_print(third_ts);
  
  /*******************************/
  /* Alignment test begins here: */
  /*******************************/

  // Put all the time series to be aligned into a single list:
  GSList *pre_aligned_list = NULL;
  pre_aligned_list = g_slist_append(pre_aligned_list, first_ts);
  pre_aligned_list = g_slist_append(pre_aligned_list, second_ts);
  pre_aligned_list = g_slist_append(pre_aligned_list, third_ts);

  // Alignment will create a new list of the same time series but they
  // all should now have the same domains:
  GSList *aligned_list = NULL, *iter_1 = NULL, *iter_2 = NULL, *iter_3 = NULL;

  // do the alignment:
  aligned_list = ts_align_hourly(pre_aligned_list);

  // now we do the check:
  GSList *al_first = (GSList *)g_slist_nth(aligned_list, 0)->data;
  GSList *al_second = (GSList *)g_slist_nth(aligned_list, 1)->data;
  GSList *al_third = (GSList *)g_slist_nth(aligned_list, 2)->data;

  // the resulting time series should all look like this:
  //date	first	second	third
  //2015-Oct-01	1	nan	nan
  //2015-Oct-02	nan	1	1
  //2015-Oct-03	nan	nan	nan
  //2015-Oct-04	nan	nan	nan
  //2015-Oct-05	1	1	1
  //2015-Oct-06	1	nan	nan
  //2015-Oct-07	nan	nan	nan
  //2015-Oct-08	nan	nan	nan
  //2015-Oct-09	nan	nan	1
  //2015-Oct-10	nan	1	nan

  // print out a table of the time series:
  g_printf("Date:\tfirst:\tsecond:\tthird:\n");
  for (iter_1 = al_first, iter_2 = al_second, iter_3 = al_third;	\
       iter_1 && iter_2 && iter_3;					\
       iter_1 = iter_1->next, iter_2 = iter_2->next, iter_3 = iter_3->next) {
    ts_datum_double *first_datum = iter_1->data;
    ts_datum_double *second_datum = iter_2->data;
    ts_datum_double *third_datum = iter_3->data;
    gchar *dt_str = (gchar *)g_date_time_format(first_datum->thedate, "%F %T");
    g_printf("%s\t%g\t%g\t%g\n",					\
	     dt_str, \
	     first_datum->thevalue,					\
	     second_datum->thevalue,					\
	     third_datum->thevalue);
    g_free(dt_str);
  }

  // clean up the lists that were used to create the pre_aligned time
  // series:
  g_slist_free_full(first_dates, g_date_time_free);
  g_slist_free(first_values);
  g_slist_free_full(second_dates, g_date_time_free);
  g_slist_free(second_values);
  g_slist_free_full(third_dates, g_date_time_free);
  g_slist_free(third_values);

  // clean up the lists that are in the pre_aligned list of time series:
  g_slist_free_full(first_ts, ts_datum_double_free);
  g_slist_free_full(second_ts, ts_datum_double_free);
  g_slist_free_full(third_ts, ts_datum_double_free);
  g_slist_free(pre_aligned_list);

  // clean up the lists that in the aligned list of time series:
  g_slist_free_full(al_first, ts_datum_double_free);
  g_slist_free_full(al_second, ts_datum_double_free);
  g_slist_free_full(al_third, ts_datum_double_free);
  g_slist_free(aligned_list);
  
  return retVal;

}
Example #19
0
/*
 * gda_ldap_attr_value_to_g_value:
 * Converts a #BerValue to a new #GValue
 *
 * Returns: a new #GValue, or %NULL on error
 */
GValue *
gda_ldap_attr_value_to_g_value (LdapConnectionData *cdata, GType type, BerValue *bv)
{
	GValue *value = NULL;
	if ((type == G_TYPE_DATE_TIME) ||
	    (type == G_TYPE_DATE)) {
		/* see ftp://ftp.rfc-editor.org/in-notes/rfc4517.txt,
		 * section 3.3.13: Generalized Time
		 */
		GTimeVal tv;
		gboolean conv;
		if (! (conv = g_time_val_from_iso8601 (bv->bv_val,
						       &tv))) {
			/* Add the 'T' char */
			gchar *tmp, *str;
			gint i, len;
			str = bv->bv_val;
			len = strlen (str);
			if (len > 8) {
				tmp = g_new (gchar, len + 2);
				for (i = 0; i < 8; i++)
					tmp[i] = str[i];
				tmp [8] = 'T';
				for (i = 9; str[i]; i++)
					tmp[i] = str[i-1];
				tmp[i] = 0;
				conv = g_time_val_from_iso8601 (tmp, &tv);
				g_free (tmp);
			}
		}
		if (conv) {
			struct tm *ptm;
#ifdef HAVE_LOCALTIME_R
			struct tm tmpstm;
			ptm = localtime_r (&(tv.tv_sec), &tmpstm);
#elif HAVE_LOCALTIME_S
			struct tm tmpstm;
			if (localtime_s (&tmpstm, &(tv.tv_sec)) == 0)
				ptm = &tmpstm;
			else
				ptm = NULL;
#else
			ptm = localtime (&(tv.tv_sec));
#endif

			if (!ptm)
				return NULL;

			if (g_type_is_a (type, G_TYPE_DATE_TIME)) {
				GTimeZone *tz = g_time_zone_new ("Z"); // UTC
				GDateTime *ts = g_date_time_new (tz,
																				 ptm->tm_year + 1900,
																				 ptm->tm_mon + 1,
																				 ptm->tm_mday,
																				 ptm->tm_hour,
																				 ptm->tm_min,
																				 ptm->tm_sec);
				value = gda_value_new (G_TYPE_DATE_TIME);
				g_value_set_boxed (value, ts);
				g_date_time_unref (ts);
			}
			else {
				GDate *date;
				date = g_date_new ();
				g_date_set_time_val (date, &tv);
				value = gda_value_new (type);
				g_value_take_boxed (value, date);
			}
		}
	}
	else if (type == GDA_TYPE_BINARY) {
		guchar *data;
		data = g_new (guchar, bv->bv_len);
		memcpy (data, bv->bv_val, sizeof (guchar) * bv->bv_len);
		value = gda_value_new_binary (data, bv->bv_len);
	}
	else
		value = gda_value_new_from_string (bv->bv_val, type);

	return value;
}
static gboolean
get_result_metas_cb (GcalShellSearchProvider  *search_provider,
                     GDBusMethodInvocation    *invocation,
                     gchar                   **results,
                     GcalShellSearchProvider2 *skel)
{
  GcalShellSearchProviderPrivate *priv;
  gint i;
  gchar *uuid, *desc;
  const gchar* location;

  g_autoptr(GTimeZone) tz;
  g_autoptr (GDateTime) datetime;
  g_autoptr (GDateTime) local_datetime;
  ECalComponentDateTime dtstart;
  gchar *start_date;

  ECalComponentText summary;
  GdkRGBA color;
  GVariantBuilder abuilder, builder;
  GVariant *icon_variant;
  GcalEventData *data;
  GdkPixbuf *gicon;

  priv = search_provider->priv;

  g_variant_builder_init (&abuilder, G_VARIANT_TYPE ("aa{sv}"));
  for (i = 0; i < g_strv_length (results); i++)
    {
      uuid = results[i];
      data = g_hash_table_lookup (priv->events, uuid);

      g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
      g_variant_builder_add (&builder, "{sv}", "id", g_variant_new_string (uuid));

      e_cal_component_get_summary (data->event_component, &summary);
      g_variant_builder_add (&builder, "{sv}", "name", g_variant_new_string (summary.value));

      get_color_name_from_source (data->source, &color);
      gicon = get_circle_pixbuf_from_color (&color, 128);
      icon_variant = g_icon_serialize (G_ICON (gicon));
      g_variant_builder_add (&builder, "{sv}", "icon", icon_variant);
      g_object_unref (gicon);
      g_variant_unref (icon_variant);

      e_cal_component_get_dtstart (data->event_component, &dtstart);

      if (dtstart.tzid != NULL)
        tz = g_time_zone_new (dtstart.tzid);
      else if (dtstart.value->zone != NULL)
        tz = g_time_zone_new (icaltimezone_get_tzid ((icaltimezone*) dtstart.value->zone));
      else
        tz = g_time_zone_new_local ();

      datetime = g_date_time_new (tz,
                                  dtstart.value->year, dtstart.value->month, dtstart.value->day,
                                  dtstart.value->hour, dtstart.value->minute, dtstart.value->second);
      local_datetime = g_date_time_to_local (datetime);

      /* FIXME: respect 24h time format */
      start_date = g_date_time_format (local_datetime,
                                       (dtstart.value->is_date == 1) ? "%x" : "%c");
      e_cal_component_free_datetime (&dtstart);

      e_cal_component_get_location (data->event_component, &location);
      if (location != NULL)
        desc = g_strconcat (start_date, ". ", location, NULL);
      else
        desc = g_strdup (start_date);

      g_variant_builder_add (&builder, "{sv}", "description", g_variant_new_string (desc));
      g_free (start_date);
      g_free (desc);

      g_variant_builder_add_value (&abuilder, g_variant_builder_end (&builder));
    }
  g_dbus_method_invocation_return_value (invocation, g_variant_new ("(aa{sv})", &abuilder));

  return TRUE;
}