lang_EN.to_year(): render correct pronunciations (#144)

* Add tests

Starting work on #141, which points out that 2017 should come out
"twenty seventeen"; it currently comes out "two thousand seventeen".

* Handle English years properly.

Resolves #141.

* Whoops -- didn't PEP-8 my tests

* Handle years MORE properly...with suffixes!

- Rendering years between 10-99 was bugged (e.g. "zero fifty").
Fixed that by thinking outside the high/low text box I had made.
- Added suffixes: default none if positive, 'BC' if negative.
User-overrideable.
This commit is contained in:
Antonio Gurgel
2017-12-18 07:56:38 -08:00
committed by Ernesto Rodriguez Ortiz
parent ab54bed93a
commit 1a2b783d31
2 changed files with 69 additions and 5 deletions

View File

@@ -82,8 +82,24 @@ class Num2Word_EN(lang_EU.Num2Word_EU):
self.verify_ordinal(value)
return "%s%s" % (value, self.to_ordinal(value)[-2:])
def to_year(self, val, longval=True):
if not (val // 100) % 10:
return self.to_cardinal(val)
return self.to_splitnum(val, hightxt="hundred", jointxt="and",
longval=longval)
def to_year(self, val, suffix=None, longval=True):
if val < 0:
val = abs(val)
suffix = 'BC' if not suffix else suffix
high, low = (val // 100, val % 100)
# If year is 00XX, X00X, or beyond 9999, go cardinal.
if (high == 0
or (high % 10 == 0 and low < 10)
or high >= 100):
valtext = self.to_cardinal(val)
else:
hightext = self.to_cardinal(high)
if low == 0:
lowtext = "hundred"
elif low < 10:
lowtext = "oh-%s" % self.to_cardinal(low)
else:
lowtext = self.to_cardinal(low)
valtext = "%s %s" % (hightext, lowtext)
return (valtext if not suffix
else "%s %s" % (valtext, suffix))