这是一个基于简单 Perl 脚本的解决方案,该脚本源自Convert 12-hour date/time to 24-hour date/time的答案。
资源s12.pl
#!/usr/bin/env perl
use strict;
use warnings;
sub time_12h_to_24h
{
my($t12) = @_;
my($hh,$mm,$ampm) = $t12 =~ m/^(\d\d?):(\d\d?)\s*([AP]M?)/i;
$hh = ($hh % 12) + (($ampm =~ m/AM?/i) ? 0 : 12);
return sprintf("%.2d:%.2d", $hh, $mm);
}
while (<>)
{
my($time_12h, $entry) = split / - /;
my $time_24h = time_12h_to_24h($time_12h);
print "$time_24h $time_12h - $entry";
}
请注意,该代码同时接受 { AM
, PM
} 和 { A
, P
} 并且在 AM/PM 指示符的大写和小写之间保持中性,并忽略时间和 AM/PM 指示符之间的空格。
输入数据
该数据集包含 12:05A 和 12:05P 的行以及来自问题的数据。
03:00P - Doctor appointment.
07:00P - Scheduled entry.
10:30A - Another entry.
11:00A - Daytime medication is due.
11:00P - Nighttime medication is due.
11:30P - Staff meeting.
12:05A - Just past midnight and long before 11:00A.
12:05P - Just past midday and long before 11:00P.
双滤波输出
$ perl s12.pl data | sort | sed 's/^..:.. //'
12:05A - Just past midnight and long before 11:00A.
10:30A - Another entry.
11:00A - Daytime medication is due.
12:05P - Just past midday and long before 11:00P.
03:00P - Doctor appointment.
07:00P - Scheduled entry.
11:00P - Nighttime medication is due.
11:30P - Staff meeting.
$
未过滤的输出
$ perl s12.pl data | sort
00:05 12:05A - Just past midnight and long before 11:00A.
10:30 10:30A - Another entry.
11:00 11:00A - Daytime medication is due.
12:05 12:05P - Just past midday and long before 11:00P.
15:00 03:00P - Doctor appointment.
19:00 07:00P - Scheduled entry.
23:00 11:00P - Nighttime medication is due.
23:30 11:30P - Staff meeting.
$
请注意,通过在输出中首先放置键列(24 小时时间列),sort
可以简化命令(并且通常也会加快排序速度)。