home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / killold1.zip / rxdates.CMD < prev    next >
OS/2 REXX Batch file  |  1996-03-02  |  7KB  |  196 lines

  1. /* rexx routine(s) to change date formats  */
  2.  
  3. /* startdescription
  4.  
  5.    This routine is normally meant to be Called from another Rexx routine.
  6.    It can be used from the command line though. I left it that way for my
  7.    testing.
  8.    It has not been optimized, but seems to work fine. There is no error
  9.    checking, so you should do your own outside this routine.
  10.    If you find errors or additions, you have the source to fix it <grin>.
  11.  
  12.    Denis Tonn        1:153/908@fidonet       DTONN@IBM.VNET.COM
  13.  
  14.    Dates types to be processed can be:
  15.    Julian (day # of year)
  16.    USA (MM/DD/YY or MM/DD/YYYY)
  17.    Month Day Year (eg: January 17 1994)
  18.    BaseDate (total days since 01/01/01)
  19.  
  20.    NOTE: Julian dates assume current year. Because of this, BaseDates less than
  21.          366 are processed as Julian. Also, to properly handle MM/DD/YYYY
  22.          requires that MM/DD/YY be restricted to the current century.
  23.  
  24.    Returned types available are:
  25.     'J' - Julian format of date (173)
  26.     'U' - USA format (MM/DD/YY)
  27.     'F' - Full format (October 23 1992)
  28.     'D' - Day number of the week (0 to 6, where 0 is Monday)
  29.     'W' - Weekday name (Thursday)
  30.     'M' - Month name (June)
  31.     'B' - Basedate (days since 01/01/01)
  32.     'L' - Leapyear - Rexx boolean (0/1) value. True if leapyear
  33.  
  34.   Format for a call from another rexx routine would be:
  35.    USA=rxdates('Febuary 27 1993 U') - returns the USA format
  36.    Julian=rxdates(08/07/93 'J') - returns the julian date for 08/07/1993
  37.    Weekday=rxdates(37 W) - returns the name/day of the week from day 37 this year
  38.    basedate=rxdates('09/03/1954 B') - returns days from 01/01/0001 to 09/03/1954
  39.  
  40. I find it particularly useful in calculating future/past dates. Lets say I
  41. want to calculate the MM/DD/YY format of a date 30 days ahead, when my starting
  42. date is today. End of month crossovers and leapyear calculation can make this
  43. tricky.. So:
  44.      newUSA=rxdates((date('B')+30) 'U')
  45.  See sample command file for other formats.
  46.  
  47. enddescription */
  48.  
  49. /* trace results */
  50.  
  51. /* low level stuff. Days in a month, month names, etc */
  52. months.names= 'January Febuary March April May June July August September October November December'
  53. months.days=  '31      28      31    30    31  30   31   31     30        31      30       31      '
  54. julian_total=365
  55. week.days='Monday Tuesday Wednesday Thursday Friday Saturday Sunday'
  56.  
  57. parse source . how myfile /* how was I called? from Command line or rexx program */
  58. if arg()<1 then
  59.  do
  60.   say filespec('n',myfile) 'inputdate outputtype'
  61.   I=1
  62.   do until pos('STARTDESCRIPTION',translate(sourceline(I)))<>0
  63.    I=I+1
  64.   end /* do */  /* found starting of description */
  65.    I=I+1
  66.   do until pos('ENDDESCRIPTION',translate(sourceline(I)))<>0
  67.    say sourceline(I)
  68.    I=I+1
  69.   end /* do */  /* found end of description */
  70.   exit
  71.  end /* do */
  72. arg month day year type         /* max possible args */
  73. if words(month)>1 then         /* command line call, split it up */
  74.                        parse var month month day year type
  75. if datatype(month,'NUM') then
  76.   do
  77.   type=day
  78.   if month<=366 then /* assume julian format of date */
  79.     do
  80.      Julian=month
  81.      parse value date('N') with . . year /* assume current year */
  82.      call correct_leapyear
  83.      I=1; mm=1; dd=julian
  84.       do while dd>word(months.days,mm)
  85.        dd=dd-word(months.days,mm)
  86.        mm=mm+1
  87.       end /* do */
  88.      day=dd; month=word(months.names,mm)
  89.     end /* do */
  90.  else              /* assume basedate format */
  91.    do
  92.     basedate=month
  93.      y400=basedate%146097; r400=basedate//146097
  94.      y100=r400%36524; r100=r400//36524
  95.      y4=r100%1461; r4=r100//1461
  96.      y1=r4%365; julian=(r4//365)+1
  97.      year=(y400*400)+(y100*100)+(y4*4)+y1+1
  98.      call correct_leapyear
  99.      I=1; mm=1; dd=julian
  100.       do while dd>word(months.days,mm)
  101.        dd=dd-word(months.days,mm)
  102.        mm=mm+1
  103.       end /* do */
  104.      day=dd; month=word(months.names,mm)
  105.    end /* do */
  106.  end /* do datatype=num */
  107. else
  108.  do
  109.   if pos('/',month)<>0 then /* MM/DD/YY format */
  110.    do
  111.     type=day
  112.     parse var month MM '/' DD '/' YY
  113.     parse value date('N') with . . year
  114.     if YY<100 then year=((year%100)*100)+YY     /* assume current century */
  115.      else year=YY
  116.     call correct_leapyear
  117.     month=word(months.names,abs(MM))
  118.     day=abs(dd)
  119.    end /* do */
  120.   else
  121.    do /* must be full name */
  122.     call correct_leapyear
  123.    end /* do */
  124.  end /* do */
  125.  
  126. call init_tables
  127. Type=translate(left(type,1))
  128.  
  129.  
  130. if translate(left(how,1))='C' then
  131. do
  132. select
  133.  when type='J' then say 'Julian date: 'julian
  134.  when type='U' then say 'USA    date: 'Usa
  135.  when type='F' then say 'Full   date: 'month day year
  136.  when type='D' then say 'Day  number: 'dayweek
  137.  when type='W' then say 'Weekday    : 'weekday
  138.  when type='M' then say 'Month      : 'month
  139.  when type='B' then say 'basedate   : 'basedate
  140.  when type='L' then say 'leapyear   : 'leapyear(year)
  141. otherwise
  142.  say 'Date:' month day year 'Julian:'julian 'Weekday:'weekday 'USA:'Usa 'BaseDate:'basedate
  143. end /* select */
  144. end /* do */
  145.  
  146. else
  147. do
  148. select
  149.  when type='J' then return(julian)
  150.  when type='U' then return(Usa)
  151.  when type='F' then return(month day year)
  152.  when type='D' then return(dayweek)
  153.  when type='W' then return(weekday)
  154.  when type='M' then return(month)
  155.  when type='B' then return(basedate)
  156.  when type='L' then return(leapyear(year))
  157. otherwise
  158.  return(0)
  159. end /* select */
  160. end /* do */
  161.  
  162. exit
  163.  
  164. init_tables:
  165. baseyear=(365*(year-1))+((year-1)%4)-((year-1)%100)+((year-1)%400)-1  /* the number of days since 01/01/0001 to 01/01/xxxx */
  166. dd=day; yy=(year//100); mm=1
  167. do while translate(left(word(months.names,mm),3)) <> translate(left(month,3))
  168.  mm=mm+1
  169. end /* do */
  170. month=word(months.names,mm); julian=0
  171. do I=1 to mm-1
  172.  julian=julian+word(months.days,I)
  173. end /* do */
  174. if length(dd)<2 then dd='0'||dd
  175. if length(mm)<2 then mm='0'||mm
  176. if length(yy)<2 then yy='0'||yy
  177. usa=mm'/'dd'/'yy; julian=julian+day; basedate=baseyear+julian
  178. dayweek=basedate//7; weekday=word(week.days,dayweek+1)
  179. return
  180.  
  181. correct_leapyear:
  182. if leapyear(year) then
  183.  do
  184.   months.days=  '31      29      31    30    31  30   31   31     30        31      30       31      '
  185.   julian_total=366
  186.  end /* do */
  187. return
  188.  
  189. leapyear: procedure  /* determines if the passed year is a leapyear */
  190. arg year
  191. if (year//100)<>0 then  /* not a century year */
  192.  return((year//4)=0)    /* true if leap year */
  193. else                    /* century year */
  194.  return(year//400=0)    /* century leapyear every 400 years */
  195.  
  196.