def _maybe_get_tz(tz, date=None): if isinstance(tz, compat.string_types): import pytz tz = pytz.timezone(tz) if com.is_integer(tz): import pytz tz = pytz.FixedOffset(tz / 60) # localize and get the tz if date is not None and tz is not None: if date.tzinfo is not None and hasattr(tz, 'localize'): tz = tz.localize(date.replace(tzinfo=None)).tzinfo return tz
def _register_param(self): self.datetime = datetime.datetime.now().replace( tzinfo=pytz.FixedOffset(480)) self.time = time.time() self.loop_time = self.io_loop.time() for fun_name, args in self.module_arg_dict.items(): if 'crontab' in args: key = args['crontab'] self.crontab_router[key]['func'] = getattr(self, fun_name) self.crontab_router[key]['iter'] = croniter( args['crontab'], self.datetime) self.crontab_router[key]['handle'] = None elif 'channel' in args: self.channel_router[args['channel']] = getattr(self, fun_name)
def parse_datetime(x): """ Parses datetime with timezone formatted as: `[day/month/year:hour:minute:second zone]` Example: `>>> parse_datetime('13/Nov/2015:11:45:42 +0000')` `datetime.datetime(2015, 11, 3, 11, 45, 4, tzinfo=<UTC>)` Due to problems parsing the timezone (`%z`) with `datetime.strptime`, the timezone will be obtained using the `pytz` library. """ if x == "-" or x == "" or x is None: return None dt = datetime.strptime(x[1:-7], '%d/%b/%Y:%H:%M:%S') dt_tz = int(x[-6:-3]) * 60 + int(x[-3:-1]) return dt.replace(tzinfo=pytz.FixedOffset(dt_tz))
def convert_ms(ms, offset=0, format="%B %d, %Y %I:%M%p"): sec = ms / 1000.0 if os.name == "nt": # Windows has a "minimum allowed" timestamp: https://stackoverflow.com/a/45372194 sec = max(sec, 86400) timestamp = dt.fromtimestamp(sec, tz=pytz.UTC) if offset: offset_in_min = offset // 1000 // 60 timestamp = timestamp.astimezone(pytz.FixedOffset(offset_in_min)) return timestamp.strftime(format)
def PythonChangeTimeZone(UTCTime, offset): """Changes the time zone of a Python DateTime object to a particular offset""" # If the offset is a number, adds or subtracts number of minutes from UTC. For example, PST = pytz.FixedOffset(-8*60) for -8 hours if (type(offset) == type(-8)) or (type(offset) == type(-8.5)): newZone = pytz.FixedOffset(offset * 60) # If the offset is a string representation of a time zone (eg: 'America/Vancouver'), then use the timezone function else: #if type(offset) == type('string'): newZone = pytz.timezone(offset) # Create new time in the desired timezone newDateTime = UTCTime.astimezone(newZone) return newDateTime
def get_tz_from_name(tz_or_name): if tz_or_name is None: tz = pytz.utc elif not isinstance(tz_or_name, str): tz = tz_or_name elif tz_or_name.upper() == 'SYSTEM': tz = get_localzone() elif ':' in tz_or_name: fields = tz_or_name.split(':') offset = int(fields[0]) * 60 tz = pytz.FixedOffset(offset) else: tz = pytz.timezone(tz_or_name) return tz
def test_different_tz(self, timezone): alter_tz = pytz.FixedOffset(-600) trigger = CronTrigger(year=2009, week=15, day_of_week=2, timezone=timezone) assert repr(trigger) == ( "<CronTrigger (year='2009', week='15', day_of_week='2', " "timezone='Europe/Berlin')>") assert str(trigger) == "cron[year='2009', week='15', day_of_week='2']" start_date = alter_tz.localize(datetime(2008, 12, 31, 22)) correct_next_date = timezone.localize(datetime(2009, 4, 8)) assert trigger.get_next_fire_time(None, start_date) == correct_next_date
def parse_gmt_based_offset(match): # GMT+2 or GMT+2:30 case sign, hours, minutes = match.groups() sign = -1 if sign == "-" else 1 try: hours = int(hours) minutes = int(minutes) if minutes else 0 except ValueError: return None if 0 <= hours < 24 and 0 <= minutes < 60: offset = sign * (hours * 60 + minutes) return pytz.FixedOffset(offset) return None
def remind(context: CallbackContext): r: Reminder = context.job.context[1] m: RemindersManager = context.job.context[0] r.update_near_ts() msg = "📆 {}".format(r.name) if r.near_ts != 0: msg += " next: {}".format( datetime.datetime.fromtimestamp( r.near_ts, tz=pytz.FixedOffset(r.utc * 60)).strftime("%H:%M:%S %m.%d.%Y")) context.bot.send_message(r.chat_id, text=msg) if r.near_ts != 0 and datetime.datetime.now().replace( hour=23, minute=59, second=59).timestamp() > r.near_ts: context.job_queue.run_once( remind, 1 + r.near_ts - datetime.datetime.now(tz=pytz.FixedOffset(r.utc * 60)).timestamp(), context=r, name=r.id()) m.update_reminder(r)
class TestSSAProfile(TableTestBase): """ Test that all profiles from an SSA file are uploaded correctly """ args = ['SSA.csv'] kwargs = {'timezone': 'MST'} UploaderClass = UploadProfileData TableClass = LayerData dt = datetime.datetime(2020, 2, 5, 13, 40, 0, 0, pytz.FixedOffset(-360)) params = { 'test_count': [dict(data_name='reflectance', expected_count=16)], # Test a value from each profile to check that the profile is there and it has integrity 'test_value': [ dict(data_name='reflectance', attribute_to_check='value', filter_attribute='depth', filter_value=10, expected=22.12), dict(data_name='specific_surface_area', attribute_to_check='value', filter_attribute='depth', filter_value=35, expected=11.2), dict(data_name='equivalent_diameter', attribute_to_check='value', filter_attribute='depth', filter_value=80, expected=0.1054), dict(data_name='sample_signal', attribute_to_check='value', filter_attribute='depth', filter_value=10, expected=186.9), dict(data_name='reflectance', attribute_to_check='comments', filter_attribute='depth', filter_value=5, expected='brush'), ], 'test_unique_count': [ # Confirm we only have 1 comment and everything else is none dict(data_name='reflectance', attribute_to_count='comments', expected_count=2), ] }
def testUnknownTZDt(self): tz = pytz.FixedOffset(540) tz.zone = "Japan/Edo" mo = timezone.make_aware(dt.datetime(2013, 4, 25, 6, 0), tz) v = vDt(mo) self.assertTrue(v) self.assertEqual(v, mo) self.assertEqual(v, vDatetime(mo)) self.assertEqual(v.date(), mo.date()) self.assertEqual(v.time(), mo.time()) self.assertEqual(v.datetime(), mo) self.assertEqual(v.tzinfo(), mo.tzinfo) self.assertEqual(v.zone(), "Japan/Edo") with self.assertRaises(CalendarTypeError): v.timezone()
def test_initialize_without_datetime_with_dateutil_timezone(self, mock_datetime_timezone): tz = tzoffset(None, -22500) dt = datetime(2015, 1, 1, tzinfo=tz) utcoffset = tz.utcoffset(None) total_seconds = ( (utcoffset.microseconds + (utcoffset.seconds + utcoffset.days * 24 * 3600) * 10 ** 6) / 10 ** 6) tz = pytz.FixedOffset(total_seconds / 60) dt = tz.normalize(dt) mock_datetime_timezone.return_value = dt do = delorean.Delorean(timezone=tz) self.assertEqual(do.datetime, dt) self.assertEqual(do.timezone, tz)
def tzinfos(abbr, offset): if abbr: if abbr in abbrs: result = abbrs[abbr] if offset: # FIXME: Check the offset matches the abbreviation we just selected. pass return result else: raise ValueError, "Unknown timezone found %s" % abbr if offset == 0: return pytz.utc if offset: return pytz.FixedOffset(offset / 60) return unknown
def test_pythonvalue(self): instance = builtins.gYearMonth() assert instance.pythonvalue("2001-10") == (2001, 10, None) assert instance.pythonvalue("2001-10+02:00") == ( 2001, 10, pytz.FixedOffset(120), ) assert instance.pythonvalue("2001-10Z") == (2001, 10, pytz.utc) assert instance.pythonvalue("2001-10+00:00") == (2001, 10, pytz.utc) assert instance.pythonvalue("-2001-10") == (-2001, 10, None) assert instance.pythonvalue("-20001-10") == (-20001, 10, None) with pytest.raises(builtins.ParseError): assert instance.pythonvalue("10-10")
def test_parse_timezone(self): # gh-22256 data = """dt,val 2018-01-04 09:01:00+09:00,23350 2018-01-04 09:02:00+09:00,23400 2018-01-04 09:03:00+09:00,23400 2018-01-04 09:04:00+09:00,23400 2018-01-04 09:05:00+09:00,23400""" parsed = self.read_csv(StringIO(data), parse_dates=['dt']) dti = pd.DatetimeIndex(start='2018-01-04 09:01:00', end='2018-01-04 09:05:00', freq='1min', tz=pytz.FixedOffset(540)) expected_data = {'dt': dti, 'val': [23350, 23400, 23400, 23400, 23400]} expected = DataFrame(expected_data) tm.assert_frame_equal(parsed, expected)
def _parse_timezone(val): """Return a pytz.tzinfo object""" if not val: return if val == 'Z' or val == '+00:00': return pytz.utc negative = val.startswith('-') minutes = int(val[-2:]) minutes += int(val[1:3]) * 60 if negative: minutes = 0 - minutes return pytz.FixedOffset(minutes)
def test_publish_with_countdown_and_timezone(self): # use timezone with fixed offset to be sure it won't be changed self.app.conf.CELERY_TIMEZONE = pytz.FixedOffset(120) prod = self.app.amqp.TaskProducer(Mock()) prod.channel.connection.client.declared_entities = set() prod.publish = Mock() now = datetime.datetime(2013, 11, 26, 16, 48, 46) prod.publish_task('tasks.add', (2, 2), {}, retry=False, countdown=20, now=now) self.assertEqual( prod.publish.call_args[0][0]['eta'], '2013-11-26T18:49:06+02:00', )
def _parse_datetime(timestamp): ''' Parses datetime with timezone formatted as: `day/month/year:hour:minute:second zone` Example: `>>> parse_datetime('13/Nov/2015:11:45:42 +0000')` `datetime.datetime(2015, 11, 3, 11, 45, 4, tzinfo=<UTC>)` Due to problems parsing the timezone (`%z`) with `datetime.strptime`, the timezone will be obtained using the `pytz` library. ''' dt = datetime.strptime(timestamp[0:-6], '%d/%b/%Y:%H:%M:%S') dt_tz = int(timestamp[-5:-2]) * 60 + int(timestamp[-2:]) return dt.replace(tzinfo=pytz.FixedOffset(dt_tz))
def test_timestamp2filename_tz(): # one hour west of UTC (i.e. an hour earlier) tz = pytz.FixedOffset(-60) dt = datetime.datetime( 2010, 6, 15, 7, 30, 10, 1234, tz, ) filename = _timestamp2filename(dt) assert filename == "20100615083010001234"
def test_query_cond_less_than(self): # Make sure type checking works q1 = pysnow.QueryBuilder().field('test') self.assertRaises(QueryTypeError, q1.less_than, 'a') # Make sure a valid operation works q2 = pysnow.QueryBuilder().field('test').less_than(1) self.assertEqual(str(q2), 'test<1') # Make sure naive dates are assumed as UTC q3 = pysnow.QueryBuilder().field('test').less_than(dt(2016, 2, 1)) self.assertEqual(str(q3), 'test<2016-02-01 00:00:00') # Make sure tz-aware dates are converted to UTC (UTC+1) q3 = pysnow.QueryBuilder().field('test').less_than(dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60))) self.assertEqual(str(q3), 'test<2016-02-01 02:00:00')
def test_filter_by_dt_with_fixed_timezone_negative_offset(self, query): """Test filter by dt with fixed timezone negative offset. utcoffset = timedelta tzname = None zone = None """ fixed_offset_timezone = pytz.FixedOffset(-60 * 3) value = datetime(2000, 1, 2, 3, 4, 5, tzinfo=fixed_offset_timezone) assert str(value) == '2000-01-02 03:04:05-03:00' query.filter_by(Filter('field', FilterCondition.equals, value)) assert 'filter.field.equals' in query.request_args.data assert query.request_args.data['filter.field.equals'] == str( value.replace(tzinfo=None, ), ) assert 'filter.tz' in query.request_args.data assert query.request_args.data['filter.tz'] == '-03:00'
def test_query_cond_greater_than(self): # Make sure type checking works q1 = pysnow.QueryBuilder().field("test") self.assertRaises(QueryTypeError, q1.greater_than, "a") # Make sure a valid operation works q2 = pysnow.QueryBuilder().field("test").greater_than(1) self.assertEqual(str(q2), "test>1") # Make sure naive dates are assumed as UTC q3 = pysnow.QueryBuilder().field("test").greater_than(dt(2016, 2, 1)) self.assertEqual(str(q3), "test>2016-02-01 00:00:00") # Make sure tz-aware dates are converted to UTC (UTC+1) q4 = (pysnow.QueryBuilder().field("test").greater_than( dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60)))) self.assertEqual(str(q4), "test>2016-02-01 02:00:00")
def handle(self, *args, **options): user = User.objects.get(username='******') known_baros = {} tz = pytz.FixedOffset(60) #Nederlandse wintertijd for well in Well.objects.all(): #for well in Well.objects.exclude(name=F('nitg')): for screen in well.screen_set.all(): print screen name = '%s COMP' % screen series, created = Series.objects.get_or_create( name=name, defaults={'user': user}) try: series.mlocatie = MeetLocatie.objects.get( name=unicode(screen)) series.save() except: pass util.recomp(screen, series, known_baros, tz) #maak/update grafiek chart, created = Chart.objects.get_or_create( name=unicode(screen), defaults={ 'title': unicode(screen), 'user': user, 'percount': 0, # 'start':datetime.datetime(2013,1,1), # 'stop': datetime.datetime(2015,12,31), }) chart.series.get_or_create(series=series, defaults={'label': 'm tov NAP'}) # handpeilingen toevoegen (als beschikbaar) try: mloc = MeetLocatie.objects.get(name=unicode(screen)) for hand in mloc.manualseries_set.all(): chart.series.get_or_create(series=hand, defaults={ 'type': 'scatter', 'order': 2 }) except: pass util.chart_for_screen(screen) util.chart_for_well(well)
def standardize_time_across_timezones(self, df): tz = pytz.timezone(cfg.getParam('dispatch_outputs_timezone')) offset = (tz.utcoffset(DT.datetime(2015, 1, 1)) + tz.dst(DT.datetime(2015, 1, 1))).total_seconds() / 60. new_index = pd.DatetimeIndex(self.active_dates_index_unique, tz=pytz.FixedOffset(offset)) # if we have hydro year, when this does a reindex, it can introduce NaNs, so we want to remove them after assert not df.isnull().any().any() standardize_df = util.reindex_df_level_with_new_elements( df.copy(), 'weather_datetime', new_index) levels = [n for n in df.index.names if n != 'weather_datetime'] standardize_df = standardize_df.groupby(level=levels).fillna( method='bfill').fillna(method='ffill') standardize_df = standardize_df[~standardize_df.isnull().values] return standardize_df
class TestUploadSMPBatch(TableTestBase): ''' Test whether we can assign meta info from an smp log to 2 profiles ''' args = [['S19M1013_5S21_20200201.CSV', 'S06M0874_2N12_20200131.CSV']] kwargs = {'db_name': 'test', 'in_timezone': 'UTC', 'smp_log_f': 'smp_log.csv', 'units': 'Newtons'} UploaderClass = UploadProfileBatch TableClass = LayerData attribute = 'depth' params = { # Test that the number of entries equals the number of lines of data from both files 'test_count': [dict(data_name='force', expected_count=(242 + 154))], 'test_value': [ dict(data_name='force', attribute_to_check='site_id', filter_attribute='depth', filter_value=-100, expected='5S21'), dict(data_name='force', attribute_to_check='site_id', filter_attribute='depth', filter_value=-0.4, expected='2N12'), dict(data_name='force', attribute_to_check='comments', filter_attribute='depth', filter_value=-0.4, expected='started 1-2 cm below surface'), dict(data_name='force', attribute_to_check='time', filter_attribute='id', filter_value=1, expected=time(hour=16, minute=16, second=49, tzinfo=pytz.FixedOffset(-360))), dict(data_name='force', attribute_to_check='units', filter_attribute='depth', filter_value=-0.4, expected='Newtons'), ], 'test_unique_count': [dict(data_name='force', attribute_to_count='date', expected_count=2), dict(data_name='force', attribute_to_count='time', expected_count=2)] } @pytest.mark.parametrize('site, count', [ ('5S21', 242), ('2N12', 154) ]) def test_single_profile_count(self, site, count): ''' Ensure that each site can be filtered to its 10 points in its own profile ''' records = self.session.query(LayerData).filter(LayerData.site_id == site).all() depth = [r.depth for r in records] value = [r.value for r in records] assert len(records) == count
def parse_datetime(cls, x): ''' Parses datetime with timezone formatted as: `[day/month/year:hour:minute:second zone]` Example: `>>> parse_datetime('13/Nov/2015:11:45:42 +0000')` `datetime.datetime(2015, 11, 3, 11, 45, 4, tzinfo=<UTC>)` Due to problems parsing the timezone (`%z`) with `datetime.strptime`, the timezone will be obtained using the `pytz` library. ''' today = datetime.today() dt = datetime.strptime(f'{today.year}:{today.month}:{x[1:-1]}', '%Y:%m:%d:%H:%M:%S') x += ' +0000' dt_tz = int(x[-6:-3]) * 60 + int(x[-3:-1]) return dt.replace(tzinfo=pytz.FixedOffset(dt_tz))
def process_results(cls, column_names, rows): # Override for Snowflake. The datetime objects returned by # snowflake-connector-python are not pickleable, so we need # to replace them with sane timezones fixed = [] for row in rows: fixed_row = [] for col in row: if isinstance(col, datetime.datetime) and col.tzinfo: offset = col.utcoffset() offset_seconds = offset.total_seconds() new_timezone = pytz.FixedOffset(offset_seconds // 60) col = col.astimezone(tz=new_timezone) fixed_row.append(col) fixed.append(fixed_row) return super().process_results(column_names, fixed)
def test_query_cond_less_than(self): # Make sure type checking works q1 = Field("test") self.assertRaises(QueryTypeError, q1.lt, "a") # Make sure a valid operation works q2 = Field("test").lt(1) self.assertEqual(str(q2), "test<1") # Make sure naive dates are assumed as UTC q3 = Field("test").lt(dt(2016, 2, 1)) self.assertEqual( str(q3), 'test<javascript:gs.dateGenerate("2016-02-01 00:00:00")') # Make sure tz-aware dates are converted to UTC (UTC+1) q3 = (Field("test").lt(dt(2016, 2, 1, 3, tzinfo=pytz.FixedOffset(60)))) self.assertEqual( str(q3), 'test<javascript:gs.dateGenerate("2016-02-01 02:00:00")')
def update_daily_hotspots(): end_date = datetime.now(tz=pytz.FixedOffset(330)) start_date = end_date - timedelta(days=7) daily_tracking_db = NotionDB(daily_tracking_url) while start_date <= end_date: set_final = False if start_date >= end_date else True st = start_date.date() row = daily_tracking_db.find_one_by('Date', st) if row and not row.Final: print('Updating:', row.title, st) data = get_data(st) for k, v in data.items(): setattr(row, k, v) if set_final: row.Final = True else: print("Skipping", st) start_date += timedelta(days=1)
def test_parse_timezone(all_parsers): # see gh-22256 parser = all_parsers data = """dt,val 2018-01-04 09:01:00+09:00,23350 2018-01-04 09:02:00+09:00,23400 2018-01-04 09:03:00+09:00,23400 2018-01-04 09:04:00+09:00,23400 2018-01-04 09:05:00+09:00,23400""" result = parser.read_csv(StringIO(data), parse_dates=["dt"]) dti = pd.date_range(start="2018-01-04 09:01:00", end="2018-01-04 09:05:00", freq="1min", tz=pytz.FixedOffset(540)) expected_data = {"dt": dti, "val": [23350, 23400, 23400, 23400, 23400]} expected = DataFrame(expected_data) tm.assert_frame_equal(result, expected)