Musings of an 8-bit Vet

A Student Coders Tech Diary

Dates/Times Are Hard

Our instructor,Jason Noble, sent out an email this morning about the homework. In this homework we had progressed beyond the ubiquitous hello_world.rb, and had moved on to calculating how many seconds there were in various blocks of time. In particular, how many seconds are there between now and 20 or 100 years from now?

Turns out that dates and times are hard to get a handle on in programming. Especially since a leap year is every 4 years from 1804, but not every 100, and then again every 400 years. Confusing, I know. Not just for us, but for own programming tools, and a lot of other folks too.

Jason’s email addressed the issue that depending on how you do your calculations you end up with different answers:

Jason originally had these values as the specified output:

That means when you turn 20, you've been alive for 628992000 seconds,
and if you make it to 100, you will have lived 3144960000 seconds.

Classmate Aron was first to finish and his output was slightly different:

That means when you turn 20, you've been alive for 630720000 seconds,
and if you make it to 100, you will have lived 3153600000 seconds.

Calculation methods were to blame.

Jason:

86400 * 7 * 52 * 20  # seconds/day * 7 days/week * 52 weeks/year * 20 years
86400 * 7 * 52 * 100  # seconds/day * 7 days/week * 52 weeks/year * 20 years

365 days != 52 weeks * 7 days/week (364 days)

Aron:

86400 * 365 * 20  # seconds / day * 365 days / year * 20 years
86400 * 365 * 100  # seconds / day * 365 days / year * 100 years

(Aron is more accurate, but he forgot to account for leap years)

Another possibility from Jason:

86400 * 365.25 * 20  # seconds / day * 365.25 days / year * 20 years  => 631152000
86400 * 365.25 * 100  # seconds / day * 365.25 days / year * 100 years => 3155760000

(Looks like Jason is getting closer by trying to account for leap years. A year is actually = 365d, 5h, 48m, 47s. The fractional diffence is why we have leap year. That is an additional 20,927 seconds per year)

Jason used the Rails console to get a slightly better answer:

20.years is 631152000 seconds
100.years is 3155760000 seconds

Who has the right answer? Is there another algorithm?

My answer was also slightly different since I actually counted the leap years for 20 and 100 years from now, 5 & 24 respectively.

I then added in the additional days to yield:

20 years from now is 631152000 seconds,
100 years from now is 3155673600 seconds.

Notice that my 20 year number is the same as the Rails computation, and my 100 year number is much closer. In fact it’s off by exactly one day. Looks like Rails missed the fact that 100 / 4 = 25 is not an accurate way to count leap years.

I also tried using the Time function in Ruby:

1
2
3
4
5
6
7
8
9
# Seconds from now till twenty years from now.
irb> puts (Time.gm(2033, 7, 23, 11, 03) - Time.new)

>> 631130357.633387

# Seconds from now till 100 years from now.
irb> puts (Time.gm(2113, 7, 23, 11, 04) - Time.new)

>> 3155651903.756982

Again, slightly different answers. There are several gems to tackle this problem as well. But this is where I stopped. Hopefully this stimulates some conversation in class this week.