Python pytz.utc() Examples

The following are 30 code examples for showing how to use pytz.utc(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pytz , or try the search function .

Example 1
Project: oxidizr   Author: pixlie   File: crawl.py    License: GNU General Public License v2.0 7 votes vote down vote up
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
Project: InsightAgent   Author: insightfinder   File: getmetrics_cadvisor.py    License: Apache License 2.0 6 votes vote down vote up
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
Project: InsightAgent   Author: insightfinder   File: getlogs_k8s.py    License: Apache License 2.0 6 votes vote down vote up
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 4
Project: InsightAgent   Author: insightfinder   File: getlogs_spark.py    License: Apache License 2.0 6 votes vote down vote up
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 5
Project: InsightAgent   Author: insightfinder   File: getmetrics_sar.py    License: Apache License 2.0 6 votes vote down vote up
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 6
Project: InsightAgent   Author: insightfinder   File: insightagent-boilerplate.py    License: Apache License 2.0 6 votes vote down vote up
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 7
Project: InsightAgent   Author: insightfinder   File: getmessages_prometheus.py    License: Apache License 2.0 6 votes vote down vote up
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
Project: InsightAgent   Author: insightfinder   File: getlogs_hadoop-mapreduce.py    License: Apache License 2.0 6 votes vote down vote up
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
Project: worker   Author: moira-alert   File: attime.py    License: GNU General Public License v3.0 6 votes vote down vote up
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 10
Project: oxidizr   Author: pixlie   File: views.py    License: GNU General Public License v2.0 6 votes vote down vote up
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 11
Project: oxidizr   Author: pixlie   File: views.py    License: GNU General Public License v2.0 6 votes vote down vote up
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 12
Project: oxidizr   Author: pixlie   File: crawl.py    License: GNU General Public License v2.0 6 votes vote down vote up
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 13
Project: normandy   Author: mozilla   File: signing.py    License: Mozilla Public License 2.0 6 votes vote down vote up
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 14
Project: snowflake-connector-python   Author: snowflakedb   File: converter.py    License: Apache License 2.0 6 votes vote down vote up
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 15
Project: InsightAgent   Author: insightfinder   File: getlogs_Pufa.py    License: Apache License 2.0 5 votes vote down vote up
def get_timestamp_for_zone(date_string, time_zone, datetime_format):
    dtexif = datetime.strptime(date_string, datetime_format)
    tz = pytz.timezone(time_zone)
    tztime = tz.localize(dtexif)
    epoch = long((tztime - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000
    return epoch 
Example 16
Project: InsightAgent   Author: insightfinder   File: getmessages_elasticsearch2.py    License: Apache License 2.0 5 votes vote down vote up
def get_timestamp_from_datetime(timestamp_datetime):
    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 17
Project: InsightAgent   Author: insightfinder   File: getmessages_file_replay.py    License: Apache License 2.0 5 votes vote down vote up
def get_timestamp_from_datetime(timestamp_datetime):
    timestamp_localize = agent_config_vars['timezone'].localize(timestamp_datetime)

    epoch = long((timestamp_localize - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000
    return epoch 
Example 18
Project: InsightAgent   Author: insightfinder   File: getlogs_tcpdump.py    License: Apache License 2.0 5 votes vote down vote up
def get_timestamp_from_datetime(timestamp_datetime):
    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 19
Project: InsightAgent   Author: insightfinder   File: getlogs_mysql.py    License: Apache License 2.0 5 votes vote down vote up
def get_gmt_timestamp(date_string, datetime_format):
    if datetime_format == Constant.NONE:
        return long(date_string)
    struct_time = datetime.strptime(date_string, datetime_format)
    time_by_zone = pytz.timezone(Constant.GMT).localize(struct_time)
    epoch = long((time_by_zone - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000
    return epoch 
Example 20
Project: InsightAgent   Author: insightfinder   File: reportMetrics.py    License: Apache License 2.0 5 votes vote down vote up
def _get_timestamp_with_timezone(date_string, time_zone, format):
    timestamp_datetime = datetime.strptime(date_string, format)
    tz = pytz.timezone(time_zone)
    time_in_tz = tz.localize(timestamp_datetime)
    epoch = long((time_in_tz - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds())*1000
    return epoch 
Example 21
Project: InsightAgent   Author: insightfinder   File: get_logs_mssql.py    License: Apache License 2.0 5 votes vote down vote up
def get_gmt_timestamp(date_string, datetime_format):
    if datetime_format == Constant.NONE:
        return long(date_string)
    struct_time = datetime.strptime(date_string, datetime_format)
    time_by_zone = pytz.timezone(Constant.GMT).localize(struct_time)
    epoch = long((time_by_zone - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()) * 1000
    return epoch 
Example 22
Project: aws-ops-automator   Author: awslabs   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def set_event_for_time(next_time, task=None, logger=None, context=None):
    next_event_time = next_time - timedelta(minutes=4)
    if logger is not None:
        logger.info(INF_NEXT_EVENT, next_time, next_event_time)
    utc_now = datetime.utcnow().replace(microsecond=0, second=0, tzinfo=pytz.utc)
    if next_event_time <= utc_now:
        if logger is not None:
            logger.info(INF_NEXT_EVENT_IN_PAST, utc_now)
        set_scheduler_rule_every_minute(task, context)
    else:
        cron = "cron({} {} * * ? *)".format(next_event_time.minute, next_event_time.hour)
        _set_scheduler_cloudwatch_rule_expression(cron, task=task, context=context, logger=logger)
    return next_event_time 
Example 23
Project: suncalcPy   Author: Broham   File: test.py    License: MIT License 5 votes vote down vote up
def test_getTimes(self):
        """Get sun times correctly"""
        # Weirdness to handle PST to UTC date/time comparisons.  Consider moving everything to PST instead of having this conversion?
        local = pytz.timezone ("America/Los_Angeles")
        naive = datetime.strptime ('2013-03-04 16','%Y-%m-%d %H')
        local_dt = local.localize(naive, is_dst=None)
        utc_dt = local_dt.astimezone (pytz.utc)
        times = suncalc.getTimes(utc_dt, self.lat, self.lng)

        for i in self.sunTimes:
            naive = datetime.strptime (times[i],'%Y-%m-%d %H:%M:%S')
            local_dt = local.localize(naive, is_dst=None)
            utc_dt = local_dt.astimezone (pytz.utc)
            self.assertEqual(self.sunTimes[i],utc_dt.strftime('%Y-%m-%d %H:%M:%S')) 
Example 24
Project: worker   Author: moira-alert   File: util.py    License: GNU General Public License v3.0 5 votes vote down vote up
def epoch(dt):
    """
    Returns the epoch timestamp of a timezone-aware datetime object.
    """
    return calendar.timegm(dt.astimezone(pytz.utc).timetuple()) 
Example 25
Project: oxidizr   Author: pixlie   File: meetup_feed.py    License: GNU General Public License v2.0 5 votes vote down vote up
def handle(self, *args, **options):
        r = requests.get('http://stream.meetup.com/2/open_events?since_count=100', stream=True)
        for line in r.iter_lines():
            if line:
                event = json.loads(line)
                if event['status'] != 'cancelled' or event['status'] != 'deleted':
                    kwargs = dict(
                        event_id=event['id'],
                        name=event['name'],
                        event_url=event['event_url'],
                    )
                    if 'description' in event:
                        # print 'Description', event['description'][:100]
                        kwargs['description'] = strip_tags(event['description'])
                    print 'URL', event['event_url']
                    print 'Name', event['name']

                    if 'time' in event:
                        event['time'] = datetime.utcfromtimestamp(int(event['time']) / 1000).replace(tzinfo=pytz.utc)
                        print 'Time', event['time']
                        kwargs['when'] = event['time']

                    if 'venue' in event:
                        if 'city' in event['venue']:
                            print 'City', event['venue']['city']
                            kwargs['venue_city'] = event['venue']['city']
                        if 'country' in event['venue']:
                            print 'Country', str(event['venue']['country']).upper()
                            kwargs['venue_country'] = str(event['venue']['country']).upper()

                    print event['group']['urlname']
                    kwargs['group_url'] = 'http://www.meetup.com/%s' % event['group']['urlname']
                    print

                    try:
                        model = Event.objects.create(**kwargs)
                        model.save()
                    except IntegrityError:
                        pass
                    sys.stdout.flush() 
Example 26
Project: hsds   Author: HDFGroup   File: timeUtil.py    License: Apache License 2.0 5 votes vote down vote up
def unixTimeToUTC(timestamp):
    """Convert unix timestamp (seconds since Jan 1, 1970, to ISO-8601
    compatible UTC time string.

    """
    utc = pytz.utc
    dtTime = datetime.fromtimestamp(timestamp, utc)
    iso_str = dtTime.isoformat()
    # isoformat returns a string like this:
    # '2014-10-30T04:25:21+00:00'
    # strip off the '+00:00' and replace
    # with 'Z' (both are ISO-8601 compatible)
    npos = iso_str.rfind('+')
    iso_z = iso_str[:npos] + 'Z'
    return iso_z 
Example 27
Project: hsds   Author: HDFGroup   File: helper.py    License: Apache License 2.0 5 votes vote down vote up
def getTestDomainName(name):
    now = time.time()
    dt = datetime.fromtimestamp(now, pytz.utc)
    domain = "/home/"
    domain += config.get('user_name')
    domain += '/'
    domain += 'hsds_test'
    domain += '/'
    domain += name.lower()
    domain += '/'
    domain += "{:04d}{:02d}{:02d}T{:02d}{:02d}{:02d}_{:06d}Z".format(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond)
    return domain 
Example 28
Project: hsds   Author: HDFGroup   File: helper.py    License: Apache License 2.0 5 votes vote down vote up
def getTestDomainName(name):
    now = time.time()
    dt = datetime.fromtimestamp(now, pytz.utc)
    domain = "{:04d}{:02d}{:02d}T{:02d}{:02d}{:02d}_{:06d}Z".format(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond)  
    domain += '.' 
    domain += name.lower()
    domain += '.'
    domain += config.get('user_name')
    domain += '.home'
    return domain 
Example 29
Project: bloop   Author: numberoverzero   File: test_pendulum.py    License: MIT License 5 votes vote down vote up
def test_datetime(timezone):
    delorean_now = pendulum.instance(now)
    typedef = DateTime(timezone)

    assert typedef.dynamo_dump(delorean_now, context={}) == now_iso8601
    assert typedef.dynamo_load(now_iso8601, context={}).in_timezone("utc") == now 
Example 30
Project: bloop   Author: numberoverzero   File: test_pendulum.py    License: MIT License 5 votes vote down vote up
def test_timestamp(timezone):
    delorean_now = pendulum.instance(now)
    typedef = Timestamp(timezone)

    assert typedef.dynamo_dump(delorean_now, context={}) == now_timestamp
    assert typedef.dynamo_load(now_timestamp, context={}).in_timezone("utc") == now