Ellen Spertus
01/28/2020, 1:26 AM// 1
val h = hour.toInt() + if (meridian.isNotEmpty() && meridian[0] == 'p') 12 else 0
or
//2
var h = hour.toInt()
if (meridian.isNotEmpty() && meridian[0] == 'p') h += 12
Czar
01/28/2020, 5:54 PMval h = hour.toInt() + meridian.toOffset()
fun Meridian.toOffset() = if(firstOrNull() == 'p') 12 else 0
If Meridian
is actually a String
make sure that the extension function is private, better yet I suggest using a wrapper class instead of String
for better typesafety.Ellen Spertus
01/28/2020, 6:04 PMmeridian
(since renamed to period
) is returned from MatchResult.destructured
. It’s for either AM or PM.Czar
01/28/2020, 6:28 PMval h = hour.toInt() + if (period.firstOrNull() == 'P') 12 else 0
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...Czar
01/28/2020, 6:30 PMEllen Spertus
01/28/2020, 6:30 PMh < 12
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.Czar
01/28/2020, 6:32 PMEllen Spertus
01/28/2020, 6:32 PMCzar
01/28/2020, 6:34 PMEllen Spertus
01/28/2020, 6:35 PMCzar
01/28/2020, 6:36 PMEllen Spertus
01/28/2020, 6:49 PM