# Code Golf - Word Clock

Write a program which takes the current time of the system clock, rounds to the nearest minute (you may choose how to round halves) and displays it formatted as the following:

A quarter past one in the morning.
A quarter to two in the afternoon.
Two o'clock in the afternoon.
Twenty-five minutes to three in the morning.
Twenty-seven minutes to seven in the evening.
One minute past twelve in the afternoon.
Twelve o'clock noon.
Twelve o'clock midnight.


Rules:

• 12:01am to 11:59am is deemed as morning.
• 12:01pm to 5:59pm is deemed as afternoon.
• 6:00pm to 11:59pm is deemed as evening.
• 15 minutes past the hour must be expressed as A quarter past the hour
• 30 minutes past the hour must be expressed as Half past the hour
• 45 minutes past the hour must be expressed as A quarter to the upcoming hour
• On-the-hour times must be expressed as the hour o'clock, with the addition of noon and midnight being expressed with the addition of noon or midnight respectively.
• Other times in hh:01 to hh:29 must be expressed as (number of minutes) past the hour.
• Other times in hh:31 to hh:59 must be expressed as (number of minutes) to the upcoming hour.
• Twenty-one to twenty-nine must be hyphenated.
• The first letter of the output must be capitalised. The other letters must be lower-case.

It would be useful to provide a testing version of your code so that it can be tested without manipulating the system clock.

• Let's see what you got... - The specification is still a bit vague. E.g. what about Fifteen minutes past one or Fourty-five minutes past one for the first two examples? – Howard Dec 10 '13 at 7:57
• I agree with Howard: there's a lot of dialectal variation here. To ensure that the contest is about golfing you need a complete spec. You also need to address rounding because the time is rarely an integer number of minutes past midnight. – Peter Taylor Dec 10 '13 at 8:39
• What? Noon is noon, and midnight is midnight. I suggest those times read "Twelve o'clock noon" and "Twelve o'clock midnight". Noon is not afternoon, or 0=1. – boothby Dec 10 '13 at 9:21
• Okay, so what do I print at 3:30 PM? Do I have to say "half past three in the afternoon"? – Doorknob Dec 10 '13 at 13:15
• I'm voting to close until the spec is clarified to avoid more people posting solutions which might have to be withdrawn for not meeting the clarified spec. – Peter Taylor Dec 10 '13 at 14:23

## Python 2.7, 159162156 161

from datetime import*
n=datetime.now()
if n.hour()<12:
t="morning"
elif n.hour()<18:
t="afternoon"
else:
t="evening"
print"%i in the %s."%(n.hour(),t)


Edit #1 - Added a period at the end of the output.

Edit #2 - Shortened the imports, lengthened code to correct syntax.

• Why the verbosity in the print statement? You're calling str on... a string... anyway, much shorter: print"%i in the %s."%(n.hour,t) (and then you can remove the period from the other strings, saving even more chars – Doorknob Dec 10 '13 at 13:16
• Attempting to test on ideone gives syntax error. (I was attempted to test because on inspection this doesn't seem to meet the spec, regardless of how the current ambiguities are resolved). – Peter Taylor Dec 10 '13 at 14:22
• Would be helpful to show an example with input and output. Looks to me like numbers are rendered as digits rather than letter strings. – DavidC Dec 10 '13 at 16:07
• @DavidCarraher They are – Timtech Dec 10 '13 at 16:15
• @Tim, Did you read the spec? It needs to output the time in a written format as I mentioned above... – WallyWest Dec 10 '13 at 23:33

# Mathematica 450

Date[] calls for the current date and time. [[{4,5}]] returns the hour and minutes

f[]:=f[Date[][[{4,5}]]];
f[{h_,m_}]:=Module[{p,w,s},
p@h1_:=Which[0<h<12," in the morning.",12<h<18," in the afternoon.",18<h<24, " in the evening."];
s[t_]:=WolframAlpha["spell "<>ToString@t,{{"Result",1},"Plaintext"}];
w=s[h~Mod~12]<>p@h;
Which[
m==0, Switch[h,12, "noon", 0|24,"midnight",_,s[h~Mod~12]<>p@h],
m==15,"a quarter past "<>w,
m==30,"half past "<>w,
m==45,"a quarter to "<>s[h~Mod~12+1]<>p@h,
m<30,s@m<>" minutes after "<>w,
m>29,s[60-m]<>" minutes before "<>s[h~Mod~12+1]<>p@h]]


Current Time

f[]


"ten minutes past three in the afternoon."

Specific times

f[{4, 45}]
f[{10, 30}]
f[{11, 0}]
f[{12, 0}]
f[{13, 15}]
f[{15, 0}]
f[{22, 17}]
f[{24, 0}]


"a quarter to five in the morning."
"half past ten in the morning."
"eleven in the morning."
"noon"
"a quarter past one in the afternoon."
"three in the afternoon."
"seventeen minutes past ten in the evening."
"midnight"

• I noticed I still need to implement "minutes before midnight/noon" and "n minutes after midnight/noon". Will get to that later. – DavidC Dec 10 '13 at 20:24
• You can save a few characters and get in spec by using "to" rather than "before" and "past" rather than "after". – Peter Taylor Dec 12 '13 at 17:20