Convert times with timezone info to UTC

Started by hk1020, September 18, 2022, 10:25:49 AM

Previous topic - Next topic

hk1020

To get a time sorted directory I'd like to do this:

exiftool -hardlink\<gpsdatetime . e -d 'all/%Y%m%d_%H%M%S-%%f.%%e'
This works fine if there is a gpsdatetime key. Unfortunately, there isn't always one, most notably on iPhone photos.

Instead of adding gpsdatetime I figured I could use SubSecDateTimeOriginal, which seems to always exist and holds correct timezone information. But how can I convert it to UTC? It looks like this:

SubSecDateTimeOriginal          : 2022:08:17 19:48:41.801+01:00

StarGeek

There really isn't an easy way to do this without using an intermediate tag copy. For an image, you could copy to the GPS date/time tags.  For a video, hopefully the Quicktime:CreateDate is already UTC.
exiftool "-GPS*Stamp<SubSecDateTimeOriginal" /path/to/files/

Alternatively, you could copy to the file system time stamps.  That has the advantage of being very quick and doesn't re-write the file.  From there, you could either use the -api TimeZone option if you're on Mac/Linux or set the ENV variable TZ if on Windows.  The -api may or may not work on some versions of Windows.  It doesn't work for me.

First, you would copy to the file system tag (all OS)
exiftool "-FileModifyDate<SubSecDateTimeOriginal" /path/to/files/

Then using the -api option on Linux/Mac
exiftool -api timezone=UTC '-hardlink<FileModifyDate' -d 'all/%Y%m%d_%H%M%S-%%f.%%e' /path/to/files/

Using the ENV variable on Windows takes 3 commands
set TZ=UTC
exiftool "-hardlink<FileModifyDate" -d "all/%Y%m%d_%H%M%S-%%f.%%e" /path/to/files/
set TZ=

I tend to go with writing to the GPS time tags because I find it more useful in the long run.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

hk1020

Thanks for your suggestions. Give me some ideas. Nevertheless, it is still clunky. As some experiments I came up this is illustration:

#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;

my $pattern = "%Y:%m:%d %H:%M:%S%z";
my $str = '2022:08:17 12:38:44+05:00' =~ s/:(..)$/$1/r;
my $u = Time::Piece->strptime($str, $pattern);

print "Time is:\n", $u->strftime($pattern), "\n";

But how can I connect this with the exiftool command as such? Can I pass this code somehow? Or would use Image::ExifTool be just as easy? I didn't study the docs yet.

StarGeek

For a limited amount of files, you could use the Perl backtick ` to execute exiftool, but too many files and you end up with Common Mistake #2.

For more files, you would either use the Perl library or create an ARGs file to pass to the -@ (Argfile) option.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype