aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-16 17:21:49 +0000
committerGitHub <noreply@github.com>2021-02-16 17:21:49 +0000
commit30becae3485bacfdd52cef0697cb3cae92ef815b (patch)
tree173fe226b4aef3e34e6ba48ab60723132546fa50
parentfb3e826100dc7388e6d6cd04ac61555db881d02b (diff)
parentdb3d3298e10b3dcf63bf1e0f940e2f69af95eb38 (diff)
downloadperlweeklychallenge-club-30becae3485bacfdd52cef0697cb3cae92ef815b.tar.gz
perlweeklychallenge-club-30becae3485bacfdd52cef0697cb3cae92ef815b.tar.bz2
perlweeklychallenge-club-30becae3485bacfdd52cef0697cb3cae92ef815b.zip
Merge pull request #3541 from frankivo/frankivo-funtime
FunTime
-rw-r--r--challenge-100/frankivo/scala/FunTime.scala54
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-100/frankivo/scala/FunTime.scala b/challenge-100/frankivo/scala/FunTime.scala
new file mode 100644
index 0000000000..97ae8f09a5
--- /dev/null
+++ b/challenge-100/frankivo/scala/FunTime.scala
@@ -0,0 +1,54 @@
+object FunTime {
+ val examples = Seq[String](
+ "07:04am",
+ "08:24pm",
+ "08:24am",
+ "09:39 am",
+ "08:24",
+ "11:59",
+ "12:00",
+ "12:01",
+ "12:34",
+ "13:37",
+ "20:24",
+ "18:04"
+ )
+
+ def getTime(time: String) : (Int, Int) = {
+ val h :: m :: _ = ("([0-9]{2})".r findAllIn time).toSeq
+ (h.toInt, m.toInt)
+ }
+
+ def makeTime(hour: Int, minute: Int, suffix: String = "") = {
+ "%02d:%02d%s".format(hour, minute, " " + suffix)
+ }
+
+ def to12H(time: String): String = {
+ val parsed = getTime(time)
+
+ val suffix = if (parsed._1 < 12) "am" else "pm"
+ val hour = if (parsed._1 > 12) parsed._1 - 12 else parsed._1
+
+ makeTime(hour, parsed._2, suffix)
+ }
+
+ def to24H(time: String): String = {
+ val parsed = getTime(time)
+ val hour = if (time.contains("pm")) parsed._1 + 12 else parsed._1
+
+ makeTime(hour, parsed._2)
+ }
+
+ def convert(time: String) : String = {
+ if (("(am|pm)$".r findFirstIn time).isDefined)
+ to24H(time)
+ else
+ to12H(time)
+ }
+
+ def main(args: Array[String]): Unit = {
+ examples
+ .map(convert)
+ .foreach(println)
+ }
+} \ No newline at end of file