home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / SREFPRC1 / DATEFMT.SRF < prev    next >
Text File  |  1996-09-27  |  7KB  |  234 lines

  1. /*  Convert, using c strftime format,a time-date formatted string */
  2. /* with thanks to Chris Madsen (ac608@yfn.usy.edu) for the documentation
  3. on the strtftime function
  4. Expects:
  5.    conversion string
  6.    date in 'B' format
  7.    time in hh:mm:ss
  8.  
  9. if no date or time, use current date & time
  10.  */
  11.  
  12. /*parse pull foo
  13. say sref_datetime_convert('%'foo)
  14. exit*/
  15.  
  16.  
  17. sref_datetime_convert:
  18. parse arg thestring,adate,atime
  19. if adate='' then adate=DATE('B')
  20. if atime=' ' then atime=time('N')
  21.  
  22. daysht='Sun Mon Tue Wed Thu Fri Sat '
  23. daylong='Sunday Monday Tuesday Wednesday Thursday Friday Saturday'
  24. monthlong='January February March April May June July August September October  November December '
  25. monthsht='Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec '
  26.  
  27.  
  28. /* uses sref_replacestrg extensively ... */
  29. doit=thestring
  30.  
  31. if pos('%%',doit)>0 then do
  32.     foo='1a'x
  33.     doit=sref_replacestrg(doit,'%%',foo,'ALL','YES')
  34. end
  35.  
  36. if pos('%a',doit)>0 then do             /* Mon Tue .. */
  37.    foo=dateconv(adate,'B','w')
  38.    aa=wordpos(upper(foo),upper(daylong)) ;
  39.    foo=word(daysht,aa)
  40.    doit=sref_replacestrg(doit,'%a',foo,'ALL','YES')
  41. end
  42.  
  43. if pos('%A',doit)>0 then do              /* MONDAY TUESDAY */
  44.    foo=dateconv(adate,'B','w')
  45.    doit=sref_replacestrg(doit,'%A',foo,'ALL','YES')
  46. end
  47.  
  48.  
  49. if pos('%b',doit)>0 then do             /* Jan Feb .. */
  50.    foo=dateconv(adate,'B','M')
  51.    aa=wordpos(upper(foo),upper(monthlong))
  52.    foo=word(monthsht,aa)
  53.    doit=sref_replacestrg(doit,'%b',foo,'ALL','YES')
  54. end
  55.  
  56.  
  57.  
  58. if pos('%B',doit)>0 then do              /* January February */
  59.  
  60.     foo=dateconv(adate,'B','M');
  61.     doit=sref_replacestrg(doit,'%B',foo,'ALL','YES')
  62. end
  63.  
  64.  
  65. if pos('%c',doit)>0 then do             /*date and time */
  66.   foo2=dateconv(adate,'B','N')
  67.   foo=atime' 'foo2
  68.   doit=sref_replacestrg(doit,'%c',foo,'ALL','YES')
  69. end
  70.  
  71. if pos('%d',doit)>0 then do            /* 1 2 .. 31 day of month */
  72.     foo=dateconv(adate,'B','N') ; foo=strip(word(foo,1))
  73.     doit=sref_replacestrg(doit,'%d',foo,'ALL','YES')
  74. end
  75.  
  76. if pos('%D',doit)>0 then do             /* 05/27/89 */
  77.    foo=dateconv(adate,'B','U')
  78.    doit=sref_replacestrg(doit,'%D',foo,'ALL','YES')
  79. end
  80.  
  81. if pos('%e',doit)>0 then do            /* 1 2 .. 31 day of month, space padded*/
  82.     foo=dateconv(adate,'B','N') ; foo=' '||strip(word(foo,1))||' '    /* must be used with <PRE> */
  83.     doit=sref_replacestrg(doit,'%e',foo,'ALL','YES')
  84. end
  85.  
  86. if pos('%h',doit)>0 then do             /* Jan Feb .. */
  87.    foo=dateconv(adate,'B','m'); aa=wordpos(upper(foo),upper(monthlong))
  88.    foo=word(monthsht,aa)
  89.    doit=sref_replacestrg(doit,'%h',foo,'ALL','YES')
  90. end
  91.  
  92. if pos('%H',doit)>0 then do                     /* 00.. 23 hour*/
  93.    parse var atime foo ':' .
  94.    doit=sref_replacestrg(doit,'%H',foo,'ALL','YES')
  95. end
  96.  
  97. if pos('%I',doit)>0 then do                     /*01 .. 12  hour*/
  98.  parse var atime foo2 ':' .
  99.   if foo2=0 then foo2=12
  100.   if foo2>12 then foo2=foo2-12
  101.    doit=sref_replacestrg(doit,'%I',foo2,'ALL','YES')
  102. end
  103.  
  104. if pos('%j',doit)>0 then do             /* 01..366 day of year */
  105.    foo=dateconv(adate,'B','D')
  106.    doit=sref_replacestrg(doit,'%j',foo,'ALL','YES')
  107. end
  108.  
  109. if pos('%m',doit)>0 then do             /* 01 - 12 month */
  110.     foo=dateconv(adate,'B','L') ; foo=word(foo,2) ;
  111.     foo=wordpos(foo,monthlong)
  112.     doit=sref_replacestrg(doit,'%m',foo,'ALL','YES')
  113. end
  114.  
  115. if pos('%M',doit)>0 then do             /* 00...59  minute*/
  116.    parse var atime foo1 ':' foo2 ':' .
  117.    doit=sref_replacestrg(doit,'%M',foo2,'ALL','YES')
  118. end
  119.  
  120.  
  121. if pos('%n',doit)>0 then do                     /* new line */
  122.   foo='<BR>'
  123.    doit=sref_replacestrg(doit,'%n',foo,'ALL','YES')
  124. end
  125.  
  126. if pos('%p',doit)>0 then do                     /* am or pm */
  127.     parse var atime hh ':' .
  128.     if hh >11 then 
  129.        foo='pm' 
  130.     else 
  131.        foo='am'
  132.     doit=sref_replacestrg(doit,'%p',foo,'ALL','YES')
  133. end
  134.  
  135. if pos('%r',doit)>0 then do                     /* 8:35pm */
  136.     parse var atime hh ':' mm ':' ss
  137.     if hh>11 then 
  138.        pp='pm'
  139.     else 
  140.        pp='am'
  141.        
  142.     if hh=0 then hh=12
  143.     if hh>12 then hh=hh-12
  144.      foo=hh':'mm':'ss':'pp
  145.     doit=sref_replacestrg(doit,'%r',foo,'ALL','YES')
  146. end
  147.  
  148. if pos('%S',doit)>0 then do                     /* 00-59 scones */
  149.     parse var atime hh ':' mm ':' foo
  150.    doit=sref_replacestrg(doit,'%S',foo,'ALL','YES')
  151. end
  152.  
  153. if pos('%t',doit)>0 then do             /* tab (better use in a <PRE> */
  154.     foo='09'x
  155.     doit=sref_replacestrg(doit,'%t',foo,'ALL','YES')
  156. end
  157.  
  158. if pos('%T',doit)>0 then do             /* 13:54:12 */
  159.    foo=atime
  160.    doit=sref_replacestrg(doit,'%T',foo,'ALL','YES')
  161. end
  162.  
  163. if pos('U',doit)>0 then do             /* week # (00-53 -- 1 on first suuda */
  164.    today=dateconv(adate,'B','D')
  165.    a=dateconv(1,'D','W')
  166.    nday_jan1=wordpos(upper(a),upper(daylong))
  167.    offset=0
  168.    if nday_jan1 > 1 then
  169.        offset=8-nday_jan1
  170.    t1=today-offset
  171.    if t1 <= 0 then
  172.       isweek=0
  173.    else
  174.       isweek=trunc(t1/7)+1
  175.    doit=sref_replacestrg(doit,'%U',isweek,'ALL','YES')
  176. end
  177.  
  178. if pos('%w',doit)>0 then do             /* 0--6 weekday (sunday=0) */
  179.   foo=dateconv(adate,'B','W')
  180.   aa=wordpos(upper(foo),upper(daylong))-1
  181.   doit=sref_replacestrg(doit,'%w',aa,'ALL','YES')
  182. end
  183.  
  184. if pos('%W',doit)>0 then do    /* week # (00-53 -- 1 on first monday */
  185.     today=dateconv(adate,'B','D')
  186.     a=dateconv(1,'D','W')
  187.     nday_jan1=wordpos(upper(a),upper(daylong))-1
  188.     if nday_jan1=0 then nday_jan1=7
  189.     offset=0
  190.     if nday_jan1 > 1 then
  191.        offset=8-nday_jan1
  192.     t1=today-offset
  193.     if t1 <= 0 then
  194.         isweekm=0
  195.     else
  196.         isweekm=trunc(t1/7)+1
  197.     doit=sref_replacestrg(doit,'%W',isweekm,'ALL','YES')
  198.  
  199. end
  200.  
  201.  
  202. if pos('%x',doit)>0 then do                     /* 27 August 1988 */
  203.     foo=dateconv(adate,'B','L')
  204.     doit=sref_replacestrg(doit,'%x',foo,'ALL','YES')
  205. end
  206.  
  207. if pos('%X',doit)>0 then do                             /* 13:01:09 */
  208.    foo=time('N') 
  209.    doit=sref_replacestrg(doit,'%X',foo,'ALL','YES')
  210. end
  211.  
  212. if pos('%y',doit)>0 then do                      /* 00-99 year */
  213.    foo=dateconv(adate,'B','L') ; foo=right(foo,2)
  214.    doit=sref_replacestrg(doit,'%y',foo,'ALL','YES')
  215. end
  216.  
  217. if pos('%Y',doit)>0 then do                      /* 1901 year */
  218.    foo=dateconv(adate,'B','L') ; foo=strip(word(foo,3))
  219.    doit=sref_replacestrg(doit,'%Y',foo,'ALL','YES')
  220. end
  221.  
  222. if pos('%Z',doit)>0 then do
  223.     foo=value('TZ',,'os2environment') ; foo=left(foo,3)
  224.     doit=sref_replacestrg(doit,'%Z',foo,'ALL','YES')
  225. end
  226.  
  227. if pos('1a'x,doit)>0 then do
  228.     doit=sref_replacestrg(doit,'1a'x,'%','ALL','YES')
  229. end 
  230.  
  231. return doit
  232.  
  233.  
  234.