1#!/usr/bin/mawk -We
2
3# edit the above to be the full pathname of 'mawk'
4# @(#) hcal - v01.00.02 - Tue Feb 27 21:21:21 EST 1996
5# @(#) prints a 3-month (highlighted) calendar centered on the target month
6# @(#) may be edited for week to start with Sun or Mon & for local language
7# @(#) to display a usage screen, execute: hcal -h
8# NOTE: to edit, set ts=4 in 'vi' (or equivalent)
9#		to print, pipe through 'pr -t -e4'
10
11# Using ideas from a KornShell script by Mikhail Kuperblum (mikhail@klm.com)
12# Bob Stockler - bob@trebor.iglou.com - Sysop CompuServe SCOForum [75162,1612]
13
14BEGIN {
15# Local Edits:
16	PROG	 = "hcal"		# Program name given to this script
17#	FMT 	 = 0			# date format dd/mm/yyyy
18#	FMT1 	 = 0			# for weekdays ordered "Mo Tu We Th Fr Sa Su"
19	FMT 	 = 1			# date format mm/dd/yyyy
20	FMT1 	 = 1 			# for weekdays ordered "Su Mo Tu We Th Fr Sa"
21# edit day & month names and abbreviations for local language names
22	Days[0]	 = "Mo Tu We Th Fr Sa Su"
23	Days[1]	 = "Su Mo Tu We Th Fr Sa"
24	MONTHS	 = "January February March April May June July August"
25	MONTHS	 = MONTHS " September October November December"
26	Months	 = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
27#	STDOUT	 = 0			# emulate SCO Unix 'cal' (NO highlighting)
28	STDOUT	 = 1			# default to highlight mode
29	MINUS	 = "-"          # possible input date field delimiter
30	SLASH	 = "/"          # possible input date field delimiter
31	DOT 	 = "."          # possible input date field delimiter
32	IDFD	 = "[" MINUS    # make MINUS the first character in this series
33	IDFD	 = IDFD SLASH   #		so that it stands for itself in the RE
34	IDFD	 = IDFD DOT "]" # Input Date Field Delimiters RE
35	ODFD	 = SLASH		# Output Date Field Delimiter (default)
36	DATE_FMT = "%.2d%s%.2d%s%.4d"	# date format
37## this script presumes 'date' recognizes these arguments in these ways:
38##	w - Day of the week - Sunday = 0
39##	m - Month of year - 01 to 12
40##	d - Day of month - 01 to 31
41##	y   Last 2 digits of year - 00 to 99
42##	Y - Year (including century), as decimal numbers
43##	j - Day of the year - 001 to 366 (Julian date)
44##	T - Time as HH:MM:SS
45##	X   Current time, as defined by the locale
46##	a - Abbreviated weekday - Sun to Sat
47##	b - Abbreviated month name
48##	Z - Timezone name, or no characters if no timezone exists
49##	Command to get today's date information:
50##	DATE = "/bin/date '+%w %m %d 19%y %j~%a %b %d %T %Z 19%y'"
51##      For sunos4
52##	DATE = DATE = "/bin/date '+%w %m %d 19%y %j~%a %h %d %T    19%y'"
53	DATE = "/bin/date '+%w %m %d %Y %j~%a %b %d %X %Z %Y'"
54# End of Local Edits
55
56	INT_RE   = "^[0-9]+$"					# unsigned integer RE
57	S_INT_RE = "^[-+][0-9]+$"				# signed integer RE
58	MNAM_RE	 = "^[A-Za-z]+$"				# month name RE
59	YEAR_RE	 = "^[0-9]?[0-9]?[0-9]?[0-9]$"
60	DATE_RE  = "^[0-9]?[0-9]" IDFD "[0-9]?[0-9]" IDFD "[0-9]?[0-9]?[0-9]?[0-9]$"
61	DAT1_RE  = "^[0-9]?[0-9]" IDFD "[0-9]?[0-9]$"
62
63	split(Months,M_Name)
64	split("31 28 31 30 31 30 31 31 30 31 30 31",Mdays) ; Mdays[0] = 0
65
66	NUM_ARGS = ARGC - 1
67	if ( ARGV[1] == "-x" ) {
68		# standout mode switch
69		if ( STDOUT == 1 ) STDOUT = 0 ; else STDOUT = 1
70		ARG1 = ARGV[2] ; ARG2 = ARGV[3] ; NUM_ARGS -= 1
71	}
72	else if ( ARGV[1] ~ /^-[h?]$/ ) { HELP = 1 ; exit }
73	else { ARG1 = ARGV[1] ; ARG2 = ARGV[2] }
74
75	if ( STDOUT == 1 ) {
76		# get the terminal standout-start & standout-end control codes
77		so = ENVIRON["so"] ; if ( ! so ) "tput smso" | getline so
78		se = ENVIRON["se"] ; if ( ! se ) "tput rmso" | getline se
79	}
80
81	if ( NUM_ARGS == 0 ) {
82		# no arguments - print a calendar display centered on today
83		DEFAULT = 1
84	}
85	else if ( NUM_ARGS == 1 ) {
86		# one argument - may be a month name, date, year, or interval of days
87		if		( ARG1 ~ DATE_RE  ) DATE1 = Fmt_Date(ARG1)
88		else if ( ARG1 ~ DAT1_RE  ) DATE1 = ARG1
89		else if ( ARG1 ~ MNAM_RE  ) { Get_Mnum() ; DATE1 = RMSO = ARG1 "/1" }
90		else if	( ARG1 ~ S_INT_RE ) INTERVAL = ARG1 + 0
91		else if	( ARG1 ~ INT_RE   ) {
92			if ( ARG1 > 0 && ARG1 <= 9999 ) YEAR = ARG1 + 0
93			else if ( ARG1 > 9999 ) { ERR = 9 ; exit }
94			else { ERR = 7 ; exit }
95		}
96		else	{ ERR = 1 ; exit }
97	}
98	else if ( NUM_ARGS == 2 ) {
99		# two arguments, the second of which must be an integer
100		if ( ARG2 ~ INT_RE ) {
101			ARG2 = ARG2 + 0
102			if  ( ARG2 < 1 ) { ERR = 7 ; exit }
103			else if ( ARG2 > 9999 ) { ERR = 9 ; exit }
104		}
105		else { ERR = 1 ; exit }
106		RMSO = 1
107		# the first may be a string or an integer
108		if ( ARG1 ~ INT_RE ) {
109			# a month number and a year
110			if ( ARG1 < 1 || ARG1 > 12 ) { ERR = 4 ; mm = ARG1 ; exit }
111		}
112		else if ( ARG1 ~ MNAM_RE ) {
113			Get_Mnum()
114		}
115		else { ERR = 6 ; exit }
116		DATE1 = ARG1 "/1/" ARG2
117	}
118	else { ERR = 2 ; exit }
119
120	if		( DEFAULT  ) { Get_Now() }
121	else if ( INTERVAL ) {
122		Get_Now()
123		daynum = daynum + ( INTERVAL % 7 )
124		this_date = ""
125		DATE1 = Get_Date(INTERVAL,m,d,y,j)
126		split(DATE1,mdy,IDFD)
127		Mon[2] = mdy[1] + 0
128		today  = mdy[2] + 0
129		Year[1] = Year[2] = Year[3] = mdy[3] + 0
130	}
131	else if ( DATE1 ) {
132		Get_Now()
133		if ( split(DATE1,mdy,IDFD) == 2 ) DATE1 = DATE1 "/" This_Year
134		Chk_Date(DATE1)
135		Mon[2] = mdy[1] + 0
136		today  = mdy[2] + 0
137		Year[1] = Year[2] = Year[3] = mdy[3] + 0
138		DATE1 = sprintf( "%.2d/%.2d/%.4d", Mon[2], today, Year[2] )
139		INTERVAL = Get_Num(DATE1,m,d,y,j)
140		daynum = daynum + ( INTERVAL % 7 )
141		this_date = ""
142	}
143	else if ( YEAR ) {
144		so = se = ""
145		Get_Now()
146		Mon[2] = 2
147		today = 1
148		Year[1] = Year[2] = Year[3] = YEAR
149		DATE1 = sprintf( "%.2d/%.2d/%.4d", Mon[2], today, Year[2] )
150		INTERVAL = Get_Num(DATE1,m,d,y,j)
151		daynum = daynum + ( INTERVAL % 7 )
152		this_date = ""
153	}
154	else { ERR = 5 ; exit }
155
156	if		( Mon[2] !=  1 ) Mon[1] = Mon[2] - 1
157	else	{ Mon[1] = 12 ; Year[1] -= 1 }
158	if		( Mon[2] != 12 ) Mon[3] = Mon[2] + 1
159	else	{ Mon[3] =  1 ; Year[3] += 1 }
160	if		( Mon[1] == 2 ) Leap(Year[1])
161	else if	( Mon[2] == 2 ) Leap(Year[2])
162	else if	( Mon[3] == 2 ) Leap(Year[3])
163
164	Start[2] = 7 - ( ( today - daynum ) % 7 )
165	Start[1] = 7 - ( ( Mdays[Mon[1]] - Start[2] ) % 7 )
166	Start[3] =       ( Mdays[Mon[2]] + Start[2] ) % 7
167
168	if ( ! YEAR ) quarters = 1
169	else {
170		quarters = 4 ; s[3] = Start[3]
171		for (i=4;i<=12;i++) { s[i] = ( Mdays[i-1] + s[i-1] ) % 7 }
172	}
173	for ( quarter = 1 ; quarter <= quarters ; quarter++ ) {
174		if ( quarter > 1 ) {
175			delete cal
176			ll = 0 ;  Mon[1] += 3 ; Mon[2] += 3 ; Mon[3] += 3
177			Start[1] = s[Mon[1]] ; Start[2] = s[Mon[2]] ; Start[3] = s[Mon[3]]
178		}
179		if ( Year[2] == 1752 && Mon[2] ~ /8|9|10/ ) Kludge_1752()
180		if ( ARG1 ) print "" ; else printf( "\n%s\n\n", this_date )
181		for (i=1;i<=3;i++) { while ( Start[i] >= 7 ) Start[i] -= 7 }
182		for (mm=1;mm<=3;mm++) { l = 1
183			if ( mm != 2 ) { So = Se = "" } else { So = so ; Se = se }
184			cal[mm SUBSEP l++] = sprintf( "%s      %-4s%.4d      %s   ", \
185				So, M_Name[Mon[mm]], Year[mm], Se )
186			cal[mm SUBSEP l++] = sprintf( "%s%3s", Days[FMT1], "" )
187			j = k = 1
188			while ( j <= Mdays[Mon[mm]] ) {
189	  			line = ""
190	  			for (i=1;i<=7;i++) {
191					if ( Start[mm] > 0 || j > Mdays[Mon[mm]] ) {
192						date = "" ; Start[mm]-- }
193					else date = j++
194					if ( Year[mm] == 1752 && Mon[mm] == 9 && date == 3 ) {
195						date = 14 ; j = 15 }
196					if ( date == today && mm == 2 && ! RMSO ) {
197						So = so ; Se = se }
198					else { So = Se = "" }
199					line = sprintf( "%s%s%2s%s ", line, So, date, Se )
200	  			}
201				cal[mm SUBSEP l++] = sprintf( "%s  ", line )
202			}
203			if ( l > ll ) ll = l
204		}
205		for (l=1;l<ll;l++) {
206			for (mm=1;mm<=3;mm++) {
207				if ( cal[mm SUBSEP l] != "" ) printf( cal[mm SUBSEP l] )
208				else printf( "%23s", "" )
209				if ( mm % 3 == 0 ) print ""
210			}
211		}
212	}
213	print
214	exit 0
215}
216END {
217	if ( ! HELP && ! ERR ) exit 0
218	if ( ERR ) {
219		for (i=1;i<ARGC;i++) { ARGS = ARGS sp ARGV[i] ; sp = " " }
220		if		( ERR == 1 ) msg = "Bad argument format or content:"
221		else if	( ERR == 2 ) msg = "Wrong argument count (" ARGC - 1 "):"
222		else if ( ERR == 3 ) msg = "Date before 01/01/0001 (" Get_Y1() "):"
223		else if ( ERR == 4 ) msg = "Bad month (" mm "):"
224		else if ( ERR == 5 ) msg = "Bad date (" dd "):"
225		else if ( ERR == 6 ) msg = "Impossible date:"
226		else if ( ERR == 7 ) msg = "The was no year 0000:"
227		else if ( ERR == 8 ) msg = "Non-unique month name (" ARG1 "):"
228		else if ( ERR == 9 ) msg = "Year greater than 9999:"
229		else				 msg = "Unknown error:"
230		HELP = 1 ; q = "\""
231	}
232	if ( HELP ) {
233	  if ( FMT == 1 )	{ fmt = "[m]m/[d]d[/yyyy]" ; range = "12/31/9999" }
234	  else				{ fmt = "[d]d/[m]m[/yyyy]" ; range = "31/12/9999" }
235	  if ( STDOUT == 0 ) n = " not " ; else n = " " ; sp = "    "
236	  u=  "\n    Usage:  " PROG " [-x] [args]\n\n" sp
237	  u=u "Prints a 3-month calendar centered on the target month.\n\n" sp
238	  u=u "The default is" n "to highlight the target month (and date if\n" sp
239	  u=u "appropriate);  -x switches the default highlight behavior.\n" sp
240	  u=u "With no arguments the target date defaults to today.\n\n" sp
241	  u=u "Arguments may be a date in " fmt " format (where\n" sp
242	  u=u "leading 0's are implied in all unfilled fields, defaulting\n" sp
243	  u=u "to the current year if that field is omitted); a month and\n" sp
244	  u=u "year (where month may be an integer or a month name, perhaps\n" sp
245	  u=u "abbreviated, but sufficient to be unique); or the month name\n" sp
246	  u=u "alone (which defaults to the current year); or a year alone\n" sp
247	  u=u "(which prints a 12-month calendar for that year).\n\n" sp
248	  u=u "A signed integer alone as an argument specifies the target\n" sp
249	  u=u "date to be -n days before or +n days after the current date.\n\n" sp
250	  u=u "Dates from 01/01/0001 to " range " are supported.\n"
251	  usage = u
252	}
253	printf( "\n%s:    %s %s%s%s\n", PROG, msg, q, ARGS, q ) >"/dev/tty"
254	print usage >"/dev/tty"
255	exit ERR
256}
257
258function Get_Now() {
259	# get the week, month, date & year numbers and the time-of-day
260	DATE | getline date
261	split(date,Date,"~")
262	split(Date[1],field)
263	daynum = field[1] + FMT1
264	m = field[2] ; This_Mon	= Mon[2] = m + 0
265	d = field[3] ; This_Date = today = d + 0
266	y = This_Year = Year[1] = Year[2] = Year[3] = field[4]
267	j = julian = field[5] + 0
268	this_date = Date[2]
269}
270
271function Fmt_Date(date) {
272    # format dates as mm/dd/yyyy or dd/mm/yyyy
273    split(date,MorD_DorM_Y,IDFD)
274    if ( FMT == 1 )	{ Dt_Fld1 = MorD_DorM_Y[1] ; Dt_Fld2 = MorD_DorM_Y[2] }
275    else			{ Dt_Fld1 = MorD_DorM_Y[2] ; Dt_Fld2 = MorD_DorM_Y[1] }
276    Dt_Fld3 = MorD_DorM_Y[3]
277    return sprintf( DATE_FMT, Dt_Fld1, ODFD, Dt_Fld2, ODFD, Dt_Fld3 )
278}
279
280function Kludge_1752() {
281	# kludge for September 1752 & the change to the Gregorian Calendar
282	Mdays[9] = 30
283	if		( Mon[2] ==  9 ) {
284		Start[1] = Start[2] = 1 + FMT1 ; Start[3] = -1 + FMT1
285	}
286	else if	( Mon[2] ==  8 ) {
287		Start[1] =  2 + FMT1 ; Start[2] =  5 + FMT1 ; Start[3] =  1 + FMT1
288	}
289	else if	( Mon[2] == 10 ) {
290		Start[1] =  1 + FMT1 ; Start[2] = -1 + FMT1 ; Start[3] =  3
291	}
292}
293
294function Get_Mnum() {
295			ARG1 = tolower(ARG1)
296			months = tolower(MONTHS)
297			split(months,month)
298			for (i=1;i<=12;i++) {
299				if ( index(month[i],ARG1) == 1 ) { ARG = i ; n++ }
300			}
301			if ( n == 1 ) ARG1 = ARG
302			else if ( n == 0 ) { ERR = 1 ; exit }
303			else { ERR = 8 ; exit }
304}
305
306function Get_Num(date,m,d,y,j) {
307	# get the number of days from one date to another date
308	NOW = y m d ; N = 0 ; M = m + 0 ; D = d + 0 ; Y = y + 0 ; J = j + 0
309	split(date,mdy,IDFD)
310	M2 = mdy[1] ; D2 = mdy[2] ; Y2 = mdy[3]
311	THEN = Y2 M2 D2 ; M2 = M2 + 0 ; D2 = D2 + 0 ; Y2 = Y2 + 0
312	Leap(Y2)
313	if ( M2 > 12 )									{ ERR = 4 ; exit }
314	if ( D2 > Mdays[M2] && Y2 != 1752 && M2 != 9 )	{ ERR = 5 ; exit }
315	if ( THEN ~ /^1752090[3-9]$|^1752091[0-3]$/ )	{ ERR = 6 ; exit }
316	Leap(Y)
317	if ( THEN > NOW ) {
318		Ydays = Ydays - J + 1 ; mdays = Mdays[M] - D + 1
319		while ( Y < Y2 ) Next_Y()
320		while ( M < M2 ) Next_M()
321		while ( D < D2 ) Next_D()
322		N *= -1
323	}
324	else {
325		Ydays = J ; mdays = D
326		while ( Y > Y2 ) Prev_Y()
327		while ( M > M2 ) Prev_M()
328		if ( Y == 1752 && M == 9 && D == 19 ) D = 30
329		while ( D > D2 ) Prev_D()
330	}
331	return N
332}
333
334function Get_Date(n,m,d,y,j) {
335	# get the date a number of days before or after a date
336	N = n + 0 ; M = m + 0 ; D = d + 0 ; Y = y + 0 ; J = j + 0
337	if ( N != 0 ) {
338		Leap(Y)
339		if ( N > 0 ) {
340			Ydays = Ydays - J + 1 ; mdays = Mdays[M] - D + 1
341			while ( N >= Ydays ) { Next_Y() ; Leap(Y) }
342			while ( N >= ( ( mdays > 0 ) ? mdays : Mdays[M] ) ) { Next_M() }
343			while ( N > 0 ) Next_D()
344		}
345		else {
346			Ydays = J ; mdays = D ; N *= -1
347			while ( N >= Ydays ) { Prev_Y() ; Leap(Y) }
348			while ( N >= ( ( mdays > 0 ) ? mdays : Mdays[M] ) ) { Prev_M() }
349			if ( Y == 1752 && M == 9 && D == 19 ) D = 30
350			while ( N > 0 ) Prev_D()
351		}
352		if ( Y < 1 ) { ERR = 3 ; exit }
353	}
354	return M ODFD D ODFD Y
355}
356
357function Leap(YR) {
358	# adjust for Leap Years
359	if ( YR % 4 == 0 && ( YR % 100 != 0 || YR % 400 == 0 || YR < 1800 ) ) {
360		Ydays = 366 ; Mdays[2] = 29 }
361	else { Ydays = 365 ; Mdays[2] = 28 }
362	if ( YR != 1752 ) Mdays[9] = 30
363	else { Ydays = 355 ; Mdays[9] = 19 }
364}
365
366function Chk_Date(date) {
367	# check validity of input dates
368	split(date,mdy,IDFD)
369	mm = mdy[1] + 0 ; dd = mdy[2] + 0 ; yy = mdy[3] + 0
370	if ( mm == 2 ) Leap(yy)
371	if ( yy  < 1 )						{ ERR = 3 ; exit }
372	if ( mm  < 1 || mm  > 12 )			{ ERR = 4 ; exit }
373	if ( dd  < 1 || dd  > Mdays[mm] )	{ ERR = 5 ; exit }
374}
375
376# day counting functions for next or previous year, month and day
377function Next_Y() {
378	N -= Ydays ; Y += 1 ; M = 1 ; D = 1 ; mdays = 0 ; Leap(Y)
379}
380function Next_M() {
381	if ( mdays != 0 ) N -= mdays ; else N -= Mdays[M]
382	M += 1 ; D = 1 ; mdays = 0
383}
384function Next_D() {
385	N -= 1 ; D += 1
386	if ( D > Mdays[M] ) { M += 1 ; D = 1 }
387	else if ( Y == 1752 && M == 9 && D == 2 ) D = 13
388}
389function Prev_Y() {
390	N -= Ydays ; Y -= 1 ; M = 12 ; D = 31 ; mdays = 0 ; Leap(Y)
391}
392function Prev_M() {
393	if ( mdays != 0 ) N -= mdays ; else N -= Mdays[M]
394	M -= 1 ; D = Mdays[M] ; mdays = 0
395}
396function Prev_D() {
397	N -= 1 ; D -= 1 ; if ( Y == 1752 && M == 9 && D == 13 ) D = 2
398}
399
400function Get_J(m,d,y) {
401	# get the Julian date for an input date
402	m = m + 0 ; d = d + 0 ; y = y + 0
403	Leap(y)
404	j = d
405	for (i=1;i<m;i++) j = j + Mdays[i]
406	return j
407}
408
409function Get_Y1() {
410	# get the number of days to day 1 for the help screen
411	if ( ! m ) {
412		if ( mm ) { m = mm ; d = dd ; y = yy }
413		else if ( M ) { m = M ; d = D ; y = Y }
414	}
415	if ( ! j ) Get_J()
416	return Get_Num("01/01/0001",m,d,y,j)
417}
418