Created
September 8, 2017 20:16
-
-
Save HyShai/c3bd27bb7dc85da92eb7d589b223134c to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/python | |
import datetime | |
import json | |
import urllib2 | |
import re | |
import pytz | |
# django utilites for parsing datetime | |
class FixedOffset(datetime.tzinfo): | |
""" | |
Fixed offset in minutes east from UTC. Taken from Python's docs. | |
Kept as close as possible to the reference version. __init__ was changed | |
to make its arguments optional, according to Python's requirement that | |
tzinfo subclasses can be instantiated without arguments. | |
""" | |
def __init__(self, offset=None, name=None): | |
if offset is not None: | |
self.__offset = datetime.timedelta(minutes=offset) | |
if name is not None: | |
self.__name = name | |
def utcoffset(self, dt): | |
return self.__offset | |
def tzname(self, dt): | |
return self.__name | |
def dst(self, dt): | |
return ZERO | |
def get_fixed_timezone(offset): | |
""" | |
Returns a tzinfo instance with a fixed offset from UTC. | |
""" | |
if isinstance(offset, datetime.timedelta): | |
offset = offset.seconds // 60 | |
sign = '-' if offset < 0 else '+' | |
hhmm = '%02d%02d' % divmod(abs(offset), 60) | |
name = sign + hhmm | |
return FixedOffset(offset, name) | |
def parse_date(value): | |
"""Parses a string and return a datetime.date. | |
Raises ValueError if the input is well formatted but not a valid date. | |
Returns None if the input isn't well formatted. | |
""" | |
date_re = re.compile( | |
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$' | |
) | |
match = date_re.match(value) | |
if match: | |
kw = {k: int(v) for k, v in dict.iteritems(match.groupdict())} | |
return datetime.date(**kw) | |
def parse_datetime(value): | |
"""Parses a string and return a datetime.datetime. | |
This function supports time zone offsets. When the input contains one, | |
the output uses a timezone with a fixed offset from UTC. | |
Raises ValueError if the input is well formatted but not a valid datetime. | |
Returns None if the input isn't well formatted. | |
""" | |
datetime_re = re.compile( | |
r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})' | |
r'[T ](?P<hour>\d{1,2}):(?P<minute>\d{1,2})' | |
r'(?::(?P<second>\d{1,2})(?:\.(?P<microsecond>\d{1,6})\d{0,6})?)?' | |
r'(?P<tzinfo>Z|[+-]\d{2}(?::?\d{2})?)?$' | |
) | |
match = datetime_re.match(value) | |
if match: | |
kw = match.groupdict() | |
if kw['microsecond']: | |
kw['microsecond'] = kw['microsecond'].ljust(6, '0') | |
tzinfo = kw.pop('tzinfo') | |
if tzinfo == 'Z': | |
tzinfo = utc | |
elif tzinfo is not None: | |
offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0 | |
offset = 60 * int(tzinfo[1:3]) + offset_mins | |
if tzinfo[0] == '-': | |
offset = -offset | |
tzinfo = get_fixed_timezone(offset) | |
kw = {k: int(v) for k, v in dict.iteritems(kw) if v is not None} | |
kw['tzinfo'] = tzinfo | |
return datetime.datetime(**kw) | |
# here's the relevant code | |
def is_shabbos_or_yt(day, zip_code): | |
url = 'http://www.hebcal.com/hebcal/?v=1&cfg=json&maj=on&min=off&mod=off&nx=off&year=now&month=x&ss=off&mf=off&c=on&b=18&m=72&geo=zip&zip={0}&m=50&s=off=&d=off&D=off&o=off'.format(zip_code) | |
try: | |
heb_dates = json.load(urllib2.urlopen(url, timeout=5)) | |
except: | |
return False | |
holidays = [i for i in heb_dates['items'] if i['category'] == 'holiday'] | |
candles = [i for i in heb_dates['items'] if i['category'] == 'candles'] | |
havdalah = [i for i in heb_dates['items'] if i['category'] == 'havdalah'] | |
today = day | |
after_candlelighting = [i for i in candles if parse_datetime(i['date']).date() == today.date() and parse_datetime(i['date']) <= today] | |
before_havdala = [i for i in havdalah if parse_datetime(i['date']).date() == today.date() and parse_datetime(i['date']) >= today] | |
today_holiday = [i for i in holidays if i.get('yomtov') and parse_date(i['date'].split('T')[0]) == today.date()] #sometimes hebcal sends a datetime | |
return (len(after_candlelighting) or len(today_holiday) or len(before_havdala)) | |
if __name__ == '__main__': | |
now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) | |
print now | |
print is_shabbos_or_yt(now, zip_code='10018') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment