Skip to content

Commit

Permalink
minor fixes, fix dateParser
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkaczor committed Jun 20, 2022
1 parent 468594a commit 1e5e642
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
Expand Up @@ -2,7 +2,6 @@ package br.com.virsox.scalexpr

import br.com.virsox.scalexpr.Context.{ContextLike, EmptyContext}

import scala.math.Ordering
import scala.reflect.runtime.universe._

/** *
Expand Down
20 changes: 13 additions & 7 deletions src/main/scala/br/com/virsox/scalexpr/DateParser.scala
Expand Up @@ -49,22 +49,28 @@ trait DateParser {
)

// months with 31 days
val months31 = Seq(1, 3, 5, 7, 8, 10, 12)
val months31: Seq[Int] = Seq(1, 3, 5, 7, 8, 10, 12)

// months with 30 days
val months30 = Seq(4, 6, 9, 11)
val months30: Seq[Int] = Seq(4, 6, 9, 11)

// parses a date and validates it
def dateParser[_: P]: P[String] =
P(yearParser.! ~ "-" ~ monthParser.! ~ "-" ~ dayParser.!)
.map { case (year, month, day) => (year.toInt, month.toInt, day.toInt) }
.filter {
case (year, month, day) =>
if (months31.contains(month) && day > 31) false
else if (months30.contains(month) && day > 30) false
else if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) && day > 29) false // leap year
else if (day > 28) false
true
if (months31.contains(month) && day > 31) {
false
} else if (months30.contains(month) && day > 30) {
false
} else if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) && day > 29) {
false // leap year
} else if (day > 28) {
false
} else {
true
}
}
.!

Expand Down
1 change: 0 additions & 1 deletion src/main/scala/br/com/virsox/scalexpr/Expression.scala
Expand Up @@ -4,7 +4,6 @@ import java.time.Instant

import br.com.virsox.scalexpr.Context.{ContextLike, EmptyContext}

import scala.math.Ordering
import scala.reflect.runtime.universe
import scala.reflect.runtime.universe._

Expand Down

0 comments on commit 1e5e642

Please sign in to comment.