#! /bin/sh
# mytime+ script.
# Tim Maher, tim@TeachMePerl.com, www.MinimalPerl.com
# Improvement to mytime, from p. 212 of Minimal Perl book.
# See also mytime2+, for a pure-Perl solution.
#
# Sample output from date: Thu Apr 6 16:12:05 PST 2006
# Index numbers for @F: 0 1 2 3 4 5
date |
perl -wnla -e '
$hms=$F[3]; # copy time field into named variable
($hour, $minute)=split /:/, $hms; # ignore $seconds
#=====> Convert military time to "civilian" time <=====#
$am_pm='AM';
$hour >= 12 and $am_pm='PM' # 12-23 are afternoon
$hour > 12 and $hour=$hour-12; # 13-23 ==> 1-11 (PM)
$hour == 0 and $hour=12; # convert first hour
$minute < 10 and $minute="0$minute"; # "5" ==> "05", etc.
print "The time is $hour:$minute $am_pm.";
'
# 12 1 2 3 4 5 6 7 8 9 10 11 / 12 01 02 03 04 05 06 07 08 09 10 11
# 00 1 2 3 4 5 6 7 8 9 10 11 / 12 13 14 15 16 17 18 19 20 21 22 23