home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 May / PCO_5_97.ISO / FilesBBS / OS2 / NETREXX.ARJ / NETREXX.ZIP / NetRexx / WordClock.nrx < prev   
Encoding:
Text File  |  1997-01-01  |  4.9 KB  |  116 lines

  1. /* WordClock -- an applet that shows the time in English.     */
  2. /*                                                            */
  3. /*   Parameters:                                              */
  4. /*                                                            */
  5. /*     face -- the font face to use                           */
  6. /*     size -- the font size to use                           */
  7. /*                                                            */
  8. /* ---------------------------------------------------------- */
  9. /* Based on the ancient QTIME.REXX, and typical Java applets. */
  10.  
  11. class WordClock extends Applet implements Runnable
  12.  
  13.  timer=Thread null                        /* the timer thread */
  14.  offsetx; offsety                            /* text position */
  15.  now                                          /* current time */
  16.  
  17.  method init
  18.   /* Get parameters from the <applet> markup */
  19.   face=getParameter("face")                      /* font face */
  20.   if face=null then face="TimesRoman"
  21.   size=getParameter("size")
  22.   if size=null then size="20"                    /* font size */
  23.  
  24.   setFont(Font(face, Font.BOLD, size))
  25.   resize(size*20, size*2)                  /* set window size */
  26.   offsetx=size/2                 /* and where text will start */
  27.   offsety=size*3/2                      /* note Y is from top */
  28.  
  29.  method start
  30.   if timer=null then timer=Thread(this)         /* new thread */
  31.   timer.setPriority(Thread.MAX_PRIORITY)      /* time matters */
  32.   timer.start                             /* start the thread */
  33.  
  34.  method stop
  35.   if timer\=null then do                       /* have thread */
  36.     timer.stop                               /* .. so stop it */
  37.     timer=null                              /* .. and discard */
  38.     end
  39.  
  40.  method run
  41.   /* Use the Java Date class to get the time */
  42.   loop while timer\=null
  43.     parse Date() . . . now .           /* time is fourth word */
  44.     this.repaint                                 /* redisplay */
  45.     parse now ':' ':'secs                  /* where in minute */
  46.     wait=30-secs                /* calculate delay in seconds */
  47.     if wait<=0 then wait=wait+60
  48.     /* say 'secs, wait:' secs wait */
  49.     Thread.sleep(1000*wait)          /* wait for milliseconds */
  50.   catch InterruptedException
  51.     say 'Interrupted...'
  52.   end
  53.   timer=null                                          /* done */
  54.  
  55.  method paint(g=Graphics)
  56.   g.drawString(wordtime(now), offsetx, offsety)    /* show it */
  57.  
  58.  /* WORDTIME -- a cut-down version of QTIME.REXX
  59.     Arg1 is the time string (hh:mm:ss)
  60.     Returns the time in english, as a Rexx string
  61.     */
  62.  method wordtime(arg) static returns Rexx
  63.   /* Extract the hours, minutes, and seconds from the time.   */
  64.   parse arg hour':'min':'sec
  65.   if sec>29 then min=min+1                /* round up minutes */
  66.  
  67.   /* Nearness phrases - this time using an array              */
  68.   near=Rexx[5]                                  /* five items */
  69.   near[0]=''                                         /* exact */
  70.   near[1]=' just gone';  near[2]=' just after'       /* after */
  71.   near[3]=' nearly';     near[4]=' almost'          /* before */
  72.  
  73.   mod=min//5              /* where we are in 5 minute bracket */
  74.   out="It's"near[mod]            /* start building the result */
  75.   if min>32 then hour=hour+1         /* we are TO the hour... */
  76.   min=min+2     /* shift minutes to straddle a 5-minute point */
  77.  
  78.   /* Now special-case the result for Noon and Midnight hours  */
  79.   if hour//12=0 & min//60<=4 then do
  80.     if hour=12 then return out 'Noon.'
  81.     return 'Midnight.'
  82.     end
  83.  
  84.   min=min-(min//5)                     /* find nearest 5 mins */
  85.   if hour>12
  86.    then hour=hour-12              /* get rid of 24-hour clock */
  87.    else
  88.     if hour=0 then hour=12       /* .. and allow for midnight */
  89.  
  90.   /* Determine the phrase to use for each 5-minute segment    */
  91.   select
  92.     when min=0  then nop               /* add "o'clock" later */
  93.     when min=60 then min=0                           /* ditto */
  94.     when min= 5 then out=out 'five past'
  95.     when min=10 then out=out 'ten past'
  96.     when min=15 then out=out 'a quarter past'
  97.     when min=20 then out=out 'twenty past'
  98.     when min=25 then out=out 'twenty-five past'
  99.     when min=30 then out=out 'half past'
  100.     when min=35 then out=out 'twenty-five to'
  101.     when min=40 then out=out 'twenty to'
  102.     when min=45 then out=out 'a quarter to'
  103.     when min=50 then out=out 'ten to'
  104.     when min=55 then out=out 'five to'
  105.     end
  106.  
  107.   numbers='one two three four five six'-      /* continuation */
  108.     'seven eight nine ten eleven twelve '
  109.   out=out numbers.word(hour)           /* add the hour number */
  110.   if min=0 then out=out "o'clock"  /* .. and o'clock if exact */
  111.  
  112.   return out'.'                    /* return the final result */
  113.  
  114. /* Mike Cowlishaw,  December 1979 - January 1985. */
  115. /* NetRexx version March 1996; applet April 1996. */
  116.