Timestamp.fromMillisecondsSinceEpoch constructor

Timestamp.fromMillisecondsSinceEpoch(
  1. int milliseconds
)

Creates a new timestamp from the given number of milliseconds since the Unix epoch 1970-01-01T00:00:00Z.

Throws an ArgumentError if the result is outside the supported range.

final Timestamp t = Timestamp.fromMillisecondsSinceEpoch(1700000000000);

Implementation

factory Timestamp.fromMillisecondsSinceEpoch(int milliseconds) {
  // Floor division, so that negative values keep non-negative nanoseconds.
  int seconds = milliseconds ~/ 1000;
  int nanoseconds = milliseconds.remainder(1000) * 1000000;

  if (nanoseconds < 0) {
    seconds -= 1;
    nanoseconds += 1000000000;
  }

  _validateRange(seconds, nanoseconds);
  return Timestamp(seconds, nanoseconds);
}