Help!

Hahahhah This is actually getting to be very funny!


Try that, it passed with the data you supplied.

Ok I just changed that line (reversed the variables) , and still no go! Maby I have some sort of ghost in my machine! Everyone keeps telling me that it works, and It just will not work here. Here is what I have so far:


Try running this and see what you come up with. I have hardcoded the times, so there will be no dispute on what time it is on my machine, and the reported moonrise time.


Code:
Dim CurrentTime
Dim Moonrise
Dim iDiff
Dim GetHourDif

sub main()

'CurrentTime = Time
CurrentTime = "01:21pm"

'Moonrise = hs.deviceString("m1")
Moonrise    = "09:00am"


  iDiff = DateDiff("H", CurrentTime, MoonRise)
        If CInt(iDiff) < 0 Then
           iDiff = iDiff + 24
        End If
   GetHourDif = iDiff
    hs.setDeviceString "M4",GetHourDif
' Show the total number of hours that the moon has been up.
    msgbox gethourdif
end sub

When ran with the above times, From 9 in the morning to 1 in the afternoon is simply 4 hours, it is reporting 20.

The very bottom line is, there are 2 times, what is the difference in hours between them? How, just explain to me HOW something that should be SO simple can turn into all of this! I unplugged the computer, unhooked all the cables and stopped just short of throwing the computer out my 2nd floor window! ! !
 
I am getting 20 as the results as well, but I get the correct results using the function HD posted, your code works if I use iDiff = DateDiff("H", CurrentTime, MoonRise)
 
John,

In your previous code snippet you referred to the following times:

'CurrentTime = "11:36am" ' This section should be 14 hours dif. Reports 10
'Moonrise = "01:00am"

'CurrentTime = "07:50am" ' This section should be 11 hours dif reports 13.
'Moonrise = "06:00pm"

To get the results of 14 and 11, you would have had to start counting from CurrentTime forward to MoonRise.

In the last post you referred to:
'CurrentTime = Time
CurrentTime = "01:21pm"

'Moonrise = hs.deviceString("m1")
Moonrise = "09:00am"

When ran with the above times, From 9 in the morning to 1 in the afternoon is simply 4 hours, it is reporting 20.

In the last post you started counting from MoonRise to CurrentTime. The function will only work in one direction. That is why when I asked you to switch that one line around it started working. If you start counting from CurrentTime to MoonRise in the last example it reports correctly. When do you need to start counting, MoonRise?

Someone check t make sure what I just said makes sense.

Rick
 
Rupp, Yes, I did try that code, with the code, as follows:

Code:
sub main()

'CurrentTime = Time
CurrentTime = "01:21pm"

'Moonrise = hs.deviceString("m1")
MoonRise = "09:01am"

Const numSecsInaDay = 86400

'  SUNRISE
'  Grab the hour of the sunrise ...
iHour = DatePart("h",Moonrise)

'  Convert to seconds ...
lCurrentSecs = (iHour * 60) * 60

'  Secons until midnight ...
lSecsUntilMidnight = numSecsInaDay - lCurrentSecs


'SUNSET
iHour = DatePart("h", CurrentTime)

lSecondsSoFar = (iHour * 60) * 60

lTimeDiffInSeconds = lSecsUntilMidnight + lSecondsSoFar

iTimeDiffInHours = (lTimeDiffInSeconds / 60) / 60

msgbox iTimeDiffInHours


end sub

It reports that the Moon has been up for 28 hours. This code is not made to work with variations in the AM/PM factor. It will only work if the first time is AM and the second time is PM It even makes that clear by calculating "seconds till midnight" but there is no trap for if the time is AFTER midnight.

Guys thanks for all your help, but this is obviousally one of those things that just can not be done simply. I have wasted FAR to much time on this already, as well as a lot of you guys time as well. The simple fact is, it is too dynamic to calculate easily sometimes the moonrise time is an am and sometimes a pm, and the same goes for the time.

I honestly thought it would be a simple bit of calculation to do this, I guess we were all wrong. Unless someone has the holy grail, and can make a change to any of the above code and get a correct answer with various times and AM/PM's, I am calling it quits. Now I just need to find a MUCH simpler project to work on :)

Thanks again for all the help!
 
John, honestly, that function works as advertised, so maybe there is an issue with the logic somewhere else in the script?

"Never give up, never surrender!"
 
I still don't understand why datediff works for everyone except JohnWPB.

I don't really know what you are trying to accomplish, but I think that somewhere it's being made much more complicated than it really is.

I'm sure you've stated it somewhere, but what exactly are you trying to get? I know you want the difference between two times, but
1) what are you using if for?
2) where are the times coming from?
3) when are you trying to compute the difference?
4) etc. - anything else you can add.
 
E! You have mentioned that it works for you, and everyone else. Take the script 2 messages back, and run it. It is a complete script, and will run as shown. If you get a msgbox that says 4 I will then beleive you and everyone else :)

Smee, yes it has been mentioned over an over throughout this thread:

1) what are you using if for?
To calculate the number of hours since the moon has risen using the moonrise time and the current time as a basis.

EA: Moonrise = "09:07am"
CurrentTime = "01:14pm"
With this scenario, the output should be "4"


2) where are the times coming from?
From another script that populates a virtual device, and the current time
The form of the time in the Virtual device is "Moonrise: 09:00am " I simply do a replace of "Moonrise: " to get the raw time.

3) when are you trying to compute the difference?
Every hour
4) etc. - anything else you can add.
This will take the number of hours the moon has been up since moonrise, and then use that information to display a graphical representation of the moon "height" compared to the horizon. The avarage time the moon is up per cycle is 14 hours. If it has been 7 hours since moonrise, then the graphic will display the moon dirrectly center overhead.

AGAIN:
This was just a simple idea I had, and there is no practicality to it whatsoever. Iwas was just a siimple idea that has turned into something MUCH MUCH to complicated!
Have a look at johnwpb.com the follow "MyHomeseer" and then the "Moon Phase" link on my HS site, to see foryourself how it will be used. It is of course not working at this time. The moonphase is, but the lower half of the page is not.
 
JohnWPB said:
E! You have mentioned that it works for you, and everyone else. Take the script 2 messages back, and run it. It is a complete script, and will run as shown. If you get a msgbox that says 4 I will then beleive you and everyone else :)
Again, why can't you use:
Code:
DateDiff("h", "09:01am", "01:21pm")
?

It returns 4, just like you are asking for.

Now, how to treat negative numbers is a little more complicated. The code that Harleydude and I gave above will assume that the moon has already risen. If it's 10 AM now and the moonrise time is 11 AM today (i.e., in 1 hour), the simple code (adding 24) is based on the assumption that the moon rose at 11 AM yesterday and will give you the time difference. This is not what you want. You want to indicate that the moon has not risen yet. So, you need to make sure the rest of the code knows this.

You need to determine what you want to show in your moonrise time virtual device in this situation. You may need to indicate somewhere whether the moon has risen yet or not.
 
John:

Here again, thinking "outside the box" I noticed your statement:
The form of the time in the Virtual device is "Moonrise: 09:00am " I simply do a replace of "Moonrise: " to get the raw time.

Do you think there may be a conflict in the date format when you extract the time from your device? The reason I ask is everyone (but you, hehe) says this works correctly but I think they are directly inputing the date, where you are not.

Can you try inputing the date directly in the script maybe to try it out?!?

Not sure if I'm helping or hurting, just trying to give some additional troubleshooting ideas :)
 
I actually noticed that there is no space between the time and am/pm in his example (by default there is when you use the time command), but I removed that space in my script, and it still worked, so I really think the problem is with the logic in the rest of the script (as smee mentioned). John, we are going to get this working, this really isn't complicated, so we can't just let it go :)
 
Can the whole process be simplified? What about something like this. This duplicates some of what you must be doing.

1) Create a function called Compute_moon_rise_and_set. This function should take the current time AND date (or have an argument for it) and compute the next moonrise and moonset times. These times should be saved in two virtual devices: moonrise_time and moonset_time. You should save the complete date and time (and I wouldn't put any labels (like "moonrise") - labels can be added when you display it).

2) Add an event that runs at moonset. This can be an event that runs every 15 minutes (or so) and checks "Is the current time/date after moonset? If so, run Compute_moon_rise_and_set.". This will update the moonrise and moonset times after each moon set.

3) Whenever you need info, do something like this (where Time = the current time and date):
Code:
if (time < moonrise) then
   result = "The moon will rise in " & datediff("h",time,moonrise) & " hours."
elseif (time < moonset) then
   result = "The moon rose " & datediff("h",moonrise,time) & " hours ago and will set in " & datediff("h",time,moonset) & "hours."
end if
Obviously, you can do whatever you want inside the clauses of the "if". You can use this kind of check whenever you want to use the data. If you want to compute the height of the moon, then if (time < moonrise) you don't show anything. If (time > moonrise) and (time < moonset) you compute the height based on your datediff number.

If you are concerned about the possible 15 minute error, you can call Compute_moon_rise_and_set before the example if statement in 3).

For this too work, you should keep the date in all the times. You can remove it when you display the numbers (on a web page). But keep it in the original data. I think this is very important because the moon rise/set times don't follow a clean once-a-day cycle.
 
Wow, this is still just going in circles :)

Smee: You are right if you use:

Code:
DateDiff("h", "09:01am", "01:21pm")

It WILL return 4.

NOW try are reverse the times as follows:
Code:
 this = DateDiff("h", "09:01pm", "01:21am")
msgbox this

and it will return -20, when in fact only 4 hours has passed

You also said:
Now, how to treat negative numbers is a little more complicated. The code that Harleydude and I gave above will assume that the moon has already risen.
EXACTLY! This "assumption" means that the code will not work half of the time, or even if the fisrt time is before midnight, and the second time after midnight for that matter.

To repeat this for about the 6th time in this thread, the problem occurs when the 2nd time is after midnight!

Rupp:
Without the date the Datepart isn't going to work. Why can't we use the date?

This should answer that question :(

the 2 variables I have to work with are the hour and the ampm state, and do not include the date.


HarleyDude: I am not sure the dateDif would even work in this situation as there are NO dates to deal with. The only 2 variables are the Moonrise & set TIMES.

Shawn: As mentioned quite a few times, in a few posts before I do NOT have any dates to work with here. The only thing I have to work with are simply a beginning time, and an ending time.

Also note, that if the moonrise hour is AFTER the current time, it should not
come up with the number of hours up, as the moon has not risen yet. Without a date, the datedif command does not know this, and will just subtract it anyway....

How about this, instead of code snippits posted in here, how about taking what is here, and doing the complete code to see if you can get it to work on your end.

Here are the rules again:

MoonriseTime = [any time throughour the day]
CurrentTime = current system time] (may be before or after the moonrise time. If the moonrise time falls after the system time, we know its not up.)
either of the times can be before or after midnight
MoonHours = should simply be the number of hours the moon has been up. If the moon is NOT up it should be 0. (The moon is concidered down the currrent time is > 14 hours of the MoonRise time).
 
JohnWPB said:
You also said:
Now, how to treat negative numbers is a little more complicated. The code that Harleydude and I gave above will assume that the moon has already risen.
EXACTLY! This "assumption" means that the code will not work half of the time, or even if the fisrt time is before midnight, and the second time after midnight for that matter.
The problem is not because of midnight. If you add 24 when the diff is negative, you will get the right number (like in harleydude's and my previous examples). This works fine across midnight.

I think the only problem is how you are treating the times. I think you should include the dates and pay attention to what is happening. The problem may be that if you look at today's moonrise and moonset, you do not know what order they occur in. That is why in my last explanation, I said you should look at the next moonrise and moonset (no matter what day they occur on) - calculated after moonset. This will eliminate any confusion.

Remember that on any given day, the moonrise time may be before or after the moonset time. Also, some days will not have one or the other.
 
Back
Top