def testStrStart1(self): self.assertEqual( datetime(2003, 4, 6, 1, 59, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EST") self.assertEqual( datetime(2003, 4, 6, 2, 00, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EDT")
def testStrStr(self): # Test that tz.tzstr() won't throw an error if given a str instead # of a unicode literal. self.assertEqual(datetime(2003, 4, 6, 1, 59, tzinfo=tz.tzstr(str("EST5EDT"))).tzname(), "EST") self.assertEqual(datetime(2003, 4, 6, 2, 00, tzinfo=tz.tzstr(str("EST5EDT"))).tzname(), "EDT")
def testStrEnd1(self): self.assertEqual( datetime(2003, 10, 26, 0, 59, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EDT") self.assertEqual( datetime(2003, 10, 26, 1, 00, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EST")
def testGMTOffset(self): # GMT and UTC offsets have inverted signal when compared to the # usual TZ variable handling. dt = datetime(2007, 8, 6, 4, 10, tzinfo=tz.tzutc()) self.assertEqual(dt.astimezone(tz=tz.tzstr("GMT+2")), datetime(2007, 8, 6, 6, 10, tzinfo=tz.tzstr("GMT+2"))) self.assertEqual(dt.astimezone(tz=tz.gettz("UTC-2")), datetime(2007, 8, 6, 2, 10, tzinfo=tz.tzstr("UTC-2")))
def test_tzstr_internal_timedeltas(): with pytest.warns(tz.DeprecatedTzFormatWarning): tz1 = tz.tzstr("EST5EDT,5,4,0,7200,11,-3,0,7200") with pytest.warns(tz.DeprecatedTzFormatWarning): tz2 = tz.tzstr("EST5EDT,4,1,0,7200,10,-1,0,7200") assert tz1._start_delta != tz2._start_delta assert tz1._end_delta != tz2._end_delta
def where420(self, irc, msg, args): """ tells you where it is 4:20 AM next """ offsets = [ 'UTC−1100', 'UTC−0800', 'UTC−0700', 'UTC−0600', 'UTC−0500', 'UTC−0100', 'UTC+0000', 'UTC+0100', 'UTC+0200', 'UTC+0300', 'UTC+0400', 'UTC+0530', 'UTC+0700', 'UTC+0800', 'UTC+0900', 'UTC+1000', 'UTC+1200', 'UTC+1300' ] utc_420timedeltas = {} for offset in offsets: now = datetime.datetime.now(tz=tz.tzstr(offset)) today_420 = now.replace(hour=4, minute=20, second=0, tzinfo=tz.tzstr(offset)) zero_td = datetime.timedelta() time_until_today_420 = today_420 - now if time_until_today_420 >= zero_td: time_until_next_420 = time_until_today_420 else: tomorrow_420 = today_420 + datetime.timedelta(days=1) time_until_next_420 = tomorrow_420 - now utc_420timedeltas[offset] = time_until_next_420.total_seconds() # find the key with the minimum value closest_utc = min(utc_420timedeltas, key=lambda k: utc_420timedeltas[k]) closest_hours = int(closest_utc[3:6]) closest_min = int(closest_utc[6:8]) utc_offset = datetime.timedelta(hours=closest_hours, minutes=closest_min) tzs420 = [ tz.zone for tz in map(pytz.timezone, pytz.all_timezones_set) if now.astimezone(tz).utcoffset() == utc_offset ] city420 = random.choice(tzs420) re = utils.str.format( f'It will be 4:20am next in {city420} in {min(utc_420timedeltas.values())} seconds. _\\|/_ ( .__.) . o O ( smoke weed )' ) irc.reply(re)
def get_last_after_hour_date(): ny_now = datetime.now(tz.tzstr("EST5EDT")) if ny_now.hour < 16 or ny_now.hour == 16 and ny_now.minute <= 30: t = ny_now - timedelta(days=1) else: t = ny_now return get_trading_date_skip_weekend(t)
def get_last_real_time_date(): ny_now = datetime.now(tz.tzstr("EST5EDT")) if (ny_now.hour == 9 and ny_now.minute < 30) or ny_now.hour < 9: t = ny_now - timedelta(days=1) else: t = ny_now return get_trading_date_skip_weekend(t)
def test_local_datetime(self): # The application is configured to use a named timzone settings.timezone = 'GMT+3' with freeze_time('2000-01-01T01:01:00'): now = localnow() assert now == datetime(2000, 1, 1, 4, 1, tzinfo=tzstr('GMT+3')) assert now.utcoffset() == timedelta(0, 10800)
def dateutil_parse(timestr, default, ignoretz=False, tzinfos=None, **kwargs): """ lifted from dateutil to get resolution""" from dateutil import tz import time fobj = StringIO(str(timestr)) res = DEFAULTPARSER._parse(fobj, **kwargs) # dateutil 2.2 compat if isinstance(res, tuple): res, _ = res if res is None: raise ValueError("unknown string format") repl = {} reso = None for attr in [ "year", "month", "day", "hour", "minute", "second", "microsecond" ]: value = getattr(res, attr) if value is not None: repl[attr] = value reso = attr if reso is None: raise ValueError("Cannot parse date.") if reso == 'microsecond': if repl['microsecond'] == 0: reso = 'second' elif repl['microsecond'] % 1000 == 0: reso = 'millisecond' ret = default.replace(**repl) if res.weekday is not None and not res.day: ret = ret + relativedelta.relativedelta(weekday=res.weekday) if not ignoretz: if callable(tzinfos) or tzinfos and res.tzname in tzinfos: if callable(tzinfos): tzdata = tzinfos(res.tzname, res.tzoffset) else: tzdata = tzinfos.get(res.tzname) if isinstance(tzdata, datetime.tzinfo): tzinfo = tzdata elif isinstance(tzdata, compat.string_types): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, int): tzinfo = tz.tzoffset(res.tzname, tzdata) else: raise ValueError("offset must be tzinfo subclass, " "tz string, or int offset") ret = ret.replace(tzinfo=tzinfo) elif res.tzname and res.tzname in time.tzname: ret = ret.replace(tzinfo=tz.tzlocal()) elif res.tzoffset == 0: ret = ret.replace(tzinfo=tz.tzutc()) elif res.tzoffset: ret = ret.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset)) return ret, reso
def test_system_est(self, mock_tz): mock_tz.return_value = tzstr('EST5EDT') test = datetime(2014, 12, 21, 4, 59, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '12/20/2014') test = datetime(2014, 12, 21, 5, 0, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '12/21/2014') test = datetime(2014, 12, 21, 5, 1, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '12/21/2014') test = datetime(2014, 12, 21, 3, 49, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '12/20/2014') test = datetime(2014, 12, 21, 4, 0, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '12/20/2014') test = datetime(2014, 12, 21, 4, 1, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '12/20/2014') test = datetime(2014, 5, 21, 4, 59, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '05/21/2014') test = datetime(2014, 5, 21, 5, 0, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '05/21/2014') test = datetime(2014, 5, 21, 5, 1, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '05/21/2014') test = datetime(2014, 5, 21, 3, 59, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '05/20/2014') test = datetime(2014, 5, 21, 4, 0, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '05/21/2014') test = datetime(2014, 5, 21, 4, 1, 0, tzinfo=tzutc()) self.assertEquals(managerlib.format_date(test), '05/21/2014')
def __init__(self, first_weekday=0, timezone=None): """Creates a Calendar object for providing date/time paths and for relative date/time manipulation. Values for `first_weekday` are 0 for Monday, 6 for Sunday. Default is 0.""" if isinstance(first_weekday, compat.string_type): try: self.first_weekday = _WEEKDAY_NUMBERS[first_weekday.lower()] except KeyError: raise ConfigurationError("Unknown weekday name %s" % first_weekday) else: value = int(first_weekday) if value < 0 or value >= 7: raise ConfigurationError("Invalid weekday number %s" % value) self.first_weekday = int(first_weekday) if timezone: self.timezone_name = timezone self.timezone = gettz(timezone) or tzstr(timezone) else: self.timezone_name = datetime.now(tzlocal()).tzname() self.timezone = tzlocal()
def testRangeCmp1(self): from dateutil.relativedelta import SU self.assertEqual( tz.tzstr("EST5EDT"), tz.tzrange( "EST", -18000, "EDT", -14400, relativedelta(hours=+2, month=4, day=1, weekday=SU(+1)), relativedelta(hours=+1, month=10, day=31, weekday=SU(-1))))
def dateutil_parse(timestr, default, ignoretz=False, tzinfos=None, **kwargs): """ lifted from dateutil to get resolution""" from dateutil import tz import time fobj = StringIO(str(timestr)) res = DEFAULTPARSER._parse(fobj, **kwargs) # dateutil 2.2 compat if isinstance(res, tuple): res, _ = res if res is None: raise ValueError("unknown string format") repl = {} reso = None for attr in ["year", "month", "day", "hour", "minute", "second", "microsecond"]: value = getattr(res, attr) if value is not None: repl[attr] = value reso = attr if reso is None: raise ValueError("Cannot parse date.") if reso == "microsecond": if repl["microsecond"] == 0: reso = "second" elif repl["microsecond"] % 1000 == 0: reso = "millisecond" ret = default.replace(**repl) if res.weekday is not None and not res.day: ret = ret + relativedelta.relativedelta(weekday=res.weekday) if not ignoretz: if callable(tzinfos) or tzinfos and res.tzname in tzinfos: if callable(tzinfos): tzdata = tzinfos(res.tzname, res.tzoffset) else: tzdata = tzinfos.get(res.tzname) if isinstance(tzdata, datetime.tzinfo): tzinfo = tzdata elif isinstance(tzdata, compat.string_types): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, int): tzinfo = tz.tzoffset(res.tzname, tzdata) else: raise ValueError("offset must be tzinfo subclass, " "tz string, or int offset") ret = ret.replace(tzinfo=tzinfo) elif res.tzname and res.tzname in time.tzname: ret = ret.replace(tzinfo=tz.tzlocal()) elif res.tzoffset == 0: ret = ret.replace(tzinfo=tz.tzutc()) elif res.tzoffset: ret = ret.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset)) return ret, reso
def test_parse_cal_datetime(self): cl = ContentLine.parse('DTSTART:20150701T060000') self.assertEqual(datetime(2015, 7, 1, 6), parse_cal_datetime(cl)) cl = ContentLine.parse('DTSTART:20150701T060000Z') self.assertEqual(datetime(2015, 7, 1, 6, tzinfo=tzutc), parse_cal_datetime(cl)) cl = ContentLine.parse('DTSTART;TZID=America/New_York:20150701T060000Z') self.assertEqual(datetime(2015, 7, 1, 6, tzinfo=tzstr('America/New_York')), parse_cal_datetime(cl))
def test_valid_tzinfo_callable_input(self): dstr = "2014 January 19 09:00 UTC" def tzinfos(*args, **kwargs): return u"UTC+0" expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzstr("UTC+0")) res = parse(dstr, tzinfos=tzinfos) self.assert_equal_same_tz(res, expected)
def testRangeCmp1(self): from dateutil.relativedelta import SU self.assertEqual(tz.tzstr("EST5EDT"), tz.tzrange("EST", -18000, "EDT", -14400, relativedelta(hours=+2, month=4, day=1, weekday=SU(+1)), relativedelta(hours=+1, month=10, day=31, weekday=SU(-1))))
def configuredtimezone(): timezone = settings.timezone if timezone in ('', None): return None if isinstance(timezone, tzinfo): return timezone if timezone in (0, 'utc', 'UTC', 'Z', 'z'): return tzutc() return tzstr(timezone)
def get_start_end_string(longest_time_range: list) -> (str, str): raw_items = [] start = arrow.get(min(longest_time_range)[1].start_timestamp) end = arrow.get(max(longest_time_range)[1].end_timestamp) timezone = timezone = tzstr("CET-1CEST,M3.5.0/2,M10.5.0/3").tzname( datetime.fromtimestamp(start.timestamp)) format_string = "YYYY-MM-DD, HH:mm:ss" start_string = start.to(timezone).format(format_string) end_string = end.to(timezone).format(format_string) return start_string, end_string
def delete_abrupt_time(original): NY_time = [(x - datetime.timedelta(hours=4)).strftime("%Y-%m-%d %H:%M:%S") for x in [ datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S") for x in original.index.tolist() ]] # minus four hour to convert it to New York time original.index = NY_time date = pd.Series([x[0:10] for x in original.index], index=original.index) time = pd.Series([x[11::] for x in original.index], index=original.index) tz = tzstr('EST+5EDT,M3.2.0,M11.1.0') #http://famschmid.net/timezones.html timezones = [ datetime.datetime(int(date[x][0:4]), int(date[x][5:7]), int(date[x][8:10]), 0, 0, tzinfo=tz).tzname() for x in range(len(date)) ] def abrupt_time_finder(timezones, time): abrupt_time = [False] * len(date) for index, timezone in enumerate(timezones): if timezone == 'EDT': if time[index][0:2] == '09': abrupt_time[index] = True elif time[index][0:2] == '15': if int(time[index][3:5]) > 30: abrupt_time[index] = True elif time[index][0:2] == '16': abrupt_time[index] = True else: pass else: if time[index][0:2] == '10': abrupt_time[index] = True elif time[index][0:2] == '16': if int(time[index][3:5]) > 30: abrupt_time[index] = True elif time[index][0:2] == '17': abrupt_time[index] = True else: pass return abrupt_time abrupt_time = pd.Series(abrupt_time_finder(timezones, time), index=original.index) original = pd.concat([original, abrupt_time], axis=1) original.columns = ['open', 'high', 'low', 'close', 'abrupt_time'] cooked = original[original['abrupt_time'].values == False][[ 'open', 'high', 'low', 'close' ]] return cooked
def do_foursquare_venues(dt=None): """Collect foursquare checkin stats for each venue that has events happening today.""" from fsq import FoursquareApi from rdutils.mathutils import farey from event.models import Event, Venue, FoursquareTrend etz = tz.tzstr('EST5EDT') # Eastern timezone headers = { 'User-Agent': getattr(settings, 'FOURSQUARE_USER_AGENT', settings.USER_AGENT) } fsq = FoursquareApi(headers) dt = dt or date.today() # Active venues events = Event.objects.filter(is_approved=True, event_date=dt).order_by( 'event_date', 'event_start_time', 'pk') venue_ids = set() venues = [] for ev in events: if ev.venue.pk not in venue_ids: venues.append(ev.venue) venue_ids.add(ev.venue.pk) previous_venues = Venue.objects.exclude(pk__in=list(venue_ids)).filter( fsq_checkins__gt=0) previous_venues.update(fsq_checkins=0, fsq_ratio=u'', fsq_mf=0, fsq_fm=0, fsq_m=0, fsq_f=0) # Get and populate FSQ ids of active venues where fsq_id was not previously obtained for v in venues: if not v.fsq_id and v.geo_loc and ',' in v.geo_loc: keyx = shorten_key(u"venue_id_4sq:%s" % v.pk) vx = cache.cache.get(keyx, None) if vx is None: lat, lng = v.geo_loc.split(',') venue_name = v.name.encode("utf-8", "ignore") _log.debug("Getting 4sq venue ID for %s", venue_name) sleep(.25) vx = fsq.get_venues(geolat=lat, geolong=lng, q=venue_name, l=1) try: vid = vx['groups'][0]['venues'][0]['id'] v.fsq_id = unicode(vid) v.save() except Exception, e: vid = 0 _log.debug( "FSQ ID for venue %s could not be obtained: %s\n%s", v.pk, e, vx) cache.cache.set(keyx, vid, 7200) # cache for 2 hours
def get_user_prices(self, below_value: int, region_identifier: int, vat_selection: int) -> Tuple[List, Optional[int]]: """Returns a list of prices which drop below or on a certain value. Also returns a integer which represents the lowest price point in the returned list. The marketprices of the price points in the returned list have VAT added if the user selected it (if vat_selection is 1). """ below_price_data = [] lowest_index = None current_index = 0 for price_point in self.data.prices: timezone = tzstr("CET-1CEST,M3.5.0/2,M10.5.0/3").tzname( datetime.fromtimestamp(price_point.start_timestamp)) now_timezone = arrow.utcnow().to(timezone) midnight = now_timezone.replace(hour=0, minute=0, second=0, microsecond=0) tomorrow_boundary_start = midnight.shift(days=+1) tomorrow_boundary_end = midnight.shift(days=+2) marketprice_with_vat = price_point.marketprice if region_identifier == 0 and vat_selection == 1: marketprice_with_vat = round( price_point.marketprice * CURRENT_VAT, 2) if (price_point.start_timestamp >= tomorrow_boundary_start.timestamp and price_point.end_timestamp <= tomorrow_boundary_end.timestamp): if marketprice_with_vat <= below_value: below_price_data.append( Box({ "start_timestamp": price_point.start_timestamp, "marketprice": marketprice_with_vat, } # Don't store end timestamp because a price point is always 1 hour long )) if lowest_index is None: lowest_index = current_index else: if marketprice_with_vat < below_price_data[ lowest_index].marketprice: lowest_index = current_index current_index += 1 return below_price_data, lowest_index
def configuredtimezone(): timezone = settings.timezone if timezone in ('', None): return None if isinstance(timezone, tzinfo): return timezone if timezone in (0, 'utc', 'UTC', 'Z', 'z'): return tzutc() if isinstance(timezone, str): return tzstr(timezone) raise ValueError(f'Invalid timezone in configuration: {timezone}')
def dateutil_parse(timestr, default, ignoretz=False, tzinfos=None, **kwargs): """ lifted from dateutil to get resolution""" from dateutil import tz import time res = DEFAULTPARSER._parse(StringIO(timestr), **kwargs) if res is None: raise ValueError("unknown string format") repl = {} for attr in ["year", "month", "day", "hour", "minute", "second", "microsecond"]: value = getattr(res, attr) if value is not None: repl[attr] = value reso = attr if reso == 'microsecond' and repl['microsecond'] == 0: reso = 'second' ret = default.replace(**repl) if res.weekday is not None and not res.day: ret = ret + relativedelta.relativedelta(weekday=res.weekday) if not ignoretz: if callable(tzinfos) or tzinfos and res.tzname in tzinfos: if callable(tzinfos): tzdata = tzinfos(res.tzname, res.tzoffset) else: tzdata = tzinfos.get(res.tzname) if isinstance(tzdata, datetime.tzinfo): tzinfo = tzdata elif isinstance(tzdata, basestring): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, int): tzinfo = tz.tzoffset(res.tzname, tzdata) else: raise ValueError("offset must be tzinfo subclass, " "tz string, or int offset") ret = ret.replace(tzinfo=tzinfo) elif res.tzname and res.tzname in time.tzname: ret = ret.replace(tzinfo=tz.tzlocal()) elif res.tzoffset == 0: ret = ret.replace(tzinfo=tz.tzutc()) elif res.tzoffset: ret = ret.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset)) return ret, reso
def do_foursquare_venues(dt=None): """Collect foursquare checkin stats for each venue that has events happening today.""" from fsq import FoursquareApi from rdutils.mathutils import farey from event.models import Event, Venue, FoursquareTrend etz = tz.tzstr('EST5EDT') # Eastern timezone headers = { 'User-Agent': getattr(settings, 'FOURSQUARE_USER_AGENT', settings.USER_AGENT) } fsq = FoursquareApi(headers) dt = dt or date.today() # Active venues events = Event.objects.filter(is_approved=True, event_date=dt).order_by('event_date', 'event_start_time', 'pk') venue_ids = set() venues = [] for ev in events: if ev.venue.pk not in venue_ids: venues.append(ev.venue) venue_ids.add(ev.venue.pk) previous_venues = Venue.objects.exclude(pk__in=list(venue_ids)).filter(fsq_checkins__gt=0) previous_venues.update(fsq_checkins=0, fsq_ratio=u'', fsq_mf=0, fsq_fm=0, fsq_m=0, fsq_f=0) # Get and populate FSQ ids of active venues where fsq_id was not previously obtained for v in venues: if not v.fsq_id and v.geo_loc and ',' in v.geo_loc: keyx = shorten_key(u"venue_id_4sq:%s" % v.pk) vx = cache.cache.get(keyx, None) if vx is None: lat, lng = v.geo_loc.split(',') venue_name = v.name.encode("utf-8", "ignore") _log.debug("Getting 4sq venue ID for %s", venue_name) sleep(.25) vx = fsq.get_venues(geolat=lat, geolong=lng, q=venue_name, l=1) try: vid = vx['groups'][0]['venues'][0]['id'] v.fsq_id = unicode(vid) v.save() except Exception, e: vid = 0 _log.debug("FSQ ID for venue %s could not be obtained: %s\n%s", v.pk, e, vx) cache.cache.set(keyx, vid, 7200) # cache for 2 hours
def get_market(): #util.clean_market(1) #dt = get_datetime() dt = datetime.datetime.now(tz.tzstr('EST5EDT')) market = Market(ref=1, date=dt.date(), time=dt.time()) market.put() url = 'http://www.barchart.com/stocks/nasdaq100.php?view=main' soup = BeautifulSoup(urlfetch.fetch(url, deadline=30).content, 'lxml') table = soup('table', attrs={'id': 'dt1'})[0] for tr in table('tr')[1:]: tds = tr('td') code = str(tds[0].text) name = str(tds[1].text) value = float(tds[2].text.replace(',', '')) text_replace = tds[4].text.replace(',', '').replace('%', '') diff = float(text_replace) if text_replace != 'unch' else 0.0 stock = Stock(name=util.get_or_create_name(1, code, name), value=value, diff=diff, market=market.key()) stock.put()
def get_market(): #util.clean_market(0) url = 'http://pregao-online.bmfbovespa.com.br/Cotacoes.aspx' soup = BeautifulSoup(urlfetch.fetch(url, deadline=50).content, 'lxml') rate = util.get_exchange() #dt = get_datetime() dt = datetime.datetime.now(tz.tzstr('EBST3EBDT')) market = Market(ref=0, date=dt.date(), time=dt.time(), exchange_rate=rate) market.put() table = soup('table', attrs={'id': 'ctl00_DefaultContent_GrdCarteiraIndice'})[0] for tr in table('tr')[1:]: tds = tr('td') code = str(tds[0].string) name = util.clean_string(tds[1].string) value = util.get_float(tds[2].string) diff = util.get_float(tds[3].text.strip()) stock = Stock(name=util.get_or_create_name(0, code, name), value=value, diff=diff, market=market.key()) stock.put()
def test_system_est(self, mock_tz): mock_tz.return_value = tzstr("EST5EDT") test = datetime(2014, 12, 21, 4, 59, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "12/20/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-12-21") test = datetime(2014, 12, 21, 5, 0, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "12/21/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-12-21") test = datetime(2014, 12, 21, 5, 1, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "12/21/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-12-21") test = datetime(2014, 12, 21, 3, 49, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "12/20/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-12-21") test = datetime(2014, 12, 21, 4, 0, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "12/20/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-12-21") test = datetime(2014, 12, 21, 4, 1, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "12/20/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-12-21") test = datetime(2014, 5, 21, 4, 59, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "05/21/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-05-21") test = datetime(2014, 5, 21, 5, 0, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "05/21/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-05-21") test = datetime(2014, 5, 21, 5, 1, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "05/21/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-05-21") test = datetime(2014, 5, 21, 3, 59, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "05/20/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-05-21") test = datetime(2014, 5, 21, 4, 0, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "05/21/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-05-21") test = datetime(2014, 5, 21, 4, 1, 0, tzinfo=tzutc()) self.assertEqual(managerlib.format_date(test), "05/21/2014") self.assertEqual(managerlib.format_iso8601_date(test), "2014-05-21")
def write_to_h5(noise_pwr, time): with h5py.File('data.h5', 'w') as h5f: h5f.attrs['fs'] = const.FS h5f.attrs['NFFT'] = const.NBINS h5f.attrs['noise_var'] = noise_pwr h5f.attrs['duration'] = time n_integrations = np.ceil(time / t_int) start_times = datetime.datetime.now( tz=tz.tzstr('America/Vancouver') ) - np.arange(n_integrations) * datetime.timedelta(seconds=1) start_timestamps = [int(t.timestamp()) for t in start_times[::-1]] h5f.create_dataset('spec', (n_integrations, const.NBINS), dtype=const.DTYPE) h5f.create_dataset('freqs', data=const.FULL_FREQS) h5f.create_dataset('times', data=start_timestamps) for i, output in enumerate(integrated_spec_gen(noise_pwr, time)): print(f"{i+1}/{n_integrations}") h5f['spec'][i] = output
from dateutil import tz import datetime posixstr = "CET-1CEST-2,M3.5.0/02:00,M10.5.0/03:00" spaintz = tz.tzstr(posixstr) print datetime.datetime.now(spaintz).ctime()
def testStrEnd1(self): self.assertEqual(datetime(2003, 10, 26, 0, 59, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EDT") self.assertEqual(datetime(2003, 10, 26, 1, 00, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EST")
async def price_drops_below_notification( db_manager, notification_defaults, config, price_data, token, below_value, region_identifier, vat_selection, ): below_price_data, lowest_index = price_data.get_user_prices( below_value, region_identifier, vat_selection) if below_price_data and lowest_index is not None: lowest_point = below_price_data[lowest_index] log.debug('Sending "Price Drops Below" notification to a user.') # Get the current timezone (either CET or CEST) timezone = tzstr("CET-1CEST,M3.5.0/2,M10.5.0/3").tzname( datetime.fromtimestamp(lowest_point.start_timestamp)) lowest_price_start = arrow.get( lowest_point.start_timestamp).to(timezone) # Full cents, for example 4 lowest_price_floored = floor(lowest_point.marketprice) # Decimal places of cent, for example 39 lowest_price_decimal = round( (lowest_point.marketprice - lowest_price_floored) * 100) # Together 4,39 formatted_lowest_price = f"{lowest_price_floored},{lowest_price_decimal}" below_value_floored = floor(below_value) below_value_decimal = round((below_value - below_value_floored) * 100) formatted_below_value = f"{below_value_floored},{below_value_decimal}" encryption_algorithm = notification_defaults.encryption_algorithm # Set token data # For reference see: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/establishing_a_token-based_connection_to_apns token_body = { "iss": notification_defaults.dev_team_id, "iat": arrow.utcnow().timestamp, } token_headers = { "alg": notification_defaults.encryption_algorithm, "kid": notification_defaults.encryption_key_id, } token_data_encoded = jwt.encode( # JWT is required by APNs for token based authentication token_body, notification_defaults.encryption_key, algorithm=encryption_algorithm, headers=token_headers, ) # Set notification payload # For reference see: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/generating_a_remote_notification#2943365 notification_payload = { "aps": { "alert": { "title-loc-key": notification_defaults.below_notification.title_loc_key, "loc-key": notification_defaults.below_notification.get_body_loc_key( len(below_price_data)), "loc-args": [ len(below_price_data), formatted_below_value, lowest_price_start.format("H"), formatted_lowest_price, ], }, "badge": 0, "sound": "default", "content-available": 0, } } # Set request headers # For reference see: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns request_headers = { "authorization": f"bearer {token_data_encoded}", "apns-push-type": "alert", "apns-topic": notification_defaults.bundle_id, "apns-expiration": f"{lowest_price_start.timestamp - 3600}", "apns-priority": "5", "apns-collapse-id": notification_defaults.below_notification.collapse_id, } url = f"{notification_defaults.apns_server_url}:{notification_defaults.apns_server_port}{notification_defaults.url_path.format(token)}" status_code = None response = None async with httpx.AsyncClient(http2=True) as client: try: response = await client.post( url, headers=request_headers, data=json.dumps(notification_payload)) except httpx.ConnectTimeout: log.warning(f"Connect attempt to {url} timed out.") raise except httpx.ReadTimeout: log.warning(f"Read from {url} timed out.") raise except Exception as e: log.warning( f"Unrecognized exception at POST request to {url}: {e}.") raise else: status_code = response.status_code if response.content.decode("utf-8") == "": data = {} else: try: data = response.json() except json.JSONDecodeError as e: log.warning( f"Couldn't decode response from APNs servers: {e}") raise except Exception as e: log.warning( f"Unknown error while decoding response from APNs servers: {e}" ) raise if response is not None and status_code is not None: await handle_apns_response(db_manager, token, data, status_code, config)
def testBrokenIsDstHandling(self): # tzrange._isdst() was using a date() rather than a datetime(). # Issue reported by Lennart Regebro. dt = datetime(2007, 8, 6, 4, 10, tzinfo=tz.tzutc()) self.assertEqual(dt.astimezone(tz=tz.gettz("GMT+2")), datetime(2007, 8, 6, 6, 10, tzinfo=tz.tzstr("GMT+2")))
def test_valid_tzinfo_unicode_input(self): dstr = "2014 January 19 09:00 UTC" tzinfos = {u"UTC": u"UTC+0"} expected = datetime(2014, 1, 19, 9, tzinfo=tz.tzstr("UTC+0")) res = parse(dstr, tzinfos=tzinfos) self.assert_equal_same_tz(res, expected)
def testStrCmp2(self): self.assertEqual(tz.tzstr("EST5EDT"), tz.tzstr("EST5EDT,4,1,0,7200,10,-1,0,7200,3600"))
autoescape=True, ) # Time zones dt_schedule = ',M3.2.0,M11.1.0' time_zones = [ ('Eastern', 'ET', 'EST+5EDT' + dt_schedule), ('Central', 'CT', 'CST+6CDT' + dt_schedule), ('Mountain', 'MT', 'MST+7MDT' + dt_schedule), ('Arizona', 'AZT', 'MST+7'), ('Pacific', 'PT', 'PST+8PDT' + dt_schedule), ('Alaska', 'AKT', 'AKST+9AKDT' + dt_schedule), ('Hawaii-Aleutian', 'HAT', 'HAST+10HADT' + dt_schedule), ('Hawaii', 'HT', 'HAST+10'), ] tzinfos = dict([(t[1], tz.tzstr(t[2])) for t in time_zones]) tz_select_array = [{'abbr': t[1], 'name': t[0]} for t in time_zones] default_tz = tz_select_array[0]['abbr'] class IdData(ndb.Model): user_id = ndb.StringProperty('u') token = ndb.StringProperty('t') next_temp_change = ndb.DateTimeProperty('n') schedule_id = ndb.StringProperty('c') timezone = ndb.StringProperty('z') schedule = ndb.TextProperty('s') @classmethod def get_key(cls, t_id): return ndb.Key('Id', t_id)
def build(src_dir, dst_dir, opts): loader = FileSystemLoader(src_dir) env = Environment(auto_reload=False, trim_blocks=True, lstrip_blocks=True, loader=loader) env.globals['environment'] = opts.get('environment') posts = [] # Make a list of the post templates and their commit dates for (root, dirs, files) in os.walk(os.path.join(src_dir, 'posts')): for f in files: if not f.startswith('.') and not f == 'index.html.j2': template_path = os.path.join(root, f) date = commitDate(template_path) date = dateutil.parser.parse(date).astimezone(tzstr("PST8PDT")) template_path = os.path.relpath(template_path, src_dir) posts.append((date, template_path)) # Sort the posts by commit date so the newest post is first posts = sorted(posts, reverse=True) post_data = {} # Generate a hash of data needed for each post for idx, (date, template_path) in enumerate(posts): # A displayable version of the date pretty_date = date.strftime('%A, %B ') + \ str(date.day) + date.strftime(', %Y') # Grab the post's title from it's "posttitle" block template = env.get_template(template_path) ctx = template.new_context() title = ' '.join(template.blocks['posttitle'](ctx)).strip() # Generate the relative URL of the post for use in links post_path = '/' + template.name if post_path.endswith('.j2'): post_path = post_path[:-3] post_data[template.name] = { 'index': idx, 'date': date, 'pretty_date': pretty_date, 'template': template_path, 'title': title, 'path': post_path } env.globals['post_templates'] = map(lambda x: x[1], posts) env.globals['post_data'] = post_data # Render all files in the src_dir that have a ".j2" extension for (src_path, dst_path) in futil.pairwalk(src_dir, dst_dir): futil.try_mkdirs(os.path.dirname(dst_path)) if futil.ext(src_path) == '.j2': template = os.path.relpath(src_path, src_dir) # If it starts with "_" then it is a partial if not os.path.basename(template).startswith('_'): env.globals['template_name'] = template out_path = futil.chompext(dst_path) env.get_template(template).stream().dump(out_path) elif not src_path.endswith(('.swp', '~')): # Copy all other files straight over shutil.copy2(src_path, dst_path)
def testStrEnd4(self): s = "EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00" self.assertEqual(datetime(2003, 10, 26, 0, 59, tzinfo=tz.tzstr(s)).tzname(), "EDT") self.assertEqual(datetime(2003, 10, 26, 1, 00, tzinfo=tz.tzstr(s)).tzname(), "EST")
def get_current_date(): return datetime.now(tzstr(s='GMT-5')).strftime(DATE_FORMAT)
def testRangeCmp2(self): self.assertEqual(tz.tzstr("EST5EDT"), tz.tzrange("EST", -18000, "EDT"))
def testStrStart6(self): s = "EST5EDT4,J96/02:00:00,J299/02:00" self.assertEqual(datetime(2003, 4, 6, 1, 59, tzinfo=tz.tzstr(s)).tzname(), "EST") self.assertEqual(datetime(2003, 4, 6, 2, 00, tzinfo=tz.tzstr(s)).tzname(), "EDT")
from datetime import datetime import sys from dateutil.tz import tzstr import pytest from javaproperties import java_timestamp # Unix timestamps and datetime objects don't support leap seconds or month 13, # so there's no need (and no way) to test handling of them here. old_pacific = tzstr('PST8PDT,M4.1.0,M10.5.0') @pytest.mark.parametrize( 'ts,s', [ (None, ''), (False, ''), (0, 'Wed Dec 31 19:00:00 EST 1969'), (1234567890.101112, 'Fri Feb 13 18:31:30 EST 2009'), (1234567890.987654, 'Fri Feb 13 18:31:30 EST 2009'), # Months: (1451624400, 'Fri Jan 01 00:00:00 EST 2016'), (1454396522, 'Tue Feb 02 02:02:02 EST 2016'), (1456992183, 'Thu Mar 03 03:03:03 EST 2016'), (1459757044, 'Mon Apr 04 04:04:04 EDT 2016'), (1462439105, 'Thu May 05 05:05:05 EDT 2016'), (1465207566, 'Mon Jun 06 06:06:06 EDT 2016'), (1467889627, 'Thu Jul 07 07:07:07 EDT 2016'), (1470658088, 'Mon Aug 08 08:08:08 EDT 2016'), (1473426549, 'Fri Sep 09 09:09:09 EDT 2016'),
def testStrStart1(self): self.assertEqual(datetime(2003, 4, 6, 1, 59, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EST") self.assertEqual(datetime(2003, 4, 6, 2, 00, tzinfo=tz.tzstr("EST5EDT")).tzname(), "EDT")
def testStrStart2(self): s = "EST5EDT,4,0,6,7200,10,0,26,7200,3600" self.assertEqual(datetime(2003, 4, 6, 1, 59, tzinfo=tz.tzstr(s)).tzname(), "EST") self.assertEqual(datetime(2003, 4, 6, 2, 00, tzinfo=tz.tzstr(s)).tzname(), "EDT")
def testStrEnd3(self): s = "EST5EDT,4,1,0,7200,10,-1,0,7200,3600" self.assertEqual(datetime(2003, 10, 26, 0, 59, tzinfo=tz.tzstr(s)).tzname(), "EDT") self.assertEqual(datetime(2003, 10, 26, 1, 00, tzinfo=tz.tzstr(s)).tzname(), "EST")
def testStrEnd6(self): s = "EST5EDT4,J96/02:00:00,J299/02" self.assertEqual(datetime(2003, 10, 26, 0, 59, tzinfo=tz.tzstr(s)).tzname(), "EDT") self.assertEqual(datetime(2003, 10, 26, 1, 00, tzinfo=tz.tzstr(s)).tzname(), "EST")
def create_toggles(cls, start_date=None, end_date=None): if not end_date: end_date = datetime.datetime.now(tz=tz.tzstr('UTC+03:30')) if not start_date: start_date = end_date - datetime.timedelta(days=30) if start_date is str: start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d') if end_date is str: end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d') response = requests.get(TOGGLE_API_URL, params={'start_date': start_date.strftime('%Y-%m-%dT%H:%M:%S+03:30'), 'end_date': end_date.strftime('%Y-%m-%dT%H:%M:%S+03:30')}, auth=TOGGLE_AUTH) times_json = response.json() entries = [] for time in times_json: entry_id = time.get('id') description = time.get('description') activity = TOGGLE_ACTIVITY_TAGS.get('CODE') tags = time.get('tags') remote = False if tags: if 'PM' in tags: continue elif 'No - PM' in tags: report('Skipping entry ("NO - PM" tag): ' + str(entry_id) + ' ' + description, Color.WARNING) continue if TOGGLE_REMOTE_TAG in tags: remote = True tags.remove(TOGGLE_REMOTE_TAG) if len(tags) > 1: report('More than one tag provided: ' + tags, Color.WARNING) report('Skipping entry ' + tags + ':' + str(entry_id) + ' ' + description, Color.WARNING) continue elif len(tags) == 1: activity = TOGGLE_ACTIVITY_TAGS.get(tags[0]) if not activity: report('Undefined tag ' + tags[0], Color.WARNING) report('Skipping entry (' + tags[0] + '): ' + str(entry_id) + ' ' + description, Color.WARNING) continue issue_description = re.search('#(?P<issue>\d+) *- *(?P<description>.*)', description) if issue_description: issue = issue_description.group('issue') description = issue_description.group('description') else: report('Skipping entry (issue id not found): ' + str(entry_id) + ' ' + description, Color.WARNING) continue start = parser.parse(time.get('start')).astimezone(tz=tz.tzstr('UTC+03:30')) date = start.date().strftime('%Y-%m-%d') start = start.time().strftime('%H:%M') stop_time = time.get('stop') if not stop_time: report('Skipping entry (already running): ' + str(entry_id) + ' ' + description, Color.WARNING) continue end = parser.parse(stop_time).astimezone(tz=tz.tzstr('UTC+03:30')) end = end.time().strftime('%H:%M') duration = int(time.get('duration')) m, s = divmod(duration, 60) h, m = divmod(m, 60) duration = "%d:%02d" % (h, m) entries.append( Toggle(entry_id, issue, duration, activity=activity, date=date, start=start, end=end, description=description, remote=remote)) return entries
def test_isostring(): assert cast.datetime("2021-03-10T00:00:00+08:00") == dt.datetime( 2021, 3, 10, tzinfo=tz.tzstr("UTC+8"))
def testStrCmp1(self): self.assertEqual(tz.tzstr("EST5EDT"), tz.tzstr("EST5EDT4,M4.1.0/02:00:00,M10-5-0/02:00"))