Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Create a Time Class in Python Supporting Addition and Subtraction

Create a Time Class in Python Supporting Addition and Subtraction

Document Object Reference Manual Python3 Examples

We will create a class named Time class that can represent a time (hours, minutes, seconds) and supports time addition and subtraction. We will implement the __add__ and __sub__ method to support time addition and subtraction.

Example

class

Time:

   

def

init

(

self

,

hours

,

minutes

,

seconds

)

:

       

self

.

hours

=

hours

       

self

.

minutes

=

minutes

       

self

.

seconds

=

seconds

       

self

.

normalize

(

)

   

def

normalize

(

self

)

:

        extra_minutes

,

self

.

seconds

=

divmod

(

self

.

seconds

,

60

)

       

self

.

minutes

=

extra_minutes

        extra_hours

,

self

.

minutes

=

divmod

(

self

.

minutes

,

60

)

       

self

.

hours

=

extra_hours

   

def

add

(

self

,

other

)

:

        total_seconds

=

self

.

hours

3600

self

.

minutes

60

self

.

seconds

        total_seconds +

=

other.

hours

3600

minutes

60

seconds

        hours

,

remainder

=

divmod

(

total_seconds

,

3600

)

        minutes

,

seconds

=

divmod

(

remainder

,

60

)

       

return

Time

(

hours

,

minutes

,

seconds

)

   

def

sub

(

self

,

other

)

:

        total_seconds

=

self

.

hours

3600

self

.

minutes

60

self

.

seconds

        total_seconds -

=

other.

hours

3600

minutes

60

seconds

        hours

,

remainder

=

divmod

(

total_seconds

,

3600

)

        minutes

,

seconds

=

divmod

(

remainder

,

60

)

       

return

Time

(

hours

,

minutes

,

seconds

)

   

def

str

(

self

)

:

       

return

f

"{self.hours:02}:{self.minutes:02}:{self.seconds:02}"

Example usage

time1

=

Time

(

2

,

30

,

45

)

time2

=

Time

(

1

,

15

,

20

)

print

(

"Time 1:"

,

time1

)

print

(

"Time 2:"

,

time2

)

time_sum

=

time1 + time2

print

(

"Time 1 + Time 2:"

,

time_sum

)

time_diff

=

time1 - time2

print

(

"Time 1 - Time 2:"

,

time_diff

)

Code analysis:

  1. __init__ method: initializes the Time An instance of the class takes hours, minutes and seconds as parameters and calls the normalize method to make sure the time format is correct.
  2. normalize method: convert seconds and minutes into a standard format, ensuring seconds do not exceed 59 and minutes do not exceed 59.
  3. __add__ method: implement the addition of two Time objects, converting the times to seconds, adding them, and converting back to hours, minutes and seconds.
  4. __sub__ method: implement the addition of two Time The subtraction of objects converts the times to seconds, subtracts them, and converts back to hours, minutes and seconds.
  5. __str__ method: return a formatted time string, ensuring hours, minutes and seconds are all two digits.

Output result:

Time 1: 02:30:45
Time 2: 01:15:20
Time 1 + Time 2: 03:46:05
Time 1 - Time 2: 01:15:25

Document Object Reference Manual Python3 Examples