Oh, btw, this discussion is very reminiscent of the Samoan baby age problem, which cropped up on the old DevForums a few years ago. [QuinceyMorris was there too!]
Note Someone pointed out that not everyone can access the old DevForums, so I’ve included a summary of the problem below.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"
[The question was: How do you calculate the age of a baby in days until they’re a week old, weeks until they’re a month old, and months until they’re a year old?]
As with most things related to dates and times, the more you think about it the harder it gets. There are two basic algorithms for calculating durations:
Take two UTC timestamps, diff them, divide (A)
Take two date components (era, year, month, day), diff them component-wise (B)
Normally you use B for anniversaries because A can result in weird side effects [1]. However, in the case of very short anniversaries, B can result in weird side effects as well. For example, consider the case of someone born at 23:15 (local time) in Samoa (UTC+14 in summer) who then takes 30 minutes to travel to American Samoa (UTC-11). The first timestamp translates to the components of yyyy/mm/dd 23:15. The second timestamp translates to the components of yyyy/mm/(dd-1) 22:45. If you diff the date components they are -1 days old!
Now, admittedly, travelling across time zones on your day of birth is pretty unusual but if you stretch the timeframe out to a week then the scenario is quite realistic: it’s perfectly reasonable for a person to travel from Samoa to American Samoa in the first week of their life, to see relatives and so on, and if that happens then it could be the difference between your app showing “6 days” and “one week” as their age.
Ultimately I don’t think this is an iOS-specific problem, but rather a problem of definition. I believe that, whatever you decide to show in this and other edge cases, iOS has APIs that can achieve that (-:
[1] For example: