Telling time in French

PHP - 521 473 bytes

I've added some newlines for readability:

$w=split(A,'AuneAdeuxAtroisAquatreAcinqAsixAseptAhuitAneufAdixAonzeAdouzeAtreizeA
quatorzeAAseizeAdix-septAdix-huitAdix-neufAvingtAvingt et une');$h=idate('H');if(
$r=($m=idate('i'))>30){$h=++$h%24;$m=60-$m;}echo'Il est '.($h?$h==12?midi:$w[$h%12]
.' heure'.($h%12<2?'':s):minuit).($r?' moins':'').($m==15?($r?' le ':' et ').quart
:($m==30?' et demi':($m?' '.($m<22?$w[$m]:"$w[20]-".$w[$m%10]):''))).($h%12?($h-=$r
)<12?' du matin':($h<17?" de l'après-midi":' du soir'):'').'.';

The method used to converting a number to its numeral in French is inspired by this answer on an other challenge by edc65.

Here is the ungolfed version:

/** Define numerals **/
$numerals = [
    '', 'une', 'deux', 'trois', 'quatre',
    'cinq', 'six', 'sept', 'huit', 'neuf',
    'dix', 'onze', 'douze', 'treize', 'quatorze',
    '', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf',
    'vingt', 'vingt et une'
];

/** Get current time **/
$hours = idate('H');
$minutes = idate('i');

/** Check if we need to count in reverse **/
$reverse = $minutes > 30;
if ($reverse) {
    $hours = ($hours + 1) % 24;
    $minutes = 60 - $minutes;
}

echo 'Il est ';

/** Print hours **/
if ($hours === 12) {
    echo 'midi';
}
else if ($hours === 0) {
    echo 'minuit';
}
else
{
    echo $numerals[$hours % 12] .' heure';
    if ($hours % 12 !== 1) {
        echo 's';
    }
}

/** Print minutes **/
if ($reverse) {
    echo ' moins';
}

if ($minutes === 15)
{
    if ($reverse) {
        echo ' le ';
    }
    else {
        echo ' et ';
    }

    echo 'quart';
}
else if ($minutes === 30) {
    echo ' et demi';
}
else if ($minutes !== 0)
{
    echo ' ';

    if ($minutes < 22) {
        echo $numerals[$minutes];
    }
    else {
        echo $numerals[20] .'-'. $numerals[$minutes % 10];
    }
}

/** Print daytime **/
if ($hours % 12 !== 0)
{
    if ($reverse) {
        --$hours;
    }

    if ($hours < 12) {
        echo ' du matin';
    }
    else if ($hours < 17) {
        echo " de l'après-midi";
    }
    else {
        echo ' du soir';
    }
}

echo '.';

Python 3, 586 547 556 506 505 502 498 497 493 Bytes

My first attempt at golfing anything. I’m really not sure about the way I choose, especially the n list. But I wanted to give it a try.

from datetime import*;t=datetime.now()
n='/une/deux/trois/quatre/cinq/six/sept/huit/neuf/dix/onze/douze/treize/quatorze/et quart/seize/dix-sept/dix-huit/dix-neuf/vingt//et demi'.split('/')
n[21:22]=['vingt-'+i for i in['et-une']+n[2:10]]
h,m=t.hour,t.minute;s=h//12;h%=12
e=' d'+('u matin',("e l'après-midi","u soir")[h>4])[s]
try:m=' '[:m]+n[m]
except:m=' moins '+(n[60-m],'le quart')[m==45];h+=1
e,h=('',e,('minuit','midi')[h//12^s],n[h]+' heures'[:h+5])[h%12>0::2]
print('Il est',h+m+e+'.')

Ungolfed:

from datetime import datetime

time = datetime.now()
numbers = [
    '', 'une', 'deux', 'trois', 'quatre',
    'cinq', 'six', 'sept', 'huit', 'neuf',
    'dix', 'onze', 'douze', 'treize', 'quatorze',
    'et quart', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf',
    'vingt', 'vingt-et-une', 'vingt-deux', 'vingt-trois', 'vingt-quatre',
    'vingt-cinq', 'vingt-six', 'vingt-sept', 'vingt-huit', 'vingt-neuf',
    'et demi']

status, hour = divmod(time.hour, 12)
if status:
    ending = " du soir" if h>4 else " de l’après-midi"
else:
    ending = " du matin"

try:
    if not time.minute:
        minutes = ""
    else:
        minutes = " " + numbers[time.minute]
except IndexError:
    hour += 1
    if time.minute == 45:
        minutes = " moins le quart"
    else:
        minutes = " moins " + numbers[60 - time.minute]

if hour%12:    # 'whole' hours
    hours = numbers[hour] + " heures"
    if hour == 1:
        # removing extra 's'
        hours = hours[:-1]
else:          # midnight or noon
    ending = ""
    if (hour == 12 and status) or (hour == 0 and status == 0):
        hours = "minuit"
    else:
        hours = "midi"

print('Il est', hours + minutes + ending + '.')

Javascript (ES6), 506 495 bytes

Edit: Compressed a to save a few bytes.

a=`
un
j
k
yre
h
six
w
v
p}
onldoultreilyorl
seize}-w}-v}-p~~ et un{j{k{yre{h{six{w{v{p
et demi`,[...`hjklpvwy{}~`].map((x,w)=>a=a.split(x).join(`cinq|deux|trois|ze
|neuf|huit|sept|quat|~-|
dix|
vingt`.split`|`[w])),a=a.split`
`,alert(`Il est ${(e=(d=((b=(z=new Date).getHours())+(y=(c=z.getMinutes())>30))%24)%12)?a[e]+` heure${e-1?"s":""}`:d?'midi':'minuit'} ${y?"moins ":""}${y?c-45?a[60-c]:'le quart':c-15?a[c]:'et quart'}${e?[' du matin'," de l'apres-midi",' du soir'][(b>12)+(b>16)]:""}.`)

Explanation:

a = 'compressed string of french numbers with 'et demi' for 30 and 15 removed';
a = // code that decompresses a

// a,b,c,d,e,y,z are all inlined into the first expression that uses them
z = new Date();
b = z.getHours();
c = z.getMinutes();
y = c > 30; // true if 60 - minutes, false otherwise
d = (b + y) % 24; // the next hour if y
e = d % 12; // is it not noon and not midnight?

alert(
    "Il est " +
    // add the hour
    // if d is not 0 or 12, add d + 'heure(s)'
    (e ? a[e] + ` heure${e-1?"s":""}` : d ? 'midi' : 'minuit') +
    " " +
    (y ? "moins " : "") + // add 'moins' if necessary

    ( // add the minute
      y? // necessary to subtract from 60?
        c-45?
          a[60-c] // add normal number word
          :'le quart' // handle special case for 45
        :c-15?
          a[c] // add normal word
          :'et quart' // handle special case for 15
    ) +
    ( // select appropriate ending
      e? // is it not noon and not midnight?
        // create array of endings
        [" du matin"," de l'apres-midi"," du soir"]
          // selects item 0 if b in [0, 12], item 1 if b in [13, 16] and item 2 if b > 16
          [(b > 12) + (b > 16)]
        :"" // if it's noon or midnight, no ending is necessary
    ) +
    "." // add period
)