diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-20 19:04:32 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-02-20 19:04:32 +0000 |
| commit | 451d55664ac490da377700f186df02af7ea76926 (patch) | |
| tree | 3f098f1c71b869a02fd3c9517de820f2ac97c751 | |
| parent | a3934a1969171025ec67589e34d74248a1360ee4 (diff) | |
| download | perlweeklychallenge-club-451d55664ac490da377700f186df02af7ea76926.tar.gz perlweeklychallenge-club-451d55664ac490da377700f186df02af7ea76926.tar.bz2 perlweeklychallenge-club-451d55664ac490da377700f186df02af7ea76926.zip | |
- Added Awk solution by Pete Houston.
| -rw-r--r-- | challenge-100/pete-houston/awk/ch-1.awk | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-100/pete-houston/awk/ch-1.awk b/challenge-100/pete-houston/awk/ch-1.awk new file mode 100644 index 0000000000..d66d687af2 --- /dev/null +++ b/challenge-100/pete-houston/awk/ch-1.awk @@ -0,0 +1,51 @@ +#!/usr/bin/gawk -f
+#===============================================================================
+#
+# FILE: 10001.awk
+#
+# USAGE: ./10001.awk < INFILE
+#
+# DESCRIPTION: Analyses each line of standard input to determine if it is
+# 12 or 24 hour clock and converts to the other.
+#
+# REQUIREMENTS: A gawk-compatible awk
+# NOTES: The leading zero pad comes from the spec examples.
+# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
+# ORGANIZATION: Openstrike
+# VERSION: 1.0
+# CREATED: 19/02/21
+#===============================================================================
+
+/[01][0-9]:[0-5][0-9]\s*[ap]m/ {
+ match ($0, /([01][0-9])(:[0-5][0-9])\s*([ap]m)/, a)
+ hh = a[1]
+ if (a[3] == "am") {
+ if (hh == "12") {
+ hh = "00"
+ }
+ } else if (hh < 12) {
+ hh += 12
+ }
+ print hh a[2]
+ next
+}
+
+/[012][0-9]:[0-5][0-9]/ {
+ match ($0, /([012][0-9])(:[0-5][0-9])/, a)
+ hh = a[1]
+ if (hh > 11) {
+ post = "pm"
+ } else {
+ post = "am"
+ }
+ if (hh > 12) {
+ hh = hh % 12
+ }
+ if (hh < 1) {
+ hh = 12
+ }
+ print hh a[2] post
+ next
+}
+
+{ next }
|
