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

Is there a way to override the system time zone in flutter for unit testing? #156

Open
Quesajo17 opened this issue Feb 24, 2023 · 2 comments

Comments

@Quesajo17
Copy link

I have a Date class that only stores a DateTime, but is supposed to convert it to the current date (regardless of time zone) at 00:00:0000 in UTC.

I would like unit tests to make sure it works on both ends of the UTC timezone, to make sure it is working properly. Is there a way to update the system timezone in flutter to do this? It seems like the setLocalLocation(X) method doesn't work in dart.

@HenriqueNas
Copy link

With the Intl package you can set the locale:

    Intl.defaultLocale = 'pt_BR';
    Intl.systemLocale = 'pt_BR';

But, to inject the current date-time of your application you must use a provider, by creating your self, or using the clock package, provided by Dart:

void main() {
  final fixedDateTime = DateTime(1997, 12, 16);
  withClock(
    Clock.fixed(fixedDateTime),
    () async {
      print(clock.now()); // 1997-12-16 00:00:00.000
    },
  );
}

but you need to replace all DateTime.now() by clock.now()

@provokateurin
Copy link

provokateurin commented Jun 23, 2024

You can just override the location using tz.setLocalLocation(tz.getLocation('Europe/Berlin')); which is exactly what I'm doing in my tests. I don't know why it wouldn't work for you.

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

3 participants