calculating time difference - page 2

 

Most likely the guy was looking for something like this…

 

string ConvertSecondsToHHMMSS(uint TimeSpenInSec)

{

 

   double NumHours = 0;          //  There are 60 minutes per 1 Hour or 60 * 60 = 3600 seconds per 1 Hour

   double NumMinutes = 0;        //  There are 60 sec per 1 Min

   double NumSeconds = 0;

  

   NumHours    = MathFloor(TimeSpenInSec / 3600);

   NumMinutes  = MathFloor(MathMod(TimeSpenInSec / 60, 60));

   NumSeconds  = MathMod(TimeSpenInSec, 60);

  

   return(StringFormat("%02d:%02d:%02d", (int)NumHours,

                                         (int)NumMinutes,

                                         (int)NumSeconds));

}

 

This is a very different question, can't you keep both servers synced with some world atomic clock? 

This is very easy and you can sync both servers every minute. 


Servers clock skills can prevent SSL, HTTPS and cryptographic system communications and functions to correctly work.

Think about it. 

 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".
 

perhaps it is too late but I was looking for a ready solution to calculate the difference between two times even if the times are in two following years, so I faced this topic.

than I write as follow, which works correctly for my application:


int HourDiff(datetime t1, datetime t2)
{
    int year_t1=TimeYear(t1);
    int year_t2=TimeYear(t2);
    int day_t1=TimeDayOfYear(t1);
    int day_t2=TimeDayOfYear(t2);
    int hour_t1=TimeHour(t1);
    int hour_t2=TimeHour(t2);
    // total day of year_t1 = TimeDayOfYear(StringToTime(StringFormat( "%4d.12.31", TimeYear(t1) ))))
    int hourDiff=((((year_t2-year_t1)*TimeDayOfYear(StringToTime(StringFormat( "%4d.12.31", TimeYear(t1) ))))+day_t2-day_t1)*24)+hour_t2-hour_t1;
    return hourDiff;
}

as a same way with related changes it could be use to calculate in minutes or seconds as well. 

 
mohsen radfar #: perhaps it is too late but I was looking for a ready solution to calculate the difference between two times even if the times are in two following years, so I faced this topic.

Simplify, the difference between to datetimes is in seconds.

int HourDiff(datetime t1, datetime t2)
{
    return (t2 - t1) / 3600;
}
 
William Roeder #:

Simplify, the difference between to datetimes is in seconds.

I tested your approch the result is not true in any kind of time unit. In my case I wanted to calculate the hour differencies between last close time to current time.

Without convertion same as your approch, you will have false result.

eg. t1=2022.07.19 12:00 and  t2=2022.07.20 12:00 the result without convertion and caring the different day will be 0 but not 24!

this will be even worse if t1=2021.12.30 12:00 and  t2=2022.01.01 12:00

thanks your comment anyway.

 
mohsen radfar #:

I tested your approch the result is not true in any kind of time unit. In my case I wanted to calculate the hour differencies between last close time to current time.

Without convertion same as your approch, you will have false result.

eg. t1=2022.07.19 12:00 and  t2=2022.07.20 12:00 the result without convertion and caring the different day will be 0 but not 24!

this will be even worse if t1=2021.12.30 12:00 and  t2=2022.01.01 12:00

thanks your comment anyway.

Please show your code that gives incorrect results.

William's function is simple and it works correctly.

If you are getting incorrect results it must be due to your mistake.

 
mohsen radfar #: eg. t1=2022.07.19 12:00 and  t2=2022.07.20 12:00 the result without convertion and caring the different day will be 0 but not 24!

Prove it. You are making assumptions.

void OnStart()
  {
  datetime t1=D'2022.07.19 12:00';
  datetime t2=D'2022.07.20 12:00';
  PrintFormat("diff=%d, hours=%d", t2-t1, (t2-t1)/3600); // testscr: diff=86400, hours=24
 
William Roeder #:

Prove it. You are making assumptions.

sorry you are right. that was my mistake also due to false probe times and wrong interpretation of my similar approch. 

my first approch was similar to yours but without division; instead i used function TimeHour() as follow:

int HourDiff2(datetime t1, datetime t2)
{
    return TimeHour(t2 - t1);
}

that was for this purpose realy disaster.

furthermore during complition of your code i got an warning, which mentioned the compiler needs an appropriate interpretator namely (int). So I asume my wrong assumtion based on my wrong test is true. 

I using it yet as follow

int HourDiff(datetime t1, datetime t2)
{
    return ((int)(t2 - t1) / 3600);
}

sorry and thanks for the tip.

 
William Roeder #:

Prove it. You are making assumptions.

Actually, I didn't pay attention to " the difference between to datetimes is in seconds"!

thanks again