覆盖 Dart 的本地时区

eja*_*ain 10 dart

DartDateTime仅支持 UTC 和“本地”时间。有没有办法在运行测试时指定本地时区,而不需要更改运行测试的计算机上的时区(或用时区感知库替换 DateTime)?请参见 Java 的user.timezone系统属性。

Ber*_*Ber 0

我认为时钟包应该有用。

在里面运行你的测试withClock()

test('clock', () {
  withClock(Clock.fixed(DateTime.parse('2002-02-27T14:00:00-0500')), () {
    final date = clock.now(); // will be the same for each launch

    // your test here
  });
});
Run Code Online (Sandbox Code Playgroud)