def test_to_UNIXtime(self): unix = 1378459200 iso = "2013-09-06 09:20:00+00:00" date = datetime(2013, 9, 6, 9, 20, 0, 0, timezone.utc) self.assertEqual(unix, formatting.to_UNIXtime(unix)) self.assertEqual(unix, formatting.to_UNIXtime(iso)) self.assertEqual(unix, formatting.to_UNIXtime(date))
def get_forecast(city, lat, lon, now): if new_day == True: day = datetime.datetime.now().day + 1 else: day = datetime.datetime.now().day year = datetime.datetime.now().year month = datetime.datetime.now().month x = 0 n = [] n1 = [] while x <= now: n.append(x) x += 3 for i in n: try: tchn = formatting.to_UNIXtime(datetime.datetime(year, month, day, i, 0, tzinfo=spb_tzinfo)) n1.append(mgr.one_call_history(lat=lat, lon=lon, dt=tchn).current.temperature("celsius")["temp"]) except Exception: pass three_h_forecaster = mgr.forecast_at_place(city, '3h') while 22 > x > now: n.append(x) n1.append(three_h_forecaster.get_weather_at(datetime.datetime(year, month, day, x, 0, tzinfo=spb_tzinfo)).temperature("celsius")["temp"]) x += 3 return n1
def get_alerts_since(self, timestamp): """ Returns all the `Alert` objects of this `Trigger` that were fired since the specified timestamp. :param timestamp: time object representing the point in time since when alerts have to be fetched :type timestamp: int, ``datetime.datetime`` or ISO8601-formatted string :return: list of `Alert` instances """ unix_timestamp = formatting.to_UNIXtime(timestamp) return [ alert for alert in self.alerts if alert.last_update >= unix_timestamp ]
def get_weather_at(self, timeobject): """ Gives the *Weather* item in the forecast that is closest in time to the time value conveyed by the parameter :param timeobject: may be a UNIX time, a ``datetime.datetime`` object or an ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` :type timeobject: long/int, ``datetime.datetime`` or str) :returns: a *Weather* object """ return weather. \ find_closest_weather(self.forecast.weathers, formatting.to_UNIXtime(timeobject))
def _will_be(self, timeobject, weather_condition): """ Tells if at the specified weather condition will occur at the specified time. The check is performed on the *Weather* item of the forecast which is closest to the time value conveyed by the parameter :param timeobject: may be a UNIX time, a ``datetime.datetime`` object or an ISO8601-formatted string in the format ``YYYY-MM-DD HH:MM:SS+00`` :type timeobject: long/int, ``datetime.datetime`` or str) :param weather_condition: the weather condition to be looked up :type weather_condition: str :returns: boolean """ time_value = formatting.to_UNIXtime(timeobject) closest_weather = weather.find_closest_weather(self.forecast.weathers, time_value) return weather.status_is(closest_weather, weather_condition, self._wc_registry)
tzinfo = datetime.datetime.now(datetime.timezone(datetime.timedelta(0))).astimezone().tzinfo spb_tzinfo = datetime.timezone(datetime.timedelta(seconds=10800)) heroku_tzinfo = datetime.timezone(datetime.timedelta(seconds=0)) # now = datetime.datetime.now().hour # now_bruh = now/3 new_day = False print(tzinfo) if tzinfo == datetime.timezone(datetime.timedelta(seconds=10800), 'Russia TZ 2 Standard Time'): print("Time zone: UTC+3(Flask local server)") now = datetime.datetime.now().hour now_bruh = int(now/3)*3 heroku_time_now = now_bruh-3 heroku_time_now = int(heroku_time_now/3)*3 now_unix = formatting.to_UNIXtime(datetime.datetime(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day, int(now_bruh), 0, tzinfo=spb_tzinfo)) else: print("Time zone: UTC(Heroku global server)") now = datetime.datetime.now().hour now_bruh = int(now/3)*3 spb_time_now = now_bruh+3 if spb_time_now > 23: spb_time_now = spb_time_now - 24 new_day = True spb_time_now = int(spb_time_now/3)*3 now_unix = formatting.to_UNIXtime(datetime.datetime(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day, int(now_bruh)-1, 0, tzinfo=heroku_tzinfo)) now = spb_time_now # spb_weather_now = mgr.one_call_history(lat=59.9386, lon=30.3141, dt=now_unix).current.temperature("celsius")["temp"] # if isinstance(now_bruh, float): # now = int(now_bruh)*3
def test_next_week(self): result = timestamps.next_week() expected = datetime.now() + timedelta(days=7) self.assertAlmostEqual(float(formatting.to_UNIXtime(expected)), float(formatting.to_UNIXtime(result)))
def test_last_three_hours(self): result = timestamps.last_three_hours() expected = datetime.now() + timedelta(hours=-3) self.assertAlmostEqual(float(formatting.to_UNIXtime(expected)), float(formatting.to_UNIXtime(result)))
def test_last_year(self): result = timestamps.last_year() expected = datetime.now() + timedelta(days=-365) self.assertAlmostEqual(float(formatting.to_UNIXtime(expected)), float(formatting.to_UNIXtime(result)))
def test_next_hour(self): result = timestamps.next_hour() expected = datetime.now(timezone.utc) + timedelta(hours=1) self.assertAlmostEqual(float(formatting.to_UNIXtime(expected)), float(formatting.to_UNIXtime(result))) self.is_timezone_aware(result)
def test_last_month(self): result = timestamps.last_month() expected = datetime.now(timezone.utc) + timedelta(days=-30) self.assertAlmostEqual(float(formatting.to_UNIXtime(expected)), float(formatting.to_UNIXtime(result))) self.is_timezone_aware(result)
def create_trigger(self, start, end, conditions, area, alert_channels=None): """ Create a new trigger on the Alert API with the given parameters :param start: time object representing the time when the trigger begins to be checked :type start: int, ``datetime.datetime`` or ISO8601-formatted string :param end: time object representing the time when the trigger ends to be checked :type end: int, ``datetime.datetime`` or ISO8601-formatted string :param conditions: the `Condition` objects representing the set of checks to be done on weather variables :type conditions: list of `pyowm.utils.alertapi30.Condition` instances :param area: the geographic are over which conditions are checked: it can be composed by multiple geoJSON types :type area: list of geoJSON types :param alert_channels: the alert channels through which alerts originating from this `Trigger` can be consumed. Defaults to OWM API polling :type alert_channels: list of `pyowm.utils.alertapi30.AlertChannel` instances :returns: a *Trigger* instance :raises: *ValueError* when start or end epochs are `None` or when end precedes start or when conditions or area are empty collections """ assert start is not None assert end is not None # prepare time period unix_start = formatting.to_UNIXtime(start) unix_end = formatting.to_UNIXtime(end) unix_current = timestamps.now(timeformat='unix') if unix_start >= unix_end: raise ValueError( "The start timestamp must precede the end timestamp") delta_millis_start = timestamps.millis_offset_between_epochs( unix_current, unix_start) delta_millis_end = timestamps.millis_offset_between_epochs( unix_current, unix_end) the_time_period = { "start": { "expression": "after", "amount": delta_millis_start }, "end": { "expression": "after", "amount": delta_millis_end } } assert conditions is not None if len(conditions) == 0: raise ValueError( 'A trigger must contain at least one condition: you provided none' ) the_conditions = [ dict(name=c.weather_param, expression=c.operator, amount=c.amount) for c in conditions ] assert area is not None if len(area) == 0: raise ValueError( 'The area for a trigger must contain at least one geoJSON type: you provided none' ) the_area = [a.to_dict() for a in area] # >>> for the moment, no specific handling for alert channels status, payload = self.http_client.post( TRIGGERS_URI, params={'appid': self.API_key}, data=dict(time_period=the_time_period, conditions=the_conditions, area=the_area), headers={'Content-Type': 'application/json'}) return Trigger.from_dict(payload)