使用 Java.time 将日期时间转换为纪元时如何处理边缘情况

how to handle edge case when using Java.time to convert date time to epoch

这是我将日期时间转换为纪元时间戳的代码,有一种情况我不知道如何处理,如果区域信息包含在时间戳中并且没有作为第三个参数传递给此函数,无论时间戳中是什么时区,它总是会转换为 UTC,如何解决这个问题?

  time_to_epoch(List(JString("2007-12-03T10:15:30 CET"), JString("yyyy-MM-dd'T'HH:mm:ss z"))) shouldEqual JString("1196669730000")

time_to_epoch(列表(JString("2007-12-03T10:15:30+02:00"), JString("yyyy-MM-dd'T'HH:mm:ssXXX"), JString("CEST")) ) 应该等于 JString("1196669730000") time_to_epoch(List(JString("2007-12-03T10:15:30 CEST"), JString("yyyy-MM-dd'T'HH:mm:ss z"))) shouldEqual JString("1196673330000")

 whatever timezone in the timestamp, the result is wrong, i.e, CEST(+02:00), output (+01:00), America/Mexico_City(+05:00), output (+04:00)

如果时区数据可以成为 timestamp 字符串的一部分,那么无论是否默认提供,您都应该让它覆盖 timezone 字符串。

formatter.parseBest(timestamp, ZonedDateTime.from _
                             , LocalDateTime.from _
                             , LocalDate.from _) match {
  case zdt: ZonedDateTime =>
    JString(zdt.toInstant().toEpochMilli.toString)
  case ldt: LocalDateTime =>
    . . .