Search code examples
nsdatenscalendarnsdatecomponents

How to calculate exact anniversary of sinking of the Titanic


yes this is an iOS programming question.

I need to calculate the exact time the RMS Titanic sank for its 100 yr anniversary.

It sunk at 15th April 1912 2:20 a.m. 

Stupid Question you say. 100th year anniversary is

15th April 1912 2:20 a.m. 
+100 years
15th April 2012 2:20 a.m. 

But I want an alarm to go off in any timezone in the world exactly and need to handle timezones, and things like British Summer Time being one hour ahead yet it didnt come into effect till 1918.... but as of today 25 mar 2012 London IS in BST so one hour ahead.

Im confused on timezones. We have GMT and UTC. The ships sank at 49° 56' 49" W, 41° 43' 32" N so few hours ahead of London. -49.94822196927015 41.72713826043066

Whats the correct way to enter a historic date into NSCalendar 
and to add 100 years to it exactly and get back the right time 
in the users current timezone?

I notice theres Japanese and Islamic calender formats in NSCalendar options. Can iOS device change their dates to these calendars? And if this was the case. how would I convert from Gregorian to say Islamic?

nice brain puzzler to start your week :)


Solution

  • (There's no code here, as I've never used the iOS API. However, I have some experience of date/time APIs and the oddities they throw up, so I hope you find this answer useful anyway.)

    What's the correct way to enter a historic date into NSCalendar and to add 100 years to it exactly and get back the right time in the users current timezone?

    It really depends on what you mean by "100 years" - it's not like that's a fixed amount of time really.

    I would take the UTC instant at which it sank, apply the user's local time zone, and then add a hundred years to that. However, you then need to consider that the result may not actually be a valid local time in that time zone.

    For example, suppose you're in a time zone where at the instant of the sinking, it was 1:20am... but in that time zone, 15th April 2012 is when the clocks change - and they skip from 1am to 2am. In that case, 1:20am never occurs... so you could potentially pick 12:20am, the instant before the DST transition, the instant of the DST transition, or 2:20am, depending on what you think is appropriate.

    Another possibility to consider is the opposite - suppose it's a DST transition which goes from 2am to 1am... so 1:20am actually occurs twice. What would you want to do in that case? You probably shouldn't make your app celebrate the anniversary twice!

    Another option which removes this possibility of ambiguity and discrepancy is to work out what the offset from UTC in the user's time zone was at the exact time of the sinking, then add 100 years to the UTC value (which will never have any DST transitions) and apply the same offset again.

    I notice theres Japanese and Islamic calender formats in NSCalendar options. Can iOS device change their dates to these calendars? And if this was the case. how would I convert from Gregorian to say Islamic?

    I don't know on that front, I'm afraid.