Hi, Jeff, Bo, et al
Sorry to beat a dead horse, but as Larry Wall has said, there's more than
one way to do things (and apologies to Bertrand Meyer ;-).
Jeff and others politely pointed out my programming-logic gaff from early
this morning. However, being a REBOL newbie, I have really enjoyed seeing
all the ways to tackle the problem (great way to learn the tricks of the
language). I would normally be more than satisfied with Jeff's solution:
time-ampm: func [dt [time!] /dd/merid][
merid: pick [PM AM] found? all [24:0 > dt dt >= 12:0 ]
reduce [(dd: dt // 12:0) + pick [12:0 0] 1:0 > dd merid]
]
but I was bothered that I couldn't get the logic to be correct using few
tricks without the code seeming to grow exponentially in comparison to
Jeff's, with his neat tricks. I mulled on it this evening and came up with:
time-ampm-mine: func [dt [time!]] [
either dt >= 12:0 [
join (either dt < 13:0 [dt] [dt - 12:0]) "PM"
][
join (either dt < 1:0 [dt + 12:0] [dt]) "AM"]
]
It seems to work at all the "boundary" cases, but please yell if I
overlooked something. Being curious as to the affect that the differences
in sources would have on execution time, I set up a loop to iterate the
functions 1 million times and measured times. On my 500 mhz (de)Celeron, my
function averaged 22 to 23 seconds, and Jeff's averaged 38 to 40 seconds. I
still prefer Jeff's because of the "beauty" of the tricks used.
As always, this information and 35 cents may buy you a phone call.
Cheers, Scott
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, January 19, 2001 1:39 PM
Subject: [REBOL] Re: how do I get ampm format for time?
>
>
> Howdy, Bo:
>
> > Before anyone else pipes in, I just realized I used a few
> > extra characters. If I'm gonna try to do this succinctly,
> > I might as well go all out, eh? ;-)
> >
> > time-ampm: func[dt][join dt // 12:0[pick"AP"dt < 12:0"M"]]
> >
> > I don't recommend doing this for readability's sake, but it
> > does show the smartness of the language parser. And I did
> > challenge someone to make it shorter...
> >
> > -Bo
>
>
> Sweet and short, but it gives unusual results around noon and
> midnight:
>
> >> time-ampm 12:00
> == "0:00PM"
> >> time-ampm 24:00
> == "0:00PM"
> >> time-ampm :59
> == "0:59AM"
> >> time-ampm 12:59
> == "0:59PM
>
> It would be nice to be able to distinguish 12:00 AM from
> 12:00 PM and to have the typical digital clock format which
> includes the hour 12-- so, a bit longer but well behaved
> around lunch and the witching hour:
>
> time-ampm: func [dt [time!] /dd/merid][
> merid: pick [PM AM] found? all [24:0 > dt dt >= 12:0 ]
> reduce [(dd: dt // 12:0) + pick [12:0 0] 1:0 > dd merid]
> ]
>
> >> time-ampm 12:0
> == [12:00 PM]
> >> time-ampm 24:0
> == [12:00 AM]
> >> time-ampm 12:59
> == [12:59 PM]
> >> time-ampm :59
> == [12:59 AM]
>
>
> -jeff
> --
> To unsubscribe from this list, please send an email to
> [EMAIL PROTECTED] with "unsubscribe" in the
> subject, without the quotes.
>
--
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the
subject, without the quotes.