Browse Source

DateTime parsing fixes

pull/63/head
Ezerous 5 years ago
parent
commit
38396f1d66
  1. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java
  2. 22
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java

2
app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentAdapter.java

@ -50,7 +50,7 @@ class RecentAdapter extends RecyclerView.Adapter<RecentAdapter.ViewHolder> {
holder.mDateTimeView.setReferenceTime(Long.valueOf(dateTimeString)); holder.mDateTimeView.setReferenceTime(Long.valueOf(dateTimeString));
} }
catch(NumberFormatException e){ catch(NumberFormatException e){
Timber.e(e, "Invalid number format."); Timber.e(e, "Invalid number format: %s", dateTimeString);
holder.mDateTimeView.setText(dateTimeString); holder.mDateTimeView.setText(dateTimeString);
} }
else else

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

@ -13,6 +13,8 @@ import org.joda.time.format.DateTimeFormatterBuilder;
import org.joda.time.format.DateTimeParser; import org.joda.time.format.DateTimeParser;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
import timber.log.Timber; import timber.log.Timber;
@ -21,9 +23,9 @@ 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").getParser(),
DateTimeFormat.forPattern("HH:mm:ss a").getParser(), DateTimeFormat.forPattern("KK:mm:ss a").getParser(),
DateTimeFormat.forPattern("MMMM d, Y, HH:mm:ss").getParser(), DateTimeFormat.forPattern("MMMM d, Y, HH:mm:ss").getParser(),
DateTimeFormat.forPattern("MMMM d, Y, HH:mm:ss a").getParser() DateTimeFormat.forPattern("MMMM d, Y, KK:mm:ss a").getParser()
}; };
private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder() private static final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
@ -33,6 +35,8 @@ public class ThmmyDateTimeParser {
private static final Locale greekLocale = Locale.forLanguageTag("el-GR"); private static final Locale greekLocale = Locale.forLanguageTag("el-GR");
private static final Locale englishLocale = Locale.forLanguageTag("en-US"); private static final Locale englishLocale = Locale.forLanguageTag("en-US");
private static final Pattern pattern = Pattern.compile("\\s(1[2-9]|2[0-3]:)");
public static String convertToTimestamp(String thmmyDateTime){ public static String convertToTimestamp(String thmmyDateTime){
DateTimeZone dtz; DateTimeZone dtz;
if(!BaseApplication.getInstance().getSessionManager().isLoggedIn()) if(!BaseApplication.getInstance().getSessionManager().isLoggedIn())
@ -41,16 +45,26 @@ public class ThmmyDateTimeParser {
dtz = DateTimeZone.getDefault(); dtz = DateTimeZone.getDefault();
//Add today's date for the first two cases //Add today's date for the first two cases
if(Character.isDigit(thmmyDateTime.charAt(0))) if(thmmyDateTime.charAt(2)==':')
thmmyDateTime = (new DateTime()).toString("MMMM d, Y, ") + thmmyDateTime; thmmyDateTime = (new DateTime()).toString("MMMM d, Y, ") + thmmyDateTime;
// For the stupid format 23:54:12 pm
Matcher matcher = pattern.matcher(thmmyDateTime);
if (matcher.find())
thmmyDateTime = thmmyDateTime.replaceAll("\\s(am|pm|π.μ.|α.μ.)","");
DateTime dateTime; DateTime dateTime;
try{ try{
thmmyDateTime = thmmyDateTime.replace("am","π.μ.");
thmmyDateTime = thmmyDateTime.replace("pm","μ.μ.");
dateTime=formatter.withZone(dtz).withLocale(greekLocale).parseDateTime(thmmyDateTime); dateTime=formatter.withZone(dtz).withLocale(greekLocale).parseDateTime(thmmyDateTime);
} }
catch (IllegalArgumentException e1){ catch (IllegalArgumentException e1){
Timber.i("Parsing DateTime using Greek Locale failed."); Timber.d("Parsing DateTime using Greek Locale failed.");
try{ try{
thmmyDateTime = thmmyDateTime.replace("π.μ.","am");
thmmyDateTime = thmmyDateTime.replace("μ.μ.","pm");
dateTime=formatter.withZone(dtz).withLocale(englishLocale).parseDateTime(thmmyDateTime); dateTime=formatter.withZone(dtz).withLocale(englishLocale).parseDateTime(thmmyDateTime);
} }
catch (IllegalArgumentException e2){ catch (IllegalArgumentException e2){

Loading…
Cancel
Save