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); }
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; }
static gboolean is_new_record (OfficeRunner *run, int *new_pos) { ORecord *o; guint i; gboolean new_record; GList *l; #if 0 GDateTime *dt; GTimeZone *tz; char *date; #endif new_record = FALSE; #if 0 /* Unused */ tz = g_time_zone_new_local (); dt = g_date_time_new_now (tz); date = g_date_time_format (dt, "%c"); g_date_time_unref (dt); g_time_zone_unref (tz); g_free (date); #endif o = new_orecord (run->elapsed); run->records = g_list_insert_sorted (run->records, o, (GCompareFunc) record_compare_func); g_debug ("Elapsed: %lf", o->time); for (l = run->records, i = GOLD; l != NULL; l = l->next, i++) g_debug ("\t%d = %lf", i, ((ORecord *) l->data)->time); *new_pos = 0; for (l = run->records, i = GOLD; i <= BRONZE; l = l->next, i++) { ORecord *o = l->data; if (run->elapsed == o->time && i <= BRONZE) { new_record = TRUE; *new_pos = i; break; } } if (new_record == FALSE) return FALSE; run->dirty_records = TRUE; return TRUE; }
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; }
/** * tpwin_sync_time_to_tp: * */ static void tpwin_sync_time_to_tp ( GtkWidget* widget, GdkEventButton *event, VikTrwLayerTpwin *tpwin ) { if ( !tpwin->cur_tp || tpwin->sync_to_tp_block ) return; if ( event->button == 3 ) { // On right click and when a time is available, allow a method to copy the displayed time as text if ( !gtk_button_get_image ( GTK_BUTTON(widget) ) ) { vu_copy_label_menu ( widget, event->button ); } return; } else if ( event->button == 2 ) { return; } if ( !tpwin->cur_tp || tpwin->sync_to_tp_block ) return; if ( tpwin->cur_tp->has_timestamp ) last_edit_time = tpwin->cur_tp->timestamp; else if ( last_edit_time == 0 ) time ( &last_edit_time ); GTimeZone *gtz = g_time_zone_new_local (); time_t mytime = vik_datetime_edit_dialog ( GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(&tpwin->parent))), _("Date/Time Edit"), last_edit_time, gtz ); g_time_zone_unref ( gtz ); // Was the dialog cancelled? if ( mytime == 0 ) return; // Otherwise use the new value tpwin->cur_tp->timestamp = mytime; tpwin->cur_tp->has_timestamp = TRUE; // TODO: consider warning about unsorted times? // Clear the previous 'Add' image as now a time is set if ( gtk_button_get_image ( GTK_BUTTON(tpwin->time) ) ) gtk_button_set_image ( GTK_BUTTON(tpwin->time), NULL ); tpwin_update_times ( tpwin, tpwin->cur_tp ); }
static void update_timestamp (IdoLocationMenuItem * self) { GTimeZone * tz; GDateTime * date_time; tz = g_time_zone_new (self->priv->timezone); if (tz == NULL) tz = g_time_zone_new_local (); date_time = g_date_time_new_now (tz); ido_time_stamp_menu_item_set_date_time (IDO_TIME_STAMP_MENU_ITEM(self), date_time); g_date_time_unref (date_time); g_time_zone_unref (tz); }
static void gnome_wall_clock_finalize (GObject *object) { GnomeWallClock *self = GNOME_WALL_CLOCK (object); if (self->priv->clock_update_id) { g_source_remove (self->priv->clock_update_id); self->priv->clock_update_id = 0; } g_clear_object (&self->priv->tz_monitor); g_clear_object (&self->priv->desktop_settings); g_time_zone_unref (self->priv->timezone); g_free (self->priv->clock_string); G_OBJECT_CLASS (gnome_wall_clock_parent_class)->finalize (object); }
/** * Creates a GDate set to yesterday. * * * @return A GDate with year, month, and day set to yesterday. */ GDate *yesterday(void) { GTimeZone *tz = g_time_zone_new_local(); GDateTime *now = g_date_time_new_now(tz); g_time_zone_unref(tz); gint year, month, day; GDateTime *temp_copy = g_date_time_add_days(now, -1); g_date_time_unref(now); g_date_time_get_ymd(temp_copy, &year, &month, &day); g_date_time_unref(temp_copy); // and now we can create yesterday: GDate *yest = g_date_new_dmy(day, month, year); return yest; }
void convert_str_to_gdatetime(void) { GTimeZone *tz = (GTimeZone *)g_time_zone_new_local(); GDateTime *date00 = str_to_gdatetime(tz, "20151001", "%4d%2d%2d"); GDateTime *date01 = str_to_gdatetime(tz, "2015/10/01", "%d/%d/%d"); GDateTime *date02 = str_to_gdatetime(tz, "2015-10-01", "%d-%d-%d"); g_print("%s", (gchar *)g_date_time_format(date00, "test00: %c.\n")); g_print("%s", (gchar *)g_date_time_format(date01, "test01: %c.\n")); g_print("%s", (gchar *)g_date_time_format(date02, "test02: %c.\n")); g_date_time_unref(date00); g_date_time_unref(date01); g_date_time_unref(date02); g_time_zone_unref(tz); }
void print_current_timezone (void) { /* Print a string representation of the local time zone to stdio. */ GTimeZone *tz; GDateTime *dt; tz = (GTimeZone *)g_time_zone_new_local(); dt = (GDateTime *)g_date_time_new_now(tz); g_print("The current timezone is %s.\n", g_date_time_get_timezone_abbreviation(dt)); g_date_time_unref(dt); g_time_zone_unref(tz); }
static GTimeZone* gnc_g_time_zone_adjust_for_dst (GTimeZone* tz, GDateTime *date) { #ifdef G_OS_WIN32 TIME_ZONE_INFORMATION tzinfo; gint64 dst = GetTimeZoneInformation (&tzinfo); gint bias, hours, minutes; gchar *tzstr; g_return_val_if_fail (date != NULL, NULL); if (dst > 0 && win32_in_dst (date, &tzinfo)) { g_time_zone_unref (tz); bias = tzinfo.Bias + tzinfo.DaylightBias; hours = -bias / 60; // 60 minutes per hour minutes = (bias < 0 ? -bias : bias) % 60; tzstr = g_strdup_printf ("%+03d:%02d", hours, minutes); tz = g_time_zone_new(tzstr); } #endif return tz; }
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); }
void print_todays_date (void) { /* Just figure out what the current date is and print it. */ GTimeZone *tz; GDateTime *dt; gchar *str; tz = (GTimeZone *)g_time_zone_new_local(); dt = (GDateTime *)g_date_time_new_now(tz); str = (gchar *)g_date_time_format(dt, "Today is %c.\n"); g_printf("%s\n", str); /* You gotta unref and free up all those gThingys. */ g_date_time_unref(dt); g_time_zone_unref(tz); g_free(str); }
/** * 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; }
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; }
static void location_changed_cb (CcTimezoneMap *map, TzLocation *location, CcDateTimePanel *self) { CcDateTimePanelPrivate *priv = self->priv; GDateTime *old_date; GTimeZone *timezone; g_debug ("location changed to %s/%s", location->country, location->zone); priv->current_location = location; old_date = priv->date; timezone = g_time_zone_new (location->zone); priv->date = g_date_time_to_timezone (old_date, timezone); g_time_zone_unref (timezone); g_date_time_unref (old_date); update_timezone (self); queue_set_timezone (self); }
void print_yesterdays_date (void) { /* Figure out what yesterday's date is and print it to stdio. */ GTimeZone *tz; GDateTime *dt; GDateTime *dt2; gchar *str; tz = (GTimeZone *)g_time_zone_new_local(); dt = (GDateTime *)g_date_time_new_now(tz); dt2 = (GDateTime *)g_date_time_add_days(dt, -1); str = (gchar *)g_date_time_format(dt2, "Yesterday was %c.\n"); g_print("%s\n", str); g_date_time_unref(dt); g_date_time_unref(dt2); g_time_zone_unref(tz); g_free(str); }
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, ×tamp); 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, ×tamp, &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; }
/** * Function to open and perform a optimization. */ void optimize_open () { GTimeZone *tz; GDateTime *t0, *t; unsigned int i, j; #if DEBUG_OPTIMIZE char *buffer; fprintf (stderr, "optimize_open: start\n"); #endif // Getting initial time #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: getting initial time\n"); #endif tz = g_time_zone_new_utc (); t0 = g_date_time_new_now (tz); // Obtaining and initing the pseudo-random numbers generator seed #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: getting initial seed\n"); #endif if (optimize->seed == DEFAULT_RANDOM_SEED) optimize->seed = input->seed; gsl_rng_set (optimize->rng, optimize->seed); // Replacing the working directory #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: replacing the working directory\n"); #endif g_chdir (input->directory); // Getting results file names optimize->result = input->result; optimize->variables = input->variables; // Obtaining the simulator file optimize->simulator = input->simulator; // Obtaining the evaluator file optimize->evaluator = input->evaluator; // Reading the algorithm optimize->algorithm = input->algorithm; switch (optimize->algorithm) { case ALGORITHM_MONTE_CARLO: optimize_algorithm = optimize_MonteCarlo; break; case ALGORITHM_SWEEP: optimize_algorithm = optimize_sweep; break; case ALGORITHM_ORTHOGONAL: optimize_algorithm = optimize_orthogonal; break; default: optimize_algorithm = optimize_genetic; optimize->mutation_ratio = input->mutation_ratio; optimize->reproduction_ratio = input->reproduction_ratio; optimize->adaptation_ratio = input->adaptation_ratio; } optimize->nvariables = input->nvariables; optimize->nsimulations = input->nsimulations; optimize->niterations = input->niterations; optimize->nbest = input->nbest; optimize->tolerance = input->tolerance; optimize->nsteps = input->nsteps; optimize->nestimates = 0; optimize->threshold = input->threshold; optimize->stop = 0; if (input->nsteps) { optimize->relaxation = input->relaxation; switch (input->climbing) { case CLIMBING_METHOD_COORDINATES: optimize->nestimates = 2 * optimize->nvariables; optimize_estimate_climbing = optimize_estimate_climbing_coordinates; break; default: optimize->nestimates = input->nestimates; optimize_estimate_climbing = optimize_estimate_climbing_random; } } #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: nbest=%u\n", optimize->nbest); #endif optimize->simulation_best = (unsigned int *) alloca (optimize->nbest * sizeof (unsigned int)); optimize->error_best = (double *) alloca (optimize->nbest * sizeof (double)); // Reading the experimental data #if DEBUG_OPTIMIZE buffer = g_get_current_dir (); fprintf (stderr, "optimize_open: current directory=%s\n", buffer); g_free (buffer); #endif optimize->nexperiments = input->nexperiments; optimize->ninputs = input->experiment->ninputs; optimize->experiment = (char **) alloca (input->nexperiments * sizeof (char *)); optimize->weight = (double *) alloca (input->nexperiments * sizeof (double)); for (i = 0; i < input->experiment->ninputs; ++i) optimize->file[i] = (GMappedFile **) g_malloc (input->nexperiments * sizeof (GMappedFile *)); for (i = 0; i < input->nexperiments; ++i) { #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: i=%u\n", i); #endif optimize->experiment[i] = input->experiment[i].name; optimize->weight[i] = input->experiment[i].weight; #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: experiment=%s weight=%lg\n", optimize->experiment[i], optimize->weight[i]); #endif for (j = 0; j < input->experiment->ninputs; ++j) { #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: stencil%u\n", j + 1); #endif optimize->file[j][i] = g_mapped_file_new (input->experiment[i].stencil[j], 0, NULL); } } // Reading the variables data #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: reading variables\n"); #endif optimize->label = (char **) alloca (input->nvariables * sizeof (char *)); j = input->nvariables * sizeof (double); optimize->rangemin = (double *) alloca (j); optimize->rangeminabs = (double *) alloca (j); optimize->rangemax = (double *) alloca (j); optimize->rangemaxabs = (double *) alloca (j); optimize->step = (double *) alloca (j); j = input->nvariables * sizeof (unsigned int); optimize->precision = (unsigned int *) alloca (j); optimize->nsweeps = (unsigned int *) alloca (j); optimize->nbits = (unsigned int *) alloca (j); for (i = 0; i < input->nvariables; ++i) { optimize->label[i] = input->variable[i].name; optimize->rangemin[i] = input->variable[i].rangemin; optimize->rangeminabs[i] = input->variable[i].rangeminabs; optimize->rangemax[i] = input->variable[i].rangemax; optimize->rangemaxabs[i] = input->variable[i].rangemaxabs; optimize->precision[i] = input->variable[i].precision; optimize->step[i] = input->variable[i].step; optimize->nsweeps[i] = input->variable[i].nsweeps; optimize->nbits[i] = input->variable[i].nbits; } if (input->algorithm == ALGORITHM_SWEEP || input->algorithm == ALGORITHM_ORTHOGONAL) { optimize->nsimulations = 1; for (i = 0; i < input->nvariables; ++i) { optimize->nsimulations *= optimize->nsweeps[i]; #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: nsweeps=%u nsimulations=%u\n", optimize->nsweeps[i], optimize->nsimulations); #endif } } if (optimize->nsteps) optimize->climbing = (double *) alloca (optimize->nvariables * sizeof (double)); // Setting error norm switch (input->norm) { case ERROR_NORM_EUCLIDIAN: optimize_norm = optimize_norm_euclidian; break; case ERROR_NORM_MAXIMUM: optimize_norm = optimize_norm_maximum; break; case ERROR_NORM_P: optimize_norm = optimize_norm_p; optimize->p = input->p; break; default: optimize_norm = optimize_norm_taxicab; } // Allocating values #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: allocating variables\n"); fprintf (stderr, "optimize_open: nvariables=%u algorithm=%u\n", optimize->nvariables, optimize->algorithm); #endif optimize->genetic_variable = NULL; if (optimize->algorithm == ALGORITHM_GENETIC) { optimize->genetic_variable = (GeneticVariable *) g_malloc (optimize->nvariables * sizeof (GeneticVariable)); for (i = 0; i < optimize->nvariables; ++i) { #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: i=%u min=%lg max=%lg nbits=%u\n", i, optimize->rangemin[i], optimize->rangemax[i], optimize->nbits[i]); #endif optimize->genetic_variable[i].minimum = optimize->rangemin[i]; optimize->genetic_variable[i].maximum = optimize->rangemax[i]; optimize->genetic_variable[i].nbits = optimize->nbits[i]; } } #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: nvariables=%u nsimulations=%u\n", optimize->nvariables, optimize->nsimulations); #endif optimize->value = (double *) g_malloc ((optimize->nsimulations + optimize->nestimates * optimize->nsteps) * optimize->nvariables * sizeof (double)); // Calculating simulations to perform for each task #if HAVE_MPI #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: rank=%u ntasks=%u\n", optimize->mpi_rank, ntasks); #endif optimize->nstart = optimize->mpi_rank * optimize->nsimulations / ntasks; optimize->nend = (1 + optimize->mpi_rank) * optimize->nsimulations / ntasks; if (optimize->nsteps) { optimize->nstart_climbing = optimize->mpi_rank * optimize->nestimates / ntasks; optimize->nend_climbing = (1 + optimize->mpi_rank) * optimize->nestimates / ntasks; } #else optimize->nstart = 0; optimize->nend = optimize->nsimulations; if (optimize->nsteps) { optimize->nstart_climbing = 0; optimize->nend_climbing = optimize->nestimates; } #endif #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: nstart=%u nend=%u\n", optimize->nstart, optimize->nend); #endif // Calculating simulations to perform for each thread optimize->thread = (unsigned int *) alloca ((1 + nthreads) * sizeof (unsigned int)); for (i = 0; i <= nthreads; ++i) { optimize->thread[i] = optimize->nstart + i * (optimize->nend - optimize->nstart) / nthreads; #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: i=%u thread=%u\n", i, optimize->thread[i]); #endif } if (optimize->nsteps) optimize->thread_climbing = (unsigned int *) alloca ((1 + nthreads_climbing) * sizeof (unsigned int)); // Opening result files optimize->file_result = g_fopen (optimize->result, "w"); optimize->file_variables = g_fopen (optimize->variables, "w"); // Performing the algorithm switch (optimize->algorithm) { // Genetic algorithm case ALGORITHM_GENETIC: optimize_genetic (); break; // Iterative algorithm default: optimize_iterate (); } // Getting calculation time t = g_date_time_new_now (tz); optimize->calculation_time = 0.000001 * g_date_time_difference (t, t0); g_date_time_unref (t); g_date_time_unref (t0); g_time_zone_unref (tz); printf ("%s = %.6lg s\n", _("Calculation time"), optimize->calculation_time); fprintf (optimize->file_result, "%s = %.6lg s\n", _("Calculation time"), optimize->calculation_time); // Closing result files fclose (optimize->file_variables); fclose (optimize->file_result); #if DEBUG_OPTIMIZE fprintf (stderr, "optimize_open: end\n"); #endif }
void log_close(void) { g_time_zone_unref(tz); fclose(logp); }
int main (int argc, char *argv[]) { GCContext gc_context; GMainLoop *mainLoop; GKeyFile *keyFile; GThread *displayThread; GThread *inputControllerThread; GThread *x11EventThread; int i; GCConfig config = { .movie_path = DEFAULT_MOVIE_PATH, .mask_path = DEFAULT_MASK_PATH, .controller_path = DEFAULT_CONTROLLER_PATH, .seconds_per_frame = DEFAULT_SECONDS_PER_FRAME, .easing_factor = DEFAULT_EASING_FACTOR, .frames_per_tick = 0, .diameter_controller = 0, .diameter_rim = 0, .ctr = 0, .fpr = 0, .number_of_frames = 0, .fullscreen = TRUE, .timeZone = NULL, }; Movie movie = { .planes = NULL, .frame_offset = 0, .fd_movie = -1, .frame = { .pitches = { FRAME_PITCH, 0, 0 } }, .fd_mask = -1, .mask = { .pitches = { MASK_PITCH, 0, 0 } }, .pre_load_surface = VDP_INVALID_HANDLE, .play_direction = DIRECTION_FORWARD, .ticks = 0, .new_frame = FALSE, .ease_to = TRUE, }; keyFile = g_key_file_new(); if (argc > 1 && g_key_file_load_from_file(keyFile, argv[1], G_KEY_FILE_NONE, NULL)) { if (g_key_file_has_group(keyFile, "MAIN")) { if (g_key_file_has_key(keyFile, "MAIN", "utc_offset", NULL)) config.timeZone = g_time_zone_new(g_key_file_get_string(keyFile, "MAIN", "utc_offset", NULL)); if (g_key_file_has_key(keyFile, "MAIN", "ease_to_time", NULL)) movie.ease_to = g_key_file_get_boolean(keyFile, "MAIN", "ease_to_time", NULL); } if (g_key_file_has_group(keyFile, "MOVIE")) { if (g_key_file_has_key(keyFile, "MOVIE", "file", NULL)) config.movie_path = g_key_file_get_string(keyFile, "MOVIE", "file", NULL); if (g_key_file_has_key(keyFile, "MOVIE", "mask", NULL)) config.mask_path = g_key_file_get_string(keyFile, "MOVIE", "mask", NULL); if (g_key_file_has_key(keyFile, "MOVIE", "seconds_per_frame", NULL)) config.seconds_per_frame = (float)g_key_file_get_double(keyFile, "MOVIE", "seconds_per_frame", NULL); if (g_key_file_has_key(keyFile, "MOVIE", "easing_factor", NULL)) config.easing_factor = (float)g_key_file_get_integer(keyFile, "MOVIE", "easing_factor", NULL); } if (g_key_file_has_group(keyFile, "CONTROLLER")) { if (g_key_file_has_key(keyFile, "CONTROLLER", "path", NULL)) config.controller_path = g_key_file_get_string(keyFile, "CONTROLLER", "path", NULL); if (g_key_file_has_key(keyFile, "CONTROLLER", "diameter_controller", NULL)) config.diameter_controller = (float)g_key_file_get_double(keyFile, "CONTROLLER", "diameter_controller", NULL); if (g_key_file_has_key(keyFile, "CONTROLLER", "diameter_rim", NULL)) config.diameter_rim = (float)g_key_file_get_double(keyFile, "CONTROLLER", "diameter_rim", NULL); if (g_key_file_has_key(keyFile, "CONTROLLER", "ctr", NULL)) config.ctr = (float)g_key_file_get_double(keyFile, "CONTROLLER", "ctr", NULL); if (g_key_file_has_key(keyFile, "CONTROLLER", "fpr", NULL)) config.fpr = (float)g_key_file_get_double(keyFile, "CONTROLLER", "fpr", NULL); if (g_key_file_has_key(keyFile, "CONTROLLER", "frames_per_tick", NULL)) config.frames_per_tick = (float)g_key_file_get_double(keyFile, "CONTROLLER", "frames_per_tick", NULL); } if (g_key_file_has_group(keyFile, "SCREEN")) { if (g_key_file_has_key(keyFile, "SCREEN", "fullscreen", NULL)) config.fullscreen = g_key_file_get_boolean(keyFile, "SCREEN", "fullscreen", NULL); } g_key_file_free(keyFile); } if (!config.timeZone) config.timeZone = g_time_zone_new_local(); if (!config.frames_per_tick && !(config.frames_per_tick = frames_per_tick(config.diameter_controller, config.diameter_rim, config.ctr, config.fpr))) { g_warning("No valid tick settings, using default frames per tick: %f", DEFAULT_FRAMES_PER_TICK); config.frames_per_tick = DEFAULT_FRAMES_PER_TICK; } config.movie_size = get_file_size(config.movie_path); config.number_of_frames = config.movie_size / FRAME_SIZE; mainLoop = g_main_loop_new(NULL, FALSE); gc_context.g_main_loop = mainLoop; gc_context.g_main_context = g_main_loop_get_context(mainLoop); gc_context.window_size.width = MOVIE_WIDTH; gc_context.window_size.height = MOVIE_HEIGHT; gc_context.exit = FALSE; gc_context.movie_context = &movie; gc_context.config = &config; g_message("movie file: %s", config.movie_path); g_message("movie size: %lu", config.movie_size); g_message("movie frames: %lu", config.number_of_frames); g_message("movie mask file: %s", config.mask_path); g_message("frames per minute: %f", get_frames_per_minute(&gc_context)); g_message("frames per day: %lu", get_frames_per_day(&gc_context)); g_message("frames per tick: %f", config.frames_per_tick); g_message("number of days: %d", get_number_of_days(&gc_context)); g_message("current day: %d", get_current_day(&gc_context)); if (movie.ease_to) { movie.ease_to_frame = get_frame_offset(&gc_context, GO_TO_RAND_DAY, DAY_OFFSET_NOW); g_message("ease to frame: %lu", movie.ease_to_frame); } x11_init(&gc_context); vdpau_init(&gc_context); load_movie(&gc_context); load_mask(&gc_context); g_cond_init(&movie.tick_cond); g_mutex_init(&movie.tick_lock); g_mutex_init(&movie.frame_lock); displayThread = g_thread_new("Display thread", display_thread, (gpointer)&gc_context); inputControllerThread = g_thread_new("Input controller thread", input_controller_thread, (gpointer)&gc_context); x11EventThread = g_thread_new("X11 Event thread", x11_event_thread, (gpointer)&gc_context); g_main_loop_run(mainLoop); gc_context.exit = TRUE; g_thread_join(displayThread); g_thread_join(inputControllerThread); g_thread_join(x11EventThread); g_cond_clear(&movie.tick_cond); g_mutex_clear(&movie.tick_lock); g_mutex_clear(&movie.frame_lock); g_time_zone_unref(config.timeZone); }