toDateTimeEpochJson static method

DateTime? toDateTimeEpochJson(
  1. dynamic json,
  2. JsonEpochScale scale
)

Converts an epoch timestamp to DateTime.

Implementation

static DateTime? toDateTimeEpochJson(dynamic json, JsonEpochScale scale) {
  if (json == null) return null;
  final int? since = json as int?;
  if (since == null) return null;
  return switch (scale) {
    JsonEpochScale.seconds => DateTime.fromMillisecondsSinceEpoch(since * 1000),
    JsonEpochScale.milliseconds => DateTime.fromMillisecondsSinceEpoch(since),
    JsonEpochScale.microseconds => DateTime.fromMicrosecondsSinceEpoch(since),
  };
}