Check If System Month IS Between A Certain Range
I'm working on using some if statement to check if the system month is between a certain range to sort of compensate for daylight savings times.
if (sysMonth == 01) {
yield return new WaitForSeconds (0.5f); // wait time
VoiceBox VBbr = FindObjectOfType<VoiceBox> ();
VBbr.BrightnessDetected ();
MessageCentreManager MCMbd = FindObjectOfType<MessageCentreManager> ();
MCMbd.GoBrightnessDetectedMessage ();
print ("Brilliant Light Detected Michael");
}
Something like that. I'm wondering if I'm on the right track and I need something like:
if (sysMonth >=10 && sysMonth <= 02) //October to Feb.
Am I on the right track with that or way off the mark? I always have a hard time wrapping my head around that stuff ;)
I think this is the end result I'm looking for? It's a real "Brain Baker" but I think it's logical? Correct me if I'm wrong. ;)
//Send Brightness Detected Audio Resoponse Request to Voice Box Object
IEnumerator BrightnessResponse(){
//Check System Time if its between 5P$$anonymous$$ - 7A$$anonymous$$ then play message
if (sysHour >= 17 && sysHour <= 7) {
//Check $$anonymous$$onths For Winter
if (sys$$anonymous$$onth >= 01 && sys$$anonymous$$onth <= 10) {
yield return new WaitForSeconds (0.5f); // wait time
VoiceBox VBbr = FindObjectOfType<VoiceBox> ();
VBbr.BrightnessDetected ();
//Send $$anonymous$$essage for $$anonymous$$essage Centre to display "BRHT LGHT DETECTED" message
$$anonymous$$essageCentre$$anonymous$$anager $$anonymous$$C$$anonymous$$bd = FindObjectOfType<$$anonymous$$essageCentre$$anonymous$$anager> ();
$$anonymous$$C$$anonymous$$bd.GoBrightnessDetected$$anonymous$$essage ();
print ("Brilliant Light Detected $$anonymous$$ichael - WINTER AT NIGHT");
}
}
//Summer $$anonymous$$onths Time Check
//Check System Time if its between 5A$$anonymous$$ - 9P$$anonymous$$ then play message
if (sysHour >= 21 && sysHour <= 5) {
//Check $$anonymous$$onths For Summer
if (sys$$anonymous$$onth >= 03 && sys$$anonymous$$onth <= 09) { //$$anonymous$$ar. to Sept.
yield return new WaitForSeconds (0.5f); // wait time
VoiceBox VBbr = FindObjectOfType<VoiceBox> ();
VBbr.BrightnessDetected ();
//Send $$anonymous$$essage for $$anonymous$$essage Centre to display "BRHT LGHT DETECTED" message
$$anonymous$$essageCentre$$anonymous$$anager $$anonymous$$C$$anonymous$$bd = FindObjectOfType<$$anonymous$$essageCentre$$anonymous$$anager> ();
$$anonymous$$C$$anonymous$$bd.GoBrightnessDetected$$anonymous$$essage ();
print ("Brilliant Light Detected $$anonymous$$ichael - SU$$anonymous$$$$anonymous$$ER AT NIGHT");
}
}
}
//Send Darkness Detected Audio Resoponse Request to Voice Box Object
IEnumerator DarknessResponse(){
//Check System Time if its between 7Am - 5P$$anonymous$$ then play message
if (sysHour >= 7 && sysHour <= 17) {
//Check $$anonymous$$onths For Winter
if (sys$$anonymous$$onth <= 02 && sys$$anonymous$$onth <= 10) { //Feb. to Oct.
yield return new WaitForSeconds (0.5f); // wait time
VoiceBox VBdr = FindObjectOfType<VoiceBox> ();
VBdr.DarknessDetected ();
//Send $$anonymous$$essage for $$anonymous$$essage Centre to display "A$$anonymous$$B LGHT INSUF" message
$$anonymous$$essageCentre$$anonymous$$anager $$anonymous$$C$$anonymous$$dd = FindObjectOfType<$$anonymous$$essageCentre$$anonymous$$anager> ();
$$anonymous$$C$$anonymous$$dd.GoDarknessDetected$$anonymous$$essage ();
print ("Darkness $$anonymous$$ust Not Be Naturally Occuring $$anonymous$$ichael - WINTER AT DAY");
}
}
//Summer $$anonymous$$onths Time Check
//Check System Time if its between 5Am - 9P$$anonymous$$ then play message
if (sysHour >= 5 && sysHour <= 21) {
//Check $$anonymous$$onths For Summer
if (sys$$anonymous$$onth >= 03 && sys$$anonymous$$onth <= 09) { //$$anonymous$$ar. to Sept.
yield return new WaitForSeconds (0.5f); // wait time
VoiceBox VBdr = FindObjectOfType<VoiceBox> ();
VBdr.DarknessDetected ();
//Send $$anonymous$$essage for $$anonymous$$essage Centre to display "A$$anonymous$$B LGHT INSUF" message
$$anonymous$$essageCentre$$anonymous$$anager $$anonymous$$C$$anonymous$$dd = FindObjectOfType<$$anonymous$$essageCentre$$anonymous$$anager> ();
$$anonymous$$C$$anonymous$$dd.GoDarknessDetected$$anonymous$$essage ();
print ("Darkness $$anonymous$$ust Not Be Naturally Occuring $$anonymous$$ichael - SU$$anonymous$$$$anonymous$$ER AT DAY");
}
}
}
Your if statements are a bit screwy.
if (sys$$anonymous$$onth <= 2 && sys$$anonymous$$onth <= 10) will only ever be true for values <= 2
Should be (>= 2 && <= 10) so the value you are looking for is greater than or equal to your lowest value 2 and less than or equal to your highest value 10.
Thanks @Dave29483, Yeah when come to figuring out the correct values for the month range I want I get really messed up for some reason. Now if I change it to this:
(sys$$anonymous$$onth >= 2 && sys$$anonymous$$onth <= 10) { //Jan. to Oct.*/
I don't get my darkness response firing, correct me if I'm wrong but is that not because the first part thinks I want the system month to be greater than 2 (Feb.) when it is currently 1 (Jan.) I would have thought this would be right:
(sys$$anonymous$$onth <= 2 && sys$$anonymous$$onth <= 10) { //Jan. to Oct.*/
Because then System month is less but equal to 2 (Below Jan & Feb.)?? Ahh this stuff bakes my brain lol
$$anonymous$$eep in $$anonymous$$d I'm up here in Canada so you might be getting the month times confused ;)
Why not use DateTime
?
if (DateTime.Now.IsDaylightSavingTime())
{
// Shenanigans!
}
Oh hey @$$anonymous$$nemotic, thanks I was not aware I could even do that. lol now I'm not sure how I would integrate that into what I have here, can you give a simple example and then I can take the ball and run with it, lol ball not scissors ;)
Answer by Danixos · Jan 16, 2016 at 10:15 PM
You can determine whether DST is active in the current time zone by calling DateTime.Now.IsDaylightSavingTime()
. It returns true
if DST is active. You can find DST delta (that is, how much time is advanced when DST is active) by accessing TimeZone.CurrentTimezone.GetDaylightChanges(DateTime.Now.Year).Delta
. All this is from System
namespace.
DateTime now = DateTime.Now;
TimeZone tz = TimeZone.CurrentTimeZone;
TimeSpan delta = tz.GetDaylightChanges(now.Year).Delta;
bool isDST = now.IsDaylightSavingTime();
Then you can make your adjustments based on this.
HTH
Thanks @$$anonymous$$nemotic, it's going to take me a little time to figure that out but that looks awesome! I had no idea I could do that in Unity :)
You're welcome. :) Take a peek into standard .NET namespaces whenever your trying to solve a problem that isn't gamedev-specific. There's a lot of good stuff in there.
Awesome @$$anonymous$$nemotic I'll do that. I just need to figure out how to work this into what I have, I think I have an idea but I'll see... ;)
Answer by KnightRiderGuy · Jan 16, 2016 at 03:57 PM
I wonder if what I need is to use the || "or" instead of && "Or" and so that the argument is something like:
(sysMonth <= 2 || sysMonth >= 10) { //Jan. to Oct.
So that System Month is Less or equals to 2 (less than or equals to February) OR System month is greater than or equals to 10 (October) That way system month has to be greater than 10 to be beyond or past October? Maybe I'm not fully understanding it right but in my head that sound logical, Am I baked in assuming that?