#!/usr/bin/env raku use v6.d; #| convert 12-hour format time to 24-hour format and vice-versa unit sub MAIN ( Str $time, #= time (format: 05:15pm or "05:15 pm" or 17:00) ); grammar Time { token TOP { ':' ' '? ? } token hour { \d ** 1..2 } token minute { \d ** 1..2 } token meridiem { ['am'|'pm'] } } # Match for time format. if Time.parse($time) -> $m { given $m { when 'am' { printf "%02d:%02d\n", $m == 12 ?? "00" !! $m, $m; } when 'pm' { printf "%02d:%02d\n", $m < 12 ?? $m + 12 !! $m, $m; } # If neither 'am' nor 'pm' is set then $time must be in # 24-hour format. default { printf "%02d:%02d%s\n", $m == 0 ?? "12" !! $m > 12 ?? $m - 12 !! $m, $m, $m < 12 ?? "am" !! "pm"; } } } else { note "Wrong format!"; exit 1; }