toDateTime method

DateTime toDateTime({
  1. TimestampUnit timestampUnit = TimestampUnit.milliseconds,
})

Converts int unix timestamp to DateTime.

Unix timestamps in other languages are sometimes seconds, such as C. So, we can use timestampUnit to specify the unit of the timestamp.

Such as:

  • 1234567890.toDateTime(timestampUnit: TimestampUnit.seconds) => DateTime(2009, 2, 14, 7, 31, 30)
  • 1234567890123.toDateTime() => DateTime(2009, 2, 14, 7, 31, 30, 0, 123)

Implementation

DateTime toDateTime({
  TimestampUnit timestampUnit = TimestampUnit.milliseconds,
}) {
  var v = this;

  if (timestampUnit == TimestampUnit.seconds) {
    v = v * 1000 * 1000;
  } else if (timestampUnit == TimestampUnit.milliseconds) {
    v = v * 1000;
  }

  return DateTime.fromMicrosecondsSinceEpoch(v);
}