Python datetime.datetime.utcfromtimestamp() Examples

The following are 30 code examples of datetime.datetime.utcfromtimestamp(). 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 datetime.datetime , or try the search function .
Example #1
Source File: client.py    From python-pilosa with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _field_time_to_roaring(self, shard_width, clear):
        standard = Bitmap()
        bitmaps = {"": standard}
        time_quantum = self.field_time_quantum
        time_formats = self._time_formats
        for b in self.columns:
            bit = b.row_id * shard_width + b.column_id % shard_width
            standard.add(bit)
            for c in time_quantum:
                fmt = time_formats.get(c, "")
                view_name = datetime.utcfromtimestamp(b.timestamp).strftime(fmt)
                bmp = bitmaps.get(view_name)
                if not bmp:
                    bmp = Bitmap()
                    bitmaps[view_name] = bmp
                bmp.add(bit)
        return self._make_roaring_request(bitmaps, clear) 
Example #2
Source File: test_source_metadata.py    From dipper with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_postgres_version_level_source_version_download_timestamp(self):
        path_to_dl_file = '/'.join(
            (self.pg_source.rawdir, self.pg_source.files.get('test_file').get('file')))
        fstat = os.stat(path_to_dl_file)

        self.downloaded_file_timestamp = \
            datetime.utcfromtimestamp(fstat[ST_CTIME]).strftime("%Y%m%d")

        triples = list(self.pg_source.dataset.graph.triples(
            (URIRef(self.theseFiles.get("test_file").get("url")),
             self.iri_retrieved_on,
             None)))
        self.assertTrue(len(triples) == 1,
                        "missing triple for ingest file retrieved on " +
                        "(download timestamp)")
        self.assertEqual(Literal(triples[0][2], datatype=XSD.date),
                         Literal(self.downloaded_file_timestamp, datatype=XSD.date),
                         "version level source version timestamp isn't " +
                         "the same as the download timestamp of the local file") 
Example #3
Source File: logs.py    From aegea with Apache License 2.0 6 votes vote down vote up
def logs(args):
    if args.log_group and (args.log_stream or args.start_time or args.end_time):
        if args.export and args.print_s3_urls:
            return ["s3://{}/{}".format(f.bucket_name, f.key) for f in export_log_files(args)]
        elif args.export:
            return export_and_print_log_events(args)
        else:
            return print_log_events(args)
    table = []
    group_cols = ["logGroupName"]
    stream_cols = ["logStreamName", "lastIngestionTime", "storedBytes"]
    args.columns = group_cols + stream_cols
    for group in paginate(clients.logs.get_paginator("describe_log_groups")):
        if args.log_group and group["logGroupName"] != args.log_group:
            continue
        n = 0
        for stream in paginate(clients.logs.get_paginator("describe_log_streams"),
                               logGroupName=group["logGroupName"], orderBy="LastEventTime", descending=True):
            now = datetime.utcnow().replace(microsecond=0)
            stream["lastIngestionTime"] = now - datetime.utcfromtimestamp(stream.get("lastIngestionTime", 0) // 1000)
            table.append(dict(group, **stream))
            n += 1
            if n >= args.max_streams_per_group:
                break
    page_output(tabulate(table, args)) 
Example #4
Source File: ConfigEmailLookup.py    From llvm-zorg with Apache License 2.0 6 votes vote down vote up
def __init__(self, author_filename, default_address, only_addresses = None, update_interval=timedelta(hours=1)):
    from ConfigParser import ConfigParser

    self.author_filename = author_filename
    self.default_address = default_address
    self.only_addresses = only_addresses
    self.update_interval = update_interval

    self.config_parser = ConfigParser()
    self.config_parser.read(self.author_filename)

    self.time_checked = datetime.utcnow()
    self.time_loaded  = datetime.utcfromtimestamp(os.path.getmtime(self.author_filename))

    if only_addresses:
      import re
      self.address_match_p = re.compile(only_addresses).match
    else:
      self.address_match_p = lambda addr: True 
Example #5
Source File: test_open.py    From smbprotocol with MIT License 6 votes vote down vote up
def test_create_message(self):
        message = SMB2CloseResponse()
        message['creation_time'] = datetime.utcfromtimestamp(0)
        message['last_access_time'] = datetime.utcfromtimestamp(0)
        message['last_write_time'] = datetime.utcfromtimestamp(0)
        message['change_time'] = datetime.utcfromtimestamp(0)
        expected = b"\x3c\x00" \
                   b"\x00\x00" \
                   b"\x00\x00\x00\x00" \
                   b"\x00\x80\x3E\xD5\xDE\xB1\x9D\x01" \
                   b"\x00\x80\x3E\xD5\xDE\xB1\x9D\x01" \
                   b"\x00\x80\x3E\xD5\xDE\xB1\x9D\x01" \
                   b"\x00\x80\x3E\xD5\xDE\xB1\x9D\x01" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00\x00\x00\x00\x00" \
                   b"\x00\x00\x00\x00"
        actual = message.pack()
        assert len(actual) == 60
        assert actual == expected 
Example #6
Source File: EOM.py    From dipper with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def fetch(self, is_dl_forced=False):
        '''connection details for DISCO'''
        cxn = {}
        cxn['host'] = 'nif-db.crbs.ucsd.edu'
        cxn['database'] = 'disco_crawler'
        cxn['port'] = '5432'
        cxn['user'] = config.get_config()['user']['disco']
        cxn['password'] = config.get_config()['keys'][cxn['user']]

        pg_iri = 'jdbc:postgresql://'+cxn['host']+':'+cxn['port']+'/'+cxn['database']
        self.dataset.set_ingest_source(pg_iri)

        # process the tables
        # self.fetch_from_pgdb(self.tables,cxn,100)  #for testing
        self.fetch_from_pgdb(self.tables, cxn)

        self.get_files(is_dl_forced)

        fstat = os.stat('/'.join((self.rawdir, 'dvp.pr_nlx_157874_1')))
        filedate = datetime.utcfromtimestamp(fstat[ST_CTIME]).strftime("%Y-%m-%d")

        self.dataset.set_ingest_source_file_version_date(pg_iri, filedate) 
Example #7
Source File: test_source_metadata.py    From dipper with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_version_level_source_version_download_timestamp(self):
        path_to_dl_file = '/'.join(
            (self.source.rawdir, self.source.files.get('test_file').get('file')))
        fstat = os.stat(path_to_dl_file)

        self.downloaded_file_timestamp = \
            datetime.utcfromtimestamp(fstat[ST_CTIME]).strftime("%Y%m%d")

        triples = list(self.source.dataset.graph.triples(
            (URIRef(self.theseFiles.get("test_file").get("url")),
             self.iri_retrieved_on,
             None)))
        self.assertTrue(len(triples) == 1,
                        "missing triple for ingest file retrieved on " +
                        "(download timestamp)")
        self.assertEqual(Literal(triples[0][2], datatype=XSD.date),
                         Literal(self.downloaded_file_timestamp, datatype=XSD.date),
                         "version level source version timestamp isn't " +
                         "the same as the download timestamp of the local file") 
Example #8
Source File: getmetrics_newrelic.py    From InsightAgent with Apache License 2.0 6 votes vote down vote up
def start_data_processing():
    track['mode'] = 'METRIC'

    # set up shared api call info
    base_url = 'https://api.newrelic.com/'
    headers = {'X-Api-Key': agent_config_vars['api_key']}
    now = int(time.time())
    to_timestamp = datetime.utcfromtimestamp(now).isoformat()
    from_timestamp = datetime.utcfromtimestamp(now - agent_config_vars['run_interval']).isoformat()
    data = {
        'to': to_timestamp,
        'from': from_timestamp,
        'period': str(if_config_vars['samplingInterval'])
    }

    get_applications(base_url, headers, data) 
Example #9
Source File: run.py    From github-stats with MIT License 6 votes vote down vote up
def check_for_update():
  if os.path.exists(FILE_UPDATE):
    mtime = os.path.getmtime(FILE_UPDATE)
    last = datetime.utcfromtimestamp(mtime).strftime('%Y-%m-%d')
    today = datetime.utcnow().strftime('%Y-%m-%d')
    if last == today:
      return
  try:
    with open(FILE_UPDATE, 'a'):
      os.utime(FILE_UPDATE, None)
    request = urllib2.Request(
      CORE_VERSION_URL,
      urllib.urlencode({'version': __version__}),
    )
    response = urllib2.urlopen(request)
    with open(FILE_UPDATE, 'w') as update_json:
      update_json.write(response.read())
  except (urllib2.HTTPError, urllib2.URLError):
    pass 
Example #10
Source File: LocalFilesMedia.py    From gphotos-sync with MIT License 6 votes vote down vote up
def get_image_date(self):
        p_date = None
        if self.got_meta:
            try:
                # noinspection PyUnresolvedReferences
                p_date = Utils.string_to_date(self.__exif.datetime_original)
            except (AttributeError, ValueError, KeyError):
                try:
                    # noinspection PyUnresolvedReferences
                    p_date = Utils.string_to_date(self.__exif.datetime)
                except (AttributeError, ValueError, KeyError):
                    pass
        if not p_date:
            # just use file date
            p_date = datetime.utcfromtimestamp(self.__full_path.stat().st_mtime)
        self.__createDate = p_date 
Example #11
Source File: LocalFilesMedia.py    From gphotos-sync with MIT License 6 votes vote down vote up
def get_video_meta(self):
        if self.__ffprobe_installed:
            try:
                command = FF_PROBE + [str(self.__full_path)]
                result = run(command, stdout=PIPE, check=True)
                out = str(result.stdout.decode("utf-8"))
                json = loads(out)
                t = json["format"]["tags"]["creation_time"]
                self.__createDate = Utils.string_to_date(t)
                self.got_meta = True
            except FileNotFoundError:
                # this means there is no ffprobe installed
                self.__ffprobe_installed = False
            except CalledProcessError:
                pass
            except KeyError:
                # ffprobe worked but there is no creation time in the JSON
                pass

        if not self.__createDate:
            # just use file date
            self.__createDate = datetime.utcfromtimestamp(
                self.__full_path.stat().st_mtime
            ) 
Example #12
Source File: authority.py    From certidude with MIT License 6 votes vote down vote up
def get_revoked(serial):
    if isinstance(serial, str):
        serial = int(serial, 16)
    path = os.path.join(config.REVOKED_DIR, "%040x.pem" % serial)
    with open(path, "rb") as fh:
        buf = fh.read()
        header, _, der_bytes = pem.unarmor(buf)
        cert = x509.Certificate.load(der_bytes)
        try:
            reason = getxattr(path, "user.revocation.reason").decode("ascii")
        except IOError: # TODO: make sure it's not required
            reason = "key_compromise"
        return path, buf, cert, \
            cert["tbs_certificate"]["validity"]["not_before"].native.replace(tzinfo=None), \
            cert["tbs_certificate"]["validity"]["not_after"].native.replace(tzinfo=None), \
            datetime.utcfromtimestamp(os.stat(path).st_ctime), \
            reason 
Example #13
Source File: test_parse_dates.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_date_parser_int_bug(all_parsers):
    # see gh-3071
    parser = all_parsers
    data = ("posix_timestamp,elapsed,sys,user,queries,query_time,rows,"
            "accountid,userid,contactid,level,silo,method\n"
            "1343103150,0.062353,0,4,6,0.01690,3,"
            "12345,1,-1,3,invoice_InvoiceResource,search\n")

    result = parser.read_csv(
        StringIO(data), index_col=0, parse_dates=[0],
        date_parser=lambda x: datetime.utcfromtimestamp(int(x)))
    expected = DataFrame([[0.062353, 0, 4, 6, 0.01690, 3, 12345, 1, -1,
                           3, "invoice_InvoiceResource", "search"]],
                         columns=["elapsed", "sys", "user", "queries",
                                  "query_time", "rows", "accountid",
                                  "userid", "contactid", "level",
                                  "silo", "method"],
                         index=Index([Timestamp("2012-07-24 04:12:30")],
                                     name="posix_timestamp"))
    tm.assert_frame_equal(result, expected) 
Example #14
Source File: test_timestamp.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_class_ops_dateutil(self):
        def compare(x, y):
            assert (int(np.round(Timestamp(x).value / 1e9)) ==
                    int(np.round(Timestamp(y).value / 1e9)))

        compare(Timestamp.now(), datetime.now())
        compare(Timestamp.now('UTC'), datetime.now(tzutc()))
        compare(Timestamp.utcnow(), datetime.utcnow())
        compare(Timestamp.today(), datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        compare(Timestamp.utcfromtimestamp(current_time),
                datetime.utcfromtimestamp(current_time))
        compare(Timestamp.fromtimestamp(current_time),
                datetime.fromtimestamp(current_time))

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(Timestamp.combine(date_component, time_component),
                datetime.combine(date_component, time_component)) 
Example #15
Source File: test_timestamp.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_class_ops_pytz(self):
        def compare(x, y):
            assert (int(Timestamp(x).value / 1e9) ==
                    int(Timestamp(y).value / 1e9))

        compare(Timestamp.now(), datetime.now())
        compare(Timestamp.now('UTC'), datetime.now(timezone('UTC')))
        compare(Timestamp.utcnow(), datetime.utcnow())
        compare(Timestamp.today(), datetime.today())
        current_time = calendar.timegm(datetime.now().utctimetuple())
        compare(Timestamp.utcfromtimestamp(current_time),
                datetime.utcfromtimestamp(current_time))
        compare(Timestamp.fromtimestamp(current_time),
                datetime.fromtimestamp(current_time))

        date_component = datetime.utcnow()
        time_component = (date_component + timedelta(minutes=10)).time()
        compare(Timestamp.combine(date_component, time_component),
                datetime.combine(date_component, time_component)) 
Example #16
Source File: test_cookies.py    From sanic with MIT License 6 votes vote down vote up
def test_cookie_expires(app, expires):
    expires = expires.replace(microsecond=0)
    cookies = {"test": "wait"}

    @app.get("/")
    def handler(request):
        response = text("pass")
        response.cookies["test"] = "pass"
        response.cookies["test"]["expires"] = expires
        return response

    request, response = app.test_client.get(
        "/", cookies=cookies, raw_cookies=True
    )
    cookie_expires = datetime.utcfromtimestamp(
        response.raw_cookies["test"].expires
    ).replace(microsecond=0)

    assert response.status == 200
    assert response.cookies["test"] == "pass"
    assert cookie_expires == expires 
Example #17
Source File: check_tap.py    From singer-tools with Apache License 2.0 6 votes vote down vote up
def extend_with_default(validator_class):
    validate_properties = validator_class.VALIDATORS["properties"]

    def set_defaults(validator, properties, instance, schema):
        for error in validate_properties(validator, properties, instance, schema):
            yield error

        for prop, subschema in properties.items():
            if "format" in subschema:
                if subschema['format'] == 'date-time' and instance.get(prop) is not None:
                    try:
                        datetime.utcfromtimestamp(rfc3339_to_timestamp(instance[prop]))
                    except Exception:
                        raise Exception('Error parsing property {}, value {}'
                                        .format(prop, instance[prop]))

    return validators.extend(validator_class, {"properties": set_defaults}) 
Example #18
Source File: test_flowlogs_reader.py    From flowlogs-reader with Apache License 2.0 6 votes vote down vote up
def test_init(self):
        self.assertEqual(self.inst.log_group_name, 'group_name')

        self.assertEqual(
            datetime.utcfromtimestamp(self.inst.start_ms // 1000),
            self.start_time
        )

        self.assertEqual(
            datetime.utcfromtimestamp(self.inst.end_ms // 1000),
            self.end_time
        )

        self.assertEqual(
            self.inst.paginator_kwargs['filterPattern'],
            'REJECT'
        ) 
Example #19
Source File: arrow_context.py    From snowflake-connector-python with Apache License 2.0 5 votes vote down vote up
def TIMESTAMP_NTZ_to_python(self, microseconds):
        return datetime.utcfromtimestamp(microseconds) 
Example #20
Source File: utils.py    From raster-vision-qgis with GNU General Public License v3.0 5 votes vote down vote up
def get_local_path(uri, working_dir):
    """
    This method will simply pass along the URI if it is local.
    If the URI is on S3, it will download the data to the working directory,
    in a structure that matches s3, and return the local path.
    If the local path already exists, and the timestamp of the S3 object is at or before
    the local path, the download will be skipped
    """

    fs = FileSystem.get_file_system(uri)
    if fs is LocalFileSystem:
        return uri


    local_path = fs.local_path(uri, working_dir)
    do_copy = True
    if os.path.exists(local_path):
        last_modified = fs.last_modified(uri)
        if last_modified:
            # If thel local file is older than the remote file, download it.
            local_last_modified = datetime.utcfromtimestamp(os.path.getmtime(local_path))
            if local_last_modified.replace(tzinfo=timezone.utc) > last_modified:
                do_copy = False
        else:
            # This FileSystem doesn't support last modified.
            # By default, don't download a new version.
            do_copy = False

    if do_copy:
        dir_name = os.path.dirname(local_path)
        make_dir(dir_name)
        fs.copy_from(uri, local_path)

    return local_path 
Example #21
Source File: converter.py    From snowflake-connector-python with Apache License 2.0 5 votes vote down vote up
def _TIMESTAMP_NTZ_to_python(self, ctx):
        """TIMESTAMP NTZ to datetime with no timezone info is attached."""
        scale = ctx['scale']

        conv0 = lambda value: datetime.utcfromtimestamp(float(value))

        def conv(value: str) -> datetime:
            microseconds = float(value[0:-scale + 6])
            return datetime.utcfromtimestamp(microseconds)

        return conv if scale > 6 else conv0 
Example #22
Source File: utils.py    From hyperledger-py with Apache License 2.0 5 votes vote down vote up
def datetime_to_timestamp(dt):
    """Convert a UTC datetime to a Unix timestamp"""
    delta = dt - datetime.utcfromtimestamp(0)
    return delta.seconds + delta.days * 24 * 3600 
Example #23
Source File: time.py    From seizure-prediction with MIT License 5 votes vote down vote up
def unix_time(dt):
    epoch = datetime.utcfromtimestamp(0)
    delta = dt - epoch
    return delta.total_seconds() 
Example #24
Source File: converter.py    From snowflake-connector-python with Apache License 2.0 5 votes vote down vote up
def _DATE_to_python(self, _):
        """Converts DATE to date."""
        def conv(value: str) -> date:
            try:
                return datetime.utcfromtimestamp(int(value) * 86400).date()
            except (OSError, ValueError) as e:
                logger.debug("Failed to convert: %s", e)
                ts = ZERO_EPOCH + timedelta(
                    seconds=int(value) * (24 * 60 * 60))
                return date(ts.year, ts.month, ts.day)

        return conv 
Example #25
Source File: converter.py    From snowflake-connector-python with Apache License 2.0 5 votes vote down vote up
def _TIME_to_python(self, ctx):
        """TIME to formatted string, SnowflakeDateTime, or datetime.time with no timezone attached."""
        scale = ctx['scale']

        conv0 = lambda value: datetime.utcfromtimestamp(float(value)).time()

        def conv(value: str) -> dt_t:
            microseconds = float(value[0:-scale + 6])
            return datetime.utcfromtimestamp(microseconds).time()

        return conv if scale > 6 else conv0 
Example #26
Source File: filtering.py    From bot with MIT License 5 votes vote down vote up
def check_send_alert(self, member: Member) -> bool:
        """When there is less than 3 days after last alert, return `False`, otherwise `True`."""
        if last_alert := await self.name_alerts.get(member.id):
            last_alert = datetime.utcfromtimestamp(last_alert)
            if datetime.utcnow() - timedelta(days=DAYS_BETWEEN_ALERTS) < last_alert:
                log.trace(f"Last alert was too recent for {member}'s nickname.")
                return False 
Example #27
Source File: BaseDataProvider.py    From RLTrader with GNU General Public License v3.0 5 votes vote down vote up
def _format_date_column(self, data_frame: pd.DataFrame, inplace: bool = True) -> pd.DataFrame:
        if inplace is True:
            formatted = data_frame
        else:
            formatted = data_frame.copy()

        date_col = self.data_columns['Date']
        date_frame = formatted.loc[:, date_col]

        if self.date_format is ProviderDateFormat.TIMESTAMP_UTC:
            formatted[date_col] = date_frame.apply(
                lambda x: datetime.utcfromtimestamp(x).strftime('%Y-%m-%d %H:%M'))
            formatted[date_col] = pd.to_datetime(date_frame, format='%Y-%m-%d %H:%M')
        elif self.date_format is ProviderDateFormat.TIMESTAMP_MS:
            formatted[date_col] = pd.to_datetime(date_frame, unit='ms')
        elif self.date_format is ProviderDateFormat.DATETIME_HOUR_12:
            formatted[date_col] = pd.to_datetime(date_frame, format='%Y-%m-%d %I-%p')
        elif self.date_format is ProviderDateFormat.DATETIME_HOUR_24:
            formatted[date_col] = pd.to_datetime(date_frame, format='%Y-%m-%d %H')
        elif self.date_format is ProviderDateFormat.DATETIME_MINUTE_12:
            formatted[date_col] = pd.to_datetime(date_frame, format='%Y-%m-%d %I:%M-%p')
        elif self.date_format is ProviderDateFormat.DATETIME_MINUTE_24:
            formatted[date_col] = pd.to_datetime(date_frame, format='%Y-%m-%d %H:%M')
        elif self.date_format is ProviderDateFormat.DATE:
            formatted[date_col] = pd.to_datetime(date_frame, format='%Y-%m-%d')
        elif self.date_format is ProviderDateFormat.CUSTOM_DATIME:
            formatted[date_col] = pd.to_datetime(
                date_frame, format=self.custom_datetime_format, infer_datetime_format=True)
        else:
            raise NotImplementedError

        formatted[date_col] = formatted[date_col].values.astype(np.int64) // 10 ** 9

        return formatted 
Example #28
Source File: help_channels.py    From bot with MIT License 5 votes vote down vote up
def get_in_use_time(self, channel_id: int) -> t.Optional[timedelta]:
        """Return the duration `channel_id` has been in use. Return None if it's not in use."""
        log.trace(f"Calculating in use time for channel {channel_id}.")

        claimed_timestamp = await self.claim_times.get(channel_id)
        if claimed_timestamp:
            claimed = datetime.utcfromtimestamp(claimed_timestamp)
            return datetime.utcnow() - claimed 
Example #29
Source File: structures.py    From instaloader with MIT License 5 votes vote down vote up
def date_utc(self) -> datetime:
        """Timestamp when the post was created (UTC)."""
        return datetime.utcfromtimestamp(self._node["date"]
                                         if "date" in self._node
                                         else self._node["taken_at_timestamp"]) 
Example #30
Source File: main.py    From gdax-ohlc-import with MIT License 5 votes vote down vote up
def get_start_date(product, cur):
    # if no date was passed, we check the DB for the latest record
    cur.execute('select max(time) from candles where market=?', (product,))
    try:
        start_date = datetime.utcfromtimestamp(int(cur.fetchone()[0]))
        logging.info(f'Resuming from {start_date}')
    except TypeError:
        # if there are no records, we start from 1st trading day
        start_date = datetime.strptime(PRODUCTS[product], '%Y-%m-%d')
        logging.info('No previous data found. Importing full history')
    return start_date