gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 09:05:14 26 4
gpt4 key购买 nike

我想检查当前时间是否在我的开门时间和我的关门时间之间,知道关门时间有时可能是凌晨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");
}

}

最佳答案

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

}

关于Flutter - 检查当前时间是否在给定的每小时范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67455657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com