Created
December 30, 2010 22:06
-
-
Save KirinDave/760375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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