def craft_and_send_email(t1, t2, config, volcano, d_Azimuth, velocity, mx_pressure, filename): from pandas import Timestamp # create the subject line subject = '{} Airwave Detection'.format(volcano['volcano']) # create the test for the message you want to send message = '{} alarm:\n'.format(config.alarm_name) message = '{}{} detection!\n\n'.format(message, volcano['volcano']) message = '{}Start: {} (UTC)\nEnd: {} (UTC)\n\n'.format( message, t1.strftime('%Y-%m-%d %H:%M'), t2.strftime('%Y-%m-%d %H:%M')) t1_local = Timestamp(t1.datetime, tz='UTC') t2_local = Timestamp(t2.datetime, tz='UTC') t1_local = t1_local.tz_convert('US/Alaska') t2_local = t2_local.tz_convert('US/Alaska') message = '{}Start: {} ({})'.format(message, t1_local.strftime('%Y-%m-%d %H:%M'), t1_local.tzname()) message = '{}\nEnd: {} ({})\n\n'.format( message, t2_local.strftime('%Y-%m-%d %H:%M'), t2_local.tzname()) message = '{}d_Azimuth: {:+.1f} degrees\n'.format(message, d_Azimuth) message = '{}Velocity: {:.0f} m/s\n'.format(message, velocity * 1000) message = '{}Max Pressure: {:.1f} Pa'.format(message, mx_pressure) utils.send_alert(config.alarm_name, subject, message, filename) utils.post_mattermost(subject, message, config.alarm_name, filename) # delete the file you just sent if filename: remove(filename)
class TimestampOps(object): params = [None, 'US/Eastern', pytz.UTC, dateutil.tz.tzutc()] param_names = ['tz'] def setup(self, tz): self.ts = Timestamp('2017-08-25 08:16:14', tz=tz) def time_replace_tz(self, tz): self.ts.replace(tzinfo=pytz.timezone('US/Eastern')) def time_replace_None(self, tz): self.ts.replace(tzinfo=None) def time_to_pydatetime(self, tz): self.ts.to_pydatetime() def time_normalize(self, tz): self.ts.normalize() def time_tz_convert(self, tz): if self.ts.tz is not None: self.ts.tz_convert(tz) def time_tz_localize(self, tz): if self.ts.tz is None: self.ts.tz_localize(tz)
class TimestampOps: params = [None, "US/Eastern", pytz.UTC, dateutil.tz.tzutc()] param_names = ["tz"] def setup(self, tz): self.ts = Timestamp("2017-08-25 08:16:14", tz=tz) def time_replace_tz(self, tz): self.ts.replace(tzinfo=pytz.timezone("US/Eastern")) def time_replace_None(self, tz): self.ts.replace(tzinfo=None) def time_to_pydatetime(self, tz): self.ts.to_pydatetime() def time_normalize(self, tz): self.ts.normalize() def time_tz_convert(self, tz): if self.ts.tz is not None: self.ts.tz_convert(tz) def time_tz_localize(self, tz): if self.ts.tz is None: self.ts.tz_localize(tz) def time_to_julian_date(self, tz): self.ts.to_julian_date() def time_floor(self, tz): self.ts.floor("5T") def time_ceil(self, tz): self.ts.ceil("5T")
class TimestampOps(object): params = [None, 'US/Eastern', pytz.UTC, dateutil.tz.tzutc()] param_names = ['tz'] def setup(self, tz): self.ts = Timestamp('2017-08-25 08:16:14', tz=tz) def time_replace_tz(self, tz): self.ts.replace(tzinfo=pytz.timezone('US/Eastern')) def time_replace_None(self, tz): self.ts.replace(tzinfo=None) def time_to_pydatetime(self, tz): self.ts.to_pydatetime() def time_normalize(self, tz): self.ts.normalize() def time_tz_convert(self, tz): if self.ts.tz is not None: self.ts.tz_convert(tz) def time_tz_localize(self, tz): if self.ts.tz is None: self.ts.tz_localize(tz) def time_to_julian_date(self, tz): self.ts.to_julian_date() def time_floor(self, tz): self.ts.floor('5T') def time_ceil(self, tz): self.ts.ceil('5T')
def test_tz_convert_utc_with_system_utc(self): # from system utc to real utc ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) # from system utc to real utc ts = Timestamp("2001-01-05 11:56", tz=timezones.maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc())
def test_tz_convert_utc_with_system_utc(self): from pandas._libs.tslibs.timezones import maybe_get_tz # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc()) # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) # check that the time hasn't changed. assert ts == ts.tz_convert(dateutil.tz.tzutc())
def test_utc_with_system_utc(self): if sys.platform == "win32": raise nose.SkipTest("Skipped on win32 due to dateutil bug.") from pandas.tslib import maybe_get_tz # from system utc to real utc ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc())) # from system utc to real utc ts = Timestamp("2001-01-05 11:56", tz=maybe_get_tz("dateutil/UTC")) # check that the time hasn't changed. self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))
def test_utc_with_system_utc(self): # Skipped on win32 due to dateutil bug tm._skip_if_windows() from pandas.tslib import maybe_get_tz # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) # check that the time hasn't changed. self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc())) # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) # check that the time hasn't changed. self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))
def test_utc_with_system_utc(self): if sys.platform == 'win32': raise nose.SkipTest('Skipped on win32 due to dateutil bug.') from pandas.tslib import maybe_get_tz # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) # check that the time hasn't changed. self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc())) # from system utc to real utc ts = Timestamp('2001-01-05 11:56', tz=maybe_get_tz('dateutil/UTC')) # check that the time hasn't changed. self.assertEqual(ts, ts.tz_convert(dateutil.tz.tzutc()))
def test_tz_conversion_freq(self, tz_naive_fixture): # GH25241 with tm.assert_produces_warning(FutureWarning, match="freq"): t1 = Timestamp("2019-01-01 10:00", freq="H") assert t1.tz_localize(tz=tz_naive_fixture).freq == t1.freq with tm.assert_produces_warning(FutureWarning, match="freq"): t2 = Timestamp("2019-01-02 12:00", tz="UTC", freq="T") assert t2.tz_convert(tz="UTC").freq == t2.freq
def test_astimezone(self, tzstr): # astimezone is an alias for tz_convert, so keep it with # the tz_convert tests utcdate = Timestamp('3/11/2012 22:00', tz='UTC') expected = utcdate.tz_convert(tzstr) result = utcdate.astimezone(tzstr) assert expected == result assert isinstance(result, Timestamp)
def test_tz_convert_roundtrip(self, stamp, tz): ts = Timestamp(stamp, tz='UTC') converted = ts.tz_convert(tz) reset = converted.tz_convert(None) assert reset == Timestamp(stamp) assert reset.tzinfo is None assert reset == converted.tz_convert('UTC').tz_localize(None)
def test_astimezone(self, tzstr): # astimezone is an alias for tz_convert, so keep it with # the tz_convert tests utcdate = Timestamp("3/11/2012 22:00", tz="UTC") expected = utcdate.tz_convert(tzstr) result = utcdate.astimezone(tzstr) assert expected == result assert isinstance(result, Timestamp)
def __init__(self, event_type: EventType, sub_type, visible_time: Timestamp, data: object): if not visible_time.tz: raise RuntimeError("事件的可见时间必须有时区") # 统一使用中国标准时间 visible_time = visible_time.tz_convert("Asia/Shanghai") self.visible_time = visible_time self.event_type = event_type self.sub_type = sub_type self.data = data
def test_tz_convert_roundtrip(self, stamp, tz_aware_fixture): tz = tz_aware_fixture ts = Timestamp(stamp, tz="UTC") converted = ts.tz_convert(tz) reset = converted.tz_convert(None) assert reset == Timestamp(stamp) assert reset.tzinfo is None assert reset == converted.tz_convert("UTC").tz_localize(None)
def craft_and_send_email(t1, t2, stations, rms, lvlv, alarm_name, filename): from pandas import Timestamp # create the subject line subject = '--- {} ---'.format(alarm_name) # create the text for the message you want to send message = 'Start: {} (UTC)\nEnd: {} (UTC)\n\n'.format( t1.strftime('%Y-%m-%d %H:%M'), t2.strftime('%Y-%m-%d %H:%M')) t1_local = Timestamp(t1.datetime, tz='UTC') t2_local = Timestamp(t2.datetime, tz='UTC') t1_local = t1_local.tz_convert('US/Alaska') t2_local = t2_local.tz_convert('US/Alaska') message = '{}Start: {} ({})'.format(message, t1_local.strftime('%Y-%m-%d %H:%M'), t1_local.tzname()) message = '{}\nEnd: {} ({})\n\n'.format( message, t2_local.strftime('%Y-%m-%d %H:%M'), t2_local.tzname()) a = np.array([''] * len(rms[:-1])) a[np.where(rms > lvlv)] = '*' sta_message = ''.join( '{}{}: {:.0f}/{}\n'.format(sta, a[i], rms[i], lvlv[i]) for i, sta in enumerate(stations[:-1])) sta_message = ''.join([ sta_message, '\nArrestor: {} {:.0f}/{}'.format(stations[-1], rms[-1], lvlv[-1]) ]) message = ''.join([message, sta_message]) utils.send_alert(alarm_name, subject, message, filename) # utils.post_mattermost(subject,message,filename) utils.post_mattermost(subject, message, alarm_name, filename) # delete the file you just sent if filename: remove(filename)
def history_data(self, codes: List[str], start: Timestamp = None, end: Timestamp = Timestamp.now(tz='Asia/Shanghai'), count=100) -> DataFrame: """ 获取时序数据,优先按照开始和结束时间查询。 其次按照结束时间和数量进行查询 这里的所有时间都是针对visible_time :param count: :param codes: :param start: :param end: :return: DataFrame, 索引为visible_time + code """ if len(codes) <= 0: raise RuntimeError("code不能为空") if start: start = start.tz_convert("Asia/Shanghai") end = end.tz_convert("Asia/Shanghai") pattern = '%Y-%m-%d %H:%M:%S' if start and end: params = { "providerName": self.data_provider_name, "tsTypeName": self.ts_type_name, "codes": ",".join(codes), "startTime": datetime.strftime(start, pattern), "endTime": datetime.strftime(end, pattern) } else: params = { "providerName": self.data_provider_name, "tsTypeName": self.ts_type_name, "codes": ",".join(codes), "endTime": datetime.strftime(end, pattern), "count": count } url = "http://localhost:32900/queryData" return self._get_ts_data(url, params)
def test_timestamp(self): # GH#17329 # tz-naive --> treat it as if it were UTC for purposes of timestamp() ts = Timestamp.now() uts = ts.replace(tzinfo=utc) assert ts.timestamp() == uts.timestamp() tsc = Timestamp('2014-10-11 11:00:01.12345678', tz='US/Central') utsc = tsc.tz_convert('UTC') # utsc is a different representation of the same time assert tsc.timestamp() == utsc.timestamp() # datetime.timestamp() converts in the local timezone with tm.set_timezone('UTC'): # should agree with datetime.timestamp method dt = ts.to_pydatetime() assert dt.timestamp() == ts.timestamp()
def test_timestamp(self): # GH#17329 # tz-naive --> treat it as if it were UTC for purposes of timestamp() ts = Timestamp.now() uts = ts.replace(tzinfo=utc) assert ts.timestamp() == uts.timestamp() tsc = Timestamp("2014-10-11 11:00:01.12345678", tz="US/Central") utsc = tsc.tz_convert("UTC") # utsc is a different representation of the same time assert tsc.timestamp() == utsc.timestamp() # datetime.timestamp() converts in the local timezone with tm.set_timezone("UTC"): # should agree with datetime.timestamp method dt = ts.to_pydatetime() assert dt.timestamp() == ts.timestamp()
def timestamp_tzaware(timestamp): """ Returns the pandas Timestamp passed as a timezone (UTC) aware Timestamp. Args: timestamp (pandas.Timestamp): Timezone naive Timestamp Returns: pandas.Timestamp: Timezone aware """ from pandas import Timestamp if not isinstance(timestamp, Timestamp): timestamp = Timestamp(timestamp) if timestamp.tzinfo is None: return timestamp.tz_localize(tz="UTC") else: return timestamp.tz_convert(tz="UTC")
def create_aligned_timestamp(time): """ Align the passed datetime / Timestamp object to the minute interval for use in dateranges and overlap checks. Args: time (str, pandas.Timestamp) Returns: pandas.Timestamp: Timestamp aligned to minute with UTC timezone """ from pandas import Timedelta, Timestamp if not isinstance(time, Timestamp): time = Timestamp(ts_input=time) if time.tzinfo is None: t = time.tz_localize(tz="UTC") else: t = time.tz_convert(tz="UTC") t -= Timedelta(f"{t.second} s") return t
def test_tz_conversion_freq(self, tz_naive_fixture): # GH25241 t1 = Timestamp("2019-01-01 10:00", freq="H") assert t1.tz_localize(tz=tz_naive_fixture).freq == t1.freq t2 = Timestamp("2019-01-02 12:00", tz="UTC", freq="T") assert t2.tz_convert(tz="UTC").freq == t2.freq
def test_tz_conversion_freq(self, tz_naive_fixture): # GH25241 t1 = Timestamp('2019-01-01 10:00', freq='H') assert t1.tz_localize(tz=tz_naive_fixture).freq == t1.freq t2 = Timestamp('2019-01-02 12:00', tz='UTC', freq='T') assert t2.tz_convert(tz='UTC').freq == t2.freq
def format_timestamp(ts: pd.Timestamp, fmt="%m-%d %H:%M:%S", tz="Asia/Jerusalem"): return pd.to_datetime(ts.tz_convert(tz)).strftime(fmt)
def test_astimezone(self): utc = Timestamp("3/11/2012 22:00", tz="UTC") expected = utc.tz_convert(self.tzstr("US/Eastern")) result = utc.astimezone(self.tzstr("US/Eastern")) self.assertEqual(expected, result) tm.assert_isinstance(result, Timestamp)
def __init__(self, code: str, start: Timestamp, end: Timestamp): self.code = code self.start = start.tz_convert("Asia/Shanghai") self.end = end.tz_convert("Asia/Shanghai")
def test_astimezone(self): utc = Timestamp('3/11/2012 22:00', tz='UTC') expected = utc.tz_convert('US/Eastern') result = utc.astimezone('US/Eastern') self.assertEquals(expected, result) self.assert_(isinstance(result, Timestamp))
def timestamp_to_epoch(timestamp: pd.Timestamp) -> int: origin = pd.Timestamp("1970-01-01") if timestamp.tzinfo is not None: timestamp = timestamp.tz_convert("UTC").tz_localize(None) return (timestamp - origin) // pd.Timedelta("1s") # type: ignore
def test_astimezone(self): utc = Timestamp('3/11/2012 22:00', tz='UTC') expected = utc.tz_convert(self.tzstr('US/Eastern')) result = utc.astimezone(self.tzstr('US/Eastern')) self.assertEqual(expected, result) tm.assertIsInstance(result, Timestamp)
def _convert_utc(timestamp: pd.Timestamp) -> pd.Timestamp: """Return the given timestamp with the UTC time zone.""" if timestamp.tz: return timestamp.tz_convert(tz.tzutc()) return timestamp.tz_localize(tz.tzutc())