From d6a8dfc00ef113eec079042fc20334d7f2355288 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Mon, 27 Feb 2017 04:48:39 +0800 Subject: [PATCH] Added {dt} timestamp binding --- .../format/ExpressionFormatMethods.java | 6 ++++ .../net/filebot/format/MediaBindingBean.java | 31 +++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/source/net/filebot/format/ExpressionFormatMethods.java b/source/net/filebot/format/ExpressionFormatMethods.java index 3c034365..9205fefc 100644 --- a/source/net/filebot/format/ExpressionFormatMethods.java +++ b/source/net/filebot/format/ExpressionFormatMethods.java @@ -14,6 +14,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.attribute.BasicFileAttributeView; import java.nio.file.attribute.BasicFileAttributes; +import java.time.format.DateTimeFormatter; +import java.time.temporal.Temporal; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -548,6 +550,10 @@ public class ExpressionFormatMethods { return Stream.of(DefaultGroovyMethods.min(self), DefaultGroovyMethods.max(self)).filter(Objects::nonNull).distinct().collect(toList()); } + public static String format(Temporal self, String pattern) { + return DateTimeFormatter.ofPattern(pattern).format(self); + } + /** * Episode utilities (EXPERIMENTAL) */ diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index e06808ff..c142ce13 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -202,22 +202,27 @@ public class MediaBindingBean { return getMovieInfo().getReleased(); if (infoObject instanceof AudioTrack) return getMusic().getAlbumReleaseDate(); + if (infoObject instanceof File) + return new SimpleDate(getTimeStamp()); + + return null; + } + + @Define("dt") + public ZonedDateTime getTimeStamp() { + File f = getMediaFile(); // try EXIF Date-Taken for image files or File Last-Modified for generic files - if (infoObject instanceof File) { - File f = (File) infoObject; + try { + return new ImageMetadata(f).getDateTaken().get(); + } catch (Exception e) { + // ignore and default to file creation date + } - try { - return new ImageMetadata(f).getDateTaken().map(SimpleDate::new).get(); - } catch (Exception e) { - // ignore and default to file creation date - } - - try { - return new SimpleDate(getCreationDate(f)); - } catch (Exception e) { - debug.warning(e::toString); - } + try { + return Instant.ofEpochMilli(getCreationDate(f)).atZone(ZoneOffset.systemDefault()); + } catch (Exception e) { + debug.warning(e::toString); } return null;