def setup(hass, config): if any(conf[CONF_TIME_AS] in (TZ_DEVICE_UTC, TZ_DEVICE_LOCAL) for conf in (config.get(DT_DOMAIN) or []) if conf[CONF_PLATFORM] == DOMAIN): pkg = config[DOMAIN][CONF_TZ_FINDER] try: asyncio.run_coroutine_threadsafe( async_process_requirements(hass, "{}.{}".format(DOMAIN, DT_DOMAIN), [pkg]), hass.loop, ).result() except RequirementsNotFound: _LOGGER.debug("Process requirements failed: %s", pkg) return False else: _LOGGER.debug("Process requirements suceeded: %s", pkg) if pkg.split("==")[0].strip().endswith("L"): from timezonefinderL import TimezoneFinder tf = TimezoneFinder() elif config[DOMAIN][CONF_TZ_FINDER_CLASS] == "TimezoneFinder": from timezonefinder import TimezoneFinder tf = TimezoneFinder() else: from timezonefinder import TimezoneFinderL tf = TimezoneFinderL() hass.data[DOMAIN] = tf return True
def __init__(self, hass, config, see, interval, home_place, members, api): self._hass = hass self._see = see self._show_as_state = config[CONF_SHOW_AS_STATE] self._home_place = home_place self._max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY) self._max_update_wait = config.get(CONF_MAX_UPDATE_WAIT) prefix = config.get(CONF_PREFIX) self._prefix = '' if not prefix else prefix + '_' self._members = members self._driving_speed = config.get(CONF_DRIVING_SPEED) self._time_as = config[CONF_TIME_AS] self._api = api self._errs = {} self._error_threshold = config[CONF_ERROR_THRESHOLD] self._warning_threshold = config.get(CONF_WARNING_THRESHOLD, self._error_threshold) if self._warning_threshold > self._error_threshold: _LOGGER.warning('Ignoring {}: ({}) > {} ({})'.format( CONF_WARNING_THRESHOLD, self._warning_threshold, CONF_ERROR_THRESHOLD, self._error_threshold)) self._max_errs = self._error_threshold + 2 self._dev_data = {} if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]: from timezonefinderL import TimezoneFinder self._tf = TimezoneFinder() self._started = dt_util.utcnow() self._update_life360() track_time_interval(self._hass, self._update_life360, interval)
def __init__(self, hass, config, see): self._hass = hass self._see = see entities = config[CONF_ENTITY_ID] self._entities = {} for entity_id in entities: self._entities[entity_id] = { WARNED: False, SOURCE_TYPE: None, STATE: None } self._dev_id = config[CONF_NAME] self._entity_id = ENTITY_ID_FORMAT.format(self._dev_id) self._time_as = config[CONF_TIME_AS] if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]: from timezonefinderL import TimezoneFinder self._tf = TimezoneFinder() self._lock = threading.Lock() self._prev_seen = None self._remove = track_state_change(hass, entities, self._update_info) for entity_id in entities: self._update_info(entity_id, None, hass.states.get(entity_id), init=True)
def __init__(self, hass, config, see): self._hass = hass self._see = see entities = config[CONF_ENTITY_ID] self._entities = {} for entity_id in entities: self._entities[entity_id] = { WARNED: False, SEEN: None, SOURCE_TYPE: None, DATA: None} self._dev_id = config[CONF_NAME] self._entity_id = ENTITY_ID_FORMAT.format(self._dev_id) self._time_as = config[CONF_TIME_AS] if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]: from timezonefinderL import TimezoneFinder self._tf = TimezoneFinder() self._req_movement = config[CONF_REQ_MOVEMENT] self._lock = threading.Lock() self._prev_seen = None self._init_complete = False self._remove = track_state_change( hass, entities, self._update_info) for entity_id in entities: self._update_info(entity_id, None, hass.states.get(entity_id)) def init_complete(event): self._init_complete = True hass.bus.listen_once(EVENT_HOMEASSISTANT_START, init_complete)
def on_clock(req): city = req.intent.slot('location').first().value if not city: current_time = datetime.now().time() #resp = req._('It\'s {}').format(current_time.strftime(req._('%I:%M %p'))) resp = req._('It\'s {}').format(req._d(current_time, time_only=True)) req.agent.answer(resp) return req.agent.done() else: try: g = geocoder.osm(city) if not g: resp = req._('Hummm! It seems {0} doesn\'t exists as city name').format(city) req.agent.answer(resp) return req.agent.done() except: resp = req._('Hummm! I encountered an error during the city information gathering') req.agent.answer(resp) return req.agent.done() tf = TimezoneFinder() tzStr = tf.timezone_at(lng=g.lng, lat=g.lat) if tzStr == '': resp = req._('Hummm! I can\'t retrieve time zone information of {0}').format(city) req.agent.answer(resp) return req.agent.done() tzObj = timezone(tzStr) current_time = datetime.now(tzObj) #resp = req._('It\'s {0} in {1}').format(current_time.strftime(req._('%I:%M %p'), city) resp = req._('It\'s {0} in {1}').format(req._d(current_time, time_only=True), city) req.agent.answer(resp) return req.agent.done()
def __init__(self, hass, see, interval, show_as_state, home_place, max_gps_accuracy, max_update_wait, prefix, members, driving_speed, time_as, api): self._hass = hass self._see = see self._show_as_state = show_as_state self._home_place = home_place self._max_gps_accuracy = max_gps_accuracy self._max_update_wait = max_update_wait self._prefix = '' if not prefix else prefix + '_' self._members = members self._driving_speed = driving_speed self._time_as = time_as self._api = api self._errs = {} self._max_errs = 2 self._dev_data = {} if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]: from timezonefinderL import TimezoneFinder self._tf = TimezoneFinder() self._started = dt_util.utcnow() self._update_life360() track_time_interval(self._hass, self._update_life360, interval)
def add_future(self, load: pd.Series) -> pd.Series: future = pd.date_range(start=load.index[-1], end=(load.index[-1] + timedelta(days=1)), freq="H").to_frame(name="load_MW") tz_finder = TimezoneFinder() lon = float(GEO_COORDS[self.iso_name]["lon"]) lat = float(GEO_COORDS[self.iso_name]["lat"]) tz_name = tz_finder.timezone_at(lng=lon, lat=lat) future["load_MW"] = None future.index = future.index.tz_convert(tz_name) return future
def get_historical_load(self) -> pd.DataFrame: if self.iso_name == "CAISO": load = self.get_caiso_load() elif ( self.iso_name == "MISO" or self.iso_name == "PJM" or self.iso_name == "ERCOT" ): load = self.get_eia_load() else: load = pd.DataFrame( self.iso.get_load( latest=False, yesterday=False, start_at=self.start, end_at=self.end ) )[LOAD_COLS].set_index("timestamp") tz_finder = TimezoneFinder() tz_name = tz_finder.timezone_at(lng=float(self.lon), lat=float(self.lat)) load.index = load.index.tz_convert(tz_name) return load.resample("H").mean()
def timezone_offset(lat, lng, date_time): tf = TimezoneFinder() """ returns a location's time zone offset from UTC in minutes. """ tz_target = pytz.timezone(tf.certain_timezone_at(lng=lng, lat=lat)) if tz_target is None: print("No timezone found in", str((lat, lng))) return () # ATTENTION: tz_target could be None! handle error case #date_time = date_time.tzinfo=None#tzinfo=tz_target)#.utcoffset() #print("tzinfo = ",str(date_time.tzinfo)) dt = date_time if dt.tzinfo is None: dated_target = tz_target.localize(dt) utc = pytz.utc dated_utc = utc.localize(dt) #return (dated_utc - dated_target).total_seconds() / 60 / 60 return (strfdelta(dated_utc - dated_target, "%s%H:%M:%S")) else: print(dt.tzinfo) return ()
def __init__(self, hass, config, see, interval, home_place_name, api): """Initialize Life360Scanner.""" self._hass = hass self._see = see self._show_as_state = config[CONF_SHOW_AS_STATE] self._home_place_name = home_place_name self._max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY) self._max_update_wait = config.get(CONF_MAX_UPDATE_WAIT) prefix = config.get(CONF_PREFIX) self._prefix = '' if not prefix else prefix + '_' self._members = config.get(CONF_MEMBERS) self._driving_speed = config.get(CONF_DRIVING_SPEED) self._time_as = config[CONF_TIME_AS] self._api = api self._errs = {} self._error_threshold = config[CONF_ERROR_THRESHOLD] self._warning_threshold = config.get(CONF_WARNING_THRESHOLD, self._error_threshold) self._max_errs = self._error_threshold + 2 self._dev_data = {} if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]: from timezonefinderL import TimezoneFinder self._tf = TimezoneFinder() self._seen_members = set() if self._members is not None: _LOGGER.debug( 'Including: %s', ', '.join([ self._prefix + slugify(name.replace(',', '_').replace('-', '_')) for name in self._members ])) self._started = dt_util.utcnow() self._update_life360() track_time_interval(self._hass, self._update_life360, interval)
def setup(hass, config): if (any(conf[CONF_TIME_AS] in (TZ_DEVICE_UTC, TZ_DEVICE_LOCAL) for conf in (config.get(DT_DOMAIN) or []) if conf[CONF_PLATFORM] == DOMAIN)): pkg = config[DOMAIN][CONF_TZ_FINDER] try: asyncio.run_coroutine_threadsafe( async_process_requirements(hass, '{}.{}'.format(DOMAIN, DT_DOMAIN), [pkg]), hass.loop).result() except RequirementsNotFound: _LOGGER.debug('Process requirements failed: %s', pkg) return False else: _LOGGER.debug('Process requirements suceeded: %s', pkg) if pkg.split('==')[0].strip().endswith('L'): from timezonefinderL import TimezoneFinder else: from timezonefinder import TimezoneFinder hass.data[DOMAIN] = TimezoneFinder() return True
import re import datetime from importlib import import_module from itertools import chain from dateutil.parser import parse from urllib.parse import urlparse from timezonefinderL import TimezoneFinder from datacube.utils import geometry from pytz import timezone, utc import logging _LOG = logging.getLogger(__name__) tf = TimezoneFinder(in_memory=True) # Use metadata time if possible as this is what WMS uses to calculate it's temporal extents # datacube-core center time accessed through the dataset API is calculated and may # not agree with the metadata document def dataset_center_time(dataset): center_time = dataset.center_time try: metadata_time = dataset.metadata_doc['extent']['center_dt'] center_time = parse(metadata_time) except KeyError: try: metadata_time = dataset.metadata_doc['properties'][ 'dtr:start_datetime'] center_time = parse(metadata_time)
# ---------- OUTPUT ------------ path_results = "/home/lmoldon/data/weekenders.json" # ------------------------------ # ---------- CONFIG ------------ datetimeFormat = "%Y-%m-%d %H:%M:%S" # ------------------------------ # ---------- INITIAL ----------- logging.basicConfig(format='%(asctime)s [%(levelname)s] - %(message)s', datefmt='%d-%m-%y %H:%M:%S', level=logging.INFO) usergroups = {} # key = userID, value = {"f": cur_usr_freetime, "w": cur_usr_worktime} userdata = {} # for location (timezone) tf = TimezoneFinder() valid_users = 0 standard_zone = timezone("UTC") # according to "Why do People Give Up Flossing? A Study of Contributor Disengagement in Open Source" GHTorrent only uses UTC as timezone # ------------------------------ log_starttime = datetime.datetime.now() logging.info("Accessing userdata ...") with open(path_source_userdata, "r") as fp: userdata = json.load(fp) logging.info("Done. (1/2)")
from peaky_finders.training_pipeline import MODEL_OUTPUT_DIR, MODEL_INPUT_DIR from peaky_finders.data_acquisition.train_model import GEO_COORDS ISO_MAP_IDS = { 56669: "MISO", 14725: "PJM", 2775: "CAISO", 13434: "ISONE", 13501: "NYISO", } ISO_LIST = ["NYISO", "ISONE", "PJM", "MISO", "CAISO"] PEAK_DATA_PATH = os.path.join(os.path.dirname(__file__), "historical_peaks") tz_finder = TimezoneFinder() def get_iso_map(): iso_df = pd.read_csv("iso_map_final.csv") iso_df["geometry"] = iso_df["geometry"].apply(wkt.loads) iso_gdf = gpd.GeoDataFrame(iso_df, crs="EPSG:4326", geometry="geometry") return iso_gdf class Predictor: def __init__(self, iso_name: str, start: str, end: str) -> None: self.start = start self.end = end self.iso_name = iso_name self.load_collector: LoadCollector = None
def insights(self, ip): """ Get insights in ip :param ip: The ip :return: Insights :rtype: geoip2.models.City """ if request.remote_addr != ip: raise RuntimeError( "Can only use GoogleAppEngine-location-driver for looking up location of request-ip (=%s) (lookup=%s)" % (request.remote_addr, ip)) raw_response = {} # Country country_iso = request.headers[ 'X-AppEngine-Country'] if 'X-AppEngine-Country' in request.headers else None country_iso = country_iso if country_iso and country_iso != 'ZZ' else None if country_iso: country_iso = Encoding.normalize(country_iso) raw_response['country'] = {'iso_code': country_iso} raw_response['registered_country'] = raw_response['country'] raw_response['represented_country'] = raw_response['country'] # Region region_iso = request.headers[ 'X-AppEngine-Region'] if 'X-AppEngine-Region' in request.headers else None region_iso = region_iso if region_iso else None if region_iso: region_iso = Encoding.normalize(region_iso) raw_response['subdivisions'] = [{'iso_code': region_iso}] # City city = request.headers[ 'X-AppEngine-City'] if 'X-AppEngine-City' in request.headers else None city = city if city else None if city: city = Encoding.normalize(city) raw_response['city'] = {'names': {'en': city}} # Location city_lat_long = request.headers[ 'X-AppEngine-CityLatLong'] if 'X-AppEngine-CityLatLong' in request.headers else None city_lat_long = city_lat_long if city_lat_long else None latitude, longitude = city_lat_long.split(',') if city_lat_long else ( None, None) if latitude and longitude: latitude = float(Encoding.normalize(latitude)) longitude = float(Encoding.normalize(longitude)) raw_response['location'] = { 'latitude': latitude, 'longitude': longitude, } timezone_finder = TimezoneFinder() timezone = timezone_finder.timezone_at(lat=latitude, lng=longitude) timezone = timezone if timezone else None if timezone: raw_response['location']['time_zone'] = timezone return City(raw_response)
def parse_fields_2017_coord(tweet_obj): # parse files of 2017 data, where coord exists tzfinder = TimezoneFinder() utc = pytz.utc def parse_text(tw_obj): # remove use mentions, urls from the text # use extended tweet if presents if 'extended_tweet' in tw_obj: text = tw_obj['extended_tweet']['full_text'] # or use normal text else: text = tw_obj['text'] # process quoted tweet and append to text if tw_obj['is_quote_status'] and 'quoted_status' in tw_obj: # process quoted tweet qt_obj = tw_obj['quoted_status'] if 'extended_tweet' in qt_obj: qt_text = qt_obj['extended_tweet']['full_text'] # or use normal text else: qt_text = qt_obj['text'] text = ''.join([text, ' %QUOTES% ', qt_text]) text_norm = normalizeTextForTagger(replace_sp_tokens(text)) # process text into list of keywords text_tokens = get_tokens(text) text_tokens = [t for t in text_tokens if t not in stopwords] token_counts = dict(Counter(itertools.chain(*[text_tokens]))) # text_tokens = [lemma(t) for t in text_tokens] return text, text_norm, text_tokens, token_counts def parse_time(tw_obj): # parse timestamp to needed format # we need an actual timestamp in utc # and a fake local timestamp in utc # get timezone info point = tw_obj['coordinates']['coordinates'] try: tz_name = tzfinder.timezone_at(lng=point[0], lat=point[1]) tz_info = timezone(tz_name) except Exception as e: # if there is error when converting timezone # give default timezone as: US/Central tz_name = 'US/Central' tz_info = timezone(tz_name) # parse the utc timestamp time_obj = datetime.strptime(tw_obj['created_at'], time_format) # convert to local timestamp local_time_obj = time_obj.astimezone(tz_info) # get local hour mark for "time-of-day" query hour_mark = local_time_obj.time().hour # make a fake local timestamp with UTC timezone fake_time_obj = utc.localize(local_time_obj.replace(tzinfo=None)) return time_obj, local_time_obj, fake_time_obj, hour_mark, tz_name def parse_category(tw_obj): # parse category into str return 'Positive' # label = int(row['label']) # if label == 1: # return 'Positive' # else: # return 'Negative' def parse_drug_category(text_norm): cats = {} for cat in drug_category: for t in drug_category[cat]: if t in text_norm: if cat in cats: cats[cat].append(t) else: cats[cat] = [t] return cats result_dict = {} time_obj, local_time_obj, fake_time_obj, hour_mark, tz_name = parse_time( tweet_obj) try: text, text_norm, text_tokens, token_counts = parse_text(tweet_obj) except: print(tweet_obj) return None # put up dict result_dict['tweetID'] = tweet_obj['id_str'] result_dict['utcTime'] = time_obj # actual local time is not neede since db always saves utc # result_dict['localTime'] = local_time_obj result_dict['fakeLocalTime'] = fake_time_obj # result_dict['hourMark'] = hour_mark result_dict['timezone'] = tz_name result_dict['Followers'] = tweet_obj['user']['followers_count'] result_dict['Friends'] = tweet_obj['user']['friends_count'] result_dict['Statuses'] = tweet_obj['user']['statuses_count'] result_dict['textRaw'] = text result_dict['textNorm'] = text_norm result_dict['textTokens'] = text_tokens result_dict['textTokenCounts'] = token_counts result_dict['drugCategory'] = parse_drug_category(text_norm) result_dict['geo'] = tweet_obj['coordinates'] result_dict['geometry'] = Point(tweet_obj['coordinates']['coordinates']) result_dict['category'] = parse_category(tweet_obj) return result_dict