Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
947 B · 30 lines
Kotlin
at mistress
12345678910111213141516171819202122232425262728293031package com.rtnbleserver
import java.text.ParsePositionimport java.text.SimpleDateFormatimport java.util.Dateimport java.util.Localeimport java.util.TimeZone
/** * ISO 8601 / RFC 3339 timestamps, matching the atproto `datetime` format. * `java.time` is avoided because it requires API 26 (or desugaring) and the * library supports API 24. */internal object IsoDatetime { private val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US).apply { timeZone = TimeZone.getTimeZone("UTC") isLenient = false }
fun format(epochMillis: Long): String = synchronized(format) { format.format(Date(epochMillis)) }
/** returns [value] when it is a valid timestamp, otherwise `null`. */ fun parse(value: String): String? = synchronized(format) { val position = ParsePosition(0) val parsed = format.parse(value, position) if (parsed == null || position.index != value.length) null else value }}