public bool fallsInbetween(Time24Hours A, Time24Hours B) { int AtoInt = (10000 * A.Hours) + (100 * A.Minutes) + (A.Seconds); int BtoInt = (10000 * B.Hours) + (100 * B.Minutes) + (B.Seconds); int thistoInt = (10000 * this.Hours) + (100 * this.Minutes) + (this.Seconds); if (AtoInt == BtoInt) //Should never happen { return(false); } if (AtoInt < BtoInt) //Takes place in the same day cycle { if (thistoInt > AtoInt && thistoInt < BtoInt) { return(true); } } else //B takes place tomorrow { if (thistoInt > AtoInt || thistoInt < BtoInt) { return(true); } } return(false); }
public static Time24Hours stringTo24HTime(String toConvert, char splitBy) { if (toConvert == null) { return(null); } String[] split = toConvert.Split(splitBy); int[] data = new int[3] { 0, 0, 0 }; if (split.Length != 3) { return(null); } if (!int.TryParse(split[0], out data[0]) || !int.TryParse(split[1], out data[1]) || !int.TryParse(split[2], out data[2])) { return(null); } Time24Hours toReturn = new Time24Hours(); toReturn.Hours = data[0]; toReturn.Minutes = data[1]; toReturn.Seconds = data[2]; if (!toReturn.isValid()) { return(null); } return(toReturn); }
public static Time24Hours null24Time() //For when you don't want to throw null around, but need some form of null { Time24Hours nTime = new Time24Hours(); nTime.Hours = -1; nTime.Minutes = -1; nTime.Seconds = -1; return(nTime); }
/* * Button1_Click: * When a new request is added, the changes are first reflected in the savefile, * then the form is updated based on the savefile. There is likely a better way to do this, * but it ensures the save file is always up to date. */ private void Button1_Click(object sender, EventArgs e) //As soon as the BrightnessRequest is added, changes are reflected in the file { running.stopThread(); FileManager fm = new FileManager(fileLoc, fileNameR); String[] rawData = null; double inputBrightness = 1.0; //If the input is not valid if (!Time24Hours.isValid24HTime(textBox1.Text, ':') || !Time24Hours.isValid24HTime(textBox2.Text, ':') || !double.TryParse(textBox3.Text, out inputBrightness)) { inputError1(); return; } //Convert user input to time values Time24Hours startT = Time24Hours.stringTo24HTime(textBox1.Text + ":00", ':'); Time24Hours endT = Time24Hours.stringTo24HTime(textBox2.Text + ":00", ':'); //Assemble request BrightnessRequest newReq = BrightnessRequest.fromString(0, startT.toFileString() + ";" + endT.toFileString() + ";" + inputBrightness.ToString()); if (newReq != null && !newReq.isValid()) //Check if request is valid { inputError1(); return; } foreach (BrightnessRequest BR in requests) { if (newReq.getStartTime().fallsInbetween(BR.getStartTime(), BR.getEndTime()) || newReq.getEndTime().fallsInbetween(BR.getStartTime(), BR.getEndTime())) { inputError2(BR.orderNum); return; } if (newReq.getStartTime().equals(BR.getStartTime()) || newReq.getEndTime().equals(BR.getEndTime())) { inputError2(BR.orderNum); return; } } requests.Add(newReq); //Add request to list fm.saveAllBR(requests); //Save all requests fm.initialize(ref rawData); //Load all requests to string loadRequests(rawData); //Overwrite displayEvents(); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); running.startThread(); }
public bool equals(Time24Hours A) { int thistoInt = (10000 * this.Hours) + (100 * this.Minutes) + (this.Seconds); int AtoInt = (10000 * A.Hours) + (100 * A.Minutes) + (A.Seconds); if (thistoInt == AtoInt) { return(true); } return(false); }
private void serviceRequests() { if (toService == null) { return; } try { while (true) { if (useRR && isValidRefreshRate(refreshRate)) { System.Threading.Thread.Sleep(refreshRate); } else { System.Threading.Thread.Sleep(3000); } Console.WriteLine("TICK\n"); DateTime localDT = DateTime.Now; Time24Hours localTime = Time24Hours.stringTo24HTime(localDT.ToString("HH:mm:ss"), ':'); if (localTime != null) { bool defaultB = true; foreach (BrightnessRequest BR in toService) { if (localTime.fallsInbetween(BR.getStartTime(), BR.getEndTime())) //If the current time is in any request in toService { setBrightness(BR.getBrightness()); defaultB = false; } } if (defaultB && useDefBright) { setBrightness(this.defaultBrightness); } } } } catch (ThreadInterruptedException) { return; } }
public static BrightnessRequest fromString(int orderN, String toConvert) { if (toConvert == null) { return(null); } BrightnessRequest toReturn = new BrightnessRequest(); toReturn.orderNum = orderN; String[] split = toConvert.Split(';'); if (split.Length != 3) { return(null); } Time24Hours startT = Time24Hours.stringTo24HTime(split[0], ','); Time24Hours endT = Time24Hours.stringTo24HTime(split[1], ','); int lightL = 1; //We never want the light level to default to 0% if (!int.TryParse(split[2], out lightL)) { return(null); } if (lightL < 0 || lightL > 100) { return(null); } if (startT == null || endT == null) { return(null); } if (!startT.isValid() || !endT.isValid()) { return(null); } toReturn.startTime = startT; toReturn.endTime = endT; toReturn.brightness = lightL; return(toReturn); }