Browse Source

DST time gaps fix

pull/71/merge
Ezerous 4 years ago
parent
commit
cd7dc5ae08
  1. 2
      app/build.gradle
  2. 4
      app/src/main/assets/apache_libraries.html
  3. 19
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParser.java
  4. 5
      app/src/test/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParserTest.java

2
app/build.gradle

@ -96,7 +96,7 @@ dependencies {
force = true //TODO: Remove when minSdkVersion >= 21 force = true //TODO: Remove when minSdkVersion >= 21
} }
implementation 'org.jsoup:jsoup:1.13.1' implementation 'org.jsoup:jsoup:1.13.1'
implementation 'joda-time:joda-time:2.10.4' implementation 'joda-time:joda-time:2.10.8'
implementation 'com.github.franmontiel:PersistentCookieJar:1.0.1' implementation 'com.github.franmontiel:PersistentCookieJar:1.0.1'
implementation 'com.github.PhilJay:MPAndroidChart:3.0.3' implementation 'com.github.PhilJay:MPAndroidChart:3.0.3'
implementation 'com.mikepenz:materialdrawer:6.1.1' implementation 'com.mikepenz:materialdrawer:6.1.1'

4
app/src/main/assets/apache_libraries.html

@ -51,8 +51,8 @@
Andrew Oberstar)</h5> Andrew Oberstar)</h5>
</li> </li>
<li> <li>
<h5><a href="https://github.com/JodaOrg/joda-time">Joda-Time</a>&nbsp;v2.10.4 (Copyright <h5><a href="https://github.com/JodaOrg/joda-time">Joda-Time</a>&nbsp;v2.10.8 (Copyright
©2002-2019 Joda.org)</h5> ©2002-2020 Joda.org)</h5>
</li> </li>
<li> <li>
<h5><a href="https://github.com/powermock/powermock">PowerMock</a>&nbsp;v2.0.2</h5> <h5><a href="https://github.com/powermock/powermock">PowerMock</a>&nbsp;v2.0.2</h5>

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

@ -5,6 +5,8 @@ import androidx.annotation.VisibleForTesting;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils; import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone; import org.joda.time.DateTimeZone;
import org.joda.time.IllegalInstantException;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder; import org.joda.time.format.DateTimeFormatterBuilder;
@ -64,8 +66,9 @@ public class ThmmyDateTimeParser {
thmmyDateTime = thmmyDateTime.replaceAll("\\spm", ""); thmmyDateTime = thmmyDateTime.replaceAll("\\spm", "");
DateTime dateTime; DateTime dateTime;
LocalDateTime localDateTime;
try { try {
dateTime = formatter.withZone(dtz).withLocale(englishLocale).parseDateTime(thmmyDateTime); localDateTime = formatter.withLocale(englishLocale).parseLocalDateTime(thmmyDateTime);
} catch (IllegalArgumentException e1) { } catch (IllegalArgumentException e1) {
Timber.v("Parsing DateTime %s using English Locale failed.", thmmyDateTime); Timber.v("Parsing DateTime %s using English Locale failed.", thmmyDateTime);
try { try {
@ -73,13 +76,23 @@ public class ThmmyDateTimeParser {
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.v("Attempting to parse DateTime %s using Greek Locale...", thmmyDateTime); Timber.v("Attempting to parse DateTime %s using Greek Locale...", thmmyDateTime);
dateTime = formatter.withZone(dtz).withLocale(greekLocale).parseDateTime(thmmyDateTime); localDateTime = formatter.withLocale(greekLocale).parseLocalDateTime(thmmyDateTime);
} catch (IllegalArgumentException e2) { } catch (IllegalArgumentException e2) {
Timber.d("Parsing DateTime %s using Greek Locale failed too.", thmmyDateTime); Timber.v("Parsing DateTime %s using Greek Locale failed too.", thmmyDateTime);
Timber.e("Couldn't convert DateTime %s to timestamp!", originalDateTime); Timber.e("Couldn't convert DateTime %s to timestamp!", originalDateTime);
return null; return null;
} }
} }
// Ensure DST time overlaps/ gaps are handled properly
try{
// For autumn overlaps
dateTime = localDateTime.toDateTime(dtz).withEarlierOffsetAtOverlap();
} catch (IllegalInstantException e2) {
// For spring gaps
dateTime = localDateTime.plusHours(1).toDateTime(dtz);
}
String timestamp = Long.toString(dateTime.getMillis()); String timestamp = Long.toString(dateTime.getMillis());
Timber.v("DateTime %s was converted to %s, or %s", originalDateTime, timestamp, dateTime.toString()); Timber.v("DateTime %s was converted to %s, or %s", originalDateTime, timestamp, dateTime.toString());

5
app/src/test/java/gr/thmmy/mthmmy/utils/parsing/ThmmyDateTimeParserTest.java

@ -25,7 +25,7 @@ public class ThmmyDateTimeParserTest {
private static final String TIME_ZONE = "Europe/Athens"; private static final String TIME_ZONE = "Europe/Athens";
private static final String GET_DTZ = "getDtz"; private static final String GET_DTZ = "getDtz";
private final String[] expTimestamps = {"1569245936000", "1569191627000", "1570050809000"}; private final String[] expTimestamps = {"1569245936000", "1569191627000", "1570050809000", "1553995543000"};
private final String[][] dateTimes = { private final String[][] dateTimes = {
{ {
"Σεπτεμβρίου 23, 2019, 16:38:56", "Σεπτεμβρίου 23, 2019, 16:38:56",
@ -45,6 +45,9 @@ public class ThmmyDateTimeParserTest {
{ {
"Οκτωβρίου 03, 2019, 12:13:29 am", "Οκτωβρίου 03, 2019, 12:13:29 am",
"Οκτωβρίου 03, 2019, 00:13:29 am" "Οκτωβρίου 03, 2019, 00:13:29 am"
},
{
"Μαρτίου 31, 2019, 03:25:43 am"
} }
}; };

Loading…
Cancel
Save