Date and Absolute Time

Date is the central class of the date and time handling in Foundation.

It encapsulates a moment in time that is internally stored as the number of seconds since Jan 1, 2001 at 00:00 UTC.

It is universal as such that it is the same moment everywhere around the world and beyond (at least until the new star date system become real).

You can see absolute time as the moment that someone in the USA has a telephone conversation with someone in Dubai; both have that conversation at the same moment (at the same abolute time) but the local time will be different due to time zones, different calendars and different alphabets or notation methods.

This is way Foundation also support Calendar objects: it allows us to deal with these different cases.

This important concept is the root of several problems for programmers when dealing with time conversions across time zones, calendars and locale: SwiftDate help us with it.


DateInRegion and Region

SwiftDate introduces two new important objects and extended several other in order to provide a full featured library to manage date and time.

The first object is a struct and it's called Region: a region, as the name suggests, define a geographic region which is characterized by three different attributes:

  • A TimeZone: defines the behavior of time zone objects. Time zone objects represent geopolitical regions. Consequently, these objects have names for these regions. Time zone objects also represent a temporal offset, either plus or minus, from Greenwich Mean Time (GMT) and an abbreviation (such as PST for Pacific Standard Time).
  • A Calendar: encapsulate information about systems of reckoning time in which the beginning, length, and divisions of a year are defined. They provide information about the calendar and support for calendrical computations such as determining the range of a given calendrical unit and adding units to a given absolute time.
  • A Locale: encapsulate information about linguistic, cultural, and technological conventions and standards. Examples of information encapsulated by a locale include the symbol used for the decimal separator in numbers and the way dates are formatted.

The other object is DateInRegion. The main goal of this object is to represent a Date (which as we said it not related to a particular timezone or calendar) and express it in a Region: so, in fact, with DateInRegion you can manipulate a date defined in a specific context.

In SwiftDate you can manage dates both using DateInRegion and plain Date instances; both share the same functions and properties so you can work either with one or the other.


The Default Region

As you may notice, when you work with plain Date object you are effectively working with a DateInRegion defined in a Date.defaultRegion; initially this a Region which has:

  • .timeZone set as GMT (Greenwich Mean Time)
  • .locale set as the default device's locale
  • .calendar set as the default device's calendar

You can change Date.defaultRegion anytime (it's a global session variable) by using Date.setDefaultRegion(:) function.

Keep in mind you need to set it at startup of your application (so, for example, in an iOS app you may want to set it at UIApplication's delegate applicationDidFinishLaunching() function.

Copyright © 2016 Daniele Margutti - All right reserved