Python pytz.utc() Examples
The following are 30
code examples of pytz.utc().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pytz
, or try the search function
.
Example #1
Source File: crawl.py From oxidizr with GNU General Public License v2.0 | 7 votes |
def extract_context(html, url): soup = BeautifulSoup(html) # Insert into Content (under this domain) texts = soup.findAll(text=True) try: Content.objects.create( url=url, title=soup.title.string, summary=helpers.strip_tags(" \n".join(filter(visible, texts)))[:4000], last_crawled_at=datetime.datetime.utcnow().replace(tzinfo=pytz.utc) ) except IntegrityError: println('%s - already existed in Content' % url) soup.prettify() return [str(anchor['href']) for anchor in soup.findAll('a', attrs={'href': re.compile("^http://")}) if anchor['href']]
Example #2
Source File: getmetrics_cadvisor.py From InsightAgent with Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(PCT_z_FMT.split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except e: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example #3
Source File: attime.py From worker with GNU General Public License v3.0 | 6 votes |
def parseATTime(s, tzinfo=None): if tzinfo is None: tzinfo = pytz.utc s = s.strip().lower().replace('_', '').replace(',', '').replace(' ', '') if s.isdigit(): if len(s) == 8 and int(s[:4]) > 1900 and int( s[4:6]) < 13 and int(s[6:]) < 32: pass # Fall back because its not a timestamp, its YYYYMMDD form else: return datetime.fromtimestamp(int(s), tzinfo) elif ':' in s and len(s) == 13: return tzinfo.localize(datetime.strptime(s, '%H:%M%Y%m%d'), daylight) if '+' in s: ref, offset = s.split('+', 1) offset = '+' + offset elif '-' in s: ref, offset = s.split('-', 1) offset = '-' + offset else: ref, offset = s, '' return ( parseTimeReference(ref) + parseTimeOffset(offset)).astimezone(tzinfo)
Example #4
Source File: views.py From oxidizr with GNU General Public License v2.0 | 6 votes |
def form_valid(self, form): user = self.user user.backend = 'django.contrib.auth.backends.ModelBackend' user.is_email_verified = True user.is_active = True user.save() email_token = self.email_token email_token.verified_from_ip = get_client_ip(self.request) email_token.verified_at = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) email_token.save() login(self.request, user) messages.add_message( message=_('Thank you for verifying your Email address, you are now logged in.'), request=self.request, level=messages.SUCCESS ) return redirect(self.get_success_url())
Example #5
Source File: views.py From oxidizr with GNU General Public License v2.0 | 6 votes |
def form_valid(self, form): form_data = form.cleaned_data try: one_hour_ago = datetime.datetime.utcnow().replace(tzinfo=pytz.utc) - datetime.timedelta(hours=1) password_token = PasswordResetCode.objects.get( verification_code=form_data['verification_code'], created_at__gt=one_hour_ago ) owner = password_token.owner owner.set_password(form_data['password']) owner.save() messages.add_message( message=_('Your password has been successfully reset, you may login now.'), level=messages.SUCCESS, request=self.request, extra_tags='page-level' ) return redirect(self.get_success_url()) except PasswordResetCode.DoesNotExist: form._errors['verification_code'] =\ ErrorList([_('The verification code does not match the one we sent you in Email.')]) context = self.get_context_data(form=form) return self.render_to_response(context)
Example #6
Source File: crawl.py From oxidizr with GNU General Public License v2.0 | 6 votes |
def union(p, q): for url in p: parsed = urlparse(str(url)) if parsed.netloc and parsed.netloc != 'www.webhostingtalk.com': url = 'http://%s/' % parsed.netloc if parsed.netloc and url not in q: print url if parsed.netloc != 'www.webhostingtalk.com': # Insert into Site try: Website.objects.create( url=url, name=parsed.netloc, last_crawled_at=datetime.datetime.utcnow().replace(tzinfo=pytz.utc) ) except IntegrityError: println('%s - already existed in Site' % url) else: # We want to deep crawl webhosting talk q.append(url)
Example #7
Source File: getlogs_hadoop-mapreduce.py From InsightAgent with Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example #8
Source File: getmessages_prometheus.py From InsightAgent with Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example #9
Source File: insightagent-boilerplate.py From InsightAgent with Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except e: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example #10
Source File: getmetrics_sar.py From InsightAgent with Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example #11
Source File: signing.py From normandy with Mozilla Public License 2.0 | 6 votes |
def check_validity(not_before, not_after, expire_early): """ Check validity dates. If not_before is in the past, and not_after is in the future, return True, otherwise raise an Exception explaining the problem. If expire_early is passed, an exception will be raised if the not_after date is too soon in the future. """ now = datetime.utcnow().replace(tzinfo=pytz.utc) if not_before > not_after: raise BadCertificate(f"not_before ({not_before}) after not_after ({not_after})") if now < not_before: raise CertificateNotYetValid(not_before) if now > not_after: raise CertificateExpired(not_after) if expire_early: if now + expire_early > not_after: raise CertificateExpiringSoon(expire_early) return True
Example #12
Source File: getlogs_spark.py From InsightAgent with Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except e: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example #13
Source File: converter.py From snowflake-connector-python with Apache License 2.0 | 6 votes |
def _pre_TIMESTAMP_LTZ_to_python(self, value, ctx) -> datetime: """Converts TIMESTAMP LTZ to datetime. This takes consideration of the session parameter TIMEZONE if available. If not, tzlocal is used. """ microseconds, fraction_of_nanoseconds = _extract_timestamp(value, ctx) tzinfo_value = self._get_session_tz() try: t0 = ZERO_EPOCH + timedelta(seconds=microseconds) t = pytz.utc.localize(t0, is_dst=False).astimezone(tzinfo_value) return t, fraction_of_nanoseconds except OverflowError: logger.debug( "OverflowError in converting from epoch time to " "timestamp_ltz: %s(ms). Falling back to use struct_time." ) return time.localtime(microseconds), fraction_of_nanoseconds
Example #14
Source File: getlogs_k8s.py From InsightAgent with Apache License 2.0 | 6 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' timestamp_localize = cli_config_vars['time_zone'].localize(timestamp_datetime) epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000 return epoch
Example #15
Source File: prize.py From donation-tracker with Apache License 2.0 | 5 votes |
def start_draw_time(self): if self.startrun and self.startrun.order: if self.prev_run: return self.prev_run.endtime - datetime.timedelta( milliseconds=TimestampField.time_string_to_int( self.prev_run.setup_time ) ) return self.startrun.starttime.replace(tzinfo=pytz.utc) elif self.starttime: return self.starttime.replace(tzinfo=pytz.utc) else: return None
Example #16
Source File: arrow_context.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def _get_session_tz(self): """Get the session timezone or use the local computer's timezone.""" try: tz = 'UTC' if not self.timezone else self.timezone return pytz.timezone(tz) except pytz.exceptions.UnknownTimeZoneError: logger.warning('converting to tzinfo failed') if tzlocal is not None: return tzlocal.get_localzone() else: try: return datetime.timezone.utc except AttributeError: return pytz.timezone('UTC')
Example #17
Source File: test_unit_arrow_chunk_iterator.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def get_timezone(timezone=None): """Gets, or uses the session timezone or use the local computer's timezone.""" try: tz = 'UTC' if not timezone else timezone return pytz.timezone(tz) except pytz.exceptions.UnknownTimeZoneError: logger.warning('converting to tzinfo failed') if tzlocal is not None: return tzlocal.get_localzone() else: try: return datetime.datetime.timezone.utc except AttributeError: return pytz.timezone('UTC')
Example #18
Source File: converter.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def _derive_offset_timestamp(self, value, is_utc: bool = False): """Derives TZ offset and timestamp from the datetime objects.""" tzinfo = value.tzinfo if tzinfo is None: # If no tzinfo is attached, use local timezone. tzinfo = self._get_session_tz() if not is_utc else pytz.UTC t = pytz.utc.localize(value, is_dst=False).astimezone(tzinfo) else: # if tzinfo is attached, just covert to epoch time # as the server expects it in UTC anyway t = value offset = tzinfo.utcoffset( t.replace(tzinfo=None)).total_seconds() / 60 + 1440 return offset, t
Example #19
Source File: converter.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def _struct_time_to_snowflake(self, value): tzinfo_value = _generate_tzinfo_from_tzoffset(time.timezone // 60) t = datetime.fromtimestamp(time.mktime(value)) if pytz.utc != tzinfo_value: t += tzinfo_value.utcoffset(t) t = t.replace(tzinfo=tzinfo_value) return self._datetime_to_snowflake(t)
Example #20
Source File: arrow_context.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def TIMESTAMP_LTZ_to_python_windows(self, microseconds): tzinfo = self._get_session_tz() try: t0 = ZERO_EPOCH + timedelta(seconds=microseconds) t = pytz.utc.localize(t0, is_dst=False).astimezone(tzinfo) return t except OverflowError: logger.debug( "OverflowError in converting from epoch time to " "timestamp_ltz: %s(ms). Falling back to use struct_time." ) return time.localtime(microseconds)
Example #21
Source File: prize.py From donation-tracker with Apache License 2.0 | 5 votes |
def end_draw_time(self): if self.endrun and self.endrun.order: if not self.next_run: # covers finale speeches return self.endrun.endtime.replace( tzinfo=pytz.utc ) + datetime.timedelta(hours=1) return self.endrun.endtime.replace(tzinfo=pytz.utc) elif self.endtime: return self.endtime.replace(tzinfo=pytz.utc) else: return None
Example #22
Source File: handler.py From dino with Apache License 2.0 | 5 votes |
def _set_last_online(self, user_id: str, session=None): u = datetime.utcnow() u = u.replace(tzinfo=pytz.utc) unix_time = int(u.timestamp()) last_online = session.query(LastOnline).filter(LastOnline.uuid == user_id).first() if last_online is None: last_online = LastOnline() last_online.uuid = user_id last_online.at = unix_time session.add(last_online) session.commit()
Example #23
Source File: test_datetime_to_ms_roundtrip.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def test_millisecond_conversion(microseconds, expected): pdt = datetime.datetime(2004, 1, 14, 8, 30, 4, microseconds, tzinfo=pytz.utc) pdt2 = datetime_to_ms(pdt) assert pdt2 == expected
Example #24
Source File: redis.py From dino with Apache License 2.0 | 5 votes |
def _set_last_online(self, user_id: str): u = datetime.utcnow() u = u.replace(tzinfo=pytz.utc) unix_time = str(int(u.timestamp())) logger.info('setting last online for {} to {}'.format(user_id, unix_time)) last_online_key = RedisKeys.user_last_online(user_id) self.cache.set(last_online_key, unix_time) self.redis.set(last_online_key, unix_time) self.redis.expire(last_online_key, SEVEN_DAYS)
Example #25
Source File: cassandra_driver.py From dino with Apache License 2.0 | 5 votes |
def msg_insert(self, msg_id, from_user_id, from_user_name, target_id, target_name, body, domain, sent_time, channel_id, channel_name, deleted=False) -> None: dt = datetime.strptime(sent_time, ConfigKeys.DEFAULT_DATE_FORMAT) dt = pytz.timezone('utc').localize(dt, is_dst=None) time_stamp = int(dt.astimezone(pytz.utc).strftime('%s')) self._execute( StatementKeys.msg_insert, msg_id, from_user_id, from_user_name, target_id, target_name, body, domain, sent_time, time_stamp, channel_id, channel_name, deleted)
Example #26
Source File: cassandra_driver.py From dino with Apache License 2.0 | 5 votes |
def msg_update(self, from_user_id, target_id, body, sent_time, deleted=False) -> None: dt = datetime.strptime(sent_time, ConfigKeys.DEFAULT_DATE_FORMAT) dt = pytz.timezone('utc').localize(dt, is_dst=None) time_stamp = int(dt.astimezone(pytz.utc).strftime('%s')) self._execute(StatementKeys.msg_update, body, deleted, target_id, from_user_id, sent_time, time_stamp)
Example #27
Source File: test_signing.py From normandy with Mozilla Public License 2.0 | 5 votes |
def test_expiring_early_ok(self): now = datetime.utcnow().replace(tzinfo=pytz.utc) not_before = now - timedelta(days=1) not_after = now + timedelta(days=3) expire_early = timedelta(days=2) assert signing.check_validity(not_before, not_after, expire_early)
Example #28
Source File: test_signing.py From normandy with Mozilla Public License 2.0 | 5 votes |
def test_expired(self): now = datetime.utcnow().replace(tzinfo=pytz.utc) not_before = now - timedelta(days=2) not_after = now - timedelta(days=1) with pytest.raises(signing.CertificateExpired): signing.check_validity(not_before, not_after, None)
Example #29
Source File: test_signing.py From normandy with Mozilla Public License 2.0 | 5 votes |
def test_it_works(self): now = datetime.utcnow().replace(tzinfo=pytz.utc) not_before = now - timedelta(days=1) not_after = now + timedelta(days=1) assert signing.check_validity(not_before, not_after, None)
Example #30
Source File: test_signing.py From normandy with Mozilla Public License 2.0 | 5 votes |
def test_not_yet_valid(self): now = datetime.utcnow().replace(tzinfo=pytz.utc) not_before = now + timedelta(days=1) not_after = now + timedelta(days=2) with pytest.raises(signing.CertificateNotYetValid): signing.check_validity(not_before, not_after, None)