def generate_reply(text, user_id): if not is_valid(text): reply_text = "Incorrect Format - Should be HH:MM (eg. 09:00 or 16:45)" return create_json(reply_text, user_id) hours = text[0:2] minutes = text[3:5] # Handle timezone now = Delorean() now.shift("Europe/London") # Currently only supports UK timezone # If today is Monday and before our target time if now.datetime.weekday() == 0 and (now.datetime.hour < int(hours) or (now.datetime.hour == int(hours) and now.datetime.minute < int(minutes))): monday = now.datetime else: monday = now.next_monday().datetime target = Delorean(datetime.datetime(monday.year, monday.month, monday.day, int(hours), int(minutes)), timezone='Europe/London') # Calculate time result = (target - now) days_until = result.days hours_until = result.seconds / 60 / 60 minutes_until = (result.seconds / 60) - (hours_until * 60) # Format message days_format = "day" if days_until == 1 else "days" hours_format = "hour" if hours_until == 1 else "hours" minutes_format = "minute" if minutes_until == 1 else "minutes" reply_text = "{} {}, {} {} and {} {} until Monday at {}:{}.".format(days_until, days_format, hours_until, hours_format, minutes_until, minutes_format, hours, minutes) return create_json(reply_text, user_id)
def process_item(self, item, spider): if 'search_results' not in getattr(spider,'pipelines',[]): return item dt = Delorean() item['id'] = item['id'][0] item['auction_id'] = item['auction_id'][0] item['site'] = item['site'][0] item['link'] = item['link'][0] item['name'] = item['name'][0] item['price'] = ','.join([x.replace(',','').replace('$','') for x in item['price']]) if 'price' in item else 0 item['modified'] = int(dt.epoch()) existing_record = Session().query(AuctionItem)\ .filter(AuctionItem.id == item['id'])\ .filter(AuctionItem.auction_id == item['auction_id']).all() item = AuctionItem(**item) if existing_record is not None: item.update() else: item.create() return item
def test_delorean_with_timezone(self): dt = datetime.utcnow() d = Delorean(datetime=dt, timezone=UTC) d = d.shift("US/Eastern") dt = utc.localize(dt) dt = est.normalize(dt) self.assertEqual(dt, d._dt) self.assertEqual(est, timezone(d._tz))
def test_delorean_with_only_timezone(self): dt = datetime.utcnow() dt = utc.localize(dt) dt = est.normalize(dt) dt = dt.replace(second=0, microsecond=0) d = Delorean(timezone="US/Eastern") d.truncate('minute') self.assertEqual(est, timezone(d._tz)) self.assertEqual(dt, d._dt)
def test_range_with_start(self): dates1 = [] for do in stops(DAILY, count=5, start=datetime.utcnow()): do.truncate('minute') dates1.append(do) do = Delorean().truncate('minute') dates2 = [] for x in range(5): dates2.append(do.next_day(x)) self.assertEqual(dates1, dates2)
def test_range_with_interval(self): dates1 = [] for do in stops(DAILY, interval=2, count=3, start=datetime.utcnow()): do.truncate('minute') dates1.append(do) do = Delorean().truncate('minute') dates2 = [] for x in range(6): if (x % 2) == 0: dates2.append(do.next_day(x)) self.assertEqual(dates1, dates2)
def test_range_with_start_and_stop(self): dates1 = [] tomorrow = datetime.utcnow() + timedelta(days=1) for do in stops(DAILY, start=datetime.utcnow(), until=tomorrow): do.truncate('minute') dates1.append(do) do = Delorean().truncate('minute') dates2 = [] for x in range(2): dates2.append(do.next_day(x)) self.assertEqual(dates1, dates2)
def test_utc_date_time_deserialize_parse_args(self): """ UTCDateTimeAttribute.deserialize """ tstamp = Delorean(timezone=UTC).datetime attr = UTCDateTimeAttribute() with patch('pynamodb.attributes.parse') as parse: attr.deserialize(Delorean(tstamp, timezone=UTC).datetime.strftime(DATETIME_FORMAT)) parse.assert_called_with(tstamp.strftime(DATETIME_FORMAT), dayfirst=False)
def __init__(self, config): self.client = MongoClient(self.build_uris(config)) self.db = self.client[config['mongo']['database']] self.collection = self.db[config['mongo']['services_collection']] self.tickets = self.db[config['mongo']['tickets_collection']] self.holidays = self.db[config['mongo']['holidays_collection']] self.queue = self.db['queue'] self.d = Delorean() self.d = self.d.shift('Europe/Amsterdam') self.check_connection()
def send_welcome(message): d = Delorean() d = d.shift("UTC") d = d.next_hour(3) if message.text == "Время": bot.send_message(message.chat.id, "Время: " + str(d.format_datetime(locale='en_US'))) elif message.text == "Кто лох?": bot.send_message(message.chat.id, "Игорь - лох!") else: bot.send_message(message.chat.id, "Команды: Время")
def test_utc_date_time_deserialize(self): """ UTCDateTimeAttribute.deserialize """ tstamp = Delorean(timezone=UTC).datetime attr = UTCDateTimeAttribute() self.assertEqual( tstamp, attr.deserialize( Delorean(tstamp, timezone=UTC).datetime.strftime(DATETIME_FORMAT)), )
def test_repr_string_timezone(self): import datetime from delorean import Delorean d1 = Delorean(datetime.datetime(2015, 1, 1), timezone='US/Pacific') d2 = eval(repr(d1)) self.assertEqual(d1, d2) d3 = Delorean(d1.datetime, timezone='UTC') d4 = eval(repr(d3)) self.assertEqual(d1, d4)
def create_quota_headers(qpd=None, qps=None, reset=datetime.now()): qpd_allotted, qpd_current = qpd if qpd else (10000, randint(1, 9000)) qps_allotted, qps_current = qps if qps else (8, randint(1, 5)) reset = Delorean(datetime=reset, timezone='UTC').datetime return { 'X-Plan-QPS-Allotted': qps_allotted, 'X-Plan-QPS-Current': qps_current, 'X-Plan-Quota-Allotted': qpd_allotted, 'X-Plan-Quota-Current': qpd_current, 'X-Plan-Quota-Reset': reset.strftime("%A, %B %d, %Y %I:%M:%S %p %Z") }
def _get_orderbook_from_api_resp(self, req): order_book = self.resp(req) timestamp = int(order_book['timestamp']) now = Delorean() if epoch(timestamp) < now.last_minute(10): raise exceptions.ExchangeAPIErrorException( self, 'Orderbook is more than 10 minutes old', ) return order_book
def filter_by_period(self, queryset, name, value): d = Delorean(timezone="Asia/Bishkek") start = d.datetime end = start if value == "week": end = d.next_sunday().datetime if value == "month": end = (d.next_month().truncate("month") - timedelta(days=1)).datetime if value == "year": end = (d.next_year().truncate("year") - timedelta(days=1)).datetime return queryset.filter(Q(start_date__gte=start, start_date__lte=end))
def test_raises(): since = datetime(2016, 6, 1, 0, 0) now = datetime(2015, 6, 3, 0, 0) assert_raises(ValueError, pycron.has_been, '* * * * *', since, now) assert_raises(ValueError, pycron.has_been, '* * * * *', pendulum.instance(since), pendulum.instance(now)) assert_raises(ValueError, pycron.has_been, '* * * * *', arrow.get(since), arrow.get(now)) assert_raises(ValueError, pycron.has_been, '* * * * *', udatetime.from_string(since.isoformat()), udatetime.from_string(now.isoformat())) assert_raises(ValueError, pycron.has_been, '* * * * *', Delorean(datetime=since, timezone='UTC').datetime, Delorean(datetime=now, timezone='UTC').datetime)
def _store_my_session(self, session: Dict) -> None: sessions = self._load() stored_session = sessions.setdefault(self.__session_id, {}) stored_session.update(session) instant = Delorean().datetime instant_s = instant.strftime(STRFTIME_FORMAT) stored_session["_updated_at"] = instant_s if self.new: expired_at = instant + timedelta(seconds=SESSION_AGE) expired_at_s = expired_at.strftime(STRFTIME_FORMAT) stored_session["_expired_at"] = expired_at_s stored_session["_created_at"] = instant_s self._store(sessions)
def truncate_calendar_data_point_to_period(data_point, period): """ Args: data_point (object): An individual calendar data point. period (string): The time period that each metric represents Returns: obj: The object. """ date_time = datetime.fromtimestamp(int(data_point['datetime'] / 1000)) truncated = Delorean(date_time, timezone='UTC').truncate(period).datetime formatted = truncated.strftime('%Y-%m-%dT%H:%M:%S') return {'datetime': formatted, 'improvement': data_point['improvement']}
def filter_by_date(results: Results) -> list: date_window = Delorean(datetime=dt.datetime(2017, 1, 1), timezone='UTC') date_window1 = Delorean(datetime=dt.datetime(2019, 9, 1), timezone='UTC') containers = [] for result in results.success: st = parse(result['create_time'], dayfirst=False, timezone='UTC') if date_window <= st <= date_window1: containers.append(result) print(f'Found {len(containers)}, filtered containers.') print(*containers, sep='\n') return [k['id'] for k in containers]
def get_next_trading_dt(current, interval): naive = current.replace(tzinfo=None) delo = Delorean(naive, pytz.utc.zone) ex_tz = trading.environment.exchange_tz next_dt = delo.shift(ex_tz).datetime while True: next_dt = next_dt + interval next_delo = Delorean(next_dt.replace(tzinfo=None), ex_tz) next_utc = next_delo.shift(pytz.utc.zone).datetime if trading.environment.is_market_hours(next_utc): break return next_utc
def get(self, month=None): start_time, end_time = self.get_start_time_and_end_time() start_time = queries.strip_datetime_for_db_operations(start_time) end_time = queries.strip_datetime_for_db_operations(end_time) start_timestamp = Delorean(start_time, 'UTC').epoch * 1000 end_timestamp = Delorean(end_time, 'UTC').epoch * 1000 usd_balances_series = {} btc_balances_series = {} for exchange in all_exchanges(): usd_balances, btc_balances = balance_util.get_balance_time_series_for_exchange(self.trading_db, exchange.name, start_time, end_time) usd_balances_series[exchange.name] = usd_balances btc_balances_series[exchange.name] = btc_balances withdrawals = self.trading_db\ .query(Transaction)\ .filter(Transaction.time_created >= start_time)\ .filter(Transaction.time_created < end_time)\ .filter(Transaction._amount_currency == 'BTC')\ .filter(Transaction.transaction_type == 'WITHDRAWL')\ .all() deposits = self.trading_db\ .query(Transaction)\ .filter(Transaction.time_created >= start_time)\ .filter(Transaction.time_created < end_time)\ .filter(Transaction._amount_currency == 'BTC')\ .filter(Transaction.transaction_type == 'DEPOSIT')\ .all() bitcoin_withdrawals = [[int(Delorean(movement.time_created, "UTC").epoch) * 1000, float(movement.amount.amount)] for movement in withdrawals] bitcoin_deposits = [[int(Delorean(movement.time_created, "UTC").epoch) * 1000, float(movement.amount.amount)] for movement in deposits] self.render_template( 'balances.html', args={ 'usd_balances_series': usd_balances_series, 'btc_balances_series': btc_balances_series, 'bitcoin_withdrawals': bitcoin_withdrawals, 'bitcoin_deposits': bitcoin_deposits, 'start_timestamp': start_timestamp, 'end_timestamp': end_timestamp, }, )
def generate_data(self): start_time, end_time = self.get_start_time_and_end_time() start_end_delta = end_time - start_time trades, fundamental_value, open_position_trades, open_position_offset = self.get_trades_and_fundamental_value(self.strategy_actor, start_time, end_time) trade_data, total_volume, daily_volumes, total_position, position_series = self.basic_trade_analysis(self.strategy_actor, trades, start_end_delta, open_position_offset, start_time) matched_trades, position_trades = revenue_lib.split_trades( open_position_trades + trades, volume_currency=self.volume_currency, ) profit, revenue, matched_fees, matched_volume_currency_fees, open_pl, open_position = self.get_current_trading_result( matched_trades, position_trades, self.price_currency, fundamental_value, ) revenue_series = self.get_revenue_series( matched_trades, start_time, ) template_args = { 'price_currency': self.price_currency, 'volume_currency': self.volume_currency, 'trade_data': trade_data, 'revenue_series': revenue_series, 'position_series': position_series, 'display_name': self.display_name, 'profit': profit, 'open_position': open_position, 'open_pl': open_pl, 'volume': total_volume, 'matched_fees': matched_fees, 'matched_volume_currency_fees': matched_volume_currency_fees, 'revenue': revenue, 'start_end_delta': start_end_delta, 'start_timestamp': Delorean(start_time, "UTC").epoch * 1000, 'end_timestamp': Delorean(end_time, "UTC").epoch * 1000, 'now_timestamp': Delorean().epoch * 1000, 'base_point_radius': float(self.base_point_radius), 'position_graph_min': float(self.position_graph_min), 'position_graph_max': float(self.position_graph_max), } return template_args
def crawl_replay(self): fail_list = [] self.total_crawled_saved, self.already_crawled_iteration = 0, 0 for match_id in self.url_suffix_list: one_match = get(self.url_prefix + match_id, headers=self.headers) # print (one_match_dict.status_code) fname = "v0727_" + match_id + ".dem.bz2" self.abs_path = self.replay_path + fname print(fname) if os.path.exists(self.abs_path): self.already_crawled_iteration += 1 state = "重复迭代" continue one_match_dict = loads(one_match.text) one_match_replay_bytes = get(one_match_dict["replay_url"], stream=True) print(one_match_dict["replay_url"]) print(len(one_match_replay_bytes.content)) if one_match_replay_bytes.status_code == 200: try: with open(self.abs_path, "wb") as f: f.write(one_match_replay_bytes.content) print(fname, "写入成功") state = "成功" self.total_crawled_saved += 1 except: print("fail-", match_id) fail_list.append(fname) state = "失败" continue rand_int = randint(1, 3) with open("dem_log.log", "a+", encoding="utf-8") as f: d = Delorean() d = d.shift('Asia/Shanghai') sp_time = d.datetime f.seek(0) print(match_id, state, len(fail_list), "此次成功文件数:", self.total_crawled_saved, "重复迭代数:", self.already_crawled_iteration, sp_time, file=f, flush=True) sleep(rand_int) state = None print(fail_list, len(fail_list))
def get_open_and_close(self, next_open): # creating a naive datetime with the correct hour, # minute, and date. this will allow us to use Delorean to # shift the time between EST and UTC. next_open = next_open.replace(hour=9, minute=30, second=0, tzinfo=None) # create a new Delorean with the next_open naive date and # the correct timezone for the exchange. open_delorean = Delorean(next_open, self.exchange_tz) open_utc = open_delorean.shift("UTC").datetime market_open = open_utc market_close = market_open + self.get_trading_day_duration(open_utc) return market_open, market_close
def dispatch(self, *args, **kwargs): t0 = Delorean() code = 500 content_length = 0 try: resp: HttpResponse = super().dispatch(*args, **kwargs) code = resp.status_code if isinstance(resp, TemplateResponse): resp.render() content_length = len(bytes(resp)) return resp finally: td = Delorean() - t0 count_visit(self.request, code, td.total_seconds(), content_length)
def create_app(environment, port): flask = Flask(__name__) flask.config.from_pyfile('config.py') config = load_config(environment) with open('info.json', 'r') as configFile: info = json.loads(configFile.read()) view_util.navigation_bar = navigation_bar view_util.app_name = info['name'] status.blueprint.navigation_bar = navigation_bar status.blueprint.info = info status.blueprint.environment = environment status.blueprint.port = port status.blueprint.start_time = Delorean.now() mongo = MongoConnect(config) state.mongo = mongo views.blueprint.mongo = mongo views.blueprint.config = config['app'] api.blueprint.mongo = mongo api.blueprint.config = config['app'] flask.register_blueprint(status.blueprint) flask.register_blueprint(views.blueprint) flask.register_blueprint(api.blueprint) return flask
def get_hourly_single_exchange_volume_in_period_from_db_trades( tc_db, exchange_name, start_time, end_time): """ Get the exchange volume for this exchange in this period from our saved version of the trade history. """ # Watch this query for performance. results = tc_db.query( func.hour(EHTrade.timestamp), func.sum(EHTrade._volume), )\ .filter(EHTrade.timestamp >= start_time)\ .filter(EHTrade.timestamp < end_time)\ .filter(EHTrade.exchange == exchange_name)\ .group_by(func.hour(EHTrade.timestamp))\ .all() formatted_results = [] for row in results: hour = row[0] timestamp = Delorean(start_time, 'UTC').next_hour(hour).datetime volume = Money(row[1], 'BTC') formatted_results.append([ timestamp, volume, ]) formatted_results = sorted(formatted_results, key=lambda r: r[0]) return formatted_results
def parse_response(self, response): raw_bids, raw_asks = self.get_bids_and_asks(response) bids, asks = self.parse_orderbook(raw_bids, raw_asks) new_orderbook = { 'timestamp': Delorean().epoch, self.exchange_name: { 'bids': bids, 'asks': asks, } } current_orderbook_string = yield self.redis.get(self.orderbook_key) if not current_orderbook_string: current_orderbook_string = '' new_orderbook_string = json.dumps(new_orderbook, ensure_ascii=False) # TODO: These will never be the same because of the timestamp. if current_orderbook_string != new_orderbook_string: # There is a new orderbook. Save it and publish it. yield self.redis.set(self.orderbook_key, new_orderbook_string) if self.producer: self.producer.publish_message(new_orderbook_string) else: log.msg('No Changes on %s' % self.exchange_name)
def test_minute(): def run(now): assert pycron.is_now('* * * * *', now) assert pycron.is_now('9 * * * *', now) assert pycron.is_now('*/1 * * * *', now) assert pycron.is_now('*/3 * * * *', now) assert pycron.is_now('*/9 * * * *', now) assert pycron.is_now('3,9,25,16 * * * *', now) assert pycron.is_now('*/2 * * * *', now) is False assert pycron.is_now('*/4 * * * *', now) is False assert pycron.is_now('*/5 * * * *', now) is False assert pycron.is_now('*/12 * * * *', now) is False assert pycron.is_now('3,25,16 * * * *', now) is False assert pycron.is_now('0-10 * * * *', now) assert pycron.is_now('0-10 0-10 * * *', now) assert pycron.is_now('10-20 * * * *', now) is False assert pycron.is_now('10-20 10-20 * * *', now) is False assert pycron.is_now('1,2,5-10 * * * *', now) assert pycron.is_now('9,5-8 * * * *', now) assert pycron.is_now('10,20-30 * * * *', now) is False # Issue 14 assert pycron.is_now('1-59/2 * * * *', now) is True assert pycron.is_now('1-59/4 * * * *', now) is True assert pycron.is_now('1-59/8 * * * *', now) is True now = datetime(2015, 6, 18, 0, 9) run(now) run(now.replace(tzinfo=utc)) run(pendulum.instance(now)) run(arrow.get(now)) run(udatetime.from_string(now.isoformat())) run(Delorean(datetime=now, timezone='UTC').datetime)
def test_start(self): l = self.basic_liability l.start() l.time_started.day.should.equal(Delorean().datetime.day) l.time_repayed.should.equal(None)
def create_app(environment, port): flask = Flask(__name__) flask.config.from_pyfile('config.py') config = load_config(environment) with open('info.json', 'r') as configFile: info = json.loads(configFile.read()) view_util.navigation_bar = navigation_bar view_util.app_name = info['name'] status.blueprint.navigation_bar = navigation_bar status.blueprint.info = info status.blueprint.environment = environment status.blueprint.port = port status.blueprint.start_time = Delorean.now() mongo = MongoConnect(config) views.blueprint.mongo = mongo views.blueprint.config = config['app'] api.blueprint.mongo = mongo api.blueprint.config = config['app'] flask.register_blueprint(status.blueprint) flask.register_blueprint(views.blueprint) flask.register_blueprint(api.blueprint) return flask
def check_open_pl(db): # beginning of current UTC day start_time = Delorean().datetime # This profit calculating logic takes about 10s to run open_position_offset = positions.fast_position( db, end_time=start_time, ) open_position_trades = revenue_lib.open_position_trades( open_position_offset, db, start_time, ) latest_order = db\ .query(Order)\ .order_by(Order.time_created.desc())\ .filter(Order.actor == 'Multi')\ .filter(Order._fundamental_value != None)\ .first() open_pl = revenue_lib.open_pl( open_position_trades, latest_order.fundamental_value.to('USD'), 'USD', ) logger.info('Open PL: %s' % open_pl) if open_pl > OPEN_PL_THRESHOLD: succeed('OPEN_PL') else: fail('OPEN_PL')
def create_uniquifier(): hasher = hashlib.sha1() hasher.update(str(Delorean().epoch)) uniquifier = int(hasher.hexdigest(), 16) % 10000 uniquifier *= Decimal("0.000001") assert (uniquifier < Decimal("0.01")) return uniquifier
def end_of_day_in_local_time(input_datetime, local_timezone="Europe/London"): # Return localtime end of day in UTC end_of_day_utc = end_of_day_in_utc(input_datetime) utc_offset_at_input_datetime = Delorean( input_datetime, timezone="utc").shift(local_timezone).datetime.utcoffset() return end_of_day_utc - utc_offset_at_input_datetime
def test_utc_date_time_serialize(self): """ UTCDateTimeAttribute.serialize """ tstamp = datetime.now() attr = UTCDateTimeAttribute() self.assertEqual(attr.serialize(tstamp), Delorean(tstamp, timezone=UTC).datetime.strftime(DATETIME_FORMAT))
def get_local_time(date, ip): reader = geoip2.database.Reader('/Users/scoward/Downloads/GeoIP2-City_20140930/GeoIP2-City.mmdb') if ip == '127.0.0.1': ip = '71.224.243.72' # ip = '167.29.0.143' # ip = '85.158.202.2' response = reader.city(ip) local_time_zone = response.location.time_zone shifted_date = Delorean(date, 'UTC') shifted_date.shift(local_time_zone) shifted_date = shifted_date.datetime.strftime('%Y-%m-%d %H:%M:%S') return shifted_date
def create_app(environment, port): flask = Flask(__name__) flask.config.from_pyfile('config.py') config_loader = ConfigLoader(verify=False) info = config_loader.load_application_info("./") config = config_loader.load_config("resources/", environment) view_util.navigation_bar = navigation_bar view_util.app_name = info['name'] status.blueprint.navigation_bar = navigation_bar status.blueprint.info = info status.blueprint.environment = environment status.blueprint.port = port status.blueprint.start_time = Delorean.now() mongo = MongoConnect(config) state.mongo = mongo views.blueprint.mongo = mongo views.blueprint.config = config['app'] api.blueprint.mongo = mongo api.blueprint.config = config['app'] flask.register_blueprint(status.blueprint) flask.register_blueprint(views.blueprint) flask.register_blueprint(api.blueprint) return flask
def generate_status(): now = Delorean.now() uptime = (now - (now - blueprint.start_time)).humanize() return { "application": { "name": blueprint.info["name"], "description": blueprint.info["description"], "group": blueprint.info["group"], "environment": blueprint.environment, "version": blueprint.info["version"], "commit": blueprint.info["commit"], "vcs_link": blueprint.info["vcs_link"] + blueprint.info["commit"], "status": "OK", "statusDetails": {}, }, "system": { "hostname": socket.gethostname(), "port": blueprint.port, "systemtime": now.format_datetime(format=get_timestamp_format()), "systemstarttime": blueprint.start_time.format_datetime(format=get_timestamp_format()), "uptime": uptime, }, "team": { "team": blueprint.info["team"], "contact_technical": blueprint.info["contact_technical"], "contact_business": blueprint.info["contact_business"], }, "serviceSpecs": {}, }
class Meetup(models.Model): id = models.CharField(max_length=100, primary_key=True) name = models.CharField(max_length=255, blank=False) description = models.TextField() event_url = models.URLField() sponsors = models.ManyToManyField(Sponsor, through=MeetupSponsorRelationship, blank=True) time = models.DateTimeField() created = models.DateTimeField() updated = models.DateTimeField( default=Delorean(datetime(1970, 1, 1), timezone="UTC").datetime) rsvps = models.IntegerField(default=0) maybe_rsvps = models.IntegerField(default=0) waitlist_count = models.IntegerField(default=0) status = models.CharField(max_length=255, blank=False) visibility = models.CharField(max_length=255, blank=False) class Meta: ordering = ["time"] def __str__(self): return self.name @classmethod def future_events(cls): today = datetime.now() return cls.objects.filter(time__gt=today).filter( time__lt=next_n_months(today, 3))
def main(): config.load_incluster_config() global v1 v1 = client.CoreV1Api() ret = v1.list_pod_for_all_namespaces(watch=False) for pod in ret.items: if pod.status.phase in ["Running", "Succeeded"] or not is_ok_to_touch(pod): continue restart_count, is_container_creating = container_info(pod) if is_container_creating: # figure out age elapsed_time = current_time - Delorean(pod.status.start_time) elapsed_time = int(elapsed_time.total_seconds()) if elapsed_time > MAX_AGE: # if it's been stuck for more than MAX_AGE # and it is a statefulset, we should detach its volume, # otherwise evict the pod if is_statefulset(pod): detach_volume(pod) else: evict_pod(pod) continue if restart_count >= MAX_RESTART: evict_pod(pod)
class MongoConnect: def __init__(self, config): self.client = MongoClient(config["mongo"]["uris"]) self.db = self.client[config["mongo"]["database"]] self.collection = self.db[config["mongo"]["collection"]] self.d = Delorean() self.d = self.d.shift("Europe/Amsterdam")
def generate_dashboard() -> DashboardT: dimensions = {"latency": "tm", "traffic": "cl"} aggregates = (Max, Min, Avg) select = { f"{dimension}_{aggregate.__name__.lower()}": aggregate(dimension) for aggregate, dimension in product(aggregates, dimensions.values()) } ts_now = Delorean() params = {} for measure_attr, minutes in zip(HourlyT.__annotations__, (5, 15, 60, 60 * 24)): delta = timedelta(minutes=minutes) ts = (ts_now - delta).datetime query = Visit.objects.filter(at__gte=ts).aggregate(**select) for dimension_attr, dimension in dimensions.items(): max_value = query[f"{dimension}_max"] min_value = query[f"{dimension}_min"] avg_value = query[f"{dimension}_avg"] metric = MinMaxT(min=min_value, max=max_value, avg=avg_value) params.setdefault(dimension_attr, {})[measure_attr] = metric dashboard = DashboardT(**params) return dashboard
def test_day_matching(): def run(now): for i in range(0, 7): # Test day matching from Sunday onwards... now += timedelta(days=1) assert pycron.is_now('* * * * %s' % (pycron.DAY_NAMES[i]), now) assert pycron.is_now('* * * * %s' % (pycron.DAY_ABBRS[i]), now) # Test weekdays assert pycron.is_now('* * * * mon,tue,wed,thu,fri', now) is (True if i not in [0, 6] else False) assert pycron.is_now( '* * * * monday,tuesday,wednesday,thursday,friday', now) is (True if i not in [0, 6] else False) assert pycron.is_now('* * * * mon-fri', now) is (True if i not in [0, 6] else False) assert pycron.is_now('* * * * monday-friday', now) is (True if i not in [0, 6] else False) assert pycron.is_now('* * * * mon,tue,wed,thu-fri', now) is (True if i not in [0, 6] else False) assert pycron.is_now( '* * * * monday,tuesday,wednesday,thursday-friday', now) is (True if i not in [0, 6] else False) # Test weekends assert pycron.is_now('* * * * sun,sat', now) is (True if i in [0, 6] else False) assert pycron.is_now('* * * * sunday,saturday', now) is (True if i in [0, 6] else False) now = datetime(2015, 6, 20, 16, 7) run(now) run(now.replace(tzinfo=utc)) run(pendulum.instance(now)) run(arrow.get(now)) run(udatetime.from_string(now.isoformat())) run(Delorean(datetime=now, timezone='UTC').datetime)
def parse_query_value(query_str, tf): """ Return value for the query string """ try: query_str = str(query_str).strip('"\' ') if query_str == 'now': d = Delorean(timezone=tf) else: # Parse datetime string or timestamp try: d = epoch(float(query_str)) d.shift(tf); except ValueError: d = parse(str(query_str)) d.shift(tf); except (TypeError, ValueError): d = None return d
def __init__(self, config): self.client = MongoClient(config['mongo']['uris']) self.db = self.client[config['mongo']['database']] self.collection = self.db[config['mongo']['services_collection']] self.tickets = self.db[config['mongo']['tickets_collection']] self.queue = self.db['queue'] self.d = Delorean() self.d = self.d.shift('Europe/Amsterdam')
def get_ticket(self, ticket_id): ticket = self.tickets.find_one({"_id": ticket_id}) now = Delorean.now().epoch if ticket and (ticket["expiration_date"] == 0 or ticket["expiration_date"] > now): ticket.pop('_id') return ticket else: self.remove_ticket(ticket_id) return None
def get_open_and_close(self, next_open): # creating a naive datetime with the correct hour, # minute, and date. this will allow us to use Delorean to # shift the time between EST and UTC. next_open = next_open.replace( hour=9, minute=30, second=0, tzinfo=None ) # create a new Delorean with the next_open naive date and # the correct timezone for the exchange. open_delorean = Delorean(next_open, self.exchange_tz) open_utc = open_delorean.shift("UTC").datetime market_open = open_utc market_close = market_open + self.get_trading_day_duration(open_utc) return market_open, market_close
def test_normalize(self): dt1 = Delorean() dt2 = Delorean(timezone="US/Eastern") dt1.truncate('minute') dt2.truncate('minute') dt_normalized = normalize(dt1.datetime, "US/Eastern") self.assertEqual(dt2.datetime, dt_normalized)
def test_time_timtravel(self, seconds=1, minutes=1, microseconds=100): d = Delorean() sample = datetime(2011, 10, 10, 1, 33, 4, 121940) d.utcdatetime = sample current_time = d.datetime() current_time = current_time + timedelta(seconds=1, minutes=1, microseconds=100) d.timetravel(seconds=1, minutes=1, microseconds=100) self.assertEqual(d.time(), current_time.time())
def test_date_timetravel(self, days=1, weeks=1): d=Delorean() sample = datetime(2002,10,10) d.utcdatetime = sample current_date = d.date() current_date = current_date + timedelta(days=1, weeks=1) d.timetravel(days=1, weeks=1) self.assertEqual(d.date(), current_date)
def gates(): try: service_list = OrderedDict() env_list = dict() now = Delorean.now() for group in blueprint.mongo.get_groups(): env_list[group] = set() service_list[group] = dict() for service_name in blueprint.mongo.get_services_in_group(group): service = blueprint.mongo.get_gate(group, service_name) for env in service["environments"]: env_list[group].add(env) if service["environments"][env]["state_timestamp"]: service["environments"][env]["state_age"] = ( now - (now - parse(service["environments"][env]["state_timestamp"])) ).humanize() if service["environments"][env]["message_timestamp"]: service["environments"][env]["message_age"] = ( now - (now - parse(service["environments"][env]["message_timestamp"])) ).humanize() service["environments"][env]["api_closed"] = gate_is_closed(service, env) for t in service["environments"][env]["queue"]: t["age"] = (now - (now - parse(t["updated"]))).humanize() service_list[group][service_name] = service env_list[group] = sorted(env_list[group]) return view_util.render( "gates.html", "Gates", env_list=env_list, gate_list=service_list, info_list=util.generate_info(blueprint.config), ) except (ConnectionFailure, OperationFailure) as error: return view_util.error_page(error.message)
def epoch(date): dt = Delorean(datetime=date, timezone='US/Eastern') return int(dt.epoch())
def __init__(self, the_datetime, the_timezone): """the_datetime must be a datetime.datetime the_timezone is a String identifing timezone: EX: 'Europe/Madrid' 'UTC' See: all_timezones""" the_timezone = self._get_timezone_parameter(the_timezone) self._delorean = Delorean(datetime=the_datetime, timezone=the_timezone).shift("UTC") self._delorean.truncate('second') # Truncate to second, its the precission of serialize
class MongoConnect: def __init__(self, config): self.client = MongoClient(config['mongo']['uris']) self.db = self.client[config['mongo']['database']] self.collection = self.db[config['mongo']['services_collection']] self.tickets = self.db[config['mongo']['tickets_collection']] self.queue = self.db['queue'] self.d = Delorean() self.d = self.d.shift('Europe/Amsterdam') def get_environment_structure(self, environment_list): data = dict() for env in environment_list: env = env.strip() data[env] = dict() data[env]['state'] = 'open' data[env]['state_timestamp'] = self.get_formatted_timestamp() data[env]['message'] = '' data[env]['message_timestamp'] = '' data[env]['queue'] = [] return data def create_new_gate(self, group, name, request): if not name or '.' in name: raise ServiceNameNotValid if not group or '.' in group: raise GroupNameNotValid if self.check_existence(group, name): raise ServiceAlreadyExists if 'environments' not in request or not request['environments'] or '' in request['environments'] or type( request['environments']) != list: raise JsonStructureError("Invalid environments.") try: data = dict() data['_id'] = str(uuid.uuid4()) data['document_version'] = 2.2 data['name'] = name.strip() data['group'] = group.strip() data['environments'] = self.get_environment_structure(request['environments']) self.collection.insert_one(data) data.pop('_id') # do not return _id return data except pymongo.errors.NotMasterError as error: raise NotMasterError(error.message) def remove_gate(self, group, name): if not self.check_existence(group, name): raise NotFound try: self.collection.remove({"name": name, "group": group}) except pymongo.errors.NotMasterError as error: raise NotMasterError(error.message) def update_gate(self, group, name, entry): try: self.collection.update({"name": name, "group": group}, {'$set': entry}, upsert=False) except pymongo.errors.ConnectionFailure as error: raise ConnectionFailure(error.message) except pymongo.errors.OperationFailure as error: raise OperationFailure(error.message) def get_gate(self, group, name): entry = self.check_existence(group, name) if not entry: raise NotFound for env, info in entry['environments'].iteritems(): tickets = [] for ticket_id in info['queue']: ticket = self.get_ticket(ticket_id) if ticket: tickets.append(ticket) else: self.remove_ticket_link(group, name, env, ticket_id) entry['environments'][env]['queue'] = tickets return entry def legacy_get_gate(self, name): entry = self.legacy_check_existence(name) if not entry: raise NotFound for env, info in entry['environments'].iteritems(): tickets = [] for ticket_id in info['queue']: ticket = self.get_ticket(ticket_id) if ticket: tickets.append(ticket) else: self.legacy_remove_ticket_link(name, env, ticket_id) entry['environments'][env]['queue'] = tickets return entry def set_gate(self, group, name, environment, state): entry = self.check_existence(group, name) self.validate_environment_state(entry, environment, state) try: current_state = entry['environments'][environment]['state'] if current_state != state: entry['environments'][environment]['state'] = state entry['environments'][environment]['state_timestamp'] = self.get_formatted_timestamp() return self.collection.update({'name': name, 'group': group}, {'$set': entry}, upsert=False) except pymongo.errors.NotMasterError as error: raise NotMasterError(error.message) @staticmethod def get_expiration_date(minutes_delta): expiration_date = Delorean.now() expiration_date += timedelta(minutes=minutes_delta) return expiration_date.epoch def add_ticket_link(self, group, name, environment, ticket_id): self.collection.update({'name': name, 'group': group}, {'$push': {"environments." + environment + ".queue": ticket_id}}) def legacy_add_ticket_link(self, name, environment, ticket_id): self.collection.update({'name': name}, {'$push': {"environments." + environment + ".queue": ticket_id}}) def remove_ticket_link(self, group, name, environment, ticket_id): self.collection.update({'name': name, 'group': group}, {'$pull': {"environments." + environment + ".queue": ticket_id}}) def legacy_remove_ticket_link(self, name, environment, ticket_id): self.collection.update({'name': name}, {'$pull': {"environments." + environment + ".queue": ticket_id}}) def add_ticket(self, ticket_id, ticket): try: self.tickets.update({"_id": ticket_id}, ticket, upsert=True) except pymongo.errors.NotMasterError as error: raise NotMasterError(error.message) return ticket def get_ticket(self, ticket_id): ticket = self.tickets.find_one({"_id": ticket_id}) now = Delorean.now().epoch if ticket and (ticket["expiration_date"] == 0 or ticket["expiration_date"] > now): ticket.pop('_id') return ticket else: self.remove_ticket(ticket_id) return None def remove_ticket(self, ticket_id): self.tickets.remove({"_id": ticket_id}) def update_ticket(self, ticket_id, ticket): self.tickets.update({"_id": ticket_id}, {'$set': ticket}, upsert=False) @staticmethod def validate_environment_state(entry, environment, state): if not entry: raise NotFound if environment not in entry['environments']: raise EnvironmentNotFound if state not in ["open", "closed"]: raise GateStateNotValid def set_message(self, group, name, environment, message): entry = self.check_existence(group, name) if not entry: raise NotFound if environment not in entry['environments']: raise EnvironmentNotFound try: entry['environments'][environment]['message'] = message entry['environments'][environment]['message_timestamp'] = self.get_formatted_timestamp() if message else "" return self.collection.update({"name": name, "group": group}, {'$set': entry}, upsert=False) except pymongo.errors.NotMasterError as error: raise NotMasterError(error.message) def check_existence(self, group, name): return self.collection.find_one({"name": name, "group": group}, {'_id': False}) def legacy_check_existence(self, name): return self.collection.find_one({"name": name}, {'_id': False}) def get_groups(self): return self.collection.distinct('group') def get_services_in_group(self, group): return self.collection.find({'group': group}).distinct("name") def get_formatted_timestamp(self): return self.d.now().format_datetime(format='y-MM-dd HH:mm:ssz')
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import pandas as pd import pytz from datetime import datetime, timedelta from dateutil import rrule from delorean import Delorean start = datetime(1990, 1, 1, tzinfo=pytz.utc) end_dln = Delorean(datetime.utcnow(), 'UTC') end_dln.shift('US/Eastern').truncate('day').shift('UTC') end = end_dln.datetime - timedelta(days=1) def get_non_trading_days(start, end): non_trading_rules = [] weekends = rrule.rrule( rrule.YEARLY, byweekday=(rrule.SA, rrule.SU), cache=True, dtstart=start, until=end ) non_trading_rules.append(weekends)
def setUp(cls): config.CURRENT_TICKET_LIFETIME = 1 cls.an_hour_from_now = Delorean.now().epoch + 3600000
def get_expiration_date(minutes_delta): expiration_date = Delorean.now() expiration_date += timedelta(minutes=minutes_delta) return expiration_date.epoch
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import pandas as pd import pytz from datetime import datetime, timedelta from dateutil import rrule from delorean import Delorean start = datetime(1990, 1, 1, tzinfo=pytz.utc) end_dln = Delorean(datetime.utcnow(), pytz.utc.zone) end_dln.shift('US/Eastern').truncate('day').shift(pytz.utc.zone) end = end_dln.datetime - timedelta(days=1) def get_non_trading_days(start, end): non_trading_rules = [] weekends = rrule.rrule( rrule.YEARLY, byweekday=(rrule.SA, rrule.SU), cache=True, dtstart=start, until=end ) non_trading_rules.append(weekends)
def utc_dt_in_exchange(self, dt): delorean = Delorean(dt, pytz.utc.zone) return delorean.shift(self.exchange_tz).datetime
def exchange_dt_in_utc(self, dt): delorean = Delorean(dt, self.exchange_tz) return delorean.shift(pytz.utc.zone).datetime