Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get location and/or timezone #10

Open
FMorschel opened this issue Jul 14, 2022 · 0 comments
Open

Get location and/or timezone #10

FMorschel opened this issue Jul 14, 2022 · 0 comments

Comments

@FMorschel
Copy link

Maybe we could introduce something like this? I wanted to create an easier way to detect the current timezone so I could make a clock/countdown app that considers the timezone for alarms and/or countdowns.

Any thoughts on why this should or not be done?

import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;

tz.Location myLocal() {
  tz.initializeTimeZones();
  final locations = tz.timeZoneDatabase.locations;
  final now = DateTime.now();
  final currentTimeZoneLocations = locations.values.where((location) {
    return location.currentTimeZone.offset == now.timeZoneOffset.inMilliseconds;
  });
  return currentTimeZoneLocations.singleWhere(
    (location) {
      final name = location.name == now.timeZoneName;
      final abbrev = location.currentTimeZone.abbreviation == now.timeZoneName;
      return name || abbrev;
    },
    orElse: () => currentTimeZoneLocations.first,
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant