On 21/05/2012 08:02, Igor de Oliveira Couto wrote:
What is the 'best' way to perform these 2 calculations with LiveCode?

'convert' just rocks. Here's a couple of throwaways to show you how I'd approach it. They probably aren't exactly what you need, but you'll get the idea.

1) Calculate a person's age, given a birthdate:

This one is a pain, as I ran it on Windows, and birthdays before 1970 weren't natively handled. I think this does the trick.
-------------------
on mouseUp
   -- put the birthday into tBd,
   -- on windows, can't do before 1970!
   put "May 21 1965" into tBd

   -- handle < 1970 (is this just on windows?)
   set the itemdel to " "
   put item 3 of tBd into tBYear
   if tBYear < 12 then
      add 2000 to tBYear
   else if tBYear < 100 then
      add 1900 to tBYear
   end if
   put 0 into tBYearOffset

   if tBYear < 1970 then
      put 1970 - tBYear into tBYearOffset
      put 1970 into item 3 of tBd
   end if

   -- back to out regularly scheduled code
   set the itemdel to ","
   convert tBd to dateItems
   if the result is not empty then
      put "Got an error:"&the result
   else
      put the long time into tNow
      convert tNow to dateItems

      subtract tBYearOffset from item 1 of tBd

      put item 1 of tNow - item 1 of tBd into tBirthdays
      answer "You've had "&tBirthdays&" birthdays."
   end if
end mouseUp
-------------------

2) Calculate whether an *anniversary* falls within a time frame - ie., within a 
week, within 2 weeks, within a month of today:

Something like this perhaps (written as in a button script)...watch out for wrapping:
-------------------
on mouseUp
   -- get now as 2012,5,21,9,12,20,2 - today's date/time/day number
   put the long time into tNow
   convert  tNow to dateItems

   -- find out this year's anniversary, replace '7' and '8' as needed
   put tNow into tAnniv
   put 6 into item 2 of tAnniv
   put 8 into item 3 of tAnniv

   -- find out distance between...
   convert tNow to seconds
   convert tAnniv to seconds
   put tAnniv - tNow into tDiff
   if tDiff < 0 then
      answer "Already passed!"
   else
      convert tDiff to dateItems
      answer "it's in "&item 2 of tDiff&" months, "&item 3 of tDiff \
        &" days, "&item 4 of tDiff&" hours."
   end if

   -- difference is shown as - 1970,2,18,0,0,0,4
   -- Ignore the year, as we purposely
end mouseUp
-------------------

Hope this helps.

-Ken

_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to