Skip to content

Instantly share code, notes, and snippets.

@philipnorton42
Last active Oct 3, 2021
Embed
What would you like to do?
Converting The Current Time Into A Sentence In Python (see https://www.hashbangcode.com/article/converting-current-time-sentence-python)
# The following code will print out the current time as a sentence.
# See https://www.hashbangcode.com/article/converting-current-time-sentence-python for information on this code.
def translate_to_or_past(minute):
to_or_past = ''
if 3 <= minute < 33:
to_or_past = 'PAST'
elif 33 <= minute <= 57:
to_or_past = 'TO'
return to_or_past
def translate_minute(minute):
if (minute > 30):
minute = 60 - minute
if minute >= 3:
minute_blocks = ['FIVE', 'TEN', 'A QUARTER', 'TWENTY', 'TWENTYFIVE', 'HALF']
mapped_minute_value = round((0 + (5 - 0) * ((minute - 3) / (28 - 3))) - 0.4)
minute_name = minute_blocks[mapped_minute_value]
else:
minute_name = ''
return minute_name
def translate_hour(hour, minute):
hours = [
'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN', 'ELEVEN', 'TWELVE', 'ONE'
]
if minute >= 33:
return hours[hour]
else:
return hours[hour - 1]
def translate_time(hour, minute, am_or_pm):
translated_minute = translate_minute(minute)
translated_hour = translate_hour(hour, minute)
translated_to_or_past = translate_to_or_past(minute)
if translated_to_or_past == '':
return "IT IS %s %s %s" % (translated_hour, 'O\'CLOCK', am_or_pm)
else:
return "IT IS %s %s %s %s" % (translated_minute, translated_to_or_past, translated_hour, am_or_pm)
# Run the code.
import time
current_time = time.localtime()
hour = int(time.strftime("%I", current_time))
minute = int(time.strftime("%M", current_time))
am_or_pm = time.strftime("%p", current_time)
print(translate_time(hour, minute, am_or_pm))
# Unit test the code.
import unittest
class TestTimeTranslateMethods(unittest.TestCase):
def test_translate_to_or_past(self):
self.assertEqual(translate_to_or_past(0), '')
self.assertEqual(translate_to_or_past(1), '')
self.assertEqual(translate_to_or_past(2), '')
self.assertEqual(translate_to_or_past(3), 'PAST')
self.assertEqual(translate_to_or_past(15), 'PAST')
self.assertEqual(translate_to_or_past(29), 'PAST')
self.assertEqual(translate_to_or_past(30), 'PAST')
self.assertEqual(translate_to_or_past(31), 'PAST')
self.assertEqual(translate_to_or_past(33), 'TO')
self.assertEqual(translate_to_or_past(34), 'TO')
self.assertEqual(translate_to_or_past(45), 'TO')
self.assertEqual(translate_to_or_past(57), 'TO')
self.assertEqual(translate_to_or_past(58), '')
self.assertEqual(translate_to_or_past(59), '')
def test_translate_minute(self):
self.assertEqual(translate_minute(1), '')
self.assertEqual(translate_minute(2), '')
self.assertEqual(translate_minute(3), 'FIVE')
self.assertEqual(translate_minute(7), 'FIVE')
self.assertEqual(translate_minute(53), 'FIVE')
self.assertEqual(translate_minute(57), 'FIVE')
self.assertEqual(translate_minute(8), 'TEN')
self.assertEqual(translate_minute(12), 'TEN')
self.assertEqual(translate_minute(48), 'TEN')
self.assertEqual(translate_minute(52), 'TEN')
self.assertEqual(translate_minute(13), 'A QUARTER')
self.assertEqual(translate_minute(17), 'A QUARTER')
self.assertEqual(translate_minute(47), 'A QUARTER')
self.assertEqual(translate_minute(43), 'A QUARTER')
self.assertEqual(translate_minute(18), 'TWENTY')
self.assertEqual(translate_minute(22), 'TWENTY')
self.assertEqual(translate_minute(38), 'TWENTY')
self.assertEqual(translate_minute(42), 'TWENTY')
self.assertEqual(translate_minute(37), 'TWENTYFIVE')
self.assertEqual(translate_minute(24), 'TWENTYFIVE')
self.assertEqual(translate_minute(27), 'TWENTYFIVE')
self.assertEqual(translate_minute(36), 'TWENTYFIVE')
self.assertEqual(translate_minute(33), 'TWENTYFIVE')
self.assertEqual(translate_minute(28), 'HALF')
self.assertEqual(translate_minute(32), 'HALF')
def test_translate_hour(self):
self.assertEqual(translate_hour(1, 29), 'ONE')
self.assertEqual(translate_hour(2, 29), 'TWO')
self.assertEqual(translate_hour(3, 29), 'THREE')
self.assertEqual(translate_hour(4, 29), 'FOUR')
self.assertEqual(translate_hour(5, 29), 'FIVE')
self.assertEqual(translate_hour(6, 29), 'SIX')
self.assertEqual(translate_hour(7, 29), 'SEVEN')
self.assertEqual(translate_hour(8, 29), 'EIGHT')
self.assertEqual(translate_hour(9, 29), 'NINE')
self.assertEqual(translate_hour(10, 29), 'TEN')
self.assertEqual(translate_hour(11, 29), 'ELEVEN')
self.assertEqual(translate_hour(12, 29), 'TWELVE')
self.assertEqual(translate_hour(1, 34), 'TWO')
self.assertEqual(translate_hour(2, 34), 'THREE')
self.assertEqual(translate_hour(3, 34), 'FOUR')
self.assertEqual(translate_hour(4, 34), 'FIVE')
self.assertEqual(translate_hour(5, 34), 'SIX')
self.assertEqual(translate_hour(6, 34), 'SEVEN')
self.assertEqual(translate_hour(7, 34), 'EIGHT')
self.assertEqual(translate_hour(8, 34), 'NINE')
self.assertEqual(translate_hour(9, 34), 'TEN')
self.assertEqual(translate_hour(10, 34), 'ELEVEN')
self.assertEqual(translate_hour(11, 34), 'TWELVE')
self.assertEqual(translate_hour(12, 34), 'ONE')
def test_translate_time(self):
self.assertEqual(translate_time(10, 0, 'AM'), 'IT IS TEN O\'CLOCK AM')
self.assertEqual(translate_time(10, 1, 'AM'), 'IT IS TEN O\'CLOCK AM')
self.assertEqual(translate_time(10, 2, 'AM'), 'IT IS TEN O\'CLOCK AM')
self.assertEqual(translate_time(10, 3, 'AM'), 'IT IS FIVE PAST TEN AM')
self.assertEqual(translate_time(10, 58, 'AM'), 'IT IS ELEVEN O\'CLOCK AM')
self.assertEqual(translate_time(10, 48, 'AM'), 'IT IS TEN TO ELEVEN AM')
self.assertEqual(translate_time(8, 55, 'AM'), 'IT IS FIVE TO NINE AM')
self.assertEqual(translate_time(1, 5, 'PM'), 'IT IS FIVE PAST ONE PM')
self.assertEqual(translate_time(1, 57, 'PM'), 'IT IS FIVE TO TWO PM')
self.assertEqual(translate_time(2, 14, 'PM'), 'IT IS A QUARTER PAST TWO PM')
self.assertEqual(translate_time(2, 31, 'PM'), 'IT IS HALF PAST TWO PM')
self.assertEqual(translate_time(2, 33, 'PM'), 'IT IS TWENTYFIVE TO THREE PM')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment