Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!netcom.com!pbewig From: pbewig@netcom.com (Phil Bewig) Newsgroups: comp.lang.icon Subject: (none) Message-ID: Date: 14 May 91 22:25:00 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 195 Hello, Here's another little procedure for formatted output. Enjoy! ! ############################################################################ # # Name: prdate.icn # # Title: print formatted dates # # Author: Philip L. Bewig # # Date: May 13, 1991 # ############################################################################ # # The prdate family of procedures provide formatted date output. They # share a common syntax, differing only in the place where output is sent: # # prdate(fmt, date) - send output to stdout # sprdate(fmt, date) - return output as value of function # fprdate(fd, fmt, date) - send output to file fd # # Essentially, the prdate functions work by copying the format string to # the output. Regular characters are copied unchanged. Escape sequences # are converted to their corresponding string values; the same escape # sequences recognized by the icon language are recognized by prdate. The # only real processing performed is to interpret conversion specifications # which begin with the "%" character. # # Conversion specifications have the form # # %xx # # where xx may be any of the following: # # m1 - month number in range 1 to 12 # m2 - month number in range 01 to 12 # m3 - month name in range Jan to Dec # M3 - month name in range JAN to DEC # m4 - month name in range January to December # M4 - month name in range JANUARY to DECEMBER # # d1 - day number in range 1 to 31 # d2 - day number in range 01 to 31 # # y1 - year number in range 0 to 99 # y2 - year number in range 00 to 99 # y3 - year number in range 0 to 9999 # y4 - year number in range 0000 to 9999 # # n1 - day name in range Sun to Sat # N1 - day name in range SUN to SAT # n2 - day name in range Sunday to Saturday # N2 - day name in range SUNDAY to SATURDAY # # A percent sign may be input literally by "%%". # # The date may be specified in one of two formats. An eight-digit number # is assumed to be a date of the form "CCYYMMDD". A smaller number is # assumed to be a julian number representing a date, calculated using the # jdate.icn code posted to icon-group by Cary Coutant. Dates preceeding # the christian era are not supported. # # For example, the function call sprdate("n2, m4 d1, y3", 17760704) # returns the string "Thursday, July 4, 1776" and the function call # sprdate("m2/d2/y4", 17760704) returns the string "07/04/1776". # ############################################################################ # # links: julian.icn - Cary Coutant's julian date routines # (gregorian, julian, datestring, dayofweek, # tomdy, and floor procedures) # ############################################################################ # # test driver # procedure main() # repeat { # writes("enter a format specification (null to quit): ") # fmt := read() # if *fmt = 0 then break # writes("enter a date in CCYYMMDD or julian number form: ") # expr := read() # write("the output is |" || sprdate(fmt, expr) || "|") # write() # } # end # ############################################################################ link julian # sprdate - return formatted date string procedure sprdate(fmt, expr) local out, i, s, g, m, d, y, dow, val static months initial { months := ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] } if *expr >= 8 then { m := integer(expr[5:7]) d := integer(expr[7:9]) y := integer(expr[1:5]) dow := dayofweek(gregorian(m,d,y)) } else { mdy := tomdy(expr) m := mdy[1]; d := mdy[2]; y := mdy[3] dow := dayofweek(expr) } out := "" fmt ? repeat { (out ||:= tab(upto('%\\'))) | (out ||:= tab(0) & break) case move(1) of { "%": { # format specification if match('%') then { out ||:= "%" move(1) } else { conv := move(2) case conv of { "m1": { val := m } "m2": { val := right(m, 2, "0") } "m3": { val := left(months[m], 3) } "M3": { val := map(left(months[m], 3), &lcase, &ucase) } "m4": { val := months[m] } "M4": { val := map(months[m], &lcase, &ucase) } "d1": { val := d } "d2": { val := right(d, 2, "0") } "y1": { val := y % 100 } "y2": { val := right(y % 100, 2, "0") } "y3": { val := y } "y4": { val := right(y, 4, "0") } "n1": { val := left(dow, 3) } "N1": { val := map(left(dow, 3), &lcase, &ucase) } "n2": { val := dow } "N2": { val := map(dow, &lcase, &ucase) } default: { val := &null; break } } out ||:= val } } "\\": { # escape sequence # escape handler derived from an ipl proc by William H. Mitchell out ||:= case c := map(move(1)) of { "b": "\b" "d": "\d" "e": "\e" "f": "\f" "l": "\l" "n": "\n" "r": "\r" "t": "\t" "v": "\v" "'": "'" "\"": "\"" "x": { move(i := 2 | 1) ? s := tab(upto(~'0123456789ABCDEFabcdef') | 0) move(*s - i) char("16r" || s) } "^": char(iand(ord(move(1)), 16r1f)) !"01234567": { move(-1) move(i := 3 | 2 | 1) ? s := tab(upto(~'01234567') | 0) move(*s - i) if s > 377 then { s := s[1:3] move(-1) } char("8r" || s) } default: c } } } } return out end # prdate - write date to standard output procedure prdate(fmt, expr) writes(sprdate(fmt, expr)) return end # fprdate - write date to file procedure fprdate(fd, fmt, expr) writes(fd, sprdate(fmt, expr)) return end