""" A python class to represent time on the planet Regulus, as described in the lab assignment. Examples: >>> t1 = Time(3 + 60 * (25 + 60 * (1 + 24 * ((1-1) + 30 * ((4-1) + 12 * (12-1)))))) >>> print t1 # 4/1/12 1:25:3 AM RST 4/1/12 1:25:3 AM RST >>> t1_original = deepcopy(t1) >>> t1.add_minutes(-120) >>> print t1 # 120 minutes before 4/1/12 1:25:3 AM RST 3/30/12 11:25:3 PM RST >>> t1 = deepcopy(t1_original) >>> t1.add_minutes(-60) >>> print t1 # 60 minutes before 4/1/12 1:25:3 AM RST 4/1/12 12:25:3 AM RST >>> t1 = deepcopy(t1_original) >>> t1.add_minutes(60) >>> print t1 # 60 minutes after 4/1/12 1:25:3 AM RST, i.e. into DST 4/1/12 3:25:3 AM RDT >>> t1 = deepcopy(t1_original) >>> Time(344775540).minutes_until(Time(344775900)) # from the assignment 6 # t2 is "10/1/12 01:55:57 AM RDT" # that's a total of 57 seconds plus 60*55 seconds ... minus 1 hour >>> t2 = Time(57 + 60 * (55 + 60 * (1 + 24 * ((1-1) + 30 * ((10-1) + 12 * (12-1))))) - 3600) >>> print t2 # "1:55:57 AM on October 1, year 12 prints as: " 10/1/12 1:55:57 AM RDT >>> print t1.minutes_until(t2) # 10/1/12 1:55:57 AM RDT vs. 4/1/12 1:25:3 AM RST 259170 >>> print t2.minutes_until(t1) # 4/1/12 1:25:3 AM RST vs. 10/1/12 1:55:57 AM RDT # note -259171 is fine too -259170 >>> t2.add_minutes(60) >>> print t2 # 1 hour after "1:55:57 AM on October 1, year 12" 10/1/12 2:55:57 AM RDT >>> t2.add_minutes(60) >>> print t2 # 2 hours after "1:55:57 AM on October 1, year 12" 10/1/12 2:55:57 AM RST >>> t2.add_minutes(60) >>> print t2 # 3 hours after "1:55:57 AM on October 1, year 12" 10/1/12 3:55:57 AM RST >>> t3 = deepcopy(t1) >>> t3.add_minutes( 3) >>> t1.minutes_until(t3) 3 >>> t3 = deepcopy(t1) >>> t3.add_minutes(-60) >>> t1.minutes_until(t3) -60 >>> t3 = deepcopy(t1) >>> t3.add_minutes(-60) >>> t3.minutes_until(t1) 60 >>> t3 = deepcopy(t1) >>> t3.add_minutes( 0) >>> t1.minutes_until(t3) 0 >>> t3 = deepcopy(t1) >>> t3.add_minutes( 60) >>> t1.minutes_until(t3) 60 >>> t3 = deepcopy(t1) >>> t3.add_minutes(120) >>> t1.minutes_until(t3) 120 >>> t3 = deepcopy(t1) >>> t3.add_minutes(180) >>> t1.minutes_until(t3) 180 >>> t3 = deepcopy(t2) >>> t3.add_minutes( 3) >>> t2.minutes_until(t3) 3 >>> t3 = deepcopy(t2) >>> t3.add_minutes(-60) >>> t2.minutes_until(t3) -60 >>> t3 = deepcopy(t2) >>> t3.add_minutes( 0) >>> t2.minutes_until(t3) 0 >>> t3 = deepcopy(t2) >>> t3.add_minutes( 60) >>> t2.minutes_until(t3) 60 >>> t3 = deepcopy(t2) >>> t3.add_minutes(120) >>> t2.minutes_until(t3) 120 >>> t3 = deepcopy(t2) >>> t3.add_minutes(180) >>> t2.minutes_until(t3) 180 """ """ import sys sys.path.append('/home/courses/python') from logic import * from copy import deepcopy """ class Time_represented_by_seconds: def __init__(self, s): self.seconds = s def show(self): seconds = self.t #calculating the number of years, months, days, minutes, and (leftover) seconds elapsed since the start of time years = self.seconds / (60*60*24*360) months = (self.seconds - (years * 60*60*24*360))/(60*60*24*30) days = (self.seconds - (years * 60*60*24*360) - (months * 60*60*24*30))/(60*60*24) hours = (self.seconds - (years * 60*60*24*360) - (months * 60*60*24*30) - (days * 60*60*24))/(60*60) minutes = (self.seconds - (years * 60*60*24*360) - (months * 60*60*24*30) - (days * 60*60*24) - (hours*60*60))/(60) seconds_remaining = (self.seconds - (years * 60*60*24*360) - (months * 60*60*24*30) - (days * 60*60*24) - (hours*60*60) - (minutes*60)) #accounting for daylight savings by adding an hour to the printed time between april 1 2am and october 1 3am if (10 > months > 4) or (months == 4 and days >= 0 and hours > 2) or (months == 10 and days == 0 and hours < 3): daylight_hours = hours + 1 else: daylight_hours = hours #acounting for AM/PM and adding one to all displayed time units to account for starting the count at 1 if daylight_hours > 12: print str(months + 1) + "/" + str(days + 1) + "/" + str(years + 1) + " " + str(daylight_hours - 11) + ":" + str(minutes + 1) + ":" + str(seconds_remaining + 1) + " PM RST" else: print str(months + 1) + "/" + str(days + 1) + "/" + str(years + 1) + " " + str(daylight_hours + 1) + ":" + str(minutes + 1) + ":" + str(seconds_remaining + 1) + " AM RST" #“2/1/12 11:5:0 AM RST” def minutes_until(self, other): #returns number of minutes from current time until specified time return (other.seconds - self.seconds)/60 def add_minutes(self, minutes_to_add): #overwrites the number of seconds passed with an additional number of minutes self.seconds = self.seconds + (minutes_to_add * 60) class Time_represented_by_clock_and_calendar: def __init__(self, s, m, h, d, y): self.seconds = s self.minutes = m self.hours = h self.days = d self.years = y def show(self): if (10 > self.months > 4) or (self.months == 4 and self.days >= 0 and self.hours > 2) or (self.months == 10 and self.days == 0 and self.hours < 3): daylight_hours = self.hours + 1 else: daylight_hours = self.hours #acounting for AM/PM and adding one to all displayed time units to account for starting the count at 1 if daylight_hours > 12: print str(self.months + 1) + "/" + str(self.days + 1) + "/" + str(self.years + 1) + " " + str(daylight_hours - 11) + ":" + str(self.minutes + 1) + ":" + str(self.seconds + 1) + " PM RST" else: print str(self.months + 1) + "/" + str(self.days + 1) + "/" + str(self.years + 1) + " " + str(self.daylight_hours + 1) + ":" + str(self.minutes + 1) + ":" + str(self.seconds + 1) + " AM RST" def minutes_until(self, other): if self.years == other.years: if self.months == other.months: if self.days == other.days: if self.hours == other.hours: difference = other.minutes - self.minutes else: difference = (other.minutes + other.hours * 60) - (self.minutes + self.hours * 60) else: difference = (other.minutes + other.hours * 60 + other.days * 24 * 60) - (self.minutes + self.hours * 60 + self.days * 24 * 60) else: difference = (other.minutes + other.hours * 60 + other.days * 24 * 60 + other.months * 30 * 24 * 60) - (self.minutes + self.hours * 60 + self.days * 24 * 60 + self.months * 30 * 24 * 60) else: difference = (other.minutes + other.hours * 60 + other.days * 24 * 60 + other.months * 30 * 24 * 60 + other.years * 12 * 30 * 24 * 60) - (self.minutes + self.hours * 60 + self.days * 24 * 60 + self.months * 30 * 24 * 60 + self.years * 12 * 30 * 24 * 60) return difference def add_minutes(self, minutes): while minutes > 0: if minutes >= 60: if minutes >= 60*24: if minutes >= 60*24*30: if minutes >= 60*24*30*12: self.years += minutes/(60*24*30*12) minutes = minutes%(60*24*30*12) else: self.months += minutes/(60*24*30) minutes = minutes%(60*24*30) else: self.days += minutes/(60*24) minutes = minutes%(60*24) else: self.hours += minutes/60 minutes = minutes%60 else: self.minutes += minutes minutes = 0 # by default, use the first representation, but this is changed in DocTest below Time = Time_represented_by_seconds # mostly copied from http://docs.python.org/lib/module-doctest.html def _test(): import doctest global Time Time = Time_represented_by_seconds result = doctest.testmod() print "Result of doctest for Time_represented_by_seconds is:", if result[0] == 0: print "Wahoo! Passed all", result[1], "tests!" else: print "Rats!" print "\n\n\n\n" Time = Time_represented_by_clock_and_calendar result = doctest.testmod() print "Result of doctest for Time_represented_by_clock_and_calendar is:", if result[0] == 0: print "Wahoo! Passed all", result[1], "tests!" else: print "Rats!" Time = Time_represented_by_seconds if __name__ == "__main__": _test()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question