Skip to content

Instantly share code, notes, and snippets.

@nitinbhojwani
Created December 3, 2015 17:58
Show Gist options
  • Save nitinbhojwani/f303a04926006cdd101a to your computer and use it in GitHub Desktop.
Save nitinbhojwani/f303a04926006cdd101a to your computer and use it in GitHub Desktop.
More Date Madness
def num_to_words(num):
units = ["", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine "]
teens = ["", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"]
thousands = ["","thousand", "million", "billion", "trillion",
"quadrillion", "quintillion", "sextillion", "septillion", "octillion",
"nonillion", "decillion", "undecillion", "duodecillion", "tredecillion",
"quattuordecillion", "sexdecillion", "septendecillion", "octodecillion",
"novemdecillion", "vigintillion "]
words = []
if num == 0:
words.append("zero")
else:
numStr = "%d" % num
numStrLen = len(numStr)
groups = (numStrLen + 2) / 3
numStr = numStr.zfill(groups * 3)
for i in range(0, groups*3, 3):
h = int(numStr[i])
t = int(numStr[i+1])
u = int(numStr[i+2])
g = groups - (i / 3 + 1)
if h >= 1:
words.append(units[h])
words.append("hundred")
if t > 1:
words.append(tens[t])
if u >= 1:
words.append(units[u])
elif t == 1:
if u >= 1:
words.append(teens[u])
else:
words.append(tens[t])
else:
if u >= 1:
words.append(units[u])
if g >= 1 and (h + t + u) > 0:
words.append(thousands[g])
return words
def day_to_rank(day):
try:
day_to_values = {
1: 'first',
2: 'second',
3: 'third',
4: 'fourth',
5: 'fifth',
6: 'sixth',
7: 'seventh',
8: 'eighth',
9: 'ninth',
10: 'tenth',
11: 'eleventh',
12: 'twelfth',
13: 'thirteenth',
14: 'fourteenth',
15: 'fifteenth',
16: 'sixteenth',
17: 'seventeenth',
18: 'eighteenth',
19: 'nineteenth',
20: 'twentieth',
30: 'thirtieth'
}
day = int(day)
if day_to_values.get(day):
return day_to_values.get(day)
elif day > 20 and day < 30:
return 'Twenty ' + day_to_values.get(day%10)
elif day == 31:
return 'Thirty ' + day_to_values.get(day%10)
except:
return ''
return ''
def num_to_month(num):
num_month_map = [
'',
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]
if num<=12 and num >=1:
return num_month_map[num]
else:
return ''
def isLeapYear(year):
try:
year = int(year)
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
return True
else:
return False
else:
return True
else:
return False
except:
return False
def num_days(mon, year):
mon_num_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if isLeapYear(year) and mon == 2:
return 29
else:
return mon_num_days[mon]
def validate_date(date):
day = date.day
month = date.month
year = date.year
if year <= 0:
print "Year's value is invalid"
if month <= 0 or month > 12:
print "Month's value is invalid"
exit()
# validate for month and day max values
if day <= 0 or day > num_days(month, year):
print "Day's value is invalid for given month and year"
exit()
return
def print_formatted_date(date):
day = date.day
month = date.month
year = date.year
year_len = len(str(year))
year = '0'*(4-year_len)+str(year)
output = ''
output += day_to_rank(int(day)).title()
output += ' of ' + num_to_month(int(month)).title() + ', '
output += ' '.join(num_to_words(int(year[0:2]))).title() + ' ' + ' '.join(num_to_words(int(year[2:4]))).title()
print output
# Calling
print 'allowed date >= 01-01-0001'
date_input = raw_input('Enter Date: ')
date_input = date_input.strip()
from datetime import datetime
try:
date_input = datetime.strptime(date_input, '%d-%m-%Y')
except ValueError:
print 'invalid date'
exit()
validate_date(date_input)
print_formatted_date(date_input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment