Pan
Pan

Reputation: 133

Flutter Timezone (as ZoneId)

I am relative new to Flutter. While I was experimenting, I came across with an issue. My REST Api takes a timezone parameter (Zone ID format such as Europe/London).

I saw both https://pub.dartlang.org/packages/flutter_native_timezone and https://pub.dartlang.org/packages/timezone, but neither of those serve my needs.

My goal is, when the user connect to the internet (without giving any location access to the app, if it is possible), get the timezone in ZoneId format and feed my back end in order to make the necessary date and time adjustments. Something similar to this

>>> var timezone = jstz.determine();
>>> timezone.name(); 
"Europe/London"

Presented in https://bitbucket.org/pellepim/jstimezonedetect

Any insights will be really helpful.

Thanks in advance

Upvotes: 8

Views: 22337

Answers (7)

The best way is to use timezone. as below

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

String getNameLocalTimeZone() {
  tz.initializeTimeZones();
  var locations = tz.timeZoneDatabase.locations;

  int milliseconds=DateTime.now().timeZoneOffset.inMilliseconds;
  String name = "";

  locations.forEach((key, value) {
    for (var element in value.zones) {
      if (element.offset == milliseconds) {
        name = value.name;
        break;
      }
    }
  });

  return name;
}

Upvotes: 0

Bigmog
Bigmog

Reputation: 11

If you are just after the Time Zone offset. All you need is one line of code

int tzOffset = DateTime.now().timeZoneOffset.inHours;

Upvotes: 1

Karim
Karim

Reputation: 1010

You can simply add .toLocal() to your DateTime object.

myDate = otherDate.toLocal();

myDate = DateTime.parse(json['server_date']).toLocal();

Upvotes: 0

Varun Kumar Medam
Varun Kumar Medam

Reputation: 91

DateTime.now().timeZoneOffset   // +5:30:00.000000 is expected format

Upvotes: -1

Alexander Krol
Alexander Krol

Reputation: 1595

I've been also looking for this and here's what I found: https://pub.dev/packages/flutter_native_timezone

It a bit hacky under the hood, but it seems to work properly on both iOS and Android. Hope this will help someone in need.

Usage:

final String currentTimeZone = await FlutterNativeTimezone.getLocalTimezone();
print(currentTimeZone); // Europe/Moscow

Upvotes: 9

dsalkamoses
dsalkamoses

Reputation: 280

Came up with a simple solution:

//server time(UTC)
String date_to_parse = "2019-06-23 12:59:43.444896+00:00";
DateTime server_datetime = DateTime.parse(date_to_parse);

//get current system local time
DateTime local_datetime = DateTime.now();

//get time diff
var timezoneOffset = local_datetime.timeZoneOffset;
var time_diff = new Duration(hours: timezoneOffset.inHours, minutes: timezoneOffset.inMinutes % 60);

//adjust the time diff
var new_local_time = server_datetime.add(time_diff);

Upvotes: 2

Pan
Pan

Reputation: 133

After doing some digging, I found a (temporary) workaround to my issue.

Using

var now = new DateTime.now();
var timezoneOffset = now.timeZoneOffset;

I got the timezoneOffset in UTC format in flutter. Then send it to my back end as string (i.e. "-04") and drift my ZoneId properly.

Integer tzOffsetHour = Integer.valueOf(covertToTimezone);
ZoneOffset offset = ZoneOffset.ofHoursMinutes(tzOffsetHour, 0);
ZoneId zoneTZ = ZoneId.ofOffset("UTC", offset);
LocalDateTime localDateTimeClient = new Date().toInstant().atZone(zoneTZ).toLocalDateTime();

As a result I take the local time and date as stated in user's device, without using any location services, which was my goal from the beginning.

Until now, I have not faced any issues... we will see...

Upvotes: 0

Related Questions