Debian Codegraph @

examples/hcal

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