The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
They Say It's Your Birthday?!

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Rick DeNatale

Posts: 269
Nickname: rdenatale
Registered: Sep, 2007

Rick DeNatale is a consultant with over three decades of experience in OO technology.
They Say It's Your Birthday?! Posted: Jan 16, 2011 2:57 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Rick DeNatale.
Original Post: They Say It's Your Birthday?!
Feed Title: Talk Like A Duck
Feed URL: http://talklikeaduck.denhaven2.com/articles.atom
Feed Description: Musings on Ruby, Rails, and other topics by an experienced object technologist.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Rick DeNatale
Latest Posts From Talk Like A Duck

Advertisement

As I mentioned earlier today, I've started a new job which involves, among other things working on medical support applications.

One recent task was generating pdf reports for lab result documents. The header at the top of the report page needs to report, among other things, the age of the patient at the time of the lab. The age should be the patients age in years, or in the case of an infant less than one year old, in months. Other documents might require reporting the age in years and months since the last birthday, and perhaps the number of days remaining.

Since my birthday is the eighth of December, I'm Plenty-Nine years, one month, and eight days old today, which happens to be the sixteenth of January.

Computing that correctly is trickier than it appears at first.

First I thought this was just a matter of taking the integer part of dividing the difference between the dates by 365.25, and so on. Think again.

Then I thought there must be some open source solution to this, but every google search I tried for something like "age in years months and days" came back with the same results as if I'd searched for age in years months OR days, which is a horse of a different color.

Leapin' Lizards!

Your age in years, is actually the number of birthdays you have had, or to use a more generic term for the yearly recurrence of an event anniversaries. This seems straightforward until you consider the case of someone born on February 29, on a leap year.

People joke that someone born on say February 29, 1980, is 'only' 7 years old, since February 29 has only occurred seven times after 1980 up to this day.

Obviously leap babies age like the rest of us, and enjoy (or is it suffer?) a birthday each year. But when?

I've heard it said that leap babies can choose to celebrate their birthdays either February 28, or March 1 in a non-leap year. While that's true in the sense that anyone can choose to celebrate anyone's birthday on any day they wish; most of us choose to celebrate Christmas on December 25, although there's more evidence that that's because the early Christians wanted to celebrate Jesus' birth under the cover of pagan celebrations around the Winter Solstice, such as the Roman Saturnalia, than because Jesus was actually born on December 25.

The actual 'legal' birthday for a leap baby is muddy. I think that it makes sense to use March 1 in non-leap years in cases where the actual birth-hour is unknown, and that's what I decided to do.

It's not just Anniversaries.

Now let's say you were born on say January 31, 1970. how old were you on say, July 4, 2010.

Since you had had 40 birthdays, or anniversaries of your birth, the year part is easy, you were Forty years old.

But how many months and days are left over. Well it should be the number of monthly recurrences of your birthday's month day since your last birthday, and the number of days since the last montly recurrence. Let's just call these monthly recurrences 'monthiversaries'.

Now if your birthday falls in the range of the 29th through the 31st, there will be months which don't have that day. It's the same problem as a leap baby. So in the case of your hypothetical January 31, 1970 birthday, the last 'monthiversary' before July 4, 2010 can't June 31 so it must, by the logic above have been July 1, so you would have been 40 years, 6 months, and 3 days old on Independence Day 2010.

Announcing the Anniversary Gem

Last weekend I decided to work on this problem, 'off-the-books', the result is a new gem 'anniversary' which adds a couple of methods to Ruby's Date class. the primary one is Date.years_months_days_since which returns a 3 element array comprising the number of years, months and days which have occurred between the argument initial_date and the receiver.

The source is available on my github account. You can just "gem install anniversary" as well

Enjoy! And as soon as it IS your birthday, Happy Birthday!


Original article writen by Rick DeNatale and published on Talk Like A Duck | direct link to this article | If you are reading this article elsewhere than Talk Like A Duck, it has been illegally reproduced and without proper authorization.

Read: They Say It's Your Birthday?!

Topic: They Say It's Your Birthday?! Previous Topic   Next Topic Topic: New Year, New Gig

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use