Time difference

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Time difference

Post by Da_Elf » Tue Dec 02, 2014 3:25 am

I usually store my times as YYYYMMDDHHmmSS with hours as 24 hour. This usually works well for telling what date is larger than the other. However what if i wanted to calculate the amount of hours between two dates which span either a month or even a year change like 20150101060000 and 20141231230000. I can easily tell that there is 7 hours difference by looking at it but how would i calculate it

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Time difference

Post by dunbarx » Tue Dec 02, 2014 3:34 am

Hu.

When in doubt, use the seconds.

Now translating your time stamp into a form LC recognizes will require a couple of lines of code. I would make a function out of it, and keep it somewhere.

Craig Newman

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Time difference

Post by Da_Elf » Tue Dec 02, 2014 3:43 am

I was thinking of converting to seconds yet my calculations would be thrown off if i cant account for a leap year.
jan 1 5am = 18000 seconds into the year is an example of converting to seconds however
feb 1 5am would be tricky since id have to know how many days came in january so it would be 31 days + 5 hours = 5202000 seconds thus
march 1 5am is even trickier without knowing if there were 28 days or 29 in february. so spanning a year like my example would have to take the year before into account

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Time difference

Post by dunbarx » Tue Dec 02, 2014 3:58 am

The seconds is set to a baseline. They are not tied to any particular year. in a button, try this:

Code: Select all

on mouseUp
   get the date
   convert it to seconds
   put 0 into it
   convert it to date
   answer it
end mouseUp
Step through the handler. Unless you need values before the 0th second, which would have to be calculated, this takes into account all that leap stuff. I am not sure if it accounts for leap centuries. Anyone know?

Anyway, if you have two dates, all you need is the absolute value of the difference of their seconds. Then convert that into whatever you like, days, attoSeconds, aeons, whatever.

Craig

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Time difference

Post by Da_Elf » Tue Dec 02, 2014 5:30 am

but i dont get how you can find how many seconds are between two dates if you dont know how many days the month has.

how many seconds are between 4 seconds to midnight on the 28th of february and 4 seconds past midnight on the 1st of march if you dont first find is there is a whole day inbetween there. To that extent i have devised this little script to find it out

Code: Select all

on mouseUp
   answer "February has "&checkLeap(2012)&" days in it"
end mouseUp

function checkLeap year
      put year&"0229120000" into testLeap
   if isNumber(the first char of ourDate("displayfull",testLeap)) then
      return 28
   else
      return 29
   end if
end checkLeap

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Time difference

Post by Da_Elf » Tue Dec 02, 2014 5:44 am

never mind i worked it out using what you gave

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Time difference

Post by dunbarx » Tue Dec 02, 2014 7:16 am

Hi.

Good to hear.

So you now know that LC does a lot of work for you under the hood. Thank goodness.

Craig

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: Time difference

Post by Da_Elf » Tue Dec 02, 2014 6:39 pm

yeah it does. the converting to seconds and then converting back to date is a huge plus

Post Reply