lookupTimeZone method

TzInstant lookupTimeZone(
  1. int millisecondsSinceEpoch
)

lookup for TimeZone and its boundaries for an instant in time expressed as milliseconds since January 1, 1970 00:00:00 UTC.

Implementation

TzInstant lookupTimeZone(int millisecondsSinceEpoch) {
  if (zones.isEmpty) {
    return const TzInstant(TimeZone.UTC, minTime, maxTime);
  }

  if (millisecondsSinceEpoch >= _cacheStart &&
      millisecondsSinceEpoch < _cacheEnd) {
    return TzInstant(_cacheZone, _cacheStart, _cacheEnd);
  }

  if (transitionAt.isEmpty || millisecondsSinceEpoch < transitionAt[0]) {
    final zone = _firstZone();
    final start = minTime;
    final end = transitionAt.isEmpty ? maxTime : transitionAt.first;
    return TzInstant(zone, start, end);
  }

  // Binary search for entry with largest millisecondsSinceEpoch <= sec.
  var lo = 0;
  var hi = transitionAt.length;
  var end = maxTime;

  while (hi - lo > 1) {
    final m = lo + (hi - lo) ~/ 2;
    final at = transitionAt[m];

    if (millisecondsSinceEpoch < at) {
      end = at;
      hi = m;
    } else {
      lo = m;
    }
  }

  return TzInstant(zones[transitionZone[lo]], transitionAt[lo], end);
}