Created
June 21, 2011 15:57
-
-
Save hedefalk/1038174 to your computer and use it in GitHub Desktop.
DateParsers.scala
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
trait DateParsers extends RegexParsers { | |
def dateTime(pattern: String): Parser[DateTime] = new Parser[DateTime] { | |
val dateFormat = DateTimeFormat.forPattern(pattern) | |
val dateParser = dateFormat.getParser | |
def jodaParse(text: CharSequence, offset: Int) = { | |
val mutableDateTime = new MutableDateTime | |
val newPos = dateFormat.parseInto(mutableDateTime, text, offset); | |
(mutableDateTime.toDateTime, newPos) | |
} | |
def apply(in: Input) = { | |
val source = in.source | |
val offset = in.offset | |
val start = handleWhiteSpace(source, offset) | |
val (dateTime, endPos) = jodaParse(source, start) | |
if (endPos >= 0) | |
Success(dateTime, in.drop(endPos - offset)) | |
else | |
Failure("Failed to parse date", in.drop(start - offset)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment