Skip to content

Commit 0cc6f02

Browse files
committed
Add the structrual type Date
1 parent 4b6246d commit 0cc6f02

33 files changed

+423
-195
lines changed

CoreFoundation/Base.subproj/CFRuntime.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,10 @@ void * objc_retainAutoreleasedReturnValue(void *obj) {
18191819
else return NULL;
18201820
}
18211821

1822+
CFHashCode __CFHashDouble(double d) {
1823+
return _CFHashDouble(d);
1824+
}
1825+
18221826
#endif
18231827

18241828
#undef __kCFAllocatorTypeID_CONST

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ CF_PRIVATE CF_EXPORT char *_Nullable *_Nonnull _CFEnviron(void);
243243

244244
CF_EXPORT void CFLog1(CFLogLevel lev, CFStringRef message);
245245

246+
CF_EXPORT CFHashCode __CFHashDouble(double d);
247+
246248
_CF_EXPORT_SCOPE_END
247249

248250
#endif /* __COREFOUNDATION_FORSWIFTFOUNDATIONONLY__ */

Foundation.xcodeproj/project.pbxproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
5B40F9EF1C124F47000E72E3 /* CFXMLInterface.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B40F9EB1C124F45000E72E3 /* CFXMLInterface.c */; };
6565
5B40F9F01C125011000E72E3 /* CFXMLInterface.h in Headers */ = {isa = PBXBuildFile; fileRef = 5B40F9EC1C124F45000E72E3 /* CFXMLInterface.h */; settings = {ATTRIBUTES = (Private, ); }; };
6666
5B40F9F41C12524C000E72E3 /* libxml2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B40F9F31C12524C000E72E3 /* libxml2.dylib */; };
67+
5B5C5EF01CE61FA4001346BD /* Date.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B5C5EEF1CE61FA4001346BD /* Date.swift */; };
6768
5B5D89761BBDADD300234F36 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D89751BBDADD300234F36 /* libicucore.dylib */; };
6869
5B5D89781BBDADDB00234F36 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D89771BBDADDB00234F36 /* libz.dylib */; };
6970
5B6228BB1C179041009587FE /* CFRunArray.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B6228BA1C179041009587FE /* CFRunArray.c */; };
@@ -273,9 +274,9 @@
273274
D3E8D6D11C367AB600295652 /* NSSpecialValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E8D6D01C367AB600295652 /* NSSpecialValue.swift */; };
274275
D3E8D6D31C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */; };
275276
D3E8D6D51C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */; };
276-
D5C40F331CDA1D460005690C /* TestNSOperationQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */; };
277277
D51239DF1CD9DA0800D433EE /* CFSocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88E01BBC9B0300234F36 /* CFSocket.c */; };
278278
D512D17C1CD883F00032E6A5 /* TestNSFileHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */; };
279+
D5C40F331CDA1D460005690C /* TestNSOperationQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */; };
279280
E1A03F361C4828650023AF4D /* PropertyList-1.0.dtd in Resources */ = {isa = PBXBuildFile; fileRef = E1A03F351C4828650023AF4D /* PropertyList-1.0.dtd */; };
280281
E1A03F381C482C730023AF4D /* NSXMLDTDTestData.xml in Resources */ = {isa = PBXBuildFile; fileRef = E1A03F371C482C730023AF4D /* NSXMLDTDTestData.xml */; };
281282
E1A3726F1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml in Resources */ = {isa = PBXBuildFile; fileRef = E1A3726E1C31EBFB0023AF4D /* NSXMLDocumentTestData.xml */; };
@@ -405,6 +406,7 @@
405406
5B40F9EC1C124F45000E72E3 /* CFXMLInterface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFXMLInterface.h; sourceTree = "<group>"; };
406407
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSXMLParser.swift; sourceTree = "<group>"; };
407408
5B40F9F31C12524C000E72E3 /* libxml2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libxml2.dylib; path = usr/lib/libxml2.dylib; sourceTree = SDKROOT; };
409+
5B5C5EEF1CE61FA4001346BD /* Date.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Date.swift; sourceTree = "<group>"; };
408410
5B5D885D1BBC938800234F36 /* SwiftFoundation.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftFoundation.framework; sourceTree = BUILT_PRODUCTS_DIR; };
409411
5B5D886A1BBC948300234F36 /* CFApplicationPreferences.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CFApplicationPreferences.c; sourceTree = "<group>"; };
410412
5B5D886C1BBC94C400234F36 /* CFPreferences.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFPreferences.h; sourceTree = "<group>"; };
@@ -649,8 +651,8 @@
649651
D3E8D6D01C367AB600295652 /* NSSpecialValue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSSpecialValue.swift; sourceTree = "<group>"; };
650652
D3E8D6D21C36982700295652 /* NSKeyedUnarchiver-EdgeInsetsTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-EdgeInsetsTest.plist"; sourceTree = "<group>"; };
651653
D3E8D6D41C36AC0C00295652 /* NSKeyedUnarchiver-RectTest.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "NSKeyedUnarchiver-RectTest.plist"; sourceTree = "<group>"; };
652-
D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOperationQueue.swift; sourceTree = "<group>"; };
653654
D512D17B1CD883F00032E6A5 /* TestNSFileHandle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSFileHandle.swift; sourceTree = "<group>"; };
655+
D5C40F321CDA1D460005690C /* TestNSOperationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOperationQueue.swift; sourceTree = "<group>"; };
654656
D834F9931C31C4060023812A /* TestNSOrderedSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSOrderedSet.swift; sourceTree = "<group>"; };
655657
DCDBB8321C1768AC00313299 /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
656658
E19E17DB1C2225930023AF4D /* TestNSXMLDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSXMLDocument.swift; sourceTree = "<group>"; };
@@ -1472,6 +1474,7 @@
14721474
children = (
14731475
5BDC3F301BCC5DCB00ED97BB /* NSCalendar.swift */,
14741476
5BDC3F341BCC5DCB00ED97BB /* NSDate.swift */,
1477+
5B5C5EEF1CE61FA4001346BD /* Date.swift */,
14751478
5BDC3F491BCC5DCB00ED97BB /* NSTimeZone.swift */,
14761479
);
14771480
name = DateTime;
@@ -1790,6 +1793,7 @@
17901793
5BF7AEB81BCD51F9008F214A /* NSRange.swift in Sources */,
17911794
EADE0B501BD09E3100C49C64 /* NSAttributedString.swift in Sources */,
17921795
5BF7AEBB1BCD51F9008F214A /* NSSwiftRuntime.swift in Sources */,
1796+
5B5C5EF01CE61FA4001346BD /* Date.swift in Sources */,
17931797
EADE0B9B1BD15DFF00C49C64 /* NSFileHandle.swift in Sources */,
17941798
EADE0BB11BD15E0000C49C64 /* NSPredicate.swift in Sources */,
17951799
5BF7AEC01BCD51F9008F214A /* NSUUID.swift in Sources */,

Foundation/Date.swift

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
//
2+
// Date.swift
3+
// Foundation
4+
//
5+
// Created by Philippe Hausler on 5/13/16.
6+
// Copyright © 2016 Apple. All rights reserved.
7+
//
8+
9+
import CoreFoundation
10+
11+
/**
12+
`Date` structs represent a single point in time.
13+
*/
14+
public struct Date : Comparable, Equatable, CustomStringConvertible {
15+
public typealias ReferenceType = NSDate
16+
17+
private var _time : NSTimeInterval
18+
19+
/// The number of seconds from 1 January 1970 to the reference date, 1 January 2001.
20+
public static let timeIntervalBetween1970AndReferenceDate = 978307200.0
21+
22+
/// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.
23+
public static var timeIntervalSinceReferenceDate : NSTimeInterval {
24+
return CFAbsoluteTimeGetCurrent()
25+
}
26+
27+
/// Returns a `Date` initialized to the current date and time.
28+
public init() {
29+
_time = CFAbsoluteTimeGetCurrent()
30+
}
31+
32+
/// Returns a `Date` initialized relative to the current date and time by a given number of seconds.
33+
public init(timeIntervalSinceNow: NSTimeInterval) {
34+
self.init(timeIntervalSinceReferenceDate: timeIntervalSinceNow + CFAbsoluteTimeGetCurrent())
35+
}
36+
37+
/// Returns a `Date` initialized relative to 00:00:00 UTC on 1 January 1970 by a given number of seconds.
38+
public init(timeIntervalSince1970: NSTimeInterval) {
39+
self.init(timeIntervalSinceReferenceDate: timeIntervalSince1970 - Date.timeIntervalBetween1970AndReferenceDate)
40+
}
41+
42+
/**
43+
Returns a `Date` initialized relative to another given date by a given number of seconds.
44+
45+
- Parameter timeInterval: The number of seconds to add to `date`. A negative value means the receiver will be earlier than `date`.
46+
- Parameter date: The reference date.
47+
*/
48+
public init(timeInterval: NSTimeInterval, since date: Date) {
49+
self.init(timeIntervalSinceReferenceDate: date.timeIntervalSinceReferenceDate + timeInterval)
50+
}
51+
52+
/// Returns an `Date` initialized relative to 00:00:00 UTC on 1 January 2001 by a given number of seconds.
53+
public init(timeIntervalSinceReferenceDate ti: NSTimeInterval) {
54+
_time = ti
55+
}
56+
57+
/// Returns a `Date` initialized with the value from a `NSDate`. `Date` does not store the reference after initialization.
58+
private init(reference: NSDate) {
59+
self.init(timeIntervalSinceReferenceDate: reference.timeIntervalSinceReferenceDate)
60+
}
61+
62+
private var reference : NSDate {
63+
return NSDate(timeIntervalSinceReferenceDate: _time)
64+
}
65+
66+
/**
67+
Returns the interval between the date object and 00:00:00 UTC on 1 January 2001.
68+
69+
This property’s value is negative if the date object is earlier than the system’s absolute reference date (00:00:00 UTC on 1 January 2001).
70+
*/
71+
public var timeIntervalSinceReferenceDate: NSTimeInterval {
72+
return _time
73+
}
74+
75+
/**
76+
Returns the interval between the receiver and another given date.
77+
78+
- Parameter another: The date with which to compare the receiver.
79+
80+
- Returns: The interval between the receiver and the `another` parameter. If the receiver is earlier than `anotherDate`, the return value is negative. If `anotherDate` is `nil`, the results are undefined.
81+
82+
- SeeAlso: `timeIntervalSince1970`
83+
- SeeAlso: `timeIntervalSinceNow`
84+
- SeeAlso: `timeIntervalSinceReferenceDate`
85+
*/
86+
public func timeIntervalSince(_ date: Date) -> NSTimeInterval {
87+
return self.timeIntervalSinceReferenceDate - date.timeIntervalSinceReferenceDate
88+
}
89+
90+
/**
91+
The time interval between the date and the current date and time.
92+
93+
If the date is earlier than the current date and time, the this property’s value is negative.
94+
95+
- SeeAlso: `timeIntervalSince(_:)`
96+
- SeeAlso: `timeIntervalSince1970`
97+
- SeeAlso: `timeIntervalSinceReferenceDate`
98+
*/
99+
public var timeIntervalSinceNow: NSTimeInterval {
100+
return self.timeIntervalSinceReferenceDate - CFAbsoluteTimeGetCurrent()
101+
}
102+
103+
/**
104+
The interval between the date object and 00:00:00 UTC on 1 January 1970.
105+
106+
This property’s value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970.
107+
108+
- SeeAlso: `timeIntervalSince(_:)`
109+
- SeeAlso: `timeIntervalSinceNow`
110+
- SeeAlso: `timeIntervalSinceReferenceDate`
111+
*/
112+
public var timeIntervalSince1970: NSTimeInterval {
113+
return self.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate
114+
}
115+
116+
/**
117+
Creates and returns a Date value representing a date in the distant future.
118+
119+
The distant future is in terms of centuries.
120+
*/
121+
public static let distantFuture = Date(timeIntervalSinceReferenceDate: 63113904000.0)
122+
123+
/**
124+
Creates and returns a Date value representing a date in the distant past.
125+
126+
The distant past is in terms of centuries.
127+
*/
128+
public static let distantPast = Date(timeIntervalSinceReferenceDate: -63114076800.0)
129+
130+
public var hashValue: Int {
131+
if #available(OSX 10.12, iOS 10.0, *) {
132+
return Int(bitPattern: __CFHashDouble(_time))
133+
} else { // 10.11 and previous behavior fallback; this must allocate a date to reference the hash value and then throw away the reference
134+
return NSDate(timeIntervalSinceReferenceDate: _time).hash
135+
}
136+
}
137+
138+
public func compare(_ other: Date) -> NSComparisonResult {
139+
if _time < other.timeIntervalSinceReferenceDate {
140+
return .OrderedAscending
141+
} else if _time > other.timeIntervalSinceReferenceDate {
142+
return .OrderedDescending
143+
} else {
144+
return .OrderedSame
145+
}
146+
}
147+
148+
/**
149+
A string representation of the date object (read-only).
150+
151+
The representation is useful for debugging only.
152+
153+
There are a number of options to acquire a formatted string for a date including: date formatters (see
154+
[NSDateFormatter](//apple_ref/occ/cl/NSDateFormatter) and [Data Formatting Guide](//apple_ref/doc/uid/10000029i)), and the `Date` functions `description(locale:)`.
155+
*/
156+
public var description: String {
157+
// Defer to NSDate for description
158+
return NSDate(timeIntervalSinceReferenceDate: _time).description
159+
}
160+
161+
/**
162+
Returns a string representation of the receiver using the given
163+
locale.
164+
165+
- Parameter locale: A `Locale` object. If you pass `nil`, `NSDate` formats the date in the same way as the `description` property.
166+
167+
- Returns: A string representation of the receiver, using the given locale, or if the locale argument is `nil`, in the international format `YYYY-MM-DD HH:MM:SS ±HHMM`, where `±HHMM` represents the time zone offset in hours and minutes from UTC (for example, “`2001-03-24 10:45:32 +0600`”)
168+
*/
169+
public func description(with locale : NSLocale?) -> String {
170+
return NSDate(timeIntervalSinceReferenceDate: _time).descriptionWithLocale(locale)
171+
}
172+
173+
public var debugDescription: String { return description }
174+
}
175+
176+
/// Returns true if the two Date values are equal.
177+
public func ==(lhs: Date, rhs: Date) -> Bool {
178+
return lhs.timeIntervalSinceReferenceDate == rhs.timeIntervalSinceReferenceDate
179+
}
180+
181+
/// Returns true if the left hand Date is less than the right hand Date.
182+
public func <(lhs: Date, rhs: Date) -> Bool {
183+
return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate
184+
}
185+
186+
/// Returns a Date with a specified amount of time added to it.
187+
public func +(lhs: Date, rhs: NSTimeInterval) -> Date {
188+
return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate + rhs)
189+
}
190+
191+
/// Returns a Date with a specified amount of time subtracted from it.
192+
public func -(lhs: Date, rhs: NSTimeInterval) -> Date {
193+
return Date(timeIntervalSinceReferenceDate: lhs.timeIntervalSinceReferenceDate - rhs)
194+
}
195+
196+
public func +=(lhs: inout Date, rhs: NSTimeInterval) {
197+
lhs = lhs + rhs
198+
}
199+
200+
public func -=(lhs: inout Date, rhs: NSTimeInterval) {
201+
lhs = lhs - rhs
202+
}

0 commit comments

Comments
 (0)