【问题标题】:DateTime.now().toUtc().difference and DateTime.now().difference give the same result in dartDateTime.now().toUtc().difference 和 DateTime.now().difference 在 dart 中给出相同的结果
【发布时间】:2020-10-22 02:36:18
【问题描述】:

基本上是标题。 我想得到 dateTime 和当前时间之间的差异。日期是UTC,我首先将当前时间转换为UTC,然后发现差异。但是这两种情况的区别,无论是否转换为 UTC,都是一样的,而事实并非如此。

 print(DateTime.now().toUtc());
    print(DateTime.now());
    print(DateTime.now().toUtc()
        .difference(dateFormat.parse(comment.postedOn))
        .inMinutes);
    print( DateTime.now()
        .difference(dateFormat.parse(comment.postedOn))
        .inMinutes);

结果是

我/颤振(20796):2020-07-01 15:33:46.221975Z

我/颤振(20796):2020-07-01 21:03:46.222207

我/颤振(20796):361

我/颤振(20796):361

【问题讨论】:

  • DateTime.now().toUtc()DateTime.now() 都代表同一时间点,只是报告方式不同。无论时区如何,那个时刻和另一个时刻之间的差异应该是相同的,所以观察到的行为对我来说听起来是正确的。你能提供你想要的例子吗?

标签: android flutter dart


【解决方案1】:

在对 Dart SDK 的源代码进行了一些挖掘之后,看起来millisecondsSinceEpoch 属性或microsecondsSinceEpoch 属性用于计算两个DateTime 变量之间的差异。这可以解释为什么 UTC DateTime 值和本地 DateTime 值的差异是相同的,因为它们都代表相同的时间。

final now = DateTime.now();
final utc = now.toUtc();
final date = DateTime.parse('2020-07-01T19:00:00Z');

print(utc);
print(now);
print(date);
print(utc.microsecondsSinceEpoch);
print(now.microsecondsSinceEpoch);
print(date.microsecondsSinceEpoch);

print(utc.difference(date).inMinutes);
print(now.difference(date).inMinutes);
2020-07-01 16:26:54.464Z

2020-07-01 17:26:54.464

2020-07-01 19:00:00.000Z

1593620814464000

1593620814464000

1593630000000000

-153

-153

./sdk/lib/core/date_time.dart

/**
 * The value of this DateTime.
 *
 * The content of this field is implementation dependent. On JavaScript it is
 * equal to [millisecondsSinceEpoch]. On the VM it is equal to
 * [microsecondsSinceEpoch].
 */
final int _value;

./js_runtime/lib/core_patch.dart

@patch
Duration difference(DateTime other) {
  return new Duration(milliseconds: _value - other._value);
}

./vm/lib/date_patch.dart

@patch
Duration difference(DateTime other) {
  return new Duration(microseconds: _value - other._value);
}

如果您不希望这种默认行为,那么您可以检查DateTime 值是否为isUtc。如果该值不是 UTC,那么您可以添加 timeZoneOffset 以实现您的预​​期行为。

extension DateTimeExtensions on DateTime {
  Duration differenceTimeZoneOffset(DateTime other) {
    if (this.isUtc) {
      return this.difference(other);
    } else {
      return this.add(this.timeZoneOffset).difference(other);
    }
  }
}

final now = DateTime.now();
final utc = now.toUtc();
final date = utc.add(Duration(hours: 1));

print(utc);
print(now);
print(date);
print(utc.microsecondsSinceEpoch);
print(now.microsecondsSinceEpoch);
print(date.microsecondsSinceEpoch);

print(utc.differenceTimeZoneOffset(date).inMinutes);
print(now.differenceTimeZoneOffset(date).inMinutes);
2020-07-01 17:24:54.923Z

2020-07-01 18:24:54.923

2020-07-01 18:24:54.923Z

1593624294923000

1593624294923000

1593627894923000

-60

0

【讨论】:

  • 知道如何正确找到差异吗?
  • 我已经用一个额外的代码示例更新了答案,展示了如何实现您的预​​期行为而不是默认行为。
猜你喜欢
  • 2017-02-02
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
  • 2014-07-05
  • 2018-11-25
  • 1970-01-01
相关资源
最近更新 更多