Anyone got a date math routine?
Jay R. Ashworth
jra at baylink.com
Tue Dec 4 13:08:48 PST 2007
On Mon, Dec 03, 2007 at 04:29:48PM -0500, Kenneth Brody wrote:
> Quoting Jay R. Ashworth (Mon, 3 Dec 2007 15:58:08 -0500):
> >Before I write it myself, does anyone have a fuzzy age routine? I need
> >something that can take 2 dates and 2 times, and subtract one from the
> >other, and then round, so that what I get is "32m", "6h", "3d", "2w",
> >"4M", etc. That is, figure out what the largest unit of time is that
> >the difference can be represented in as a clipped integer, and return
> >that as a 3 character string.
> >
> >I have a design for such a routine in my head, but I figure if anyone's
> >already plowed the ground...
> >
> >If not, I'll post it here when I'm done with it.
>
> Well, here's my first pass, off the top of my head...
>
> Given that this is to give approximate ages anyway, this will assume:
>
> One year is 365.25 days.
> One month is 1/12th of a year.
>
> Use date/time math to get the number of seconds between the date+time.
> (Date minus date equals days. Time minus time equals hours.) Then,
> use the following:
>
> 1 minute = 60 seconds
> 1 hour = 3600 seconds
> 1 day = 86,400 seconds
> 1 week = 604,800 seconds
> 1 month = 2,629,800 seconds
> 1 year = 51,557,600 seconds
>
> You then just need to decide cutoffs. For example, how many seconds
> do you need before switching to hours? How many before switching to
> days? And so on.
You return minutes until you have about 80 of them, then switch to
hours until 23, days until 6, weeks until 4, months until 11.
That's what *I* want, anyway.
> Then, divide by that unit.
Yep, that was my plan, to within delta. I was just hoping someone'd
written the code already. :-)
My issue -- the thing I figured would require testing, is that you
can't actually just do the days and the hours, I'm pretty sure:
You need to do the days, and convert to seconds, and then do
ENDTIME - midnight == seconds to add
and
23:59:59 - STARTTIME = seconds to subtract
And *then* convert back. No?
Or is that an identity, mathematically, with the way you suggested
doing it?
> Plus, are you sure you want to clip and not do some rounding at least?
> Is 51,557,599 seconds really "11 months"?
You're right; I probably need to set my cutoffs to about the 5/4 rounding
points. Which means I need to decide *before* I convert... or use
floats internally.
Cheers,
-- jra
--
Jay R. Ashworth Baylink jra at baylink.com
Designer The Things I Think RFC 2100
Ashworth & Associates http://baylink.pitas.com '87 e24
St Petersburg FL USA http://photo.imageinc.us +1 727 647 1274
Witty slogan redacted until AMPTP stop screwing WGA
More information about the Filepro-list
mailing list