Dart/Flutter 由于夏令时而无法识别昨天
Dart/Flutter Not recognizing yesterday because of daylight savings time
我有一个应用程序,用户可以在其中跟踪他们的连胜记录。由于处理夏令时的方式,今天的条纹正在打破。日期以 "MM/dd/yyyy" 格式的字符串形式存储在 Firebase 数据库中。因此,对于在 2019 年 11 月 3 日星期日使用该应用程序的用户,字符串值为“11/03/2019”,它转换为日期值,打印
2019-11-03 00:00:00.000
以下是我如何从当前日期中删除 hours/minutes,以便我可以将其与其他日期进行比较:
final dateFormat = intl.DateFormat('MM/dd/yyyy');
final todayString = dateFormat.format(DateTime.now());
final today = dateFormat.parse(todayString);
print('Today is ' + today.toString());
当我打印今天的值时,我得到:
2019-11-04 00:00:00.000
当我尝试像这样获取昨天的 DateTime 值时:
today.subtract(Duration(days: 1))
打印出来的值是这样的:
2019-11-03 01:00:00.000
注意小时值现在有一个 1。
判断是打破还是继续条纹。我正在使用 DateTime.compareTo() 函数将用户的最后使用日期与昨天进行比较。
final activeStreak = this.lastTraining.compareTo(today.subtract(Duration(days: 1)));
对于在 2019 年 11 月 3 日星期日使用该应用程序的用户,值应该返回为 0,因为昨天应该等于昨天。但由于夏令时,现在昨天被视为昨天之前,所以它返回值 -1。
我怎样才能在接下来的几年里防止这个错误?
我可以建议使用 DateTime.utc() 构造函数创建 DateTime 对象,它可以防止夏令时问题。比较这两个例子:
void main() {
//Example 1: Without UTC
DateTime yesterday = DateTime(2019, 11, 3);
DateTime today = DateTime(2019, 11, 4);
DateTime now = DateTime.now();
print(yesterday); // 2019-11-03 00:00:00.000
print(today.subtract(Duration(days: 1))); // 2019-11-03 01:00:00.000
print(yesterday.compareTo(today.subtract(Duration(days: 1)))); // -1
print(today.compareTo(now));
print("");
//Example 2: With UTC constructor
DateTime yesterdayUtc = DateTime.utc(2019, 11, 3);
DateTime todayUtc = DateTime.utc(2019, 11, 4);
DateTime nowUtc = DateTime.now().toUtc();
print(yesterdayUtc); // 2019-11-03 00:00:00.000Z
print(todayUtc.subtract(Duration(days: 1))); // 2019-11-03 00:00:00.000Z
print(yesterdayUtc.compareTo(todayUtc.subtract(Duration(days: 1)))); // 0
print(today.compareTo(nowUtc));
}
DartPad link: https://dartpad.dartlang.org/c4244a97890798373ee053f04032ccad
When dealing with dates or historic events prefer to use UTC DateTimes, since they are unaffected by daylight-saving changes and are unaffected by the local timezone.
来源: https://api.dartlang.org/stable/2.6.0/dart-core/DateTime/DateTime.utc.html
我有一个应用程序,用户可以在其中跟踪他们的连胜记录。由于处理夏令时的方式,今天的条纹正在打破。日期以 "MM/dd/yyyy" 格式的字符串形式存储在 Firebase 数据库中。因此,对于在 2019 年 11 月 3 日星期日使用该应用程序的用户,字符串值为“11/03/2019”,它转换为日期值,打印
2019-11-03 00:00:00.000
以下是我如何从当前日期中删除 hours/minutes,以便我可以将其与其他日期进行比较:
final dateFormat = intl.DateFormat('MM/dd/yyyy');
final todayString = dateFormat.format(DateTime.now());
final today = dateFormat.parse(todayString);
print('Today is ' + today.toString());
当我打印今天的值时,我得到:
2019-11-04 00:00:00.000
当我尝试像这样获取昨天的 DateTime 值时:
today.subtract(Duration(days: 1))
打印出来的值是这样的:
2019-11-03 01:00:00.000
注意小时值现在有一个 1。
判断是打破还是继续条纹。我正在使用 DateTime.compareTo() 函数将用户的最后使用日期与昨天进行比较。
final activeStreak = this.lastTraining.compareTo(today.subtract(Duration(days: 1)));
对于在 2019 年 11 月 3 日星期日使用该应用程序的用户,值应该返回为 0,因为昨天应该等于昨天。但由于夏令时,现在昨天被视为昨天之前,所以它返回值 -1。
我怎样才能在接下来的几年里防止这个错误?
我可以建议使用 DateTime.utc() 构造函数创建 DateTime 对象,它可以防止夏令时问题。比较这两个例子:
void main() {
//Example 1: Without UTC
DateTime yesterday = DateTime(2019, 11, 3);
DateTime today = DateTime(2019, 11, 4);
DateTime now = DateTime.now();
print(yesterday); // 2019-11-03 00:00:00.000
print(today.subtract(Duration(days: 1))); // 2019-11-03 01:00:00.000
print(yesterday.compareTo(today.subtract(Duration(days: 1)))); // -1
print(today.compareTo(now));
print("");
//Example 2: With UTC constructor
DateTime yesterdayUtc = DateTime.utc(2019, 11, 3);
DateTime todayUtc = DateTime.utc(2019, 11, 4);
DateTime nowUtc = DateTime.now().toUtc();
print(yesterdayUtc); // 2019-11-03 00:00:00.000Z
print(todayUtc.subtract(Duration(days: 1))); // 2019-11-03 00:00:00.000Z
print(yesterdayUtc.compareTo(todayUtc.subtract(Duration(days: 1)))); // 0
print(today.compareTo(nowUtc));
}
DartPad link: https://dartpad.dartlang.org/c4244a97890798373ee053f04032ccad
When dealing with dates or historic events prefer to use UTC DateTimes, since they are unaffected by daylight-saving changes and are unaffected by the local timezone.
来源: https://api.dartlang.org/stable/2.6.0/dart-core/DateTime/DateTime.utc.html