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