From: Niki Roo Date: Sat, 2 May 2020 10:40:06 +0000 (+0200) Subject: fix creation date format X-Git-Tag: fanfix-swing-1.1.0~9 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=70fd1e4a7a43b951d18bbe0cb64fd39ddd6b7933;p=fanfix-swing.git fix creation date format --- diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport.java b/src/be/nikiroo/fanfix/supported/BasicSupport.java index 900bcf9d..0a5ec368 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupport.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupport.java @@ -272,8 +272,10 @@ public abstract class BasicSupport { Story story = new Story(); MetaData meta = getMeta(); - if (meta.getCreationDate() == null || meta.getCreationDate().isEmpty()) { - meta.setCreationDate(StringUtils.fromTime(new Date().getTime())); + if (meta.getCreationDate() == null + || meta.getCreationDate().trim().isEmpty()) { + meta.setCreationDate(bsHelper + .formatDate(StringUtils.fromTime(new Date().getTime()))); } story.setMeta(meta); pg.put("meta", meta); diff --git a/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java b/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java index b5c7bb9c..7768052c 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java @@ -5,10 +5,14 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.utils.Image; +import be.nikiroo.utils.StringUtils; /** * Helper class for {@link BasicSupport}, mostly dedicated to text formating for @@ -220,4 +224,58 @@ public class BasicSupportHelper { return author; } + + /** + * Try to convert the date to a known, fixed format. + *

+ * If it fails to do so, it will return the date as-is. + * + * @param date + * the date to convert + * + * @return the converted date, or the date as-is + */ + public String formatDate(String date) { + long ms = 0; + + if (date != null && !date.isEmpty()) { + // Default Fanfix format: + try { + ms = StringUtils.toTime(date); + } catch (ParseException e) { + } + + // Second chance: + if (ms <= 0) { + SimpleDateFormat sdf = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ssSSS"); + try { + ms = sdf.parse(date).getTime(); + } catch (ParseException e) { + } + } + + // Last chance: + if (ms <= 0 && date.length() >= 10) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + try { + ms = sdf.parse(date.substring(0, 10)).getTime(); + } catch (ParseException e) { + } + } + + // If we found something, use THIS format: + if (ms > 0) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.format(new Date(ms)); + } + } + + if (date == null) { + date = ""; + } + + // :( + return date; + } } diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport_Deprecated.java b/src/be/nikiroo/fanfix/supported/BasicSupport_Deprecated.java index 47cb7a2c..bc3738a2 100644 --- a/src/be/nikiroo/fanfix/supported/BasicSupport_Deprecated.java +++ b/src/be/nikiroo/fanfix/supported/BasicSupport_Deprecated.java @@ -205,8 +205,9 @@ public abstract class BasicSupport_Deprecated extends BasicSupport { Story story = new Story(); MetaData meta = getMeta(url, getInput()); if (meta.getCreationDate() == null - || meta.getCreationDate().isEmpty()) { - meta.setCreationDate(StringUtils.fromTime(new Date().getTime())); + || meta.getCreationDate().trim().isEmpty()) { + meta.setCreationDate(bsHelper.formatDate( + StringUtils.fromTime(new Date().getTime()))); } story.setMeta(meta); pg.put("meta", meta); diff --git a/src/be/nikiroo/fanfix/supported/E621.java b/src/be/nikiroo/fanfix/supported/E621.java index 4d4c7b64..dc7cb1b3 100644 --- a/src/be/nikiroo/fanfix/supported/E621.java +++ b/src/be/nikiroo/fanfix/supported/E621.java @@ -61,7 +61,7 @@ class E621 extends BasicSupport { meta.setTitle(getTitle()); meta.setAuthor(getAuthor()); - meta.setDate(getDate()); + meta.setDate(bsHelper.formatDate(getDate())); meta.setTags(getTags()); meta.setSource(getType().getSourceName()); meta.setUrl(getSource().toString()); diff --git a/src/be/nikiroo/fanfix/supported/FimfictionApi.java b/src/be/nikiroo/fanfix/supported/FimfictionApi.java index 6c6d7ba3..43d01d19 100644 --- a/src/be/nikiroo/fanfix/supported/FimfictionApi.java +++ b/src/be/nikiroo/fanfix/supported/FimfictionApi.java @@ -124,7 +124,8 @@ class FimfictionApi extends BasicSupport { meta.setTitle(getKeyJson(json, 0, "type", "story", "title")); meta.setAuthor(getKeyJson(json, 0, "type", "user", "name")); - meta.setDate(getKeyJson(json, 0, "type", "story", "date_published")); + meta.setDate(bsHelper.formatDate( + getKeyJson(json, 0, "type", "story", "date_published"))); meta.setTags(getTags()); meta.setSource(getType().getSourceName()); meta.setUrl(getSource().toString()); diff --git a/src/be/nikiroo/fanfix/supported/InfoReader.java b/src/be/nikiroo/fanfix/supported/InfoReader.java index 405b28fb..206464f4 100644 --- a/src/be/nikiroo/fanfix/supported/InfoReader.java +++ b/src/be/nikiroo/fanfix/supported/InfoReader.java @@ -138,7 +138,7 @@ public class InfoReader { meta.setTitle(getInfoTag(in, "TITLE")); meta.setAuthor(getInfoTag(in, "AUTHOR")); - meta.setDate(getInfoTag(in, "DATE")); + meta.setDate(bsHelper.formatDate(getInfoTag(in, "DATE"))); meta.setTags(getInfoTagList(in, "TAGS", ",")); meta.setSource(getInfoTag(in, "SOURCE")); meta.setUrl(getInfoTag(in, "URL")); @@ -164,7 +164,8 @@ public class InfoReader { } catch (NumberFormatException e) { meta.setWords(0); } - meta.setCreationDate(getInfoTag(in, "CREATION_DATE")); + meta.setCreationDate( + bsHelper.formatDate(getInfoTag(in, "CREATION_DATE"))); meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER"))); if (withCover && meta.getCover() == null) { diff --git a/src/be/nikiroo/fanfix/supported/MangaLel.java b/src/be/nikiroo/fanfix/supported/MangaLel.java index 9929699c..de0b8713 100644 --- a/src/be/nikiroo/fanfix/supported/MangaLel.java +++ b/src/be/nikiroo/fanfix/supported/MangaLel.java @@ -32,7 +32,7 @@ class MangaLel extends BasicSupport { meta.setTitle(getTitle()); meta.setAuthor(getAuthor()); - meta.setDate(getDate()); + meta.setDate(bsHelper.formatDate(getDate())); meta.setTags(getTags()); meta.setSource(getType().getSourceName()); meta.setUrl(getSource().toString()); @@ -102,15 +102,6 @@ class MangaLel extends BasicSupport { } } - if (!value.isEmpty()) { - try { - long time = StringUtils.toTime(value); - value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") - .format(time); - } catch (ParseException e) { - } - } - return value; } diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java index e082a737..ade797fb 100644 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@ -85,7 +85,7 @@ class Text extends BasicSupport { meta.setTitle(getTitle()); meta.setAuthor(getAuthor()); - meta.setDate(getDate()); + meta.setDate(bsHelper.formatDate(getDate())); meta.setTags(new ArrayList()); meta.setSource(getType().getSourceName()); meta.setUrl(getSourceFile().toURI().toURL().toString());