1
+ /** A wrapper around the [JavaScript Date
2
+ class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) that
3
+ exposes its properties in a type-safe way. This doesn't 100% match the JS API, for example
4
+ `getMonth`/`setMonth` etc accessor methods are converted to properties, but the rest of it matches
5
+ in the naming. Parts of the JavaScript `Date` API that are not consistent across browsers and JS
6
+ implementations are not exposed in a type-safe manner, you should access the underlying `jsObject`
7
+ property if you need those.
8
+ */
1
9
public final class JSDate {
10
+ /// The constructor function used to create new `Date` objects.
2
11
private static let constructor = JSObject . global. Date. function!
3
- public let ref : JSObject
4
12
13
+ /// The underlying JavaScript `Date` object.
14
+ public let jsObject : JSObject
15
+
16
+ /** Creates a new instance of the JavaScript `Date` class with a given amount of milliseconds
17
+ that passed since midnight 01 January 1970 UTC.
18
+ */
5
19
public init ( millisecondsSinceEpoch: Double ? = nil ) {
6
20
if let milliseconds = millisecondsSinceEpoch {
7
- ref = Self . constructor. new ( milliseconds)
21
+ jsObject = Self . constructor. new ( milliseconds)
8
22
} else {
9
- ref = Self . constructor. new ( )
23
+ jsObject = Self . constructor. new ( )
10
24
}
11
25
}
12
26
@@ -22,199 +36,210 @@ public final class JSDate {
22
36
seconds: Int = 0 ,
23
37
milliseconds: Int = 0
24
38
) {
25
- ref = Self . constructor. new ( year, monthIndex, day, hours, minutes, seconds, milliseconds)
39
+ jsObject = Self . constructor. new ( year, monthIndex, day, hours, minutes, seconds, milliseconds)
26
40
}
27
41
28
42
/// Year of this date in local time zone.
29
43
public var fullYear : Int {
30
44
get {
31
- Int ( ref . getFullYear!( ) . number!)
45
+ Int ( jsObject . getFullYear!( ) . number!)
32
46
}
33
47
set {
34
- _ = ref . setFullYear!( newValue)
48
+ _ = jsObject . setFullYear!( newValue)
35
49
}
36
50
}
37
51
38
- /// Month of this date in `0–11` range in local time zone
52
+ /// Month of this date in `0–11` range in local time zone.
39
53
public var month : Int {
40
54
get {
41
- Int ( ref . getMonth!( ) . number!)
55
+ Int ( jsObject . getMonth!( ) . number!)
42
56
}
43
57
set {
44
- _ = ref . setMonth!( newValue)
58
+ _ = jsObject . setMonth!( newValue)
45
59
}
46
60
}
47
61
48
62
/// The day of the month in `1..31` range in local time zone.
49
63
public var date : Int {
50
64
get {
51
- Int ( ref . getDate!( ) . number!)
65
+ Int ( jsObject . getDate!( ) . number!)
52
66
}
53
67
set {
54
- _ = ref . setDate!( newValue)
68
+ _ = jsObject . setDate!( newValue)
55
69
}
56
70
}
57
71
58
72
/// The day of the week in `0..6` range in local time zone.
59
73
public var day : Int {
60
- Int ( ref . getDay!( ) . number!)
74
+ Int ( jsObject . getDay!( ) . number!)
61
75
}
62
76
63
77
/// The amount of hours in this day from `0..23` range in local time zone.
64
78
public var hours : Int {
65
79
get {
66
- Int ( ref . getHours!( ) . number!)
80
+ Int ( jsObject . getHours!( ) . number!)
67
81
}
68
82
set {
69
- _ = ref . setHours!( newValue)
83
+ _ = jsObject . setHours!( newValue)
70
84
}
71
85
}
72
86
73
87
/// The amount of minutes in this hours from `0..59` range in local time zone.
74
88
public var minutes : Int {
75
89
get {
76
- Int ( ref . getMinutes!( ) . number!)
90
+ Int ( jsObject . getMinutes!( ) . number!)
77
91
}
78
92
set {
79
- _ = ref . setMinutes!( newValue)
93
+ _ = jsObject . setMinutes!( newValue)
80
94
}
81
95
}
82
96
83
97
/// The amount of seconds in this minute from `0..59` range in local time zone.
84
98
public var seconds : Int {
85
99
get {
86
- Int ( ref . getSeconds!( ) . number!)
100
+ Int ( jsObject . getSeconds!( ) . number!)
87
101
}
88
102
set {
89
- _ = ref . setSeconds!( newValue)
103
+ _ = jsObject . setSeconds!( newValue)
90
104
}
91
105
}
92
106
93
107
/// The amount of milliseconds in this second `0..999` range in local time zone.
94
108
public var milliseconds : Int {
95
109
get {
96
- Int ( ref . getMilliseconds!( ) . number!)
110
+ Int ( jsObject . getMilliseconds!( ) . number!)
97
111
}
98
112
set {
99
- _ = ref . setMilliseconds!( newValue)
113
+ _ = jsObject . setMilliseconds!( newValue)
100
114
}
101
115
}
102
116
103
- /// Year of this date in the UTC time zone
117
+ /// Year of this date in the UTC time zone.
104
118
public var utcFullYear : Int {
105
119
get {
106
- Int ( ref . getUTCFullYear!( ) . number!)
120
+ Int ( jsObject . getUTCFullYear!( ) . number!)
107
121
}
108
122
set {
109
- _ = ref . setUTCFullYear!( newValue)
123
+ _ = jsObject . setUTCFullYear!( newValue)
110
124
}
111
125
}
112
126
113
- /// Month of this date in `0–11` range in the UTC time zone
127
+ /// Month of this date in `0–11` range in the UTC time zone.
114
128
public var utcMonth : Int {
115
129
get {
116
- Int ( ref . getUTCMonth!( ) . number!)
130
+ Int ( jsObject . getUTCMonth!( ) . number!)
117
131
}
118
132
set {
119
- _ = ref . setUTCMonth!( newValue)
133
+ _ = jsObject . setUTCMonth!( newValue)
120
134
}
121
135
}
122
136
123
- /// The day of the month in `1..31` range in the UTC time zone
137
+ /// The day of the month in `1..31` range in the UTC time zone.
124
138
public var utcDate : Int {
125
139
get {
126
- Int ( ref . getUTCDate!( ) . number!)
140
+ Int ( jsObject . getUTCDate!( ) . number!)
127
141
}
128
142
set {
129
- _ = ref . setUTCDate!( newValue)
143
+ _ = jsObject . setUTCDate!( newValue)
130
144
}
131
145
}
132
146
133
- /// The day of the week in `0..6` range in the UTC time zone
147
+ /// The day of the week in `0..6` range in the UTC time zone.
134
148
public var utcDay : Int {
135
- Int ( ref . getUTCDay!( ) . number!)
149
+ Int ( jsObject . getUTCDay!( ) . number!)
136
150
}
137
151
138
- /// The amount of hours in this day from `0..23` range in the UTC time zone
152
+ /// The amount of hours in this day from `0..23` range in the UTC time zone.
139
153
public var utcHours : Int {
140
154
get {
141
- Int ( ref . getUTCHours!( ) . number!)
155
+ Int ( jsObject . getUTCHours!( ) . number!)
142
156
}
143
157
set {
144
- _ = ref . setUTCHours!( newValue)
158
+ _ = jsObject . setUTCHours!( newValue)
145
159
}
146
160
}
147
161
148
- /// The amount of minutes in this hours from `0..59` range in the UTC time zone
162
+ /// The amount of minutes in this hours from `0..59` range in the UTC time zone.
149
163
public var utcMinutes : Int {
150
164
get {
151
- Int ( ref . getUTCMinutes!( ) . number!)
165
+ Int ( jsObject . getUTCMinutes!( ) . number!)
152
166
}
153
167
set {
154
- _ = ref . setUTCMinutes!( newValue)
168
+ _ = jsObject . setUTCMinutes!( newValue)
155
169
}
156
170
}
157
171
158
- /// The amount of seconds in this minute from `0..59` range in the UTC time zone
172
+ /// The amount of seconds in this minute from `0..59` range in the UTC time zone.
159
173
public var utcSeconds : Int {
160
174
get {
161
- Int ( ref . getUTCSeconds!( ) . number!)
175
+ Int ( jsObject . getUTCSeconds!( ) . number!)
162
176
}
163
177
set {
164
- _ = ref . setUTCSeconds!( newValue)
178
+ _ = jsObject . setUTCSeconds!( newValue)
165
179
}
166
180
}
167
181
168
- /// The amount of milliseconds in this second `0..999` range in the UTC time zone
182
+ /// The amount of milliseconds in this second `0..999` range in the UTC time zone.
169
183
public var utcMilliseconds : Int {
170
184
get {
171
- Int ( ref . getUTCMilliseconds!( ) . number!)
185
+ Int ( jsObject . getUTCMilliseconds!( ) . number!)
172
186
}
173
187
set {
174
- _ = ref . setUTCMilliseconds!( newValue)
188
+ _ = jsObject . setUTCMilliseconds!( newValue)
175
189
}
176
190
}
177
191
178
- /// Offset in minutes between the local time zone and UTC
192
+ /// Offset in minutes between the local time zone and UTC.
179
193
public var timezoneOffset : Int {
180
- Int ( ref . getTimezoneOffset!( ) . number!)
194
+ Int ( jsObject . getTimezoneOffset!( ) . number!)
181
195
}
182
196
197
+ /// Returns a string conforming to ISO 8601 that contains date and time, e.g.
198
+ /// `"2020-09-15T08:56:54.811Z"`.
183
199
public func toISOString( ) -> String {
184
- ref . toISOString!( ) . string!
200
+ jsObject . toISOString!( ) . string!
185
201
}
186
202
203
+ /// Returns a string with date parts in a format defined by user's locale, e.g. `"9/15/2020"`.
187
204
public func toLocaleDateString( ) -> String {
188
- ref . toLocaleDateString!( ) . string!
205
+ jsObject . toLocaleDateString!( ) . string!
189
206
}
190
207
208
+ /// Returns a string with time parts in a format defined by user's locale, e.g. `"10:04:14"`.
191
209
public func toLocaleTimeString( ) -> String {
192
- ref . toLocaleTimeString!( ) . string!
210
+ jsObject . toLocaleTimeString!( ) . string!
193
211
}
194
212
213
+ /** Returns a string formatted according to
214
+ [rfc7231](https://tools.ietf.org/html/rfc7231#section-7.1.1.1) and modified according to
215
+ [ecma-262](https://www.ecma-international.org/ecma-262/10.0/index.html#sec-date.prototype.toutcstring),
216
+ e.g. `Tue, 15 Sep 2020 09:04:40 GMT`.
217
+ */
195
218
public func toUTCString( ) -> String {
196
- ref . toUTCString!( ) . string!
219
+ jsObject . toUTCString!( ) . string!
197
220
}
198
221
199
- /// Number of milliseconds since midnight 01 January 1970 UTC to the present moment ignoring leap
200
- /// seconds
222
+ /** Number of milliseconds since midnight 01 January 1970 UTC to the present moment ignoring
223
+ leap seconds.
224
+ */
201
225
public static func now( ) -> Double {
202
226
constructor. now!( ) . number!
203
227
}
204
228
205
- /// Number of milliseconds since midnight 01 January 1970 UTC to the given date ignoring leap
206
- /// seconds
229
+ /** Number of milliseconds since midnight 01 January 1970 UTC to the given date ignoring leap
230
+ seconds.
231
+ */
207
232
public func valueOf( ) -> Double {
208
- ref . valueOf!( ) . number!
233
+ jsObject . valueOf!( ) . number!
209
234
}
210
235
}
211
236
212
237
extension JSDate : Comparable {
213
- public static func == ( lhs: JSDate , rhs: JSDate ) -> Bool {
238
+ public static func == ( lhs: JSDate , rhs: JSDate ) -> Bool {
214
239
return lhs. valueOf ( ) == rhs. valueOf ( )
215
240
}
216
241
217
- public static func < ( lhs: JSDate , rhs: JSDate ) -> Bool {
242
+ public static func < ( lhs: JSDate , rhs: JSDate ) -> Bool {
218
243
return lhs. valueOf ( ) < rhs. valueOf ( )
219
244
}
220
245
}
0 commit comments