diff src/main/kotlin/name/blackcap/passman/Time.kt @ 6:711cc42e96d7

Got the list subcommand working, but needs efficiency improvements.
author David Barts <n5jrn@me.com>
date Tue, 20 Sep 2022 20:52:21 -0700
parents a6cfdffcaa94
children
line wrap: on
line diff
--- a/src/main/kotlin/name/blackcap/passman/Time.kt	Sun Sep 11 21:29:20 2022 -0700
+++ b/src/main/kotlin/name/blackcap/passman/Time.kt	Tue Sep 20 20:52:21 2022 -0700
@@ -1,5 +1,25 @@
 package name.blackcap.passman
 
+import java.text.ParseException
 import java.text.SimpleDateFormat
 
 val ISO8601 = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
+private val PARSERS = arrayOf<SimpleDateFormat>(
+    SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").apply { isLenient = false },
+    SimpleDateFormat("yyyy-MM-dd'T'HH:mm").apply { isLenient = false },
+    SimpleDateFormat("yyyy-MM-dd").apply { isLenient = false },
+)
+
+fun parseDateTime(unparsed: String): Long? {
+    for (parser in PARSERS) {
+        val ret = try {
+            parser.parse(unparsed)
+        } catch (e: ParseException) {
+            null
+        }
+        if (ret != null) {
+            return ret.time
+        }
+    }
+    return null
+}