{"id":3229,"date":"2016-09-20T21:29:34","date_gmt":"2016-09-21T02:29:34","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=3229"},"modified":"2016-09-20T21:29:34","modified_gmt":"2016-09-21T02:29:34","slug":"handling-dates-with-swift-3-0","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/handling-dates-with-swift-3-0\/","title":{"rendered":"Handling Dates with Swift 3.0"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.shields.io\/badge\/Swift-3.0-orange.svg?style=flat\" alt=\"Swift 3.0\" \/><\/p>\n<p>When you take on a goal as ambitious as the <a href=\"https:\/\/developer.apple.com\/videos\/play\/wwdc2016\/403\/\">Great Renaming<\/a> it is a challenge to ensure that all of the reference documentation is updated.  As of September 20, 2016, for example, the <a href=\"https:\/\/developer.apple.com\/reference\/foundation\/nsdateformatter\">DateFormatter<\/a> documentation contains inconsistencies and references to Swift 2.3-style APIs.  As time passes this will undoubtedly be corrected, but in the meantime here are a few examples for formatting dates with the <code>Date<\/code> and <code>DateFormatter<\/code>.<\/p>\n<p>The current example in the above-reference documentation is:<\/p>\n<pre class=\"style:swift\">\nlet dateFormatter = DateFormatter()\ndateFormatter.dateStyle = .mediumStyle\ndateFormatter.timeStyle = .noStyle\n \nlet date = Date(timeIntervalSinceReferenceDate: 118800)\n \n\/\/ US English Locale (en_US)\ndateFormatter.locale = Locale(localeIdentifier: \"en_US\")\nNSLog(\"%@\", dateFormatter.stringFromDate(date)) \/\/ Jan 2, 2001\n \n\/\/ French Locale (fr_FR)\ndateFormatter.locale = Locale(localeIdentifier: \"fr_FR\")\nNSLog(\"%@\", dateFormatter.stringFromDate(date)) \/\/ 2 janv. 2001\n \n\/\/ Japanese Locale (ja_JP)\ndateFormatter.locale = Locale(localeIdentifier: \"ja_JP\")\nNSLog(\"%@\", dateFormatter.stringFromDate(date)) \/\/ 2001\/01\/02\n<\/pre>\n<p>In Swift 3.0 this changes to:<\/p>\n<pre class=\"style:swift\">\nlet dateFormatter = DateFormatter()\ndateFormatter.dateStyle = .medium\ndateFormatter.timeStyle = .none\n<\/pre>\n<p>Notice that <code>.mediumStyle<\/code> is dropped in favor of <code>.medium<\/code>.  This is in line with simplified naming; we know that the type is of <code>DateFormatter.Style<\/code> so there is no reason to repeat the word <code>Style<\/code>.  The same applies for <code>.none<\/code> as opposed to <code>.noStyle<\/code>.<\/p>\n<p>Now, let&#8217;s look at the changes to setting the formatter&#8217;s locale:<\/p>\n<pre class=\"style:swift\">\n\/\/ US English Locale (en_US)\ndateFormatter.locale = Locale(identifier: \"en_US\")\nprint(dateFormatter.string(from:date)) \/\/ Jan 2, 2001\n \n\/\/ French Locale (fr_FR)\ndateFormatter.locale = Locale(identifier: \"fr_FR\")\nprint(dateFormatter.string(from:date)) \/\/ 2 janv. 2001\n \n\/\/ Japanese Locale (ja_JP)\ndateFormatter.locale = Locale(identifier: \"ja_JP\")\nprint(dateFormatter.string(from:date)) \/\/ 2001\/01\/02\n<\/pre>\n<p>Again, we see the simplification from <code>Locale(localeIdentifier:)<\/code> to <code>Locale(identifier:)<\/code>.  Less typing and to the point.  Likewise the <code>stringFromDate<\/code> method of the <code>DateFormatter<\/code> has been streamlined to <code>string(from:)<\/code>, which if you use the entire signature is <code>string(from:Date)<\/code>.  See the pattern?<\/p>\n<p>Moving on to creating a <code>Date<\/code> from a <code>String<\/code> representation, the Apple documentation has this example:<\/p>\n<pre class=\"lang:swift\">\nlet RFC3339DateFormatter = DateFormatter()\nRFC3339DateFormatter.locale = Locale(localeIdentifier: \"en_US_POSIX\")\nRFC3339DateFormatter.dateFormat = \"yyyy-MM-dd'T'HH:mm:ssZZZZZ\"\nRFC3339DateFormatter.timeZone = TimeZone(forSecondsFromGMT: 0)\n \nlet string = \"1996-12-19T16:39:57-08:00\"\nlet date = RFC3339DateFormatter.dateFromString(string)\n<\/pre>\n<p>Applying the principle of reducing verbiage and unnecessary words we arrive at:<\/p>\n<pre>\nlet RFC3339DateFormatter = DateFormatter()\nRFC3339DateFormatter.locale = Locale(identifier: \"en_US_POSIX\")\nRFC3339DateFormatter.dateFormat = \"yyyy-MM-dd'T'HH:mm:ssZZZZZ\"\nRFC3339DateFormatter.timeZone = TimeZone(secondsFromGMT: 0)\n \nlet string = \"1996-12-19T16:39:57-08:00\"\nlet date = RFC3339DateFormatter.date(from:string)\n<\/pre>\n<p>The <code>TimeZone<\/code> initializer drops an extraneous three characters (<code>for<\/code>), and as expected the <code>dateFromString<\/code> method becomes <code>date(from:)<\/code>.<\/p>\n<h3>Rules of Thumb<\/h3>\n<p>It should be apparent that the general rule of thumb when transitioning from Swift 2 to Swift 3 is to remove superfluous words.  If you were used to writing <code>formatter.timeStyle = .fullStyle<\/code> before, get used to writing <code>formatter.timeStyle = .full<\/code>.  If you see <code>someTypeFromAnotherType()<\/code> it&#8217;s likely been replaced with <code>someType(from:AnotherType)<\/code>.<\/p>\n<p>Speaking from experience, after working with Swift 3 for several months now, going back to Swift 2 feels overly verbose, and this is coming from someone who actually <i>likes<\/i> verbose languages.  Once you get the hang of it you&#8217;ll be embracing Hemingway and eschewing Tolstoy.<\/p>\n<p>Happy Swifting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you take on a goal as ambitious as the Great Renaming it is a challenge to ensure that all of the reference documentation is updated. As of September 20, 2016, for example, the DateFormatter documentation contains inconsistencies and references to Swift 2.3-style APIs. As time passes this will undoubtedly be corrected, but in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3108,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[66],"class_list":["post-3229","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-swift","tag-swift-nsdate"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3229"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=3229"}],"version-history":[{"count":6,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3229\/revisions"}],"predecessor-version":[{"id":3235,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3229\/revisions\/3235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/3108"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=3229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=3229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=3229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}