channelping exactly two in the morning

ProseClock an alternative to digital and analog time displays

//<![CDATA[

//  ProseClock: an alternative to digital and analog formats
//
//  Copyright (c) 2006-Eternity Gerald Gold and channelping. All rights
//  reserved. This class is free software; you may redistribute it
//  and/or modify it under the same terms as JavaScript itself.
//
//
//  USAGE:
//
//    <html>
//    <head>
//    <title>Page with ProseClock</title>
//    *** put ProseClock.js code here, within script tags ***
//    </head>
//    <body onLoad="InitProseClock('prose-clock');">
//    <div id="prose-clock" />
//    </body>
//    </html>
//
//  The div id value ("prose-clock" in the example) can be any
//  valid CSS selector string, so long as the InitProseClock()
//  argument in the body's onLoad event and the div's id value
//  are the same.  The JavaScript needs no modification.


function InitProseClock(id)
{
  var pc = new ProseClock(id);
  pc.SetPhrases();
  pc.Display();
}

function ProseClock(id)
{
  this.ID                   = id;
  this.current_time         = new Object();
  this.min_phrase           = "";
  this.mul_phrase           = "";
  this.hour_phrase          = "";
  this.mul                  = null;
  this.RefreshTimeInSeconds = 20;
}

ProseClock.prototype.Display = function()
{
  var el = document.getElementById(this.ID);
  el.innerHTML = this.GetProseTime();
  var self = this;
  setTimeout(function() {self.Display(); }, self.RefreshTimeInSeconds * 1000);
}

ProseClock.prototype.GetProseTime = function()
{
  this.get_time();
  this.set_minute();
  this.set_hour();
  var text = this.min_phrase + " " + this.mul_phrase + " " + this.hour_phrase;
  return text.replace(/ {2,}/, " ");
}

ProseClock.prototype.set_minute = function()
{
  var minutes = this.current_time.minutes;

  var remainder = minutes % 5;

  this.mul = minutes - remainder;

  // switch frame of reference
  if (remainder >= 3)
  {
    this.mul += 5;
  }

  this.min_phrase = this.minute_phrases[remainder];

  if (this.multiples[this.mul] != null)
  {
    this.mul_phrase = this.multiples[this.mul];
  }
  else
  {
    this.mul_phrase = "";
  }
}

ProseClock.prototype.set_hour = function()
{
  var hours = this.current_time.hours;

  if (this.mul >= 35)
  {
    if (hours == 23)
    {
      hours = 0;
    }
    else
    {
      hours += 1;
    }
  }
  
  this.hour_phrase = this.hour_phrases[hours];

  if ((hours == 0) && (this.current_time.minutes < 3))
  {
    this.mul_phrase = "";
  }
}

ProseClock.prototype.get_time = function()
{
  var d = new Date();
  this.current_time.hours   = d.getHours();
  this.current_time.minutes = d.getMinutes();
}

ProseClock.prototype.SetPhrases = function()
{
  this.minute_phrases = new Array("exactly",
                                  "just after",
                                  "a little after",
                                  "coming up to",
                                  "almost");

  this.hour_phrases = new Array("midnight",
                                "one in the morning",
                                "two in the morning",
                                "three in the morning",
                                "four in the morning",
                                "five in the morning",
                                "six in the morning",
                                "seven in the morning",
                                "eight in the morning",
                                "nine in the morning",
                                "ten in the morning",
                                "eleven in the morning",
                                "noon",
                                "one in the afternoon",
                                "two in the afternoon",
                                "three in the afternoon",
                                "four in the afternoon",
                                "five in the afternoon",
                                "six at night",
                                "seven at night",
                                "eight at night",
                                "nine at night",
                                "ten at night",
                                "eleven at night");

  this.multiples = new Object();
  this.multiples["0"]  = "";
  this.multiples["5"]  = "five past";
  this.multiples["10"] = "ten past";
  this.multiples["15"] = "quarter past";
  this.multiples["20"] = "twenty past";
  this.multiples["25"] = "twenty-five past";
  this.multiples["30"] = "half past";
  this.multiples["35"] = "twenty-five to";
  this.multiples["40"] = "twenty to";
  this.multiples["45"] = "quarter to";
  this.multiples["50"] = "ten to";
  this.multiples["55"] = "five to";

  return;
}

//]]>

^