Program to convert hour minutes' time to text format in Python

Converting time from numerical format (hours and minutes) to text format is a common programming task. This involves translating numbers into words and applying English time conventions like "quarter past", "half past", and "to" for times after 30 minutes.

Understanding the Time Format Rules

The conversion follows these English time conventions −

  • 8:00 : eight o'clock
  • 8:01 : one minute past eight
  • 8:10 : ten minutes past eight
  • 8:15 : quarter past eight
  • 8:30 : half past eight
  • 8:40 : twenty minutes to nine
  • 8:45 : quarter to nine
  • 8:47 : thirteen minutes to nine

Algorithm

The solution uses these steps −

  • Create a list of text representations for numbers 1-30
  • Handle special cases: exact hours (0 minutes), quarter hours (15, 45), and half hours (30)
  • For minutes 1-29: use "past" with current hour
  • For minutes 31-59: use "to" with next hour and calculate remaining minutes

Complete Implementation

def convert_time_to_text(h, m):
    text = ["one", "two", "three", "four", "five", "six", "seven", "eight", 
            "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", 
            "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "twenty-one",
            "twenty-two", "twenty-three", "twenty-four", "twenty-five",
            "twenty-six", "twenty-seven", "twenty-eight", "twenty-nine", "half"]
    
    if m == 0:
        return text[h - 1] + " o'clock"
    elif m == 1:
        return text[m - 1] + " minute past " + text[h - 1]
    elif m == 15:
        return "quarter past " + text[h - 1]
    elif m == 30:
        return "half past " + text[h - 1]
    elif m < 30:
        return text[m - 1] + " minutes past " + text[h - 1]
    elif m == 45:
        return "quarter to " + text[h % 12]
    else:
        minutes_to_next_hour = 60 - m
        next_hour = (h % 12) if h != 12 else 0
        return text[minutes_to_next_hour - 1] + " minutes to " + text[next_hour]

# Test examples
print(convert_time_to_text(8, 0))   # exact hour
print(convert_time_to_text(8, 15))  # quarter past
print(convert_time_to_text(8, 30))  # half past
print(convert_time_to_text(9, 42))  # minutes to next hour
print(convert_time_to_text(8, 45))  # quarter to
eight o'clock
quarter past eight
half past eight
eighteen minutes to ten
quarter to nine

How It Works

The function handles different time scenarios −

  • Exact hours (m=0): Returns "eight o'clock"
  • Single minute (m=1): Uses singular "minute" instead of "minutes"
  • Quarter hours (m=15, 45): Uses "quarter past/to" instead of "fifteen"
  • Half hour (m=30): Uses "half past" instead of "thirty"
  • Past the hour (m<30): Uses current hour with "past"
  • To the hour (m>30): Calculates minutes remaining and uses next hour with "to"

Key Features

Time Range Format Example
0 minutes hour o'clock eight o'clock
1-29 minutes minutes past hour ten minutes past eight
31-59 minutes minutes to next_hour twenty minutes to nine

Conclusion

This solution converts numerical time to natural English text format by handling special cases and using appropriate prepositions. The key is managing the transition from "past" to "to" at the 30-minute mark and correctly calculating the next hour for times after 30 minutes.

Updated on: 2026-03-26T14:39:18+05:30

712 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements