All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avsystem.commons.serialization.IsoInstant.scala Maven / Gradle / Ivy

There is a newer version: 2.22.0
Show newest version
package com.avsystem.commons
package serialization

import java.time.Instant
import java.time.format.DateTimeParseException

import com.avsystem.commons.serialization.GenCodec.ReadFailure

object IsoInstant {
  def format(millis: Long): String = {
    val res = Instant.ofEpochMilli(millis).toString
    // add trailing .000Z if omitted to align with JS implementation
    if (res.charAt(res.length - 5) == '.') res
    else res.substring(0, res.length - 1) + ".000Z"
  }

  def parse(string: String): Long =
    try Instant.parse(string).toEpochMilli catch {
      case _: DateTimeParseException => throw new ReadFailure(s"invalid ISO instant: $string")
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy