Page 1 of 2
datediff not accurate.
Posted: 16 Mar 2011 18:54
by tiago
datediff should calculate hours intervals based on minutes actually involved and not the simple 14:00:01 - 13:58:32 = 1 hour! Same goes for minutes being calculated on actual number of seconds and rounding down for each passed 60 secs.
Re: datediff not accurate.
Posted: 16 Mar 2011 19:57
by admin
Well, depends on your purpose. I formed the functionality after the widely used VBA DateDiff.
Re: datediff not accurate.
Posted: 19 Mar 2011 01:41
by tiago
Well, the purpose is to have an accurate datediff method to rely on.
Tried some scripting on this but got stuck at a point, had come ideas, made notes and report back if I find a reasonable solution.
Re: datediff not accurate.
Posted: 19 Mar 2011 09:54
by admin
tiago wrote:Well, the purpose is to have an accurate datediff method to rely on.
Tried some scripting on this but got stuck at a point, had come ideas, made notes and report back if I find a reasonable solution.
You can simply increase the resolution by asking for the difference in seconds, and then calculate from there manually.
Re: datediff not accurate.
Posted: 19 Mar 2011 14:49
by tiago
Yeah, that was the first idea.
Then I obviously stumble upon the inaccuracy itself again and using format/formatbytes didn't helped much as far as I can remember. I'll probably revise my attempts again and post any solution I may find, in hopes that someone bothers for such a thing.
Re: datediff not accurate.
Posted: 19 Mar 2011 18:09
by tiago
This has turning into an interesting problem.
Yes, it seems to be a "widely used" method out there or two, the most dangerous being: almost all online converters I found to debug my calculations just do the whole hours or minutes part, so they're not doing a reliable calculation too.
The problem I'm stuck in: XY does not rounds down or up decimal numbers. Does it?
So in case your diff is 3601 seconds, prepare to have a little annoyance.
Code: Select all
// $reportModifiedDate2 = <date hh:nn:ss yyyy/mm/dd>; // this is the real thing, replaced for debug reasons
$reportModifiedDate1 = "13:58:58"; //
//time of script execution, emulated by wait()
//in case, YOUR SCRIPT GOES HERE.
// focus pi; //debug purposes only - remove for real usage.
// status "Plz wait 1 min - timer on, $reportModifiedDate1"; //debug purposes only - remove for real usage.
// wait(62000); //debug purposes only - remove for real usage.
// focus pi; //debug purposes only - remove for real usage.
//in case, YOUR SCRIPT ENDS HERE.
// $reportModifiedDate2 = <date hh:nn:ss yyyy/mm/dd>; // this is the real thing, replaced for debug reasons
$reportModifiedDate2 = "16:32:01"; // 15:02:01 , test hr // 14:02:01 , test min // 13:59:41 , test sec
$differenceOfDates = datediff($reportModifiedDate1, $reportModifiedDate2, s);
// $differenceOfDates = 3601; // debug purposes
IF ($differenceOfDates <= 59)
{
echo "$reportModifiedDate1 - starting date;<crlf>$reportModifiedDate2 - ending date;<crlf><crlf>$differenceOfDates - difference in seconds.";
}
ELSEIF ($differenceOfDates <= 3599)
{
$minMIN1 = $differenceOfDates / 60;
$minMIN2a = gettoken($minMIN1, 1, ".");
$minMIN2b = gettoken($minMIN1, 2, ".");
$minMIN3 = "0.$minMIN2b" * 60;
$minMIN4 = format($minMIN2a, "00");
$minMIN5 = format($minMIN3, "00");
$minR = "$minMIN4" . ":" . "$minMIN5";
echo "$reportModifiedDate1 - starting date;<crlf>$reportModifiedDate2 - ending date;<crlf><crlf>$differenceOfDates - difference in seconds;<crlf>$minR - difference in minutes.";
}
ELSEIF ($differenceOfDates >= 3600)
{
$minMINa = $differenceOfDates / 3600;
$minMINba = gettoken($minMINa, 1, ".");
$minMINbb = gettoken($minMINa, 2, ".");
$minMINc = "0.$minMINbb" * 3600;
$minMINd = format($minMINba, "00");
$minMINe = format($minMINc, "00");
$minMIN6 = $minMINe / 60;
$minMIN7a = gettoken($minMIN6, 1, ".");
$minMIN7b = gettoken($minMIN6, 2, ".");
$minMIN8 = "0.$minMIN7b" * 60;
$minMIN9 = format($minMIN7a, "00");
$minMINx = format($minMIN8, "00");
$minRH = "$minMINba" . ":" . "$minMIN9";
$minMIN1 = $differenceOfDates / 60;
$minMIN2a = gettoken($minMIN1, 1, ".");
$minMIN2b = gettoken($minMIN1, 2, ".");
$minMIN3 = "0.$minMIN2b" * 60;
$minMIN4 = format($minMIN2a, "00");
$minMIN5 = format($minMIN3, "00");
$minR = "$minMIN4" . ":" . "$minMIN5";
echo "$reportModifiedDate1 - starting date;<crlf>$reportModifiedDate2 - ending date;<crlf><crlf>$differenceOfDates - difference in seconds;<crlf>$minR - difference in minutes;<crlf>$minRH - difference in hours.";
}
status "done.";
Re: datediff not accurate.
Posted: 19 Mar 2011 18:54
by admin
Hmm, these are a lot of lines. What are you trying to achieve? Can you give an example?
Re: datediff not accurate.
Posted: 19 Mar 2011 19:19
by tiago
I need to report among other stuff how much time XY toke to perform a job. Accurately, since currently I'm working for a non-profit NGO.
If I had a way to have gettoken being able to get tokens without separators, I guess I can easily solve the rounding up-or-down issue.
echo gettoken("Donald Duck", 4, ""); // returns "a"
echo gettoken("a,b,c,d,e", 4, ""); // returns ","
echo gettoken("a,b,c,d,e", 3, ""); // returns "b"
Re: datediff not accurate.
Posted: 19 Mar 2011 19:24
by admin
tiago wrote:I need to report among other stuff how much time XY toke to perform a job. Accurately, since currently I'm working for a non-profit NGO.
If I had a way to have gettoken being able to get tokens without separators, I guess I can easily solve the rounding up-or-down issue.
echo gettoken("Donald Duck", 4, ""); // returns "a"
echo gettoken("a,b,c,d,e", 4, ""); // returns ","
echo gettoken("a,b,c,d,e", 3, ""); // returns "b"
A non-profit NGO needs to know how much time XY toke to perform a job? Anyway, seconds should be accurate enough, so where's the problem? Or do you want milliseconds?
Re: datediff not accurate.
Posted: 19 Mar 2011 19:29
by tiago
No, on the opposite: sometimes an operation takes HOURS to run.
And yes, I agree that seconds should suffice. I'm just bothered by delivering them "13601 seconds" as an output.
Precision is everything.
Re: datediff not accurate.
Posted: 19 Mar 2011 19:33
by tiago
User-friendliness, too.
Re: datediff not accurate.
Posted: 20 Mar 2011 07:44
by admin
tiago wrote:No, on the opposite: sometimes an operation takes HOURS to run.
And yes, I agree that seconds should suffice. I'm just bothered by delivering them "13601 seconds" as an output.
Precision is everything.
What is unprecise about "13601 seconds"?
Re: datediff not accurate.
Posted: 20 Mar 2011 13:28
by tiago
The problem with precision is: your (VBA) datediff method says that 14:00:01 - 13:58:32 = 1 hour.
Correct: 89 secs or 01min 29sec.
13601 secs. Cool. Human friendliness: 226min 41 sec or 03h 46 min.
I think that math on XY could benefit If only there would be a way to round up or down decimals like in 2.33333333333333 turning into 2.33 or 2.3 or 2.4 (2.53333333333333) as per user choice.
Re: datediff not accurate.
Posted: 20 Mar 2011 21:50
by tiago
Two beta on a single day and one containing a suggestion o'mine?
wow, thank you, Admin... Downloaded the version and will check it later so I can solve that little glitch on my script.
Re: datediff not accurate.
Posted: 21 Mar 2011 01:58
by tiago
Any idea on how do I translate the following PHP code into XYplorer script?
$seconds = 631;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
echo "$hours : $minutes : $seconds";