20 Jul, 2009, Igabod wrote in the 1st comment:
First I'll post the results of this program I'm tinkering with.

Quote
Hit enter to show your current age.

Mon Jul 20 03:59:43 -0500 2009 <—-Current date
Wed Feb 29 15:15:00 -0600 1984 <—-Birthday
27 <—-Years
331 <—-Months
1324 <—-Weeks
9272 <—-Days
222539 <—-Hours
13352384 <—-Minutes
801143083 <—-Seconds

Type quit to exit this program. Or hit enter to update your current age.

Notice the numbers there are quite wrong. I'm not 27 years old I'm 25 for starters.

Here's the code. Hopefully someone can find what I messed up. It may just be my math that's screwy right now.

`def timea = Time.newb = Time.mktime(1984, 2, 29, 15, 15)seconds = a.to_i-b.to_iminutes = seconds.to_i/60hours = minutes.to_i/60days = hours.to_i/24weeks = days.to_i/7months = weeks.to_i/4years = months.to_i/12puts a.to_s+ ' <—-Current date'puts b.to_s+ ' <—-Birthday'puts years.to_s+ ' <—-Years'puts months.to_s+ ' <—-Months'puts weeks.to_s+ ' <—-Weeks'puts days.to_s+ ' <—-Days'puts hours.to_s+ ' <—-Hours'puts minutes.to_s+ ' <—-Minutes'puts seconds.to_s+ ' <—-Seconds'endcommand = ' 'puts "Hit enter to show your current age.\r\n\r\n"while command != 'quit'  puts time.to_s + ' '  puts "\r\nType quit to exit this program. Or hit enter to update your current age.\r\n\r\n"  command = gets.chompend`

Please point out what I did wrong here.

[edit to add] I should clarify what I want the program to do here. I want it to eventually say "You are XX years XX months XX weeks XX days and XX seconds old.".
20 Jul, 2009, quixadhal wrote in the 2nd comment:
Your conversions are incorrect… Our calendar isn't neatly divided to work that way (mostly because people refused to have 13 months of 28 days each, it's the devil's number you know).

seconds, hours, days, even weeks are all fine… but at that point, you get into trouble. :)

However, you can't say there are 4 weeks to a month, that varies… you can't even say a month has any given number of days, or that a year always has 365 days to it. Leap years come into play, as well as the rules about when they don't happen (on century marks vs. millenium marks, etc).

In perl, I'd tell you to use Date::Calc, and specifically the Delta_YMDHMS function. I don't know if Ruby has a similar package. It doesn't look like the default Time class has a way to represent a chunk of time as an interval, rather than an actual date.
20 Jul, 2009, Igabod wrote in the 3rd comment:
so what you're telling me is that it isn't possible to do?
20 Jul, 2009, Silenus wrote in the 4th comment:
I remember seeing the basic Gregorian date calculation in certain introductory number theory books. Fortunately these days there is something even easier- wikipedia! :-). I believe this link contains the calculation you want:

http://en.wikipedia.org/wiki/Julian_day
20 Jul, 2009, quixadhal wrote in the 5th comment:
Curse the mouse back button for erasing 15 minutes of typing… CURSE IT!

Anyways, it's obviously not impossible… you have to look up the calculations as they are not trivial, or find a library from someone who's already done it.

60 seconds per minute, 60 minutes per hour, 24 hours per day, 7 days per week… all good (mostly).
365 days per year unless it's a leap year.
January, March, May, July, August, October, December all have 31 days.
April, June, September, November all have 30 days.
Febrary has 28 days, unless it's a leap year, when it has 29 days.

Leap years happen every 4 years (evenly divisible), unless it's also evenly divisible by 100, UNLESS it's ALSO evenly divisible by 400.

I'm not making this up! See!

This stems from history dating back 2400 years, and lots of fear. Thus, and thus.

Personally, I prefer the Old English ..., but hey… since when has simplicity and logic worked?
20 Jul, 2009, Igabod wrote in the 6th comment:
Note that I was born ON leap day so I'm well aware of how leap days work.

I found a mostly accurate way to calculate years btw.
`a = Time.newb = Time.mktime(1984, 2, 29, 15, 15)seconds = a.to_i-b.to_iminutes = seconds.to_i/60hours = minutes.to_i/60days = hours.to_i/24weeks = days.to_i/7months = weeks.to_i/4years = seconds.to_i/31556925 #approximate number of seconds per year. Not going to be completely accurate.`

31556925 is the approximate number of seconds in a non-leap year. and since it's only to calculate the number of years it should be mostly accurate. I'm not looking for down to the second accuracy on the years so this should be fine. Now I need to get to work on figuring out the math so I can find the number of months. I'm currently 25 years and 5 months old. I want this program to show me that along with the days hours and minutes. I can skip the weeks bit since that's quite pointless.
20 Jul, 2009, Tyche wrote in the 7th comment:
20 Jul, 2009, David Haley wrote in the 8th comment:
The short answer is that date calculations are complicated and you can't just use a single computation. The fact that there aren't always 4 weeks to a month, or 30 days to a month, or 365 days to a year is proof enough of this. That said most languages have date libraries that can help compute differences between dates; presumably Tyche linked to one for Ruby.
20 Jul, 2009, Scandum wrote in the 9th comment:
David Haley said:
The short answer is that date calculations are complicated and you can't just use a single computation.

You're saying that anything that takes more than a single computation is complicated?
20 Jul, 2009, Cratylus wrote in the 10th comment:
Scandum said:
David Haley said:
The short answer is that date calculations are complicated and you can't just use a single computation.

You're saying that anything that takes more than a single computation is complicated?

Starting to sound like a personal feud Scandum.
20 Jul, 2009, elanthis wrote in the 11th comment:
He made two different statements. Data calculations are complicated. You can't use a single computation. Reading more into that is the same as reading into the number of pirates in the world decreasing while the global temperature average is increasing. Makes a funny chart, but it doesn't actually mean anything of value. :)
20 Jul, 2009, David Haley wrote in the 12th comment:
Eh, Scandum just has an agenda to push, anything I say will end up proving that I'm MudBytes' token homosexual Mexican Muslim (and I quote!). :lol:
20 Jul, 2009, elanthis wrote in the 13th comment:
Wow! I never knew you were a homosexual Mexican Muslim, David! I can longer agree with any opinion you present. Thank the Christian God that Republican scientists have found a cure for homosexuality. And Mexicanism.
20 Jul, 2009, Runter wrote in the 14th comment:
elanthis said:
Thank the Christian God that Republican scientists have found a cure for homosexuality. And Mexicanism.

Oh, Elanthis.
21 Jul, 2009, quixadhal wrote in the 15th comment:
Taco, burrito.
-Beavis.
21 Jul, 2009, Ssolvarain wrote in the 16th comment:
Are you threatening me?!
21 Jul, 2009, Scandum wrote in the 17th comment:
David Haley said:
Eh, Scandum just has an agenda to push, anything I say will end up proving that I'm MudBytes' token homosexual Mexican Muslim (and I quote!). :lol:

Token homosexual Mexican Muslims are so 2000 and late, you've got nothing on the true test of bleeding heart liberalism, the token white sex offender.

Anyways, date calculations are annoying, I wouldn't call them complicated.
21 Jul, 2009, David Haley wrote in the 18th comment:
(Are you for real?)
21 Jul, 2009, Tyche wrote in the 19th comment:
elanthis said:
Wow! I never knew you were a homosexual Mexican Muslim, David! I can longer agree with any opinion you present. Thank the Christian God that Republican scientists have found a cure for homosexuality. And Mexicanism.

"You are a very bad man. I sentence you to 5 years in a federal pound-me-in-the-ass penitentiary."
21 Jul, 2009, Tyche wrote in the 20th comment: