# Date handling summary

Created 31st May, 2009 12:11 (UTC), last edited 31st May, 2009 13:45 (UTC)

## How many seconds were there in the year 2007, and how many seconds were there in the year 2008

There's an obvious formula to this. 2007 CE had 365 days, days are 24 hours long, hours are 60 minutes long and a minute has 60 seconds. So the answer should be 365 × 24 × 60 × 60 which gives 31,536,000. So far so very primary school.

2008 CE was of course a leap year, so the answer should then be 366 × 24 × 60 × 60, or 31,622,400.

These numbers don't tell the whole story though. As well as being a leap year, 2008 also had a leap second on the last day. That means of course that the right answer for 2008 is 31,622,401 seconds. This sort of thing causes no end of problems. Note that this means that some minutes have 61 seconds, or if there ever is a -1 leap second then maybe 59 seconds.

This gets complicated. The leap seconds are announced about six months before they happen and they can happen on December 31st or June 30th of any given year. If you're afraid that your software won't understand leap seconds then you may need to use International Atomic Time (TIA) instead. This has been in use since the 1950s and doesn't do the whole leap second thing. Right now the difference between UTC and TIA is 34 seconds.

The leap seconds are needed because there's another time system that's also very important. This one is called UT1 and is based on the actual motion of the Earth. Unfortunately for time keepers the Earth's rotation is slowing. Not by much of course, just a couple of milliseconds per day, but that adds up. In order to keep the difference between UT1 and UTC to less than a second a new leap second is needed about every 18 months.

## In the dates from 1st January 1901 to 31st December 2000 how many Sundays were there in New York, London and Moscow?

We'll have to see here if I can get these calculations correct myself. The two easy ones are New York and London. There is no difference between how many Sundays there are in these two locations. The time difference can't affect the answer even if we weren't using local time (trying to use non-local time for this would be nonsensical).

January 1st 1901 was a Tuesday, so the first Sunday was January 6th.

```import datetime as dt
date, count = dt.date(1901, 1, 6), 0
while date < dt.date(2001, 1, 1):
count += 1
date = date + dt.timedelta(7)
print count```

I make this 5218.

In Russia things were a little bit different. At the beginning of the Twentieth Century Russia was still using the old Gregorian calendar. It wasn't until after the October Revolution that a decree was issued to bring Russia's calendar into accord with most of the rest of the world. The day after Wednesday, 31st January 1918 was Thursday, 14 February 1918. The Sunday before 17th February 1918 was the 28th January!

This time it will be easier to count backwards from Sunday 31st December 2000.

```import datetime as dt
date, count = dt.date(2000, 12, 31), 0
while date >= dt.date(1901, 1, 1):
count += 1
if date == dt.date(1918, 2, 17):
date = dt.date(1918, 1, 28)
else:
date = date - dt.timedelta(7)
print count```

I make that 5216 Sundays.

## What was the year number for 1901 CE in Thailand?

This requires also a little chasing through of things and looking up a bit of history. Thailand adopted the Gregorian calendar in 1889* [*Or maybe it was 1888.]. Anybody who knows anything about Thai localisation will know that Thailand counts years using the Budhist Era. This starts in 543 BCE. So, 1901 CE was 2444 BE.

This of course isn't quite the final twist. Thailand didn't adopt the Budhist Era dates until 1913 CE. For its recent history before then it had been using years that counted from the founding of Bangkok — Bangkok Era. So, when Thailand adopted the Gregorian calendar it was 1st April 108 Bangkok Era. 1901 was thus 120 Bangkok Era, unless of course it was before 1st April 1901 in which case it might have been 119 Bangkok Era (depending on when Thailand switched from counting new years in April to January — something I've not been able to find out).