Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Displaying AM / PM times

From: joel:neely:fedex at: 27-Aug-2001 2:54

[sqlab--gmx--net] wrote:
> I use 12 hours schemes very rarely, but I see nice-time giving > two times per day the same time. > > 12:00 ==> 12:00 PM > 24:00 ==> 12:00 PM >
J. Random Manonthestreet will not likely be comfortable with identifying which of 12:00 AM and 12:00 PM are "noon" and midnight , but will have to stop and think about it. Also, 24:00 is not a valid 24-hour time *of day* (00:00..23:59), although it is a completely valid time *interval*. REBOL blurs the distinction between those two meanings of the word "time", which allows conversations like the following:
>> foo: 37:48 == 37:48 >> print foo 37:48 >> type? foo == time! >> now == 27-Aug-2001/2:25:04-5:00 >> now + foo == 28-Aug-2001/16:13:14-5:00 >> now/time == 2:25:20 >> now/time + foo == 40:13:24
Evaluating DATE! + TIME! appears to normalize the time to time-of-day, while calculating TIME! + TIME! appears not to do so. (Good reasons not to use 12-hour time... ;-)
> So I would use > if time/1 = 0 [time/1: 12 am: " NOON"] > .) >
Not quite! The full set of exceptions and boundaries is: 00:00 => 12:00 MIDNIGHT 00:01 => 12:01 AM ... ... 00:59 => 12:59 AM 01:00 => 01:00 AM ... ... 11:59 => 11:59 AM 12:00 => 12:00 NOON 12:01 => 12:01 PM 12:59 => 12:59 PM 13:00 => 01:00 PM ... ... 23:29 => 11:59 PM
> > I believe Carl is the origin of this one.. > > > > nice-time: func [time /local am][ > > time/3: 0 > > am: pick [" AM" " PM"] time < 12:00 > > if time >= 12:00 [time: time - 12:00] > > if time/1 = 0 [time/1: 12] > > rejoin [time am] > > ] > >
Incorporating the above tweaks, we get a "human time-of-day" function: humantod: func [t [time!] /local h suf] [ t/3: 0 suf: pick ["AM" "PM"] 12 > h: t/1: t/1 // 24 if h >= 12 [t: t - 12:00] if t/1 = 0 [ t/1: 12 if t = 12:00 [ suf: pick ["NOON" "MIDN"] 12 = h ] ] rejoin [t " " suf] ] which can be tested as follows:
>> do func [/local it tt ht] [
[ for it 0 86400 30 * 60 [ [ ht: humantod tt: to-time it [ print [ht tt it] [ ] [ ] 12:00 MIDN 0:00 0 12:30 AM 0:30 1800 1:00 AM 1:00 3600 1:30 AM 1:30 5400 [snip] 8:30 AM 8:30 30600 9:00 AM 9:00 32400 9:30 AM 9:30 34200 10:00 AM 10:00 36000 10:30 AM 10:30 37800 11:00 AM 11:00 39600 11:30 AM 11:30 41400 12:00 NOON 12:00 43200 12:30 PM 12:30 45000 1:00 PM 13:00 46800 1:30 PM 13:30 48600 [snip] 9:30 PM 21:30 77400 10:00 PM 22:00 79200 10:30 PM 22:30 81000 11:00 PM 23:00 82800 11:30 PM 23:30 84600 12:00 MIDN 24:00 86400 HTH! -jn- -- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com