Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com>2021-07-15 17:22:42 +0300
committerGitHub <noreply@github.com>2021-07-15 17:22:42 +0300
commit481ae70a6395f50701a283c0801b864cdad3b338 (patch)
treec08ba1b9c1c7d53ffad50091f42a2f7cddcee430 /app/src/main
parent9c22f3c5931d9671d33b78a1529fe24888d0746e (diff)
Fix #1034 Due date are always shown with hours (french language)
* Fix #1034 Due date are always shown with hours (french language) Signed-off-by: Stefan Niedermann <info@niedermann.it> * #1034 more tolerant fuckup detection * #1034 more tolerant fuckup detection * Fix #1034 Due date are always shown with hours (french language) Signed-off-by: Stefan Niedermann <info@niedermann.it> * Fix #1034 Due date are always shown with hours (french language) Use final modifier for immutable `now` value Signed-off-by: Stefan Niedermann <info@niedermann.it> * Fix #1034 Add unit tests Signed-off-by: Stefan Niedermann <info@niedermann.it> Co-authored-by: desperateCoder <echotodevnull@gmail.com>
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/it/niedermann/nextcloud/deck/util/DateUtil.java59
1 files changed, 45 insertions, 14 deletions
diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/DateUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/DateUtil.java
index 93046a9eb..db906ab31 100644
--- a/app/src/main/java/it/niedermann/nextcloud/deck/util/DateUtil.java
+++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/DateUtil.java
@@ -1,46 +1,77 @@
package it.niedermann.nextcloud.deck.util;
import android.content.Context;
+import android.os.Build;
import android.text.format.DateUtils;
import androidx.annotation.NonNull;
import java.time.ZonedDateTime;
+import java.util.Locale;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import it.niedermann.nextcloud.deck.R;
public final class DateUtil {
private static final int DATE_TIME_PARTS_SIZE = 2;
+ private static final String ISO3_LANGUAGE_FRENCH = "fra";
+ private static final Pattern FRENCH_TIME_SUFFIX = Pattern.compile(" ([^0-9] )?[0-9]{1,2}:[0-9]{2}$");
private DateUtil() {
throw new UnsupportedOperationException("This class must not get instantiated");
}
public static CharSequence getRelativeDateTimeString(@NonNull Context context, long time) {
- long now = ZonedDateTime.now().toInstant().toEpochMilli();
- if ((now - time) < 60 * 1000 && now > time) {
- // < 60 seconds -> seconds ago
+ final long now = ZonedDateTime.now().toInstant().toEpochMilli();
+ if ((now - time) < 60_000 && now > time) {
+ // < 60 seconds → seconds ago
return context.getString(R.string.seconds_ago);
} else {
// in the future or past (larger than 60 seconds)
- final CharSequence dateString = DateUtils.getRelativeDateTimeString(
+ final String dateTimeString = DateUtils.getRelativeDateTimeString(
context,
time,
DateUtils.SECOND_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0
- );
- final String[] parts = dateString.toString().split(",");
- if (parts.length == DATE_TIME_PARTS_SIZE) {
- if (parts[1].contains(":") && !parts[0].contains(":")) {
- return parts[0];
- } else if (parts[0].contains(":") && !parts[1].contains(":")) {
- return parts[1];
+ ).toString().trim();
+
+ return getRelativeDateStringWithoutTime(dateTimeString)
+ .orElse(dateTimeString);
+ }
+ }
+
+ /**
+ * Tries to strip the time part of a relative, human readable, localized date time string.
+ */
+ private static Optional<String> getRelativeDateStringWithoutTime(@NonNull String dateTimeString) {
+ final String[] parts = dateTimeString.split(",");
+ if (parts.length == DATE_TIME_PARTS_SIZE) {
+ if (parts[1].contains(":") && !parts[0].contains(":")) {
+ return Optional.of(parts[0]);
+ } else if (parts[0].contains(":") && !parts[1].contains(":")) {
+ return Optional.of(parts[1]);
+ }
+ }
+
+ /*
+ * Date and time are note separated by a <code>,</code>.
+ *
+ * Relative date time strings on Android <= 11 do not have a <code>,</code> to separate the date from the time in french language.
+ * To provide a similar result, we try to find this case and work around the Android limitation.
+ *
+ * @see <a href="https://github.com/stefan-niedermann/nextcloud-deck/issues/1034">GitHub issue</a>
+ */
+ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
+ if (ISO3_LANGUAGE_FRENCH.equalsIgnoreCase(Locale.getDefault().getISO3Language())) {
+ final Matcher matcher = FRENCH_TIME_SUFFIX.matcher(dateTimeString);
+ if (matcher.find()) {
+ return Optional.of(dateTimeString.substring(0, dateTimeString.length() - matcher.group().length()));
}
}
- // dateString contains unexpected format.
- // fallback: use relative date time string from android api as is.
- return dateString.toString();
}
+ return Optional.empty();
}
}