Browse Source

Relative time fixes

pull/67/merge
Ezerous 5 years ago
parent
commit
5d79e99a86
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 4
      app/src/main/java/gr/thmmy/mthmmy/utils/DateTimeUtils.java
  2. 11
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java
  3. 12
      app/src/test/java/gr/thmmy/mthmmy/utils/DateTimeUtilsTest.java

4
app/src/main/java/gr/thmmy/mthmmy/utils/DateTimeUtils.java

@ -43,12 +43,10 @@ class DateTimeUtils {
count += 1; count += 1;
} else if (duration < 26*DAY_IN_MILLIS) { } else if (duration < 26*DAY_IN_MILLIS) {
count = duration/DAY_IN_MILLIS; count = duration/DAY_IN_MILLIS;
format = "%d day"; format = "%dd";
mod = duration % DAY_IN_MILLIS; mod = duration % DAY_IN_MILLIS;
if(mod >= 12*HOUR_IN_MILLIS) if(mod >= 12*HOUR_IN_MILLIS)
count += 1; count += 1;
if(count>1)
format = format + 's';
} else if (duration < 320*DAY_IN_MILLIS) { } else if (duration < 320*DAY_IN_MILLIS) {
count = duration/MONTH_IN_MILLIS; count = duration/MONTH_IN_MILLIS;
format = "%d month"; format = "%d month";

11
app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java

@ -20,11 +20,10 @@ import timber.log.Timber;
public class ThmmyDateTimeParser { public class ThmmyDateTimeParser {
private static final DateTimeParser[] parsers = { private static final DateTimeParser[] parsers = {
DateTimeFormat.forPattern("HH:mm:ss").getParser(),
DateTimeFormat.forPattern("hh:mm:ss a").getParser(), DateTimeFormat.forPattern("hh:mm:ss a").getParser(),
DateTimeFormat.forPattern("MMMM d, Y, HH:mm:ss").getParser(), DateTimeFormat.forPattern("HH:mm:ss").getParser(),
DateTimeFormat.forPattern("MMMM d, Y, hh:mm:ss a").getParser(), DateTimeFormat.forPattern("MMMM d, Y, hh:mm:ss a").getParser(),
DateTimeFormat.forPattern("d MMMM Y, HH:mm:ss").getParser(), DateTimeFormat.forPattern("MMMM d, Y, HH:mm:ss").getParser(),
DateTimeFormat.forPattern("d MMMM Y, HH:mm:ss").getParser(), DateTimeFormat.forPattern("d MMMM Y, HH:mm:ss").getParser(),
DateTimeFormat.forPattern("Y-M-d, HH:mm:ss").getParser(), DateTimeFormat.forPattern("Y-M-d, HH:mm:ss").getParser(),
DateTimeFormat.forPattern("d-M-Y, HH:mm:ss").getParser() DateTimeFormat.forPattern("d-M-Y, HH:mm:ss").getParser()
@ -38,9 +37,12 @@ public class ThmmyDateTimeParser {
private static final Locale greekLocale = new Locale("el", "GR"); private static final Locale greekLocale = new Locale("el", "GR");
private static final Locale englishLocale = new Locale("en", "US"); private static final Locale englishLocale = new Locale("en", "US");
private static final Pattern pattern = Pattern.compile("\\s(1[3-9]|2[0-3]:)"); private static final Pattern pattern = Pattern.compile("\\s((1[3-9]|2[0-3]):)");
private ThmmyDateTimeParser(){}
public static String convertToTimestamp(String thmmyDateTime){ public static String convertToTimestamp(String thmmyDateTime){
Timber.d("Will attempt to convert %s to timestamp.", thmmyDateTime);
String originalDateTime = thmmyDateTime; String originalDateTime = thmmyDateTime;
DateTimeZone dtz = getDtz(); DateTimeZone dtz = getDtz();
@ -67,6 +69,7 @@ public class ThmmyDateTimeParser {
DateFormatSymbols dfs = DateTimeUtils.getDateFormatSymbols(greekLocale); DateFormatSymbols dfs = DateTimeUtils.getDateFormatSymbols(greekLocale);
thmmyDateTime = thmmyDateTime.replace("am",dfs.getAmPmStrings()[0]); thmmyDateTime = thmmyDateTime.replace("am",dfs.getAmPmStrings()[0]);
thmmyDateTime = thmmyDateTime.replace("pm",dfs.getAmPmStrings()[1]); thmmyDateTime = thmmyDateTime.replace("pm",dfs.getAmPmStrings()[1]);
Timber.d("Attempting to parse DateTime %s using Greek Locale...", thmmyDateTime);
dateTime=formatter.withZone(dtz).withLocale(greekLocale).parseDateTime(thmmyDateTime); dateTime=formatter.withZone(dtz).withLocale(greekLocale).parseDateTime(thmmyDateTime);
} }
catch (IllegalArgumentException e2){ catch (IllegalArgumentException e2){

12
app/src/test/java/gr/thmmy/mthmmy/utils/DateTimeUtilsTest.java

@ -40,12 +40,12 @@ public class DateTimeUtilsTest {
"21h", "21h",
"21h", "21h",
"22h", "22h",
"1 day", "1d",
"1 day", "1d",
"2 days", "2d",
"2 days", "2d",
"3 days", "3d",
"16 days", "16d",
"1 month", "1 month",
"2 months", "2 months",
"1 year", "1 year",

Loading…
Cancel
Save