aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-100/frankivo/scala/FunTime.scala37
1 files changed, 23 insertions, 14 deletions
diff --git a/challenge-100/frankivo/scala/FunTime.scala b/challenge-100/frankivo/scala/FunTime.scala
index 2b06d901cb..6aa4d83229 100644
--- a/challenge-100/frankivo/scala/FunTime.scala
+++ b/challenge-100/frankivo/scala/FunTime.scala
@@ -1,35 +1,44 @@
object FunTime {
val examples = Seq[String](
+ "07:04am",
"08:24pm",
"08:24am",
"09:39 am",
"08:24",
"20:24",
"13:37",
- "12:34"
+ "12:34",
+ "18:04"
)
+ def getTime(time: String) : (Int, Int) = {
+ val h :: m :: _ = ("([0-9]{2})".r findAllIn time).toSeq
+ (h.toInt, m.toInt)
+ }
- def to12H(time: String): Option[String] = {
-
- None
+ def makeTime(hour: Int, minute: Int, suffix: String = "") = {
+ "%02d:%02d%s".format(hour, minute, " " + suffix)
}
- def to24H(time: String): Option[String] = {
- if (!("(am|pm)$".r findFirstIn time).isDefined)
- return None
-
- val h :: m :: _ = ("([0-9]{2})".r findAllIn time).toSeq
- val hour = if (time.contains("pm")) h.toInt + 12 else h
+ def to12H(time: String): String = {
+ time
+ }
+
+ def to24H(time: String): String = {
+ val parsed = getTime(time)
+ val hour = if (time.contains("pm")) parsed._1 + 12 else parsed._1
- Some(s"$hour:$m")
+ makeTime(hour, parsed._2)
}
def main(args: Array[String]): Unit = {
examples
- .map(e => Seq(to12H(e), to24H(e)))
- .flatten
- .flatten
+ .map(e => {
+ if (("(am|pm)$".r findFirstIn e).isDefined)
+ to24H(e)
+ else
+ to12H(e)
+ })
.foreach(println)
}
} \ No newline at end of file