com.gu.identity.model.circe.DateTimeUtils.scala Maven / Gradle / Ivy
package com.gu.identity.model.circe
import org.joda.time.{DateTime, DateTimeZone}
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter}
import scala.util.control.NonFatal
private[circe] object DateTimeUtils {
// Uses same serialisation format as com.gu.identity.modelDateTimeSerializer.
// This is the serialisation format of date times stored in users table.
val dateTimeFormatter: DateTimeFormatter =
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC)
// It has become apparent that some date times in the users table are stored in milliseconds.
val dateTimeFormatter2: DateTimeFormatter =
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(DateTimeZone.UTC)
// Accounts for the fact that we use 2 different formats to store date times in the users table.
def parseDateTime(dateTime: String): DateTime = {
try {
dateTimeFormatter.parseDateTime(dateTime)
} catch {
case NonFatal(_) => dateTimeFormatter2.parseDateTime(dateTime)
}
}
}