Skip to content

Commit

Permalink
import datetime detectio: use exif datetime standard instead of local…
Browse files Browse the repository at this point in the history
…ized time
  • Loading branch information
phweyland committed Feb 11, 2022
1 parent e62658e commit de9188b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/common/camera_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,8 @@ void dt_camctl_select_camera(const dt_camctl_t *c, const dt_camera_t *cam)
time_t dt_camctl_get_image_file_timestamp(const dt_camctl_t *c, const char *path, const char *filename)
{
time_t timestamp = 0;
if(!path || !filename)
return 0;
// Lets check the type of file...
CameraFileInfo cfi;
if(!(gp_camera_file_get_info(c->active_camera->gpcam, path, filename, &cfi, c->gpcontext) == GP_OK))
Expand Down
7 changes: 7 additions & 0 deletions src/common/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,13 @@ gboolean dt_metadata_already_imported(const char *filename, const char *datetime
return res;
}

void dt_metadata_unix_time_to_text(char *exif, const size_t exif_len, const time_t *unix)
{
struct tm tt;
(void)localtime_r(unix, &tt);
strftime(exif, exif_len, "%Y:%m:%d %H:%M:%S", &tt);
}

// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
3 changes: 3 additions & 0 deletions src/common/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ void dt_metadata_clear(const GList *imgs, const gboolean undo_on); // libs/metad
/** check if the "Xmp.darktable.image_id" already exists */
gboolean dt_metadata_already_imported(const char *filename, const char *datetime);

/** temporary routine waiting for common datetime.c */
void dt_metadata_unix_time_to_text(char *exif, const size_t exif_len, const time_t *unix);

// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
9 changes: 4 additions & 5 deletions src/control/jobs/camera_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,15 @@ void _camera_import_image_downloaded(const dt_camera_t *camera, const char *in_p
dt_camera_import_t *t = (dt_camera_import_t *)data;
const int32_t imgid = dt_image_import(dt_import_session_film_id(t->shared.session), filename, FALSE, TRUE);

const time_t timestamp = dt_camctl_get_image_file_timestamp(darktable.camctl, in_path, in_filename);
const time_t timestamp = (!in_path || !in_filename) ? 0 :
dt_camctl_get_image_file_timestamp(darktable.camctl, in_path, in_filename);
if(timestamp && imgid >= 0)
{
GDateTime *dt_datetime = g_date_time_new_from_unix_local(timestamp);
gchar *dt_txt = g_date_time_format(dt_datetime, "%x %X");
char dt_txt[DT_DATETIME_LENGTH];
dt_metadata_unix_time_to_text(dt_txt, sizeof(dt_txt), &timestamp);
gchar *id = g_strconcat(in_filename, "-", dt_txt, NULL);
dt_metadata_set(imgid, "Xmp.darktable.image_id", id, FALSE);
g_free(dt_txt);
g_free(id);
g_date_time_unref(dt_datetime);
}

dt_control_queue_redraw_center();
Expand Down
8 changes: 3 additions & 5 deletions src/control/jobs/control_jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2091,14 +2091,12 @@ static int _control_import_image_copy(const char *filename,
G_FILE_QUERY_INFO_NONE, NULL, &error);
const char *fn = g_file_info_get_name(info);
// FIXME set a routine common with import.c
const guint64 datetime = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
GDateTime *dt_datetime = g_date_time_new_from_unix_local(datetime);
gchar *dt_txt = g_date_time_format(dt_datetime, "%x %X");
const time_t datetime = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
char dt_txt[DT_DATETIME_LENGTH];
dt_metadata_unix_time_to_text(dt_txt, sizeof(dt_txt), &datetime);
char *id = g_strconcat(fn, "-", dt_txt, NULL);
dt_metadata_set(imgid, "Xmp.darktable.image_id", id, FALSE);
g_free(id);
g_free(dt_txt);
g_date_time_unref(dt_datetime);
g_object_unref(info);
g_object_unref(gfile);
*imgs = g_list_prepend(*imgs, GINT_TO_POINTER(imgid));
Expand Down
16 changes: 11 additions & 5 deletions src/libs/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,13 @@ static guint _import_from_camera_set_file_list(dt_lib_module_t *self)
if(include_jpegs || (ext && g_ascii_strncasecmp(ext, ".jpg", sizeof(".jpg"))
&& g_ascii_strncasecmp(ext, ".jpeg", sizeof(".jpeg"))))
{
const guint64 datetime = file->timestamp;
const time_t datetime = file->timestamp;
GDateTime *dt_datetime = g_date_time_new_from_unix_local(datetime);
gchar *dt_txt = g_date_time_format(dt_datetime, "%x %X");
gchar *basename = g_path_get_basename(file->filename);
const gboolean already_imported = dt_metadata_already_imported(basename, dt_txt);
char dtid[DT_DATETIME_LENGTH];
dt_metadata_unix_time_to_text(dtid, sizeof(dtid), &datetime);
const gboolean already_imported = dt_metadata_already_imported(basename, dtid);
g_free(basename);
GtkTreeIter iter;
gtk_list_store_append(d->from.store, &iter);
Expand Down Expand Up @@ -703,7 +705,7 @@ static guint _import_set_file_list(const gchar *folder, const int folder_lgth,
const char *filename = g_file_info_get_name(info);
if(!filename)
continue;
const guint64 datetime = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
const time_t datetime = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
GDateTime *dt_datetime = g_date_time_new_from_unix_local(datetime);
gchar *dt_txt = g_date_time_format(dt_datetime, "%x %X");
const GFileType filetype = g_file_info_get_file_type(info);
Expand All @@ -728,8 +730,12 @@ static guint _import_set_file_list(const gchar *folder, const int folder_lgth,
if(filmroll_id != -1)
already_imported = dt_image_get_id(filmroll_id, filename) != -1 ? TRUE : FALSE;
}
else already_imported = dt_metadata_already_imported(&fullname[offset], dt_txt);

else
{
char dtid[DT_DATETIME_LENGTH];
dt_metadata_unix_time_to_text(dtid, sizeof(dtid), &datetime);
already_imported = dt_metadata_already_imported(&fullname[offset], dtid);
}
GtkTreeIter iter;
gtk_list_store_append(d->from.store, &iter);
gtk_list_store_set(d->from.store, &iter,
Expand Down

0 comments on commit de9188b

Please sign in to comment.