formatTimestamp method

int formatTimestamp(
  1. DateTime dateTime
)

Format DateTime to timestamp.

Example:

TimeUnit.milliseconds.formatTimestamp(DateTime(2022, 9, 1, 1, 23, 45, 123)); // 1661966625123
TimeUnit.seconds.formatTimestamp(DateTime(2022, 9, 1, 1, 23, 45, 123)); // 1661966625

Implementation

int formatTimestamp(DateTime dateTime) {
  int timestamp;
  switch (this) {
    case TimeUnit.microseconds:
      timestamp = dateTime.microsecondsSinceEpoch;
      break;
    case TimeUnit.milliseconds:
      timestamp = dateTime.millisecondsSinceEpoch;
      break;
    case TimeUnit.seconds:
      timestamp = dateTime.millisecondsSinceEpoch ~/ 1000;
      break;
  }
  return timestamp;
}