# Times.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-06 File created. # 2022-12-27 Converted to Python 3. from datetime import datetime # CurrentTime is a kind of abstract class since its method show() # does not print anything. The show() method is redefined (overridden) # in subclasses. class CurrentTime : def __init__( self ) : current_system_time = datetime.today() self.current_hours = current_system_time.hour self.current_minutes = current_system_time.minute self.current_seconds = current_system_time.second def show( self ) : pass # It is not entirely True that the 12-hour a.m./p.m. time # would be used everywhere in America, and the 24-hour time # would be used everywhere in Europe. The names AmericanTime # and EuropeanTime just happen to be nice names to # distinguish these two different ways to display time. class AmericanTime ( CurrentTime ) : def show( self ) : american_hours = ( 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ) print( "%d:%02d:%02d" % ( american_hours[ self.current_hours ], self.current_minutes, self.current_seconds ), end="" ) if self.current_hours < 12 : print( " a.m." ) else : print( " p.m." ) class EuropeanTime ( CurrentTime ) : def show( self ) : print( "%d:%02d:%02d" % ( self.current_hours, self.current_minutes, self.current_seconds ) ) # The main program begins. print( "\n Type 12 to see the time in 12-hour a.m./p.m format." \ "\n Any other number gives the 24-hour format. ", end="" ) user_response = int( input() ) if user_response == 12 : time_to_show = AmericanTime() else : time_to_show = EuropeanTime() print( "\n The time is now ", end="" ) time_to_show.show()