Skip to content

Commit

Permalink
Merge pull request #650 from StefanOltmann/fix_date_parsing
Browse files Browse the repository at this point in the history
Fixed parsing of illegal dates + handle known null value
  • Loading branch information
drewnoakes authored Feb 4, 2024
2 parents 67dfddd + 49d82de commit 753cf1f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Source/com/drew/metadata/Directory.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,12 @@ public java.util.Date getDate(int tagType, @Nullable String subsecond, @Nullable

String dateString = o.toString();

/*
* This is a common NULL value known for cameras like Olympus C750UZ.
*/
if (dateString.equals("0000:00:00 00:00:00"))
return null;

// if the date string has subsecond information, it supersedes the subsecond parameter
Pattern subsecondPattern = Pattern.compile("(\\d\\d:\\d\\d:\\d\\d)(\\.\\d+)");
Matcher subsecondMatcher = subsecondPattern.matcher(dateString);
Expand All @@ -913,7 +919,23 @@ public java.util.Date getDate(int tagType, @Nullable String subsecond, @Nullable

for (String datePattern : datePatterns) {
try {

DateFormat parser = new SimpleDateFormat(datePattern);

/*
* Some older digital cameras, such as the Olympus C750UZ, may not have recorded a proper
* exif:DateTimeOriginal value. Instead of leaving this field empty, they used
* 0000:00:00 00:00:00 as a placeholder.
*
* "0000:00:00 00:00:00" will result in "Sun Nov 30 00:00:00 GMT 2",
* which is not what a user would expect.
*
* Any illegal formats should result in an exception and not be parsed.
*
* It's best to turn lenient mode off.
*/
parser.setLenient(false);

if (timeZone != null)
parser.setTimeZone(timeZone);
else
Expand Down

0 comments on commit 753cf1f

Please sign in to comment.