Created
September 19, 2019 13:15
-
-
Save devxoul/f84f7fb7f9a41cca3c7b7184fbd78aa0 to your computer and use it in GitHub Desktop.
Date+ISO8601.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Date+ISO8601.swift | |
| // SSFoundation | |
| // | |
| // Created by Suyeol Jeon on 19/09/2019. | |
| // Copyright © 2019 StyleShare Inc. All rights reserved. | |
| // | |
| import Foundation | |
| public extension Date { | |
| init?(iso8601String: String) { | |
| if let date = DateFormatter.iso8601.date(from: iso8601String) { | |
| self = date | |
| } else if let date = DateFormatter.iso8601Extended.date(from: iso8601String) { | |
| self = date | |
| } else { | |
| return nil | |
| } | |
| } | |
| } | |
| @objc public extension NSDate { | |
| convenience init?(iso8601String: String) { | |
| if let date = DateFormatter.iso8601.date(from: iso8601String) { | |
| self.init(timeIntervalSince1970: date.timeIntervalSince1970) | |
| } else if let date = DateFormatter.iso8601Extended.date(from: iso8601String) { | |
| self.init(timeIntervalSince1970: date.timeIntervalSince1970) | |
| } else { | |
| return nil | |
| } | |
| } | |
| } | |
| private extension DateFormatter { | |
| static let iso8601 = DateFormatter( | |
| format: "yyyy-MM-dd'T'HH:mm:ssZ", | |
| locale: "en_US_POSIX" | |
| ) | |
| static let iso8601Extended = DateFormatter( | |
| format: "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ", | |
| locale: "en_US_POSIX" | |
| ) | |
| private convenience init(format: String, locale: String) { | |
| self.init() | |
| self.dateFormat = format | |
| self.locale = Locale(identifier: locale) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Date+ISO8601Spec.swift | |
| // StyleShareTests | |
| // | |
| // Created by Suyeol Jeon on 19/09/2019. | |
| // Copyright © 2019 StyleShare Inc. All rights reserved. | |
| // | |
| import SSFoundation | |
| import SSTest | |
| final class Date_ISO8601Spec: QuickSpec { | |
| override func spec() { | |
| describe("Date") { | |
| it("ISO-8601 포맷의 문자열에서 생성할 수 있습니다.") { | |
| expect(Date(iso8601String: "1995-01-14T12:34:56+0900")?.components) == DateComponents(year: 1995, month: 1, day: 14, hour: 12, minute: 34, second: 56) | |
| } | |
| it("밀리초가 포함된 ISO-8601 포맷의 문자열에서 생성할 수 있습니다.") { | |
| expect(Date(iso8601String: "1987-11-04T13:30:25.543211+0900")?.components) == DateComponents(year: 1987, month: 11, day: 4, hour: 13, minute: 30, second: 25) | |
| } | |
| } | |
| describe("NSDate") { | |
| it("ISO-8601 포맷의 문자열에서 생성할 수 있습니다.") { | |
| expect(NSDate(iso8601String: "1995-01-14T12:34:56+0900")?.components) == DateComponents(year: 1995, month: 1, day: 14, hour: 12, minute: 34, second: 56) | |
| } | |
| it("밀리초가 포함된 ISO-8601 포맷의 문자열에서 생성할 수 있습니다.") { | |
| expect(NSDate(iso8601String: "1987-11-04T13:30:25.543211+0900")?.components) == DateComponents(year: 1987, month: 11, day: 4, hour: 13, minute: 30, second: 25) | |
| } | |
| } | |
| } | |
| } | |
| private extension Date { | |
| var components: DateComponents? { | |
| // nanoseconds의 경우 부동소수점으로 인해 정확한 비교가 어렵습니다. 실제로 나노초를 비교하는 일은 없기 때문에 굳이 검증하지 않습니다. | |
| return Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self) | |
| } | |
| } | |
| private extension NSDate { | |
| var components: DateComponents? { | |
| return (self as Date).components | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment