aboutsummaryrefslogtreecommitdiff
path: root/challenge-100/pete-houston/awk/ch-1.awk
blob: d66d687af28da5169a417fbac853ca1d20262c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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 }