parseTimestamp method

DateTime parseTimestamp(
  1. String timestamp, {
  2. bool isUtc = false,
})

Parse timestamp to DateTime.

Example:

TimeUnit.milliseconds.parseTimestamp('1661966625123'); // 2022-09-01 01:23:45.123

Implementation

DateTime parseTimestamp(String timestamp, {bool isUtc = false}) {
  int microsecondsSinceEpoch;
  switch (this) {
    case TimeUnit.microseconds:
      microsecondsSinceEpoch = timestamp.toInt();
      break;
    case TimeUnit.milliseconds:
      microsecondsSinceEpoch = timestamp.toInt() * 1000;
      break;
    case TimeUnit.seconds:
      microsecondsSinceEpoch = timestamp.toInt() * 1000 * 1000;
      break;
  }
  return DateTime.fromMicrosecondsSinceEpoch(microsecondsSinceEpoch, isUtc: isUtc);
}