Author |
|
tbeckman Senior Member
Joined: December 30 2007 Location: United States
Online Status: Offline Posts: 149
|
Posted: October 10 2008 at 19:57 | IP Logged
|
|
|
I have reviewed examples on this forum and can seem to find one using the combination below... but I have tried this and it is not working for me... I am using the following boolean in a trigger to trigger if the time is after 10pm and before sunrise... can anyone take a look at what I am trying to do and see if my boolean format looks correct. Thank you.
if(NOW()> 22 AND NOW() < RELATIVETIME( 00:00:00, [SUNRISE] ), 1, 0)
|
Back to Top |
|
|
jbbtex Senior Member
Joined: February 15 2007 Location: United States
Online Status: Offline Posts: 181
|
Posted: October 10 2008 at 21:51 | IP Logged
|
|
|
For the first condtion try - HOUR(NOW())>22
__________________ Brady
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity." - Gen. George S. Patton
|
Back to Top |
|
|
JaredM Newbie
Joined: November 04 2007
Online Status: Offline Posts: 36
|
Posted: October 10 2008 at 21:54 | IP Logged
|
|
|
And you'll also want to make it >= 22 otherwise it won't fire until 11pm or later and change the AND to an OR.
if(HOUR(NOW())>= 22 OR NOW() < RELATIVETIME( 00:00:00, [SUNRISE] ), 1, 0)
|
Back to Top |
|
|
BeachBum Super User
Joined: April 11 2007 Location: United States
Online Status: Offline Posts: 1880
|
Posted: October 10 2008 at 22:00 | IP Logged
|
|
|
Like Brady you may also want to modify the NOW()> 22 to NOW() > 22:00:00
__________________ Pete - X10 Oldie
|
Back to Top |
|
|
grif091 Super User
Joined: March 26 2008 Location: United States
Online Status: Offline Posts: 1357
|
Posted: October 11 2008 at 00:03 | IP Logged
|
|
|
I believe this covers the conditions you documented. After 22:00:00 and before Sunrise. There are some issues when comparing time with an IF. The time(“22”) function returns hh:mm:ss 22:00:00; the now() function returns hh:mm:ss:mmm (down to milliseconds’). I chose to put all times into hh:mm:ss format so the IF comparison would be comparing apples to apples. I have successfully run this statement with the Formula Builder. Also tested it across midnight. It returns 1 (true) from 22:00:00 + 1 second to SUNRISE - 1 second. It returns 0 (false) outside that time range.
The first part of the IF checks for the current time being between 22:00:00 and 23:59:59. If current time is outside that range the IF checks for the current time being before SUNRISE.
if((ph_relativetime(now(),0)>time("22") and ph_relativetime(now(),0)<=time("23:59:59")) or ph_relativetime(now(),0)< ph_relativetime(time("00"),long(ph_getsystemvar("SUNRISE"))) ,1,0)
EDIT:
The "and" part of the time check, <= 23:59:59, is not necessary and can be removed. Old habit of defensive programming. I'm sure someone would point that out eventually.
Tested published statement across sunrise, no surprises. Sunrise is 07:17:34 here. Returned 1 (true) until sunrise, at which point the IF started retuning 0 (false).
Edited by grif091 - October 11 2008 at 07:19
__________________ Lee G
|
Back to Top |
|
|
grif091 Super User
Joined: March 26 2008 Location: United States
Online Status: Offline Posts: 1357
|
Posted: October 12 2008 at 00:02 | IP Logged
|
|
|
Sorry folks, this is what happens when you don’t read carefully all the previous posts. The original post from tbeckman with JaredM changes is much simpler, easier to understand, and it works!
__________________ Lee G
|
Back to Top |
|
|
tbeckman Senior Member
Joined: December 30 2007 Location: United States
Online Status: Offline Posts: 149
|
Posted: October 12 2008 at 12:03 | IP Logged
|
|
|
Thank you all for your help... I applied the if statement to my boolean field statement and it works...THANK YOU. However, I don't really understand why I would use and OR instead of an AND. An AND seems to make more sense to me because I would want both statements to be TRUE. It would seem using and OR in this statement would make it TRUE all the time. Is it after 10 or before sunrise? Seems like this would always be TRUE. Why would I use an OR? Thank you.
|
Back to Top |
|
|
BeachBum Super User
Joined: April 11 2007 Location: United States
Online Status: Offline Posts: 1880
|
Posted: October 12 2008 at 12:44 | IP Logged
|
|
|
I don’t know how the OR would work.. If is was after 22:00 period the condition would be met or if it was before sunrise and before a 23:59 time period the condition would be met because of RelativeTime which means the 22:00 did not apply. Lee, you know more about this than I do, chime in..
__________________ Pete - X10 Oldie
|
Back to Top |
|
|
grif091 Super User
Joined: March 26 2008 Location: United States
Online Status: Offline Posts: 1357
|
Posted: October 12 2008 at 13:08 | IP Logged
|
|
|
It has to do with how time is represented. At 10 PM the time is 22:00:00. The first part of the IF compare returns True for times from 10 PM to 11:59:59 PM (22:00:00 through 23:59:59). At midnight the time is 00:00:00, at 1 AM the time is 01:00:00, and so on. A time of 1 AM, 01:00:00, is not greater numerically than 10 PM, 22:00:00, therefore the first comparison is False, making the entire IF False when the conditions are ANDed together. All conditions of an AND must be True for the IF to return True. The second part of the IF, the OR condition, covers the times from midnight to SUNRISE. The IF is read, if the time is from 22:00:00 to 1 second before midnight, OR from midnight to SUNRISE, return True, otherwise False. Once the time passes SUNRISE, the IF returns False because both comparisons are False. When the time reaches 10 PM again, the first part of the IF is True, causing the IF to return True because the comparisons are ORed, Either comparison being True returns True.
__________________ Lee G
|
Back to Top |
|
|
JaredM Newbie
Joined: November 04 2007
Online Status: Offline Posts: 36
|
Posted: October 12 2008 at 13:12 | IP Logged
|
|
|
Tbeckman, I had the same thought at first when I was messing with it. But is has to be OR.
if(HOUR(NOW())>= 22 OR NOW() < RELATIVETIME(00:00:00, [SUNRISE]), 1, 0)
The reason it's an OR is that both statements will never be true in the same day. In a given day -- in one 24hr period -- the time can't be both after 10pm (>22) and before 7am (<7) at the same time. NOW() resets to 0 every day, so this is why OR works and AND doesn't.
|
Back to Top |
|
|
BeachBum Super User
Joined: April 11 2007 Location: United States
Online Status: Offline Posts: 1880
|
Posted: October 12 2008 at 13:19 | IP Logged
|
|
|
I must be confused then. I thought RelativeTime was a 24 hour period before. From the Help menu:
relativetime PowerHome formula function
Description
Obtains a time that occurs a specified number of seconds after or before another time within a 24-hour period.
__________________ Pete - X10 Oldie
|
Back to Top |
|
|
tbeckman Senior Member
Joined: December 30 2007 Location: United States
Online Status: Offline Posts: 149
|
Posted: October 12 2008 at 15:07 | IP Logged
|
|
|
Thank you for this explaination... this makes sense.
JaredM wrote:
Tbeckman, I had the same thought at first when I was messing with it. But is has to be OR.
if(HOUR(NOW())>= 22 OR NOW() < RELATIVETIME(00:00:00, [SUNRISE]), 1, 0)
The reason it's an OR is that both statements will never be true in the same day. In a given day -- in one 24hr period -- the time can't be both after 10pm (>22) and before 7am (<7) at the same time. NOW() resets to 0 every day, so this is why OR works and AND doesn't. |
|
|
|
Back to Top |
|
|
BeachBum Super User
Joined: April 11 2007 Location: United States
Online Status: Offline Posts: 1880
|
Posted: October 12 2008 at 15:12 | IP Logged
|
|
|
To answer my own question I was confused. Pea brain finally got it worked out. Back to the beach.
__________________ Pete - X10 Oldie
|
Back to Top |
|
|