Last active
January 1, 2016 07:59
-
-
Save horiajurcut/8115524 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from datetime import timedelta | |
from datetime import date | |
class Holiday(object): | |
_message = "Hello" | |
def get_start_date(self): | |
raise NotImplementedError | |
def is_now(self): | |
current_year = date.today().year | |
day = self.get_start_date().split("-") | |
return datetime(current_year, int(day[0]), int(day[1])) - datetime.now() < timedelta(hours=24) | |
def cheer(self): | |
if self.is_now(): | |
print self._message | |
else: | |
print "Have patience my young padawan" | |
class Christmas(Holiday): | |
_message = "Hello and a Merry Christmas to all!!!" | |
def get_start_date(self): | |
return "12-25" | |
class Easter(Holiday): | |
def get_start_date(self): | |
return "4-20" | |
Christmas().cheer() |
Then realize that you'll have to deal with leap years as well, (multiplier xor 4 == 0) in which case christmas_offset = christmas_offset + one day_in_seconds constant.
and we can put everything into a class :D, and after that it will be simple to add new features to the way how christmas is handled :D
Done. Sherban, you can wrap it in a class :)
class Holiday:
pass
class Christmas(Holiday):
pass
.....
from datetime import datetime
from datetime import timedelta
from datetime import date
class Holiday(object):
_message = "Hello"
def get_start_date(self):
raise NotImplementedError
def is_now(self):
return self.get_start_date() - datetime.now() < timedelta(hours=24)
def cheer(self):
if self.is_now():
print self._message
else:
print "Have patience my young padawan"
class Christmas(Holiday):
_message = "Hello and a Merry Christmas to all!!!"
def get_start_date(self):
current_year = date.today().year
return datetime(current_year, 12, 25)
class Easter(Holiday):
....
pass
Christmas().cheer()
from datetime import datetime
from datetime import timedelta
from datetime import date
class Holiday(object):
_message = "Hello"
def get_start_date(self):
raise NotImplementedError
def is_now(self):
current_year = date.today().year
day = self.get_start_date().split("-")
return datetime(current_year, int(day[0]), int(day[1])) - datetime.now() < timedelta(hours=24)
def cheer(self):
if self.is_now():
print self._message
else:
print "Have patience my young padawan"
class Christmas(Holiday):
_message = "Hello and a Merry Christmas to all!!!"
def get_start_date(self):
return "12-25"
class Easter(Holiday):
def get_start_date(self):
return "4-20"
Christmas().cheer()
This escalated quickly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add a year_in_seconds constant, a multiplier (number of years since 1970), a christmas_offset (number of seconds from Jan 01 to Christmas), and it will work every year.