math with dates? Please Help.

I am currently developing an irc server bot in applescript that works from inside his own copy of ircle. One of the features of this bot is the <!seen> command. When a user joins an empty channel (empty exept for the bot of course) and is looking for another user he can type <!seen > and the bot until now has typed back <I last saw May 25th at 6:37 pm est> This has worked but many users that join this channel our from all corners of the globe. Australia, New Zealand, Japan, France, Canada… and dealing with timezones sucks. I’m trying to get him to type back <I last saw 15 days, 13 hours and 26 minutes ago.> This would be universal and eliminate the need for dealing with timezones but I’m having a hell of a time with it. No pun intended. The way it works is when a user leaves the bot sets a variable associated with that user to the current date. The variable is named lastSeen. So when some one does the seen command the bot needs to take the date in the lastSeen variable and subtract it from the current date to come up with an elapsed time that then needs to be broken down in to weeks, days, hours and minutes. Seems like it should be simple right? I’m to ashamed to post what I tried that didn’t work. It looks pathetic and I didn’t think it would work. But I know there’s got to be a simple solution. Could somebody please right up a quick routine that would do that for me? Pleaeaeaeaeasssseee!

BTW, If you would like to see him in action (Most of the bot does work quite well.) you can connect to my server at PolarBear.dyndns.org and join channel #SkyFighters

on grammaticalNumber(n, s)
  if (n is 1) then return " 1" & s
  return " " & n & s & "s"
end grammaticalNumber

on timeSince(d)
  set t to (current date) - d -- the difference in seconds.
  
  set timeUnits to {weeks, days, hours, minutes}
  set unitNames to {" week", " day", " hour", " minute"}
  
  set timeDiff to ""
  repeat with i from 1 to 4
    set n to t div (item i of timeUnits)
    -- Ignore time units whose value is 0.
    if (n > 0) then set timeDiff to timeDiff & grammaticalNumber(n, item i of unitNames)
    set t to t mod (item i of timeUnits)
  end repeat
  return timeDiff -- output begins with a space from grammaticalNumber().
end timeSince

set lastSeen to (date "Monday, 9 August 2004 23:11:35") -- for demo purposes only
display dialog "I last saw <usersnick>" & timeSince(lastSeen) & " ago." with icon note

Works like a charm right off the bat. No debugging neccessary. The odd thing is The line that I was getting hung up on was the same as the way you had it. Right at the start where you subtract the last seen from the cuurent date to come up with time since. That’s where I was throwing an error. Tried it a bunch of different ways but couldn’t get past it. The rest of what I had might have worked if I could just have gotten past that line although yours is much shorter and more efficient any way. Again THANKS A WHOLE S**T LOAD!

The conversion works fine but there’s a catch. See, I run three different operating systems on my G3 here. OS 8.6, 9.2 and 10.2.8. I do this because I still have some old software I like to use. Infact I have to be running in OS 8.6 just to watch tv because my crappy usb tv tuner doesn’t work with QT6. Anyway to create the illusion of stability on my server I have the bot write all the user data to a prefs file That it can reload from file after reboot. This has worked fine exept that now I can’t do any math with the lastSeen date mentioned above because it gets converted to a string in that write to file proccess. I’ve tried just putting after the read from file line but doesn’t seem to work. It still throws an error when some one does the !seen command. Any one have any ideas on how to preserve or restore the dates mathmatical value through that write to file read from file proccess?

If you have a date that’s been coerced to string, you can recreate the date object by putting the word ‘date’ in front of the variable holding the string. Unfortunately, you can’t use this date specifier directly in a subtraction:

(current date) - (date d) --> error

You have to resolve it first and then do the subtraction:

set d to date d
(current date) - d

-- or:
(current date) - (get (date d))

It might be a good idea in your case to insert a line at the beginning of the timeSince() handler like this:

on timeSince(d)
  if d's class is in {string, Unicode text} then set d to date (d as string)
  
  set t to (current date) - d -- the difference in seconds.
  -- etc.

A single line fix. Nigel you RULE!