datetimeToRippleTime static method

int datetimeToRippleTime(
  1. DateTime dateTime
)

This function converts a DateTime object to Ripple time format.

Implementation

static int datetimeToRippleTime(DateTime dateTime) {
  /// Constants for the Ripple Epoch and maximum XRPL time.
  const int rippleEpoch = 946684800;
  const int maxXRPLTime = 4294967296;

  /// Calculate the Ripple time.
  int rippleTime =
      dateTime.toUtc().millisecondsSinceEpoch ~/ 1000 - rippleEpoch;

  /// Check if the calculated time is before the Ripple Epoch.
  if (rippleTime < 0) {
    throw ArgumentError('Datetime $dateTime is before the Ripple Epoch');
  }

  /// Check if the calculated time is later than the maximum XRPL time.
  if (rippleTime >= maxXRPLTime) {
    throw ArgumentError(
        '$dateTime is later than any time that can be expressed on the XRP Ledger.');
  }

  /// Return the calculated Ripple time.
  return rippleTime;
}