parse/format/convert invalid date format to the valid date format

Started by John_Smith, January 19, 2023, 08:33:45 AM

Previous topic - Next topic

John_Smith

I have some .flv/.mkv files with invalid date format in the CreateDate/CreationDate tags, e.g. 'Tue May 05 16:14:02 2009.'

Is it possible to parse/format/convert these values to the valid date format using ExifTool ( for updating FileModifyDate ) ?


(I am on Windows 7, default cmd/powershell code page cp852, ExifTool 12.55)

Thank you

Phil Harvey

This is a bit tricky, but may work with a command like this:

exiftool -wm w "-creationdate<creationdate#" -d "%a %b %d %H:%M:%S %Y" -api strictdate=0 FILE

The API StrictDate option is necessary to allow the invalid CreationDate to be extracted.

On Mac/Linux this uses the posix strptime library function, which returns something unexpected for this case and doesn't work properly (this will be fixed in 12.56).

On Windows this uses the Time::Piece library, which stands a chance of working but I can't easily test this right now.   If it doesn't work then there is a chance the the 12.56 patch will fix this too.

- Phil

Edit: Strike out incorrect conclusions.  It does work on Mac/Linux with 12.55

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

John_Smith

It works for the .flv files where the date in the CreateDate tag does not contain the final ./dot , e.g.

Wed Sep 30 06:57:13 2009


It does not work for the .mkv files where  the date in the CreationDate tag does contain the final ./dot , e.g.

Tue May 05 16:14:02 2009.

U:\exiftool -v5 -wm w "-FileModifyDate<CreationDate#" -d "%a %b %d %H:%M:%S %Y" -api StrictDate=0 "in.mkv"
======== in.mkv
Setting new values from in.mkv
garbage in File:FileModifyDate (PrintConvInv)
Warning: garbage in File:FileModifyDate (PrintConvInv) - in.mkv
Warning: No writable tags set from in.mkv
Nothing changed in in.mkv
    0 image files updated
    1 image files unchanged

StarGeek

"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

John_Smith

Tried, does not work, same result.


Weird, tried export to json, there the ./dot is not shown as ./dot, but as '\n'


-d "%a %b %d %H:%M:%S %Y\n"  does not work either. How do I escape the \ ?

Phil Harvey

Wow.  Tricky.  Reading the strftime man page, it says the %n represents a newline.  I would try that.

- Phil

Edit:  Also, I take back what I said about this not working in Mac/Linux (and I'll strike this out in my previous post).  The problem was in a print statement that I had added to debug this when I was trying to figure out what was going on.
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

John_Smith

Tried -d "%a %b %d %H:%M:%S %Y%n" , does not work, same result.

Phil Harvey

Well, it was worth a try.  The different date/time parsing libraries have their own quirks.  The "garbage in" message is coming from the library.

OK then, we can do it manually:

exiftool "-filemodifydate<${creationdate;s/\w+ (\w+) (\d+) (\S+) (\d+)/sprintf('%s:%.2d:%s %s',$4,{Jan=>1,Feb=>2,Mar=>3,Apr=>4,May=>5,Jun=>6,Jul=>7,Aug=>8,Sep=>9,Oct=>10,Nov=>11,Dec=>12}->{$1},$2,$3)/e}" FILE

A lot more complex, but it should get the job done if the format is exactly as in your first post (and it doesn't matter what comes after the year).

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).