Flutter-检查当前时间是否在给定的小时范围内

cxfofazt  于 2023-02-13  发布在  Flutter
关注(0)|答案(5)|浏览(250)

我想检查当前时间是否在我的开放时间和关闭时间之间,知道关闭时间有时是凌晨2点,开放时间是凌晨3点,例如,我已经尝试逻辑地处理这个问题2周了,现在我不能绕着它缠绕我的头,这是我最好的尝试:

open = new DateTime(now.year, now.month, now.day, open.hour, open.minute);
  close = new DateTime(now.year, now.month, now.day, close.hour, close.minute);
  midnight = new DateTime(now.year, now.month, now.day, midnight.hour, midnight.minute);

  if(close.hour > midnight.hour && close.hour < open.hour){

   
    if(now.hour < midnight.hour){
      DateTime theClose = new DateTime(now.year, now.month, now.day + 1, close.hour, close.minute);

    

      if(now.isBefore(theClose) && now.isAfter(open)){
        sendIt(context, notes);
      }else{
    
        _showToast("this branch is closed right now");
      }

    }else{
      open = new DateTime(now.year, now.month, now.day - 1, open.hour, open.minute);

      if(now.isBefore(close) && now.isAfter(open)){
        sendIt(context, notes);
      }else{
  
        _showToast("this branch is closed right now");
      }

    }

  }else{

    if(now.isBefore(close) && now.isAfter(open)){
      sendIt(context, notes);

    }else{
 
      _showToast("this branch is closed right now");
    }

  }
plicqrtu

plicqrtu1#

//checks if restaurant is open or closed 
// returns true if current time is in between given timestamps
//openTime HH:MMAM or HH:MMPM same for closedTime
bool checkRestaurentStatus(String openTime, String closedTime) {
    //NOTE: Time should be as given format only
    //10:00PM
    //10:00AM

    // 01:60PM ->13:60
    //Hrs:Min
    //if AM then its ok but if PM then? 12+time (12+10=22)

    TimeOfDay timeNow = TimeOfDay.now();
    String openHr = openTime.substring(0, 2);
    String openMin = openTime.substring(3, 5);
    String openAmPm = openTime.substring(5);
    TimeOfDay timeOpen;
    if (openAmPm == "AM") {
      //am case
      if (openHr == "12") {
        //if 12AM then time is 00
        timeOpen = TimeOfDay(hour: 00, minute: int.parse(openMin));
      } else {
        timeOpen =
            TimeOfDay(hour: int.parse(openHr), minute: int.parse(openMin));
      }
    } else {
      //pm case
      if (openHr == "12") {
//if 12PM means as it is
        timeOpen =
            TimeOfDay(hour: int.parse(openHr), minute: int.parse(openMin));
      } else {
//add +12 to conv time to 24hr format
        timeOpen =
            TimeOfDay(hour: int.parse(openHr) + 12, minute: int.parse(openMin));
      }
    }

    String closeHr = closedTime.substring(0, 2);
    String closeMin = closedTime.substring(3, 5);
    String closeAmPm = closedTime.substring(5);

    TimeOfDay timeClose;

    if (closeAmPm == "AM") {
      //am case
      if (closeHr == "12") {
        timeClose = TimeOfDay(hour: 0, minute: int.parse(closeMin));
      } else {
        timeClose =
            TimeOfDay(hour: int.parse(closeHr), minute: int.parse(closeMin));
      }
    } else {
      //pm case
      if (closeHr == "12") {
        timeClose =
            TimeOfDay(hour: int.parse(closeHr), minute: int.parse(closeMin));
      } else {
        timeClose = TimeOfDay(
            hour: int.parse(closeHr) + 12, minute: int.parse(closeMin));
      }
    }

    int nowInMinutes = timeNow.hour * 60 + timeNow.minute;
    int openTimeInMinutes = timeOpen.hour * 60 + timeOpen.minute;
    int closeTimeInMinutes = timeClose.hour * 60 + timeClose.minute;

//handling day change ie pm to am
    if ((closeTimeInMinutes - openTimeInMinutes) < 0) {
      closeTimeInMinutes = closeTimeInMinutes + 1440;
      if (nowInMinutes >= 0 && nowInMinutes < openTimeInMinutes) {
        nowInMinutes = nowInMinutes + 1440;
      }
      if (openTimeInMinutes < nowInMinutes &&
          nowInMinutes < closeTimeInMinutes) {
        return true;
      }
    } else if (openTimeInMinutes < nowInMinutes &&
        nowInMinutes < closeTimeInMinutes) {
      return true;
    }

    return false;

  }
r1wp621o

r1wp621o2#

bool isValidTimeRange(TimeOfDay startTime, TimeOfDay endTime) {
    TimeOfDay now = TimeOfDay.now();
    return ((now.hour > startTime.hour) || (now.hour == startTime.hour && now.minute >= startTime.minute))
        && ((now.hour < endTime.hour) || (now.hour == endTime.hour && now.minute <= endTime.minute));
  }
pb3skfrl

pb3skfrl3#

正如您所注意到的,在我们的例子中使用DateTime并不是最好的解决方案,因为它依赖于月/年/日,相反,我们可以使用TimeOfDay类,它不依赖于特定的日期,而只依赖于时间:

List<TimeOfDay> openingTimeRange = [TimeOfDay(hour: 2, minute: 30), TimeOfDay(hour: 15, minute: 45)]; // as an example
bool isOpen(List<TimeOfDay> openingTimeRange) {
   TimeOfDay now = TimeOfDay.now();
   return now.hour >= openingTimeRange[0].hour
      && now.minute >= openingTimeRange[0].minute
      && now.hour <= openingTimeRange[1].hour
      && now.minute <= openingTimeRange[1].minute;
}
1zmg4dgp

1zmg4dgp4#

如果你的时间格式是24小时,我有一个简单的解决方案,因为你不需要任何外部库。

bool _getStoreOpenStatus(String openTime, String closeTime) {
    bool result = false;

    DateTime now = DateTime.now();
    int nowHour = now.hour;
    int nowMin = now.minute;

    print('Now: H$nowHour M$nowMin');

    var openTimes = openTime.split(":");
    int openHour = int.parse(openTimes[0]);
    int openMin = int.parse(openTimes[1]);

    print('OpenTimes: H$openHour M$openMin');

    var closeTimes = closeTime.split(":");
    int closeHour = int.parse(closeTimes[0]);
    int closeMin = int.parse(closeTimes[1]);

    print('CloseTimes: H$closeHour M$closeMin');

    if(nowHour >= openHour && nowHour <= closeHour) {
      if(nowMin > openMin && nowMin < closeMin) result = true;
    }

    return result;
  }
sirbozc5

sirbozc55#

如果您使用数字格式[0..23]表示小时,则可以将时间转换为当天过去的秒数。对要检查的范围执行相同操作,查看当前过去的秒数是否在两个数字范围(秒)之间:

TimeOfDay now = TimeOfDay.now(); // or DateTime object
TimeOfDay openingTime = TimeOfDay(hours: ??, minutes:??); // or leave as DateTime object
TimeOfDay closingTime = TimeOfDay(hours: ??, minutes:??); // or leave as DateTime object

int shopOpenTimeInSeconds = openingTime.hour * 60 + openingTime.minute;
int shopCloseTimeInSeconds = closingTime.hour * 60 + closingTime.minute;
int timeNowInSeconds = now.hour * 60 + now.minute;

if (shopOpenTimeInSeconds <= timeNowInSeconds &&
    timeNowInSeconds <= shopCloseTimeInSeconds) {
  // OPEN;
} else {
  // CLOSED;
}

相关问题