def test_dt_from_iso(self): tester = '2019-06-06T16:30:54+02:00' tester_utc = '2019-06-06T14:30:54+00:00' dt = tzutils.dt_from_iso(tester) self.assertIsNotNone(dt.tzinfo) self.assertEqual( tzutils.dt_from_iso(tester, as_utc=True).isoformat(), tester_utc)
def reset_state(self, ctxt, res_data): LOG.info('Received state reset command. {}'.format(res_data)) random.shuffle(res_data['scopes']) for scope in res_data['scopes']: lock_name, lock = get_lock(self._coord, scope['scope_id']) LOG.debug('[ScopeEndpoint] Trying to acquire lock "{}" ...'.format( lock_name, )) if lock.acquire(blocking=True): LOG.debug('[ScopeEndpoint] Acquired lock "{}".'.format( lock_name, )) state_dt = tzutils.dt_from_iso(res_data['state']) try: self._storage.delete(begin=state_dt, end=None, filters={ scope['scope_key']: scope['scope_id'], }) self._state.set_state( scope['scope_id'], state_dt, fetcher=scope['fetcher'], collector=scope['collector'], scope_key=scope['scope_key'], ) finally: lock.release() LOG.debug('[ScopeEndpoint] Released lock "{}" .'.format( lock_name, ))
def _build_dataframes(self, docs): dataframes = {} nb_points = 0 for doc in docs: source = doc['_source'] start = tzutils.dt_from_iso(source['start']) end = tzutils.dt_from_iso(source['end']) key = (start, end) if key not in dataframes.keys(): dataframes[key] = dataframe.DataFrame(start=start, end=end) dataframes[key].add_point(self._doc_to_datapoint(source), source['type']) nb_points += 1 output = list(dataframes.values()) output.sort(key=lambda frame: (frame.start, frame.end)) return output
def _get_total_elem(self, begin, end, groupby, series_groupby, point): if groupby and 'time' in groupby: begin = tzutils.dt_from_iso(point['time']) period = point.get(PERIOD_FIELD_NAME) or self._default_period end = tzutils.add_delta(begin, datetime.timedelta(seconds=period)) output = { 'begin': begin, 'end': end, 'qty': point['qty'], 'rate': point['price'], } if groupby: for group in _sanitized_groupby(groupby): output[group] = series_groupby.get(group, '') return output
def _build_dataframes(self, points): dataframes = {} for point in points: point_type = point['type'] time = tzutils.dt_from_iso(point['time']) period = point.get(PERIOD_FIELD_NAME) or self._default_period timekey = (time, tzutils.add_delta(time, datetime.timedelta(seconds=period))) if timekey not in dataframes.keys(): dataframes[timekey] = dataframe.DataFrame(start=timekey[0], end=timekey[1]) dataframes[timekey].add_point( self._point_to_dataframe_entry(point), point_type) output = list(dataframes.values()) output.sort(key=lambda frame: (frame.start, frame.end)) return output
def _normalize_time(t): if isinstance(t, datetime.datetime): return tzutils.utc_to_local(t) return tzutils.dt_from_iso(t)
def dt_from_iso_as_utc(date_string): return tzutils.dt_from_iso(date_string, as_utc=True)