Skip to content

Instantly share code, notes, and snippets.

@tachesimazzoca
Last active October 10, 2015 02:17
Show Gist options
  • Save tachesimazzoca/3616882 to your computer and use it in GitHub Desktop.
Save tachesimazzoca/3616882 to your computer and use it in GitHub Desktop.
Parse a time string #scala
val timeParser: (String => (String, String)) = { str =>
val ptn = """^(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9])$""".r
ptn findFirstIn str match {
case Some(ptn(h, m)) => ("%02d".format(h.toInt), "%02d".format(m.toInt))
case None => ("--", "--")
}
}
Array("01:23", "1:23", "12:34", "23:60", "25:00") foreach { str =>
val (hour, min) = timeParser(str)
println("%s:%s".format(hour, min))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment