Ellen Spertus01/28/2020, 1:26 AM
// 1 val h = hour.toInt() + if (meridian.isNotEmpty() && meridian == 'p') 12 else 0
//2 var h = hour.toInt() if (meridian.isNotEmpty() && meridian == 'p') h += 12
Czar01/28/2020, 5:54 PM
val h = hour.toInt() + meridian.toOffset() fun Meridian.toOffset() = if(firstOrNull() == 'p') 12 else 0
is actually a
make sure that the extension function is private, better yet I suggest using a wrapper class instead of
for better typesafety.
Ellen Spertus01/28/2020, 6:04 PM
(since renamed to
) is returned from
. It’s for either AM or PM.
Czar01/28/2020, 6:28 PM
would be my favourite. That said, midnight can be represented as 12 AM, and noon as 12 PM so this algorithm will return 12 for midnight and 24 for noon, I somehow doubt that that is the desired behaviour...
val h = hour.toInt() + if (period.firstOrNull() == 'P') 12 else 0
Ellen Spertus01/28/2020, 6:30 PM
before adding. Technically, 12 AM and 12 PM don’t exist and should be 12 midnight and 12 noon (respectively?). I’m planning to have a separate regex.
h < 12
Czar01/28/2020, 6:32 PM
Ellen Spertus01/28/2020, 6:32 PM
Czar01/28/2020, 6:34 PM
Ellen Spertus01/28/2020, 6:35 PM
Czar01/28/2020, 6:36 PM
Ellen Spertus01/28/2020, 6:49 PM