• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] [Solved] Get Difference Between Time Period
#1
I have an function to get difference between time period.

But it has a little problem. If start_hour is the current hour and stop_hour is 24am, it does not accurately calculate the difference Between Time.



Code:
new start_hour, start_minute, start_second, hours, minutes, seconds;



gettime(start_hour, start_minute, start_second);

DifferenceBetweenTimePeriod(start_hour, 24, start_minute, 0, start_second, 0, hours, minutes, seconds);







function:

Code:
DifferenceBetweenTimePeriod(start_hour, stop_hour, start_minute, stop_minute, start_second, stop_second, &hours, &minutes, &seconds) {

? ? while (stop_second > start_second) {

? ? ? ? --start_minute;

? ? ? ? start_second = 60;

? ? }

? ? seconds = start_second - stop_second;

? ? while (stop_minute > start_minute) {

? ? ? ? --start_hour;

? ? ? ? start_minute = 60;

? ? }

? ? minutes = start_minute - stop_minute;

? ? hours = start_hour - stop_hour;

}



Is there a problem with the function?



Solved:



This is resolved if I put?24 in?start_hour.

Code:
DifferenceBetweenTimePeriod(24,?start_hour, start_minute, 0, start_second, 0, hours, minutes, seconds);
  Reply
#2
Hours in a day are counted from 0, there's no hour 24... that's 00. That function should never be receiving 24 as an hour in a normal scenario
  Reply
#3
(2021-05-18, 01:45 AM)Markski Wrote: Hours in a day are counted from 0, there's no hour 24... that's 00. That function should never be receiving 24 as an hour in a normal scenario



Yes you are right. But if for example start_hour is 3am and stop_hour is 0am, it returns:

02 Hours 00 Minutes 00 Seconds?



It should return?

21 Hours 00 Minutes 00 Seconds
  Reply
#4
Use unix timestamps. If you ever have a question about time, they are the answer.
  Reply


Forum Jump: