Python pendulum.from_timestamp() Examples

The following are 12 code examples of pendulum.from_timestamp(). 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 pendulum , or try the search function .
Example #1
Source File: nextBusData.py    From performance_tracker with GNU General Public License v3.0 5 votes vote down vote up
def get_report_time(last_report_time, offset, secs_since_report):
    # This method is up for debate.
    # To account for possible latency in the connection, we are subtracting
    # "seconds_since_report" from the last report time,
    # not the time the request was made or the time returned in the header.
    # Therefore an "offset" is added - equal to the seconds_since_report
    # of the most recent report, in order to make seconds_since_report
    # relative to the last report time --> not "now".
    # The reason for not using "now" is that it is unclear what
    # seconds_since_report is measured relative to. No info from NextBus
    # or Metro on this.
    return pendulum.from_timestamp(
        int(last_report_time + offset - secs_since_report)
    ).to_rfc3339_string() 
Example #2
Source File: nextbus.py    From performance_tracker with GNU General Public License v3.0 5 votes vote down vote up
def get_report_time(last_report_time, offset, secs_since_report):
    # This method is up for debate.
    # To account for possible latency in the connection, we are subtracting
    # "seconds_since_report" from the last report time,
    # not the time the request was made or the time returned in the header.
    # Therefore an "offset" is added - equal to the seconds_since_report
    # of the most recent report, in order to make seconds_since_report
    # relative to the last report time --> not "now".
    # The reason for not using "now" is that it is unclear what
    # seconds_since_report is measured relative to. No info from NextBus
    # or Metro on this.
    return pendulum.from_timestamp(
        int(last_report_time + offset - secs_since_report)
    ).to_rfc3339_string() 
Example #3
Source File: core.py    From google-music-scripts with MIT License 5 votes vote down vote up
def filter_google_dates(
	songs,
	*,
	creation_dates=None,
	modification_dates=None,
):
	matched_songs = songs

	def _dt_from_gm_timestamp(gm_timestamp):
		return pendulum.from_timestamp(gm_utils.from_gm_timestamp(gm_timestamp))

	def _match_created_date(songs, period):
		return (
			song
			for song in songs
			if _dt_from_gm_timestamp(song['creationTimestamp']) in period
		)

	def _match_modified_date(songs, period):
		return (
			song
			for song in songs
			if _dt_from_gm_timestamp(song['lastModifiedTimestamp']) in period
		)

	if creation_dates:
		for period in creation_dates:
			matched_songs = _match_created_date(matched_songs, period)

	if modification_dates:
		for period in modification_dates:
			matched_songs = _match_modified_date(matched_songs, period)

	return list(matched_songs) 
Example #4
Source File: serialized_objects.py    From airflow with Apache License 2.0 5 votes vote down vote up
def _deserialize(cls, encoded_var: Any) -> Any:  # pylint: disable=too-many-return-statements
        """Helper function of depth first search for deserialization."""
        # JSON primitives (except for dict) are not encoded.
        if cls._is_primitive(encoded_var):
            return encoded_var
        elif isinstance(encoded_var, list):
            return [cls._deserialize(v) for v in encoded_var]

        if not isinstance(encoded_var, dict):
            raise ValueError(f"The encoded_var should be dict and is {type(encoded_var)}")
        var = encoded_var[Encoding.VAR]
        type_ = encoded_var[Encoding.TYPE]

        if type_ == DAT.DICT:
            return {k: cls._deserialize(v) for k, v in var.items()}
        elif type_ == DAT.DAG:
            return SerializedDAG.deserialize_dag(var)
        elif type_ == DAT.OP:
            return SerializedBaseOperator.deserialize_operator(var)
        elif type_ == DAT.DATETIME:
            return pendulum.from_timestamp(var)
        elif type_ == DAT.TIMEDELTA:
            return datetime.timedelta(seconds=var)
        elif type_ == DAT.TIMEZONE:
            return Timezone(var)
        elif type_ == DAT.RELATIVEDELTA:
            if 'weekday' in var:
                var['weekday'] = relativedelta.weekday(*var['weekday'])  # type: ignore
            return relativedelta.relativedelta(**var)
        elif type_ == DAT.SET:
            return {cls._deserialize(v) for v in var}
        elif type_ == DAT.TUPLE:
            return tuple([cls._deserialize(v) for v in var])
        else:
            raise TypeError('Invalid type {!s} in deserialization.'.format(type_)) 
Example #5
Source File: test_create_from_timestamp.py    From pendulum with MIT License 5 votes vote down vote up
def test_create_from_timestamp_returns_pendulum():
    d = pendulum.from_timestamp(pendulum.datetime(1975, 5, 21, 22, 32, 5).timestamp())
    assert_datetime(d, 1975, 5, 21, 22, 32, 5)
    assert d.timezone_name == "UTC" 
Example #6
Source File: test_create_from_timestamp.py    From pendulum with MIT License 5 votes vote down vote up
def test_create_from_timestamp_with_timezone_string():
    d = pendulum.from_timestamp(0, "America/Toronto")
    assert d.timezone_name == "America/Toronto"
    assert_datetime(d, 1969, 12, 31, 19, 0, 0) 
Example #7
Source File: test_create_from_timestamp.py    From pendulum with MIT License 5 votes vote down vote up
def test_create_from_timestamp_with_timezone():
    d = pendulum.from_timestamp(0, timezone("America/Toronto"))
    assert d.timezone_name == "America/Toronto"
    assert_datetime(d, 1969, 12, 31, 19, 0, 0) 
Example #8
Source File: schema.py    From pytezos with MIT License 5 votes vote down vote up
def decode_literal(node, prim):
    core_type, value = next(iter(node.items()))
    if prim in ['int', 'nat']:
        return int(value)
    if prim == 'timestamp':
        if core_type == 'int':
            return pendulum.from_timestamp(int(value))
        else:
            return pendulum.parse(value)
    if prim == 'mutez':
        return Decimal(value) / 10 ** 6
    if prim == 'bool':
        return value == 'True'
    if prim == 'address' and core_type == 'bytes':
        prefix = {'0000': b'tz1', '0001': b'tz2', '0002': b'tz3'}  # TODO: check it's ttr
        return base58_encode(bytes.fromhex(value[4:]), prefix[value[:4]]).decode()
    return value 
Example #9
Source File: schema.py    From pytezos with MIT License 5 votes vote down vote up
def encode_literal(value, prim, binary=False):
    if prim in ['int', 'nat']:
        core_type = 'int'
        value = str(value)
    elif prim == 'timestamp':
        core_type = 'string'
        if isinstance(value, int):
            value = pendulum.from_timestamp(value)
        if isinstance(value, pendulum.DateTime):
            value = value.strftime('%Y-%m-%dT%H:%M:%SZ')
    elif prim == 'mutez':
        core_type = 'int'
        if isinstance(value, Decimal):
            value = int(value * 10 ** 6)
        if isinstance(value, int):
            value = str(value)
    elif prim == 'bool':
        core_type = 'prim'
        value = 'True' if value else 'False'
    elif prim == 'bytes':
        core_type = 'bytes'
    else:
        core_type = 'string'
        value = str(value)

    return {core_type: value} 
Example #10
Source File: main.py    From bitfinex-ohlc-import with MIT License 5 votes vote down vote up
def symbol_start_date(symbol):
    """
    Return the datetime when `symbol` first started trading.
    """
    with open('symbols_trading_start_days.json') as f:
        data = json.load(f)

    # objects are timestamps with milliseconds, divide
    # by 1000 to remove milliseconds
    return pendulum.from_timestamp(int(data[symbol])/1000) 
Example #11
Source File: db.py    From bitfinex-ohlc-import with MIT License 5 votes vote down vote up
def get_latest_candle_date(self, symbol):
        """
        Get the time of the most recent candle for a symbol
        """
        r = self.con.execute('select max(time) from candles where symbol=?',
                             (symbol,))
        result = r.fetchone()[0]
        if result is None:
            return
        else:
            return pendulum.from_timestamp(int(result)/1000) 
Example #12
Source File: parser.py    From shadowreader with Apache License 2.0 4 votes vote down vote up
def print_stats(res: list, tzinfo, app: str):
    """
    Print the results of the parse
    :param res: Results of the parsing
    :param tzinfo: Timezone found in logs
    :param app: Name of application for the logs
    """
    mins_of_data = len(res)
    total_reqs = sum([x["num_uris"] for x in res])
    max_reqs = max([x["num_uris"] for x in res])
    min_reqs = min([x["num_uris"] for x in res])
    avg_reqs = total_reqs // mins_of_data

    first, last = res[0], res[-1]
    first, last = first["timestamp"], last["timestamp"]
    first = pendulum.from_timestamp(first, tzinfo)
    last = pendulum.from_timestamp(last, tzinfo)
    first, last = first.isoformat()[:16], last.isoformat()[:16]

    test_params = {
        "base_url": "http://$your_base_url",
        "rate": 100,
        "replay_start_time": first,
        "replay_end_time": last,
        "identifier": "oss",
    }

    dump = json.dumps(test_params, indent=2)
    click.echo(f"{mins_of_data} minutes of traffic data was uploaded to S3.")
    click.echo(f"Average requests/min: {avg_reqs}")
    click.echo(f"Max requests/min: {max_reqs}")
    click.echo(f"Min requests/min: {min_reqs}")
    click.echo(f"Timezone found in logs: {tzinfo.name}")
    click.echo(
        f"To load test with these results, use the below parameters for the orchestrator in serverless.yml"
    )
    click.echo(f"==========================================")
    click.echo(f"test_params: '{dump}'")
    click.echo(f"apps_to_test: '[\"{app}\"]'")
    click.echo(f"==========================================")

    """
    Output shoud look like:
    5 minutes of traffic data was uploaded to S3.
    Average requests/min: 6
    Max requests/min: 8
    Min requests/min: 2
    Timezone found in logs: +00:00
    To load test with these results, use the below parameters for the orchestrator in serverless.yml
    ==========================================
    test_params: {
      "base_url": "http://$your_base_url",
      "rate": 100,
      "replay_start_time": "2019-03-15T04:12",
      "replay_end_time": "2019-03-15T04:16",
      "identifier": "oss"
    }
    apps_to_test: ["app1"]
    ==========================================
    """