package com.rtnbleserver import java.text.ParsePosition import java.text.SimpleDateFormat import java.util.Date import java.util.Locale import 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 } }