view src/main/kotlin/name/blackcap/passman/Time.kt @ 28:287eadf5ab30 default tip

Check for timeouts inside subcommands while in interactive mode as well.
author David Barts <n5jrn@me.com>
date Wed, 31 Jul 2024 11:21:18 -0700
parents 711cc42e96d7
children
line wrap: on
line source

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
}