home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / CRONRF.ZIP / DATE2STR.CMD < prev    next >
OS/2 REXX Batch file  |  1992-06-18  |  15KB  |  363 lines

  1. /*
  2. program: date2str.cmd
  3. type:    REXXSAA-OS/2
  4. purpose: allow formatting of sorted date into string pattern
  5. version: 1.0
  6. date:    1991-05-20
  7. changed: 1992-06-18, removed bundling to ATRGF.CMD etc., RGF
  8. author:  Rony G. Flatscher,
  9.          Wirtschaftsuniversität/Vienna
  10.          RONY@AWIWUW11.BITNET
  11.          flatscher@wu-wien.ac.at
  12.  
  13. needs:   DATERGF.CMD
  14.  
  15. usage:   DATE2STR(SDATE, PATTERN[, DELIMITER])
  16.          see enclosed Tutorial "RGFSHOW.CMD" and syntax below
  17.  
  18. All rights reserved, copyrighted 1991, 1992, no guarantee that it works without
  19. errors, etc. etc.
  20.  
  21. donated to the public domain granted that you are not charging anything
  22. (money etc.) for it and derivates based upon it, as you did not write it,
  23. etc. if that holds you may bundle it with commercial programs too
  24.  
  25. Please, if you find an error, post me a message describing it, I will
  26. try to fix and rerelease it to the net.
  27.  
  28. usage:    DATE2STR(SDATE, PATTERN[, DELIMITER])
  29.  
  30.  
  31. syntax:
  32.     SDATE ...... sorted date (YYYYMMDD)
  33.     PATTERN .... string with embedded date-tokens led in by '%'
  34.     DELIMITER .. one character overriding standard delimiter '%'
  35.  
  36.     date-tokens take the form (results shown for sorted date "19940106"):
  37.  
  38.     token:  => result:
  39.  
  40.     %y[y]   => 94       ... two digit year
  41.     %yyy[y] => 1994     ... four digit year
  42.  
  43.     %m      => 3        ... monthnumber without leading null
  44.     %mm     => 03       ... monthnumber with leading null if one digit
  45.     %mmm    => Jan      ... monthname with three letters (mixed case)
  46.     %MMM    => JAN      ... monthname with three letters (uppercase)
  47.     %mmmm   => January  ... monthname in full (mixed case)
  48.     %MMMM   => JANUARY  ... monthname in full (uppercase)
  49.  
  50.     %d      => 6        ... daynumber without leading null
  51.     %dd     => 06       ... daynumber with leading null if one digit
  52.     %ddd    => Thu      ... dayname with three letters (mixed case)
  53.     %DDD    => THU      ... dayname with three letters (uppercase)
  54.     %dddd   => Thursday ... dayname in full (mixed case)
  55.     %DDDD   => THURSDAY ... dayname in full (uppercase)
  56.  
  57.     %w      =>   1      ... week-no. without leading null
  58.     %ww     =>  01      ... week-no. with leading null
  59.  
  60.     %j      =>   6      ... Julian days without leading nulls
  61.     %jj     =>  06      ... Julian days with one leading null if one digit
  62.     %jjj    => 006      ... Julian days with leading nulls (3 digits width)
  63. */
  64.  
  65. IF ARG(1) = '' | ARG(1) = '?' THEN SIGNAL usage
  66.  
  67. date = ARG(1)
  68. pattern = ARG(2)
  69. IF ARG(3) = '' THEN delimiter = '%'     /* default delimiter */
  70. ELSE
  71.    IF LENGTH(ARG(3)) <> 1 THEN DO
  72.       errmsg = ARG(3)': delimiter must be ONE character only'
  73.       SIGNAL error
  74.    END
  75.    ELSE delimiter = ARG(3)
  76.  
  77. result_date2str = pattern
  78. pattern2 = TRANSLATE(pattern)
  79. dayname   = datergf(date, "DN") /* get dayname */
  80.  
  81. IF dayname = '' THEN DO         /* error in sorted date ? */
  82.    errmsg = ARG(1)': illegal date'
  83.    SIGNAL error
  84. END
  85.  
  86. monthname = datergf(date, "MN") /* get monthname   */
  87. week      = datergf(date, "W")  /* get week        */
  88. julian    = datergf(date, "J")  /* get Julian date */
  89.  
  90. index = 1
  91. token_nr = 1
  92.  
  93. /*
  94.     token.x.type    ... type of token
  95.     token.x.start   ... start position of token
  96.     token.x.length  ... length of token
  97.     x = token number in pattern
  98. */
  99.  
  100. char_nr = LENGTH(pattern)
  101.  
  102. DO index = 1 TO char_nr
  103.    letter = SUBSTR(pattern2, index, 1)
  104.    IF letter = delimiter THEN DO
  105.       token.token_nr.start = index              /* start of token with delimiter */
  106.       index = index + 1
  107.  
  108.       IF index > char_nr THEN LEAVE             /* end of pattern ? */
  109.  
  110.       letter = SUBSTR(pattern2, index, 1)
  111.  
  112.       IF letter = delimiter THEN DO             /* successive delimiters */
  113.          index = index - 1                      /* reset index */
  114.          ITERATE
  115.       END
  116.       i2 = 1                                    /* length of symbol */
  117.  
  118.       SELECT
  119.          WHEN letter = 'Y' THEN                 /* year in hand ? */
  120.               DO
  121.                  DO index = index + 1 TO char_nr
  122.                     IF SUBSTR(pattern2, index, 1) = 'Y' THEN i2 = i2 + 1
  123.                     ELSE LEAVE
  124.                  END
  125.  
  126.                  IF i2 > 2 THEN token.token_nr.type = 12     /* year: 4 digits */
  127.                  ELSE token.token_nr.type = 11               /* year; 2 digits */
  128.               END
  129.  
  130.          WHEN letter = 'M' THEN                 /* month in hand ? */
  131.               DO
  132.                  DO index = index + 1 TO char_nr
  133.                     IF SUBSTR(pattern2, index, 1)  = 'M' THEN i2 = i2 + 1
  134.                     ELSE LEAVE
  135.                  END
  136.  
  137.                  SELECT
  138.                     WHEN i2 = 1 THEN token.token_nr.type = 21  /* digit without leading null */
  139.  
  140.                     WHEN i2 = 2 THEN token.token_nr.type = 22  /* digit with leading null */
  141.  
  142.                     WHEN i2 = 3 THEN
  143.                          IF SUBSTR(pattern, token.token_nr.start+1, 1) = 'm' THEN
  144.                             token.token_nr.type = 23           /* three mixed letters     */
  145.                          ELSE
  146.                             token.token_nr.type = 24           /* three uppercase letters */
  147.  
  148.                     OTHERWISE DO
  149.                          IF SUBSTR(pattern, token.token_nr.start+1, 1) = 'm' THEN
  150.                             token.token_nr.type = 25           /* full name mixed letters     */
  151.                          ELSE
  152.                             token.token_nr.type = 26           /* full name uppercase letters */
  153.                        END
  154.                  END
  155.               END
  156.  
  157.          WHEN letter = 'D' THEN                    /* day in hand ? */
  158.               DO
  159.                  DO index = index + 1 TO char_nr
  160.                     IF SUBSTR(pattern2, index, 1) = 'D' THEN i2 = i2 + 1
  161.                     ELSE LEAVE
  162.                  END
  163.  
  164.                  SELECT
  165.                     WHEN i2 = 1 THEN token.token_nr.type = 31  /* digit without leading null */
  166.  
  167.                     WHEN i2 = 2 THEN token.token_nr.type = 32  /* digit with leading null */
  168.  
  169.                     WHEN i2 = 3 THEN
  170.                          IF SUBSTR(pattern, token.token_nr.start+1, 1) = 'd' THEN
  171.                             token.token_nr.type = 33           /* three mixed letters     */
  172.                          ELSE
  173.                             token.token_nr.type = 34           /* three uppercase letters */
  174.  
  175.                     OTHERWISE DO
  176.                          IF SUBSTR(pattern, token.token_nr.start+1, 1) = 'd' THEN
  177.                             token.token_nr.type = 35           /* full name mixed letters     */
  178.                          ELSE
  179.                             token.token_nr.type = 36           /* full name uppercase letters */
  180.                        END
  181.                  END
  182.               END
  183.  
  184.          WHEN letter = 'J' THEN                    /* Julian day in hand ? */
  185.               DO
  186.                  DO index = index + 1 TO char_nr
  187.                     IF SUBSTR(pattern2, index, 1) = 'J' THEN i2 = i2 + 1
  188.                     ELSE LEAVE
  189.                  END
  190.  
  191.                  SELECT
  192.                     WHEN i2 = 1 THEN token.token_nr.type = 41  /* digit without leading null   */
  193.  
  194.                     WHEN i2 = 2 THEN token.token_nr.type = 42  /* digit with leading null      */
  195.  
  196.                     OTHERWISE token.token_nr.type = 43         /* digit with two leading nulls */
  197.                  END
  198.               END
  199.  
  200.          WHEN letter = 'W' THEN                    /* week in hand ? */
  201.               DO
  202.                  DO index = index + 1 TO char_nr
  203.                     IF SUBSTR(pattern2, index, 1) = 'W' THEN i2 = i2 + 1
  204.                     ELSE LEAVE
  205.                  END
  206.  
  207.                  IF i2 = 1 THEN token.token_nr.type = 51   /* digit without leading null   */
  208.                  ELSE token.token_nr.type = 52             /* digit with leading null */
  209.               END
  210.  
  211.          OTHERWISE NOP
  212.       END
  213.       index = index - 1                 /* reduce index, as it will be incremented on top */
  214.       token.token_nr.length = i2 + 1    /* length of token, including delimiter */
  215.       token_nr = token_nr + 1           /* increase token count */
  216.    END
  217.  
  218. END
  219.  
  220. token_nr = token_nr - 1                 /* adjust token count */
  221.  
  222. IF token_nr > 0 THEN DO                 /* if tokens found overlay them */
  223.     DO i = token_nr TO 1 BY -1
  224.        SELECT
  225.           WHEN token.i.type < 20 THEN   /* year to format */
  226.                IF token.i.type = 11 THEN tmp = SUBSTR(date, 3, 2)  /* two year digits */
  227.                ELSE tmp = SUBSTR(date, 1, 4)                      /* all year digits */
  228.  
  229.           WHEN token.i.type < 30 THEN   /* month to format */
  230.                SELECT
  231.                   WHEN token.i.type = 21 THEN           /* no leading zeros */
  232.                       tmp = SUBSTR(date, 5, 2) % 1
  233.  
  234.                   WHEN token.i.type = 22 THEN           /* two digits       */
  235.                       tmp = SUBSTR(date, 5, 2)
  236.  
  237.                   WHEN token.i.type = 23 | token.i.type = 24 THEN DO
  238.                         tmp = SUBSTR(monthname, 1, 3)  /* first three letters */
  239.                         IF token.i.type = 24 THEN tmp = TRANSLATE(tmp)
  240.                       END
  241.  
  242.                   OTHERWISE DO                         /* all letters */
  243.                      tmp = monthname
  244.                      IF token.i.type = 26 THEN tmp = TRANSLATE(tmp)
  245.                   END
  246.                END
  247.  
  248.           WHEN token.i.type < 40 THEN   /* day to format */
  249.                SELECT
  250.                   WHEN token.i.type = 31 THEN           /* no leading zeros */
  251.                       tmp = SUBSTR(date, 7, 2) % 1
  252.  
  253.                   WHEN token.i.type = 32 THEN           /* two digits       */
  254.                       tmp = SUBSTR(date, 7, 2)
  255.  
  256.                   WHEN token.i.type = 33 | token.i.type = 34 THEN DO
  257.                         tmp = SUBSTR(dayname, 1, 3)    /* first three letters */
  258.                         IF token.i.type = 34 THEN tmp = TRANSLATE(tmp)
  259.                       END
  260.  
  261.                   OTHERWISE DO                         /* all letters */
  262.                      tmp = dayname
  263.                      IF token.i.type = 36 THEN tmp = TRANSLATE(tmp)
  264.                   END
  265.                END
  266.  
  267.           WHEN token.i.type < 50 THEN   /* Julian day to format */
  268.                DO
  269.                   tmp = SUBSTR(Julian, 5, 3)
  270.                   SELECT
  271.                      WHEN token.i.type = 41 THEN   /* no leading zeros */
  272.                         tmp = tmp % 1
  273.  
  274.                      WHEN token.i.type = 42 THEN DO  /* two digits, leading zero if necessary */
  275.                           tmp = tmp % 1
  276.                           IF tmp < 100 THEN
  277.                              tmp = RIGHT(tmp, 2, '0')
  278.                         END
  279.  
  280.                      OTHERWISE NOP                /* all three Julian digits */
  281.                   END
  282.                END
  283.  
  284.           OTHERWISE                     /* week to format */
  285.                 IF tmp = 52 THEN tmp = RIGHT(week, 2, '0')
  286.                 ELSE tmp = week
  287.        END
  288.        /* prepare string according to received pattern */
  289.        right_part = SUBSTR(result_date2str, (token.i.start + token.i.length))
  290.        result_date2str = SUBSTR(pattern, 1, token.i.start - 1)||tmp||right_part
  291.     END
  292. END
  293.  
  294.  
  295. RETURN result_date2str         /* return value */
  296. /* end of main routine */
  297.  
  298.  
  299. USAGE:
  300. /* define some colors to demonstrate them */
  301. esc    = '1B'x          /* define ESCape character */
  302. red    = esc||"[31m"    /* ANSI.SYS-control for red foreground */
  303. yellow = esc||"[33m"    /* ANSI.SYS-control for yellow foreground */
  304. cyan   = esc||"[36m"    /* ANSI.SYS-control for cyan foreground */
  305. normal = esc||"[0m"     /* ANSI.SYS-control for resetting attributes to normal */
  306.  
  307. SAY
  308. SAY cyan'DATE2STR:'normal' format string with date-tokens'
  309. SAY
  310. SAY red'usage:'yellow'    DATE2STR(SDATE, PATTERN[, DELIMITER])'
  311. SAY
  312. SAY red'syntax:'
  313. SAY cyan'    SDATE'normal' ...... sorted date (YYYYMMDD)'
  314. SAY cyan'    PATTERN'normal' .... string with embedded date-tokens led in by "'cyan'%'normal'"'
  315. SAY cyan'    DELIMITER'normal' .. one character overriding standard delimiter "%"'
  316. SAY
  317. SAY '    date-tokens take the form (results shown for sorted date "'cyan'19940106'normal'"):'
  318. SAY
  319. SAY '    token:  => result:'
  320. SAY
  321. SAY cyan'    %y[y]'normal'   => 'cyan'94       'normal'... two digit year'
  322. SAY cyan'    %yyy[y]'normal' => 'cyan'1994     'normal'... four digit year'
  323. SAY
  324. SAY cyan'    %m      'normal'=> 'cyan'3        'normal'... monthnumber without leading null'
  325. SAY cyan'    %mm     'normal'=> 'cyan'03       'normal'... monthnumber with leading null if one digit'
  326. SAY cyan'    %mmm    'normal'=> 'cyan'Jan      'normal'... monthname with three letters (mixed case)'
  327. SAY cyan'    %MMM    'normal'=> 'cyan'JAN      'normal'... monthname with three letters (uppercase)'
  328. SAY cyan'    %mmmm   'normal'=> 'cyan'January  'normal'... monthname in full (mixed case)'
  329. SAY cyan'    %MMMM   'normal'=> 'cyan'JANUARY  'normal'... monthname in full (uppercase)'
  330. SAY
  331. SAY cyan'    %d      'normal'=> 'cyan'6        'normal'... daynumber without leading null'
  332. SAY cyan'    %dd     'normal'=> 'cyan'06       'normal'... daynumber with leading null if one digit'
  333. SAY cyan'    %ddd    'normal'=> 'cyan'Thu      'normal'... dayname with three letters (mixed case)'
  334. SAY cyan'    %DDD    'normal'=> 'cyan'THU      'normal'... dayname with three letters (uppercase)'
  335. SAY cyan'    %dddd   'normal'=> 'cyan'Thursday 'normal'... dayname in full (mixed case)'
  336. SAY cyan'    %DDDD   'normal'=> 'cyan'THURSDAY 'normal'... dayname in full (uppercase)'
  337. SAY
  338. SAY cyan'    %w      'normal'=> 'cyan'  1      'normal'... week-no. without leading null'
  339. SAY cyan'    %ww     'normal'=> 'cyan' 01      'normal'... week-no. with leading null'
  340. SAY
  341. SAY cyan'    %j      'normal'=> 'cyan'  6      'normal'... Julian days without leading nulls'
  342. SAY cyan'    %jj     'normal'=> 'cyan' 06      'normal'... Julian days with one leading null if one digit'
  343. SAY cyan'    %jjj    'normal'=> 'cyan'006      'normal'... Julian days with leading nulls (3 digits width)'
  344. SAY
  345. SAY red'examples:'normal
  346. SAY '    (1) with default escape character being: 'cyan'%'
  347. SAY yellow'        DATE2STR(19940106, "%dddd, %d. %mmmm in the year %yyyy")'normal
  348. SAY '        results in:'
  349. SAY yellow'        "Thursday, 6. January in the year 1994"'normal
  350. SAY
  351. SAY '    (2) with escape character being: 'cyan'!'
  352. SAY yellow'        DATE2STR(19940106, "!DDD, !dd. !MMMM in the year !yy", "!")'normal
  353. SAY '        results in:'
  354. SAY yellow'        "THU, 06. JANUARY in the year 94"'normal
  355.  
  356. EXIT
  357.  
  358. ERROR:
  359. /* error message on device "STDERR" */
  360. '@ECHO DATE2STR: 'errmsg' >&2'
  361. EXIT ''
  362.  
  363.