Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
From the author of

One of the things that is very easy to localize if you remember, and very hard if you forget, it time. The simplest problem is handling time zones. This is something that Apple's iCal does very badly. If you are traveling to a different time zone and have a meeting when you arrive, you will arrange it based on the local time at the destination and enter it in the calendar. When you arrive, you'll set the system clock to the new time zone, and find that the meeting time moved. A better solution would be to allow the user to define a time zone when entering a time, rather than forcing a specific locale.

A more complex problem is that of calendars. A calendar is a way of converting from an abstract time, like now, to something relative to a fixed reference point, like the 8th of January, 2010. In most of the western world, we use the Gregorian calendar, which has a complex system of leap years to stay roughly synchronized with the solar year, so the solstice is on the same date every year. In Islamic countries they tend to use a lunar calendar, which shifts by a small amount every solar year. This made sense in more equatorial regions, where variation between seasons was small, but the moon was a constant fixture.

Other countries have their own calendars. OS X supports a small selection of the most common ones. A well-written Cocoa program cleanly separates dates used for storage from dates used for presentation.

When you want to store a date, you should use an NSDate object. This stores a number of milliseconds from some reference date. The reference date is a fixed point, such as the UNIX epoc (the first of January, 1970). As such, it is independent of time zones and calendars. Before you display it to the user, you can convert it using the NSCalendar class. This can create an NSDateComponents object, encapsulating information like the day of the week, month, and year, from an NSDate.

In older code, you may come across the NSCalendarDate class. This was an NSDate subclass that incorporated information about the Gregorian calendar. It was not well suited to localization. It is deprecated on Snow Leopard and its use hasn't been recommended since 10.4.

  • + Share This
  • 🔖 Save To Your Account