Utilities

Convenience utilities for the XRP Ledger

exception xrpl.utils.XRPLTimeRangeException

Bases: xrpl.constants.XRPLException

Exception for invalid XRP Ledger time data.

exception xrpl.utils.XRPRangeException

Bases: xrpl.constants.XRPLException

Exception for invalid XRP amounts.

xrpl.utils.datetime_to_ripple_time(dt: datetime.datetime) int

Convert from a datetime object to an XRP Ledger ‘Ripple Epoch’ time.

Parameters

dt – The datetime to convert

Returns

The equivalent time in whole seconds since the Ripple Epoch of 2001-01-01T00:00Z

Raises

XRPLTimeRangeException – if the time is outside the range that can be represented in Ripple Epoch time

xrpl.utils.drops_to_xrp(drops: str) decimal.Decimal

Convert from drops to decimal XRP.

Parameters

drops – String representing indivisible drops of XRP

Returns

Decimal representation of the same amount of XRP

Raises
  • TypeError – if drops not given as a string

  • XRPRangeException – if the given number of drops is invalid

xrpl.utils.posix_to_ripple_time(timestamp: Union[int, float]) int

Convert from a POSIX-like timestamp such as one returned by time.time() to an XRP Ledger ‘Ripple Epoch’ time.

Parameters

timestamp – An integer or floating-point timestamp such as one returned by time.time().

Returns

The equivalent time in whole seconds since the Ripple Epoch of 2001-01-01T00:00Z

Raises

XRPLTimeRangeException – if the time is outside the range that can be represented in Ripple Epoch time

xrpl.utils.ripple_time_to_datetime(ripple_time: int) datetime.datetime

Convert from XRP Ledger ‘Ripple Epoch’ time to a UTC datetime object.

Parameters

ripple_time – Whole seconds since the Ripple Epoch of 2001-01-01T00:00Z

Returns

The equivalent time as a datetime instance.

Raises

XRPLTimeRangeException – if the given ripple_time is not valid

xrpl.utils.ripple_time_to_posix(ripple_time: int) int

Convert from XRP Ledger ‘Ripple Epoch’ time to a POSIX-like integer timestamp.

Parameters

ripple_time – A timestamp as the number of whole seconds since the Ripple Epoch of 2001-01-01T00:00Z

Returns

The equivalent time in whole seconds since the UNIX epoch of 1970-01-01T00:00Z

Raises

XRPLTimeRangeException – if the given ripple_time is not valid

xrpl.utils.xrp_to_drops(xrp: Union[int, float, decimal.Decimal]) str

Convert a numeric XRP amount to drops of XRP.

Parameters

xrp – Numeric representation of whole XRP

Returns

Equivalent amount in drops of XRP

Raises
  • TypeError – if xrp is given as a string

  • XRPRangeException – if the given amount of XRP is invalid