forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateIntervalFormatter.swift
183 lines (155 loc) · 7.18 KB
/
DateIntervalFormatter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
import CoreFoundation
#if _runtime(_ObjC)
internal let kCFDateIntervalFormatterNoStyle = CFDateIntervalFormatterStyle.noStyle
internal let kCFDateIntervalFormatterShortStyle = CFDateIntervalFormatterStyle.shortStyle
internal let kCFDateIntervalFormatterMediumStyle = CFDateIntervalFormatterStyle.mediumStyle
internal let kCFDateIntervalFormatterLongStyle = CFDateIntervalFormatterStyle.longStyle
internal let kCFDateIntervalFormatterFullStyle = CFDateIntervalFormatterStyle.fullStyle
internal let kCFDateIntervalFormatterBoundaryStyleDefault = _CFDateIntervalFormatterBoundaryStyle.cfDateIntervalFormatterBoundaryStyleDefault
internal let kCFDateIntervalFormatterBoundaryStyleMinimizeAdjacentMonths = _CFDateIntervalFormatterBoundaryStyle.cfDateIntervalFormatterBoundaryStyleMinimizeAdjacentMonths
#endif
extension DateIntervalFormatter {
// Keep these in sync with CFDateIntervalFormatterStyle.
public enum Style: UInt {
case none = 0
case short = 1
case medium = 2
case long = 3
case full = 4
}
}
internal extension DateIntervalFormatter.Style {
init(_ cfStyle: CFDateIntervalFormatterStyle) {
switch cfStyle {
case kCFDateIntervalFormatterNoStyle: self = .none
case kCFDateIntervalFormatterShortStyle: self = .short
case kCFDateIntervalFormatterMediumStyle: self = .medium
case kCFDateIntervalFormatterLongStyle: self = .long
case kCFDateIntervalFormatterFullStyle: self = .full
default: fatalError()
}
}
}
internal extension CFDateIntervalFormatterStyle {
init(_ style: DateIntervalFormatter.Style) {
switch style {
case .none: self = kCFDateIntervalFormatterNoStyle
case .short: self = kCFDateIntervalFormatterShortStyle
case .medium: self = kCFDateIntervalFormatterMediumStyle
case .long: self = kCFDateIntervalFormatterLongStyle
case .full: self = kCFDateIntervalFormatterFullStyle
}
}
}
internal extension DateIntervalFormatter.BoundaryStyle {
init(_ cfStyle: _CFDateIntervalFormatterBoundaryStyle) {
switch cfStyle {
case kCFDateIntervalFormatterBoundaryStyleDefault: self = .default
case kCFDateIntervalFormatterBoundaryStyleMinimizeAdjacentMonths: self = .minimizeAdjacentMonths
default: fatalError()
}
}
}
internal extension _CFDateIntervalFormatterBoundaryStyle {
init(_ style: DateIntervalFormatter.BoundaryStyle) {
switch style {
case .default: self = kCFDateIntervalFormatterBoundaryStyleDefault
case .minimizeAdjacentMonths: self = kCFDateIntervalFormatterBoundaryStyleMinimizeAdjacentMonths
}
}
}
// DateIntervalFormatter is used to format the range between two NSDates in a locale-sensitive way.
// DateIntervalFormatter returns nil and NO for all methods in Formatter.
open class DateIntervalFormatter: Formatter {
let core: CFDateIntervalFormatter
public override init() {
core = CFDateIntervalFormatterCreate(nil, nil, kCFDateIntervalFormatterMediumStyle, kCFDateIntervalFormatterMediumStyle)
super.init()
}
private init(cfFormatter: CFDateIntervalFormatter) {
self.core = cfFormatter
super.init()
}
public required init?(coder: NSCoder) {
NSUnimplemented()
}
/*@NSCopying*/ open var locale: Locale! {
get { return CFDateIntervalFormatterCopyLocale(core)._swiftObject }
set { CFDateIntervalFormatterSetLocale(core, newValue?._cfObject) }
}
/*@NSCopying*/ open var calendar: Calendar! {
get { return CFDateIntervalFormatterCopyCalendar(core)._swiftObject }
set { CFDateIntervalFormatterSetCalendar(core, newValue?._cfObject) }
}
/*@NSCopying*/ open var timeZone: TimeZone! {
get { return CFDateIntervalFormatterCopyTimeZone(core)._swiftObject }
set { CFDateIntervalFormatterSetTimeZone(core, newValue?._cfObject) }
}
open var dateTemplate: String! {
get { return CFDateIntervalFormatterCopyDateTemplate(core)._swiftObject }
set { CFDateIntervalFormatterSetDateTemplate(core, newValue?._cfObject) }
}
open var dateStyle: Style {
get { return Style(CFDateIntervalFormatterGetDateStyle(core)) }
set { CFDateIntervalFormatterSetDateStyle(core, CFDateIntervalFormatterStyle(newValue)) }
}
open var timeStyle: Style {
get { return Style(CFDateIntervalFormatterGetTimeStyle(core)) }
set { CFDateIntervalFormatterSetTimeStyle(core, CFDateIntervalFormatterStyle(newValue)) }
}
internal enum BoundaryStyle: UInt {
case `default` = 0
case minimizeAdjacentMonths = 1
}
internal var boundaryStyle: BoundaryStyle {
get { return BoundaryStyle(_CFDateIntervalFormatterGetBoundaryStyle(core)) }
set { _CFDateIntervalFormatterSetBoundaryStyle(core, _CFDateIntervalFormatterBoundaryStyle(newValue) )}
}
/*
If the range smaller than the resolution specified by the dateTemplate, a single date format will be produced. If the range is larger than the format specified by the dateTemplate, a locale-specific fallback will be used to format the items missing from the pattern.
For example, if the range is 2010-03-04 07:56 - 2010-03-04 19:56 (12 hours)
- The pattern jm will produce
for en_US, "7:56 AM - 7:56 PM"
for en_GB, "7:56 - 19:56"
- The pattern MMMd will produce
for en_US, "Mar 4"
for en_GB, "4 Mar"
If the range is 2010-03-04 07:56 - 2010-03-08 16:11 (4 days, 8 hours, 15 minutes)
- The pattern jm will produce
for en_US, "3/4/2010 7:56 AM - 3/8/2010 4:11 PM"
for en_GB, "4/3/2010 7:56 - 8/3/2010 16:11"
- The pattern MMMd will produce
for en_US, "Mar 4-8"
for en_GB, "4-8 Mar"
*/
open func string(from fromDate: Date, to toDate: Date) -> String {
return CFDateIntervalFormatterCreateStringFromDateToDate(core, fromDate._cfObject, toDate._cfObject)._swiftObject
}
open func string(from dateInterval: DateInterval) -> String? {
let result = CFDateIntervalFormatterCreateStringFromDateToDate(core, dateInterval.start._cfObject, dateInterval.end._cfObject)._swiftObject
return result.isEmpty ? nil : result
}
open override func string(for obj: Any) -> String? {
guard let interval = obj as? DateInterval else {
return nil
}
return string(from: interval)
}
open override func editingString(for obj: Any) -> String? {
return nil
}
open override func objectValue(_ string: String) throws -> Any? {
return nil
}
open override func copy(with zone: NSZone? = nil) -> Any {
return DateIntervalFormatter(cfFormatter: CFDateIntervalFormatterCreateCopy(nil, core))
}
}