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

Aym*_*aly 2 time dart flutter

我想检查当前时间是否在我的开放时间和关闭时间之间,知道关闭时间有时可能是凌晨 2 点,开放时间是凌晨 3 点,例如,我一直在尝试逻辑地处理这个问题已经两周了,我无法理解它,这是我迄今为止最好的尝试:

  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");
    }

  }
Run Code Online (Sandbox Code Playgroud)

OM *_*ALE 6

//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;

  }
Run Code Online (Sandbox Code Playgroud)