Run2 Scala Datetime Handling

Robust temporal tokenization with Java 8 Time API and DateTimeFormatter.

cxcscmu Updated

File contents

Temporal Tokenization in Scala

Use java.time for all date/time operations.

Robust Formatting

Handle LocalDateTime, LocalDate, ZonedDateTime, and Instant.

import java.time.{LocalDate, LocalDateTime, ZonedDateTime, Instant}
import java.time.format.DateTimeFormatter

def formatTemporal(value: Any, fmtStr: Option[String]): String = {
  val isoFormat = DateTimeFormatter.ISO_LOCAL_DATE_TIME
  val dateFormat = DateTimeFormatter.ISO_LOCAL_DATE
  
  value match {
    case dt: LocalDateTime => 
      val fmt = fmtStr.map(DateTimeFormatter.ofPattern).getOrElse(isoFormat)
      dt.format(fmt)
    case d: LocalDate =>
      val fmt = fmtStr.map(DateTimeFormatter.ofPattern).getOrElse(dateFormat)
      d.format(fmt)
    case other => other.toString
  }
}

Pattern Matching on Any

Useful for implementing UniversalTokenizer's dispatch logic.

cxcscmu/SkillLearnBench commit 49c6fcfce2

Frequently asked questions

npx get-skill add cxcscmu/run2-scala-datetime-handling