Skip to content

Instantly share code, notes, and snippets.

@horiajurcut
Last active January 1, 2016 07:59
Show Gist options
  • Save horiajurcut/8115524 to your computer and use it in GitHub Desktop.
Save horiajurcut/8115524 to your computer and use it in GitHub Desktop.
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()
@horiajurcut
Copy link
Author

Punem de un open source? Hai ca facem revisions.

@sony-headware4
Copy link

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.

@sony-headware4
Copy link

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.

@mebusila
Copy link

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

@horiajurcut
Copy link
Author

Done. Sherban, you can wrap it in a class :)

@mebusila
Copy link

class Holiday:
pass

class Christmas(Holiday):
pass

.....

@mebusila
Copy link

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()

@mebusila
Copy link

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()

@horiajurcut
Copy link
Author

This escalated quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment