def is_cache_available(self, cache_file_path): if not cache_file_path.isfile(): logger.debug("cache not found: {}".format(cache_file_path)) return False try: dtr = DateTimeRange(datetime.fromtimestamp(cache_file_path.mtime), datetime.now()) except OSError: return False if not dtr.is_valid_timerange(): return False cache_elapsed = CacheTime(dtr.get_timedelta_second()) cache_msg = "path={path}, lifetime={lifetime:.1f}h, elapsed={elapsed:.1f}h".format( path=cache_file_path, lifetime=self.__cache_lifetime.hour, elapsed=cache_elapsed.hour) if cache_elapsed < self.__cache_lifetime: logger.debug("cache available: {}".format(cache_msg)) return True logger.debug("cache expired: {}".format(cache_msg)) return False
def parseIbHours(ibHours, tz): if not isinstance(ibHours, str): fatal.errorAndExit('trading hours is a string') openHours = [] # '20200427:0930-20200427:1600;20200428:0930-20200428:1600' ranges = ibHours.split(';') m = re.compile('.*:CLOSED') for range_ in ranges: if range_ == '': continue if m.match(range_): # skip closed days continue ts = range_.split('-') if len(ts) != 2: fatal.errorAndExit( 'only two timestamps per range: {} {}'.format(ts, ibHours)) start = tz.localize(datetime.strptime( ts[0], '%Y%m%d:%H%M')).astimezone(pytz.utc) end = tz.localize(datetime.strptime(ts[1], '%Y%m%d:%H%M')).astimezone( pytz.utc) r = DateTimeRange(start, end) if not r.is_valid_timerange(): fatal.errorAndExit('should get a valid timerange') openHours.append(r) logging.debug('openHours: %s', openHours) return openHours