]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/utils/Globals.java
rework transaction date handling
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / Globals.java
index ecbffda495c819d44c2edfed501de486dac8e2e3..e1ce8eea5430e287e122a7aecbf2be9f02a57c98 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
@@ -19,28 +19,33 @@ package net.ktnx.mobileledger.utils;
 
 import android.app.Activity;
 import android.content.Context;
-import android.support.annotation.ColorInt;
 import android.view.View;
 import android.view.inputmethod.InputMethodManager;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
-import java.util.Date;
 import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public final class Globals {
-    @ColorInt
-    public static int tableRowLightBG;
-    @ColorInt
-    public static int tableRowDarkBG;
-    @ColorInt
-    public static int primaryDark, defaultTextColor;
+    private static final ThreadLocal<SimpleDateFormat> dateFormatter =
+            new ThreadLocal<SimpleDateFormat>() {
+                @Override
+                protected SimpleDateFormat initialValue() {
+                    return new SimpleDateFormat("yyyy/MM/dd", Locale.US);
+                }
+            };
+    private static final ThreadLocal<SimpleDateFormat> isoDateFormatter =
+            new ThreadLocal<SimpleDateFormat>() {
+                @Override
+                protected SimpleDateFormat initialValue() {
+                    return new SimpleDateFormat("yyyy-MM-dd", Locale.US);
+                }
+            };
     public static String[] monthNames;
-    private static SimpleDateFormat ledgerDateFormatter =
-            new SimpleDateFormat("yyyy/MM/dd", Locale.US);
+    public static String developerEmail = "dam+mole-crash@ktnx.net";
     private static Pattern reLedgerDate =
             Pattern.compile("^(?:(\\d+)/)??(?:(\\d\\d?)/)?(\\d\\d?)$");
     public static void hideSoftKeyboard(Activity act) {
@@ -52,30 +57,48 @@ public final class Globals {
             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
         }
     }
-    public static Date parseLedgerDate(String dateString) throws ParseException {
+    public static SimpleDate parseLedgerDate(String dateString) throws ParseException {
         Matcher m = reLedgerDate.matcher(dateString);
-        if (!m.matches()) throw new ParseException(dateString, 0);
+        if (!m.matches()) throw new ParseException(
+                String.format("'%s' does not match expected pattern '%s'", dateString,
+                        reLedgerDate.toString()), 0);
 
-        String year = m.group(1);
-        String month = m.group(2);
-        String day = m.group(3);
+        String yearStr = m.group(1);
+        String monthStr = m.group(2);
+        String dayStr = m.group(3);
+
+        int year, month, day;
 
         String toParse;
-        if (year == null) {
-            Calendar now = Calendar.getInstance();
-            int thisYear = now.get(Calendar.YEAR);
-            if (month == null) {
-                int thisMonth = now.get(Calendar.MONTH) + 1;
-                toParse = String.format(Locale.US, "%04d/%02d/%s", thisYear, thisMonth, dateString);
+        if (yearStr == null) {
+            SimpleDate today = SimpleDate.today();
+            year = today.year;
+            if (monthStr == null) {
+                month = today.month;
             }
-            else toParse = String.format(Locale.US, "%04d/%s", thisYear, dateString);
+            else month = Integer.parseInt(monthStr);
+        }
+        else {
+            year = Integer.parseInt(yearStr);
+            assert monthStr != null;
+            month = Integer.parseInt(monthStr);
         }
-        else toParse = dateString;
 
-//        Log.d("globals", String.format("Parsing date '%s'", toParse));
-        return ledgerDateFormatter.parse(toParse);
+        assert dayStr != null;
+        day = Integer.parseInt(dayStr);
+
+        return new SimpleDate(year, month, day);
+    }
+    public static Calendar parseLedgerDateAsCalendar(String dateString) throws ParseException {
+        return parseLedgerDate(dateString).toCalendar();
+    }
+    public static SimpleDate parseIsoDate(String dateString) throws ParseException {
+        return SimpleDate.fromDate(isoDateFormatter.get().parse(dateString));
+    }
+    public static String formatLedgerDate(SimpleDate date) {
+        return dateFormatter.get().format(date.toDate());
     }
-    public static String formatLedgerDate(Date date) {
-        return ledgerDateFormatter.format(date);
+    public static String formatIsoDate(SimpleDate date) {
+        return isoDateFormatter.get().format(date.toDate());
     }
 }
\ No newline at end of file