-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime2.py
More file actions
74 lines (59 loc) · 3.35 KB
/
time2.py
File metadata and controls
74 lines (59 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'''
script: cis131_lab_modifying_internal_data_representation
action: A class that stores a time in 12 hr format (hh:mm:ss) and allows each value to be changed or called individualy, while only using the seconds since midnight to represent and retain the info
Date: 9/29/2025
'''
class Time:
"""Class Time with read-write properties."""
def __init__(self, hour=0, minute=0, second=0):
"""Initialize each attribute."""
self._total_seconds = hour*3600 + minute*60 + second #set total_seconds to the total seconds in hour, minute, second
@property
def hour(self):
"""Return the hour."""
return self._total_seconds // 3600 #integer divide by 3600 to convert seconds to hours
@hour.setter
def hour(self, hour):
"""Set the hour."""
if not (0 <= hour < 24):
raise ValueError(f'Hour ({hour}) must be 0-23')
self._remainingSeconds = self._total_seconds % 3600 #the remainer is the remaining seconds
self._total_seconds = self._remainingSeconds+hour*3600 #set total_seconds to remainingSeconds + converted hours to seconds
@property
def minute(self):
"""Return the minute."""
return (self._total_seconds % 3600) // 60 #integer divide the remaining seconds by 60
@minute.setter
def minute(self, minute):
"""Set the minute."""
if not (0 <= minute < 60):
raise ValueError(f'Minute ({minute}) must be 0-59')
self._remainingHours = (self._total_seconds // 3600)*3600 #first give the remainning hours, then convert to seconds
self._remainingSeconds = (self._total_seconds % 3600) % 60 #first find the remaining seconds from hours, then find the remaining seconds from the minutes
self._total_seconds = self._remainingHours + self._remainingSeconds+ minute *60 #set total_seconds
@property
def second(self):
"""Return the second."""
return (self._total_seconds % 3600) % 60 #first find the remaining seconds from hours, then find the remaining seconds from the minutes
@second.setter
def second(self, second):
"""Set the second."""
if not (0 <= second < 60):
raise ValueError(f'Second ({second}) must be 0-59')
self._remainingHours = (self._total_seconds // 3600)*3600 #first give the remainning hours, then convert to seconds
self._remainingMinutes = ((self._total_seconds % 3600) //60) * 60 #first give the remainning hours, then find the remaining minutes, then convert to seconds
self._total_seconds = self._remainingHours + self._remainingMinutes+ second #set total_seconds
def set_time(self, hour=0, minute=0, second=0):
"""Set values of hour, minute, and second."""
self.hour = hour #call the hour property
self.minute = minute #call the minute property
self.second = second #call the second property
def __repr__(self):
"""Return Time string for repr()."""
return (f'Time(hour={self.hour}, minute={self.minute}, ' +
f'second={self.second})')
def __str__(self):
"""Return Time string in 12-hour clock format."""
return (('12' if self.hour in (0, 12) else str(self.hour % 12)) +
f':{self.minute:0>2}:{self.second:0>2}' +
(' AM' if self.hour < 12 else ' PM'))