|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | + |
| 10 | +import CoreFoundation |
| 11 | + |
| 12 | +extension ISO8601DateFormatter { |
| 13 | + |
| 14 | + public struct Options : OptionSet { |
| 15 | + |
| 16 | + public private(set) var rawValue: UInt |
| 17 | + |
| 18 | + public init(rawValue: UInt) { self.rawValue = rawValue } |
| 19 | + |
| 20 | + public static var withYear = ISO8601DateFormatter.Options(rawValue: 1 << 0) |
| 21 | + |
| 22 | + public static var withMonth = ISO8601DateFormatter.Options(rawValue: 1 << 1) |
| 23 | + |
| 24 | + public static var withWeekOfYear = ISO8601DateFormatter.Options(rawValue: 1 << 2) |
| 25 | + |
| 26 | + public static var withDay = ISO8601DateFormatter.Options(rawValue: 1 << 4) |
| 27 | + |
| 28 | + public static var withTime = ISO8601DateFormatter.Options(rawValue: 1 << 5) |
| 29 | + |
| 30 | + public static var withTimeZone = ISO8601DateFormatter.Options(rawValue: 1 << 6) |
| 31 | + |
| 32 | + public static var withSpaceBetweenDateAndTime = ISO8601DateFormatter.Options(rawValue: 1 << 7) |
| 33 | + |
| 34 | + public static var withDashSeparatorInDate = ISO8601DateFormatter.Options(rawValue: 1 << 8) |
| 35 | + |
| 36 | + public static var withColonSeparatorInTime = ISO8601DateFormatter.Options(rawValue: 1 << 9) |
| 37 | + |
| 38 | + public static var withColonSeparatorInTimeZone = ISO8601DateFormatter.Options(rawValue: 1 << 10) |
| 39 | + |
| 40 | + public static var withFullDate = ISO8601DateFormatter.Options(rawValue: withYear.rawValue + withMonth.rawValue + withDay.rawValue + withDashSeparatorInDate.rawValue) |
| 41 | + |
| 42 | + public static var withFullTime = ISO8601DateFormatter.Options(rawValue: withTime.rawValue + withTimeZone.rawValue + withColonSeparatorInTime.rawValue + withColonSeparatorInTimeZone.rawValue) |
| 43 | + |
| 44 | + public static var withInternetDateTime = ISO8601DateFormatter.Options(rawValue: withFullDate.rawValue + withFullTime.rawValue) |
| 45 | + } |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +open class ISO8601DateFormatter : Formatter, NSSecureCoding { |
| 50 | + |
| 51 | + typealias CFType = CFDateFormatter |
| 52 | + private var __cfObject: CFType? |
| 53 | + private var _cfObject: CFType { |
| 54 | + guard let obj = __cfObject else { |
| 55 | + #if os(OSX) || os(iOS) |
| 56 | + let format = CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue) |
| 57 | + #else |
| 58 | + let format = CFISO8601DateFormatOptions(self.formatOptions.rawValue) |
| 59 | + #endif |
| 60 | + let obj = CFDateFormatterCreateISO8601Formatter(kCFAllocatorSystemDefault, format)! |
| 61 | + CFDateFormatterSetProperty(obj, kCFDateFormatterTimeZone, timeZone._cfObject) |
| 62 | + __cfObject = obj |
| 63 | + return obj |
| 64 | + } |
| 65 | + return obj |
| 66 | + } |
| 67 | + |
| 68 | + /* Please note that there can be a significant performance cost when resetting these properties. Resetting each property can result in regenerating the entire CFDateFormatterRef, which can be very expensive. */ |
| 69 | + |
| 70 | + open var timeZone: TimeZone! { willSet { _reset() } } |
| 71 | + |
| 72 | + open var formatOptions: ISO8601DateFormatter.Options { willSet { _reset() } } |
| 73 | + |
| 74 | + public override init() { |
| 75 | + timeZone = TimeZone(identifier: "GMT") |
| 76 | + formatOptions = [.withInternetDateTime, .withDashSeparatorInDate, .withColonSeparatorInTime, .withColonSeparatorInTimeZone] |
| 77 | + super.init() |
| 78 | + } |
| 79 | + |
| 80 | + public required init?(coder aDecoder: NSCoder) { NSUnimplemented() } |
| 81 | + open override func encode(with aCoder: NSCoder) { NSUnimplemented() } |
| 82 | + public static var supportsSecureCoding: Bool { return true } |
| 83 | + |
| 84 | + open func string(from date: Date) -> String { |
| 85 | + return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, _cfObject, date._cfObject)._swiftObject |
| 86 | + } |
| 87 | + |
| 88 | + open func date(from string: String) -> Date? { |
| 89 | + |
| 90 | + var range = CFRange(location: 0, length: string.length) |
| 91 | + let date = withUnsafeMutablePointer(to: &range) { (rangep: UnsafeMutablePointer<CFRange>) -> Date? in |
| 92 | + guard let res = CFDateFormatterCreateDateFromString(kCFAllocatorSystemDefault, _cfObject, string._cfObject, rangep) else { |
| 93 | + return nil |
| 94 | + } |
| 95 | + return res._swiftObject |
| 96 | + } |
| 97 | + return date |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + open class func string(from date: Date, timeZone: TimeZone, formatOptions: ISO8601DateFormatter.Options = []) -> String { |
| 102 | + |
| 103 | + #if os(OSX) || os(iOS) |
| 104 | + let format = CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue) |
| 105 | + #else |
| 106 | + let format = CFISO8601DateFormatOptions(formatOptions.rawValue) |
| 107 | + #endif |
| 108 | + |
| 109 | + let obj = CFDateFormatterCreateISO8601Formatter(kCFAllocatorSystemDefault, format) |
| 110 | + CFDateFormatterSetProperty(obj, kCFDateFormatterTimeZone, timeZone._cfObject) |
| 111 | + return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, obj, date._cfObject)._swiftObject |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + private func _reset() { |
| 116 | + __cfObject = nil |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments