Skip to content

Instantly share code, notes, and snippets.

@KirinDave
Created December 30, 2010 22:06
Show Gist options
  • Select an option

  • Save KirinDave/760375 to your computer and use it in GitHub Desktop.

Select an option

Save KirinDave/760375 to your computer and use it in GitHub Desktop.
package com.banksimple.util
import org.scala_tools.time.Imports._
import org.joda.time.format.{DateTimeFormat,DateTimeFormatter}
class SafeDateTimeFormatter(val self: DateTimeFormatter) extends Proxy {
def safelyParseDateTime(input: String): Option[DateTime] =
try Some(self.parseDateTime(input))
catch { case _ => None }
}
object DateTimeImports {
implicit def dtf2SafeDtf(in: DateTimeFormatter) = new SafeDateTimeFormatter(in)
}
object DateTimeUnionParser {
def apply(patterns: String*)(input: String): Option[DateTime] = {
import DateTimeImports._
val matchers = patterns.map({DateTimeFormat.forPattern(_)})
matchers.toStream.flatMap(_.safelyParseDateTime(input)).headOption
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment