1. datetime:diff() question
- Posted by jessedavis Dec 08, 2017
- 1441 views
I think there is a problem with the documentation for datetime:diff()
From the euManual: 8.4.5.19 diff(dt1,dt2)
include std/datetime.e include std/console.e include std/os.e include std/convert.e datetime dt1 = datetime:now() sleep(2) -- sleep for 2 seconds datetime dt2 = datetime:now() puts(1,"datetime dt1 = " & to_string(dt1) & 10) puts(1,"datetime dt2 = " & to_string(dt2) & 10) atom i = diff(dt1, dt2) -- i is 2 --dt2 is subtracted from dt1 from euManual 8.4.5.19 diff printf(1,"i = %d\n",i) any_key("Done...")
Now, if a bigger number is subtracted from a smaller number the difference is negative; however, according to the program output the difference is positive.
datetime dt1 = {2017,12,8,9,51,41} datetime dt2 = {2017,12,8,9,51,43} i = 2 Done...Oh, well!
2. Re: datetime:diff() question
- Posted by Lnettnay Dec 08, 2017
- 1394 views
The docs are wrong! dt1 is subtracted from dt2
This is from std/datetime.e
public function diff(datetime dt1, datetime dt2) return datetimeToSeconds(dt2) - datetimeToSeconds(dt1) end function
The example in the manual is correct.
Lonny
3. Re: datetime:diff() question
- Posted by ghaberek (admin) Dec 08, 2017
- 1416 views
The manual says this:
But the code says this:
public function diff(datetime dt1, datetime dt2) return datetimeToSeconds(dt2) - datetimeToSeconds(dt1) end function
If I am indeed "subtracting dt2 from dt1" then I would write that as dt1 - dt2.
I am not sure which is supposed to be correct, the code or the manual.
-Greg
4. Re: datetime:diff() question
- Posted by _tom (admin) Dec 08, 2017
- 1383 views
arguments are described as:
diff( end_time, start_time )
- documentation says calculation is: end_time - start_time
- source-code says calculation is: start_time - end_time
Something is backwards.
At the moment the documentation is wrong because it has to describe the source-code.
The question is now: what is the expected order of arguments in the diff function?
for those that believe that time moves in one direction only
- One could easily argue that diff( start_time, end_time ) makes sense; the return should be positive since you "expect" end_time to come after start_time.
for those just doing arithmetic
- The alternative ##diff( end_time, start_time) is more like "arithmetic". This looks like the original intent of this function.
Which one do we like better?
_tom
5. Re: datetime:diff() question
- Posted by _tom (admin) Dec 08, 2017
- 1378 views
From wikipedia I get:
minuend minus subtrahend equals difference 4 - 6 = - 2 (verbally, "four minus six equals negative two")
same as: difference equals minuend minus subtrahend
as a function you would say ... difference( minuend, subtrahend )
diff( end_time, start_time )is what was intended
_tom
6. Re: datetime:diff() question
- Posted by petelomax Dec 08, 2017
- 1419 views
Something is backwards.
Agreed
The question is now: what is the expected order of arguments in the diff function?
diff( start_time, end_time )
That one.
Feel free to compare/copy the Phix docs: http://phix.x10.mx/docs/html/timedate_diff.htm
Pete
7. Re: datetime:diff() question
- Posted by Ekhnat0n Dec 08, 2017
- 1388 views
It's all a matter of semantics (and logic) Neither manual nor sourcecode are wrong because they do not contradict each other
The problem is that jesse's vars dt1 and dt2 bear the same name as the arguments INSIDE the function diff(arg1, arg2), however, and if they had been named start and stop no misunderstanding would have risen.
If you are asked if val1 is lower or higher than val2, you will answer heigher in the case of TS.
Indicating a positive difference between them, but in order to achieve that, diff() must calculate arg2-arg1
to show that the 2nd is highest.
The scope of dt1 and dt2 inside diff() is limited to the function, and that's causing the problem for this particular case.
8. Re: datetime:diff() question
- Posted by jessedavis Dec 08, 2017
- 1405 views
The problem is that jesse's vars dt1 and dt2 bear the same name as the arguments INSIDE the function diff(arg1, arg2), however, and if they had been named start and stop no misunderstanding would have risen.
If you are asked if val1 is lower or higher than val2, you will answer heigher in the case of TS.
Indicating a positive difference between them, but in order to achieve that, diff() must calculate arg2-arg1
to show that the 2nd is highest.
The scope of dt1 and dt2 inside diff() is limited to the function, and that's causing the problem for this particular case.
I am not concerned with what is "inside" the function. I want the answer to be as predicted by the documentation. As to scope, my variables are totally unrelated to those inside the function. Having the same name was a chance occurrence. In my universe the meaning of the statement "subtract 1 from 2 means: ans = 2 - 1. Start, Stop, Beginning, End, etc. have no meaning and time's arrow does flow only in one direction (to the right in my universe); however, if one number is bigger than another only one answer for the difference is possible!
regards, jd
9. Re: datetime:diff() question
- Posted by Ekhnat0n Dec 08, 2017
- 1408 views
Hello mr davis,
If you were asked the difference between 3 and 8, what would be your answer?
Let me guess.... 5
If I am correct, you have DEDUCTED 3 from 8 as everybody would do.
However I would (maybe) have answered MINUS 5, because I deducted 8 from 3, using Reverse Polish Notation
Hence the function diff(a,b) does exactly what you yourself did.
Regards,
Antoine
Under the hood, this function almost certainly looks like
function diff(object a,b)
if a<b then return b-a
else return a-b
end
OR
function diff(object a,b) return ABS(a-b) end
BECAUSE for finding the difference you will have to deduct the lower value from the higher one, not the 2nd argument from the 1st
10. Re: datetime:diff() question
- Posted by petelomax Dec 08, 2017
- 1379 views
Neither manual nor sourcecode are wrong because they do not contradict each other
The problem is they do, and in fact the documentation contradicts itself.
The example is right, and matches both what the code actually does, and what any reasonable person would expect.
The Parameters/Returns/Comments sections are, however, completely backwards, both physically and logically.
There is nothing worth changing in the actual code itself (see post 2).
Pete
11. Re: datetime:diff() question
- Posted by Ekhnat0n Dec 08, 2017
- 1334 views
Hello Pete,
As I did not look up the commented code in the manual and you did you surely will be right here.
To be honest, I didn't even try it, as it gives me the answer I would expect under the circumstances.
The reason i didn't try is that ATM neither Eu nor Phix works for me under Ubuntu.
The installation of both seemed to be alright, but if I try to run (eg) example.ex in the terminal with Phix,
I get an errormessage "p not found" even though I cd-ed to the phix-dir and the example.ex resides there too.
This was one of several points I would appreciate to have your "trouble-shooting advice" on.
On other topics I would like to approach you for you will get a message on LinkedIn.
The email-address you can use to contact me is achnaton45@gmail.com.
Just a little game with numbers: I have my Bday today (born in '45) so at 3² - 2²*3 I turn 2³*3² (my kind of numerology ;)
Kind regards,
Antoine
12. Re: datetime:diff() question
- Posted by petelomax Dec 09, 2017
- 1386 views
I get an errormessage "p not found" even though I cd-ed to the phix-dir and the example.ex resides there too.
Are you using "./p" to run things? (see also setting path in ubuntu)
Happy birthday, Pete
13. Re: datetime:diff() question
- Posted by jessedavis Dec 09, 2017
- 1320 views
If you were asked the difference between 3 and 8, what would be your answer?
Let me guess.... 5
If I am correct, you have DEDUCTED 3 from 8 as everybody would do.
The DIFFERENCE(a,b) = abs(a-b), hence:
Difference(8,5) = Difference(5,8) = 3; It is the distance on the number line between the two.
However,
(5-8) = -3 and (8-5) = 3; the algebraic sum.
Again, the semantics aside, the documentation and the result should agree.
Thanks for your comments.
regards,
jd