I had a very quiet schedule at work today, so I thought I'd finish this

script. In theory, it should generate a random time between two moments

specified, round it off to a conventional interval if desired, and

convert it into text if desired. The text conversion routine is a

separate function which could be used more generally if desired.

It mostly seems to work ok. Anyone want to either borrow it or show me

some horribly obvious flaw that I've missed, or show me how my code is

woefully inefficient? I'm just inordinately pleased that I managed to

write all this without blatantly copying any pre-existing code.

Yeah, this is going to be used for one of my language learning games on

my website, strange as it may seem.

--

Fabian

Visit my website often and for long periods!

http://www.lajzar.co.uk
function timr(s,e,r,c,t) {

/*

created and (c) by Fabian van-de-l'Isle 2003-10-24

Free for non-profit use

May be distributed freely

No fee may be charged for distribution

(s) earliest random time, 24 hour decimal notation

(e) latest random time, 24 hour decimal notation

For random times spanning midnight, use s=23 and e=25 (or whatever)

(r) rounding mode:

0 - 1 minute

1 - 5 minutes

2 - 10 minutes

3 - 15 minutes

4 - 30 minutes

5 - 60 minutes

(c) clock mode:

0 - 24 hour clock

1 - 12 hour clock, am/pm style

2 - 12 hour clock, verbose text style

3 - 12 hour clock, null style

(t) output text mode:

0 - numerals - 7:31

1 - text - seven thirty one

*/

//generate an output time in 24 hour decimal

var o, hh, mm;

while (e < s) { e = e + 24; }

o = (Math.random() * (e-s)) + s;

if (o > 24) { o = o - 24; }

// o should now be between 0 and 23.999

// round to the specified minutes value

var rav = [[60,12,6,4,2,1], [1,5,10,15,30,60]];

o = (o * rav[0][r]) + 0.5;

o = Math.floor(o);

o = o * rav[1][r];

// o is the rounded time in minutes, 0 to 1439

// force into 12 hour clock if mode is so set

var tt = "";

var oo = o;

if (c == 1) {

tt = " am";

if (o >= 720) { tt = " pm"; }

}

if (c == 2) {

tt = " at night";

if (o > 300) { tt = " in the morning"; }

if (o > 720) { tt = " in the afternoon"; }

if (o > 1080) { tt = " in the evening"; }

if (o > 1320) { tt = " at night"; }

}

if (c == 3) {

tt = "";

}

if (c>0) {

if (oo < 60) { oo = oo + 60; }

if (oo >= 780) { oo = oo - 720; }

}

// end 12 hour clock modes

// generate outputs

hh = Math.floor(oo / 60);

while (oo > 59) { oo = oo - 60; }

mm = oo;

var finl;

//numeric outputs

if (t == 0) {

if (mm < 10) { mm = "0" + mm; }

finl = hh + ":" + mm + tt;

}

// text mode outputs

if (t == 1) {

finl = numri(hh) + " ";

if (mm==0) { finl = finl + "o' clock" + tt; }

else {

if (mm<10) { finl = finl + "oh "; }

finl = finl + numri(mm) + tt;

}

if (o == 0) { finl = "midnight"; }

if (o == 720) { finl = "midday"; }

}

return finl;

}

function numri(i) {

// billions are US billions. replace "billion" string with "hundred

million" for traditional UK usage.

// comment out "and" lines for strict US usage.

var uu, tu, hu;

var ut, tt, ht;

var um, tm, hm;

var ub, tb, hb;

var leng

var txt = "";

i = Math.floor(i) + "";

leng = i.length;

hb = i.substring(leng -12,leng -11);

tb = i.substring(leng -11,leng -10);

ub = i.substring(leng -10,leng - 9);

hm = i.substring(leng - 9,leng - 8);

tm = i.substring(leng - 8,leng - 7);

um = i.substring(leng - 7,leng - 6);

ht = i.substring(leng - 6,leng - 5);

tt = i.substring(leng - 5,leng - 4);

ut = i.substring(leng - 4,leng - 3);

hu = i.substring(leng - 3,leng - 2);

tu = i.substring(leng - 2,leng - 1);

uu = i.substring(leng - 1,leng);

if ((hb + tb + ub) > 0) {

if (hb > 0) {

txt = txt + anmar(hb) + " hundred";

if ((tb + ub) > 0) { txt = txt + " and "; }

}

if (tb > 1) {

txt = txt + anmar(tb * 10);

if (ub > 0) { txt = txt + " " + anmar(ub); }

}

if (((tb + ub) > 0) && (tb<2)) { txt = txt + anmar(tb + ub); }

txt = txt + " billion";

}

if (txt != "") { txt = txt + " ";}

if ((hm + tm + um) > 0) {

if (hm > 0) {

txt = txt + anmar(hm) + " hundred";

if ((tm + um) > 0) { txt = txt + " and "; }

}

if (tm > 1) {

txt = txt + anmar(tm * 10);

if (um > 0) { txt = txt + " " + anmar(um); }

}

if (((tm + um) > 0) && (tm<2)) { txt = txt + anmar(tm + um); }

txt = txt + " million";

}

if (txt != "") { txt = txt + " ";}

if ((ht + tt + ut) > 0) {

if (ht > 0) {

txt = txt + anmar(ht) + " hundred";

if ((tt + ut) > 0) { txt = txt + " and "; }

}

if (tt > 1) {

txt = txt + anmar(tt * 10);

if (ut > 0) { txt = txt + " " + anmar(ut); }

}

if (((tt + ut) > 0) && (tt<2)) { txt = txt + anmar(tt + ut); }

txt = txt + " thousand";

}

if (txt != "") { txt = txt + " ";}

if ((hu + tu + uu) > 0) {

if (hu > 0) {

txt = txt + anmar(hu) + " hundred";

if ((tu + uu) > 0) { txt = txt + " and "; }

}

if (tu > 1) {

txt = txt + anmar(tu * 10);

if (uu > 0) { txt = txt + " " + anmar(uu); }

}

if (((tu + uu) > 0) && (tu<2)) { txt = txt + anmar(tu + uu); }

}

return txt;

}

function anmar(i) {

if (i == 1) { return "one"; }

if (i == 2) { return "two"; }

if (i == 3) { return "three"; }

if (i == 4) { return "four"; }

if (i == 5) { return "five"; }

if (i == 6) { return "six"; }

if (i == 7) { return "seven"; }

if (i == 8) { return "eight"; }

if (i == 9) { return "nine"; }

if (i == 10) { return "ten"; }

if (i == 11) { return "eleven"; }

if (i == 12) { return "twelve"; }

if (i == 13) { return "thirteen"; }

if (i == 14) { return "fourteen"; }

if (i == 15) { return "fifteen"; }

if (i == 16) { return "sixteen"; }

if (i == 17) { return "seventeen"; }

if (i == 18) { return "eighteen"; }

if (i == 19) { return "nineteen"; }

if (i == 20) { return "twenty"; }

if (i == 30) { return "thirty"; }

if (i == 40) { return "forty"; }

if (i == 50) { return "fifty"; }

if (i == 60) { return "sixty"; }

if (i == 70) { return "seventy"; }

if (i == 80) { return "eighty"; }

if (i == 90) { return "ninety"; }

return null;

}