initializeTimeZone function

Future<void> initializeTimeZone([
  1. String path = tzDataDefaultPath
])

Initialize Time Zone database.

Throws TimeZoneInitException when something is wrong.

import 'package:timezone/browser.dart';

initializeTimeZone().then(() {
  final detroit = getLocation('America/Detroit');
  final detroitNow = TZDateTime.now(detroit);
});

Implementation

Future<void> initializeTimeZone([String path = tzDataDefaultPath]) async {
  final client = browser.BrowserClient();
  try {
    final response = await client.get(
      Uri.parse(path),
      headers: {'Accept': 'application/octet-stream'},
    );
    if (response.statusCode == 200) {
      initializeDatabase(response.bodyBytes);
    } else {
      throw TimeZoneInitException(
        'Request failed with status: ${response.statusCode}',
      );
    }
  } on TimeZoneInitException {
    rethrow;
  } on Exception catch (e) {
    throw TimeZoneInitException(e.toString());
  } finally {
    client.close();
  }
}