简体   繁体   English

"如何在 DART (Flutter) 中四舍五入最接近 30 分钟间隔的日期时间?"

[英]How to Round up the Date-Time nearest to 30 min interval in DART (Flutter)?

I would like to round DateTime to the nearest 30 mins.我想将 DateTime 舍入到最近的 30 分钟。 Is there rounding mechanism provided in DART? DART 中是否提供了舍入机制?

"

This function converts a DateTime to the nearest 30 minute mark in a clock.此函数将DateTime转换为时钟中最接近的 30 分钟标记。 Be warned that this 30 minute mark is obtained with respect to the local time zone of the machine in which this code runs on.请注意,此 30 分钟标记是相对于运行此代码的机器的本地时区获得的。

 DateTime roundWithin30Minutes(DateTime d) {
  final int deltaMinute;
  if (d.minute < 15) {
    deltaMinute = -d.minute; // go back to zero
  } else if (d.minute < 45) {
    deltaMinute = 30 - d.minute; // go to 30 minutes
  } else {
    deltaMinute = 60 - d.minute;
  }
  return d.add(Duration(
      milliseconds: -d.millisecond,
      // reset milliseconds to zero
      microseconds: -d.microsecond,
      // reset microseconds to zero,
      seconds: -d.second,
      // reset seconds to zero
      minutes: deltaMinute));
}

If you are presenting this DateTime in another non local time zone whose offset duration is not a multiple of 30 minutes (eg: Nepal time zone is GMT+5:45) this implementation will not work .如果您在另一个非本地时区显示此DateTime时间,其偏移持续时间不是 30 分钟的倍数(例如:尼泊尔时区为 GMT+5:45),则此实现将不起作用

extension DateTimeExt on DateTime {
  DateTime get roundMin =>
      DateTime(this.year, this.month, this.day, this.hour, () {
        if (this.minute <= 15) {
          return 0;
        } else if (this.minute > 15 && this.minute <= 45) {
          return 30;
        } else {
          return 60;
        }
      }());
}

You can do It with a simple extension just call it like this你可以用一个简单的扩展来做它,就像这样称呼它

var a = DateTime(2021, 5, 4, 3, 46, 4, 7);
print(a.roundMin);

You can use this function to roundup the time.您可以使用此函数来四舍五入时间。

DateTime alignDateTime(DateTime dt, Duration alignment,
    [bool roundUp = false]) {
  assert(alignment >= Duration.zero);
  if (alignment == Duration.zero) return dt;
  final correction = Duration(
      days: 0,
      hours: alignment.inDays > 0
          ? dt.hour
          : alignment.inHours > 0
              ? dt.hour % alignment.inHours
              : 0,
      minutes: alignment.inHours > 0
          ? dt.minute
          : alignment.inMinutes > 0
              ? dt.minute % alignment.inMinutes
              : 0,
      seconds: alignment.inMinutes > 0
          ? dt.second
          : alignment.inSeconds > 0
              ? dt.second % alignment.inSeconds
              : 0,
      milliseconds: alignment.inSeconds > 0
          ? dt.millisecond
          : alignment.inMilliseconds > 0
              ? dt.millisecond % alignment.inMilliseconds
              : 0,
      microseconds: alignment.inMilliseconds > 0 ? dt.microsecond : 0);
  if (correction == Duration.zero) return dt;
  final corrected = dt.subtract(correction);
  final result = roundUp ? corrected.add(alignment) : corrected;
  return result;
}

and then use it the following way然后按以下方式使用它

void main() {
  DateTime dt = DateTime.now();
  var newDate = alignDateTime(dt,Duration(minutes:30));
  print(dt);  // prints 2022-01-07 15:35:56.288
  print(newDate); // prints 2022-01-07 15:30:00.000
}

I had a similar problem today and but it was to clamp to the nearest 15 mins.我今天遇到了类似的问题,但它是钳制到最近的 15 分钟。

DateTime nearestQuarter(DateTime val) {
  return DateTime(val.year, val.month, val.day, val.hour,
    15, 30, 45, 60][(val.minute / 15).floor()]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM