Skip to content

Instantly share code, notes, and snippets.

@hedefalk
Created June 21, 2011 15:57
Show Gist options
  • Save hedefalk/1038174 to your computer and use it in GitHub Desktop.
Save hedefalk/1038174 to your computer and use it in GitHub Desktop.
DateParsers.scala
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