Love or Hate for Dates on Swift

Swift Dates Love or Hate it, you just can’t ignore it



At some point of our career we say it Enough !!!  We say it loudly, no more  evading from Date. Prior to this understanding; we used to find some cunning corners to keep our some hours study for later time and embrace some SO, stackoverflow, answers that just do the work we wanted. Right or Wrong, we don’t judge it. It’s a solution that gonna  work.And ya…, the rest is mostly common for all of us. We fall hard and realize: we need talk some more details on Date. Love it or Hate it, doesn’t matter Dates gonna bite you if you are not prepared, so Let us have some introductory talk on Date[s] through some Swift code.

Background

We can always find the necessary docs on the Apple doc over Date. Feel free to revisit. But for now let’s drive deep ๐ŸŠ on our Swift Dates introductory talk, you gonna love it or hate it? Let’s see.

Date

By definition Date is the type that represents Time on Foundation framework. Hmmm a bit confusing, date represents time.

So on a regular conversation if we say, which date it is? One will answer something like 25 October, 2018. And if we ask, what time it is? The answer will be 10 am or similar. What about Day? Answer, Thursday.

But when we talk about Date on Swift; be prepare to include all those components, that we just talked about.

Perhaps Time will be more appropriate name, as Time is a more core concepts even for date also.

Wait, thats not actually the end of a confused discussion. Date has more insights to make us even more puzzled about its Name.  

Date value basically represents a time interval from an absolute reference date. WTH!!! And what does it means. Lets talk…

So the typical reference date is 00:00:00 UTC on 1 January 2001, it can also be 00:00:00 UTC on 1 January 1970 or even the ref date we want.

When we initialize an Date instance, the value of that instance actually tells us the difference/interval from that ref date. So hence Date is the timer interval from an absolute ref date. 
Just a small note: reference date is not a Swift Dateits our regular date.

So are we thinking that, TimeInterval cloud be a more appropriate Name? Nope can’t be. TimeInterval is a typealias of Double.

You know what, it doesn’t matter anymore. At some point of our career we will eventually figure this out.

We just need to understand the base idea, despite the Name as Date, Date is actually a difference based value from a reference.

So lets move on…

Your are free, Date

The interesting part about Date is, it is not dependent on time-zone or any Calendrical system. As Date instance is a time-interval from a reference-point, if we transform the reference-point to another system then the Date instance will automatically converted to the new system.

let defaultRefDate = Date(timeIntervalSinceReferenceDate: 120) 

Here the defaultRefDate is 120 Unit/seconds, represented as Double, right to the ref-point 0. 

Note: Seconds has only the fixed duration, thats why it is used as the Unit for Date. Month can be 28/29/30/31, years can have 52 weeks + 1/2 days, and don’t forget days have to handle the daylight-saving-time.

Moving back to our Date independency talking: if someone has a local of Arabic, Ar,  then he will see something like: ู ูกโ€/ู ูกโ€/ูขู ู ูกุŒ ูฆ:ู ูข ุต
but on someone who has an English local will see it as Jan 1, 2001 at 6:02 AM on their respective Xcode’s playground view.

What happens here is that, Playground prints the defaultRefDate base on the user’s locale. It has no connection with Date instance. Similarly we can convert the date to another calendar or format. We will see those in the upcoming talk.

Date Interval

Date Interval is the time-frame/timeline between two Date instance. It is a close boundary interval.

Time Interval

Time Interval is a representative type of seconds, unit of time. It is a typealias of Double.

Date Localizing

On different community/zone date are represented on different way. Some has day first, some has year first when printing a date. Sometimes Full month name is required, sometimes the regular three letters format of month-name is accepted. 
Another major concern is the Month name. On Spain it is marzo for March. Do we want to go with Japanese or Chinese. Think its enough. ๐Ÿ˜›

So how we gonna support different locale for our dates. The good think is its already provided by the system. We just need to set the local and thats it.

We will talk in more details on the next blog post, Format your Date[s].

Calendar

So Date is an absolute points in time. Calendar makes it possible for a Date instance to be operated on different Calendrical Unit, day weekday year etc, with Calendrical systems;  example Gregorian or Islamic calendar.

In other words Calendar provides the calculation and comparison of Dates on different Calendrical system.

Calendar handles the things like leap-year, daylight saving and any other things that are related with a Calendar.

Conclusion:

This blog post may seems to be end. But behold its not. There is one more major part we need to learn and its the DateFormatter, which is our next blog post. So the Swift Dates, Love or Hate, still continues ๐Ÿ˜‰.

After the DateFormatter talk we will again start some Q/A to make sure we understand the whole flow for Dates and its related concepts.

Till then Happy Talking. ๐Ÿค“

One thought on “Swift Dates Love or Hate it, you just can’t ignore it

Leave a Reply

Notifications for mobidevtalk! Cool ;) :( not cool