Skip to content

Commit 9fbb5e7

Browse files
committed
Updated JSDate
1 parent cfc85ce commit 9fbb5e7

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

Example/JavaScriptKitExample/Sources/JavaScriptKitExample/main.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ divElement.innerText = "Swift Bluetooth Web App"
99
let body = document.body.object!
1010
_ = body.appendChild!(divElement)
1111

12-
JSConsole.log("Date:", Date())
12+
let date = Date()
13+
JSConsole.info("Date:", date)
14+
JSConsole.log(date.description)
1315

1416
if let bluetooth = JSBluetooth.shared {
1517
bluetooth.isAvailable.then {

Sources/JavaScriptKit/Foundation/Date.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public extension SwiftFoundation.Date {
7272
extension SwiftFoundation.Date: CustomStringConvertible {
7373

7474
public var description: String {
75-
return JSDate(self).description
75+
return JSDate(self).toUTCString()
7676
}
7777
}
7878

@@ -96,10 +96,31 @@ public extension JSDate {
9696
}
9797
}
9898

99+
public extension SwiftFoundation.Date {
100+
101+
init(_ date: JSDate) {
102+
self.init(timeIntervalSince1970: date.rawValue)
103+
}
104+
}
105+
106+
// MARK: - JSValueConvertible
107+
99108
extension SwiftFoundation.Date: JSValueConvertible {
100109

101110
public func jsValue() -> JSValue {
102111
let date = JSDate()
103112
return date.jsValue()
104113
}
105114
}
115+
116+
// MARK: - JSValueConstructible
117+
118+
extension SwiftFoundation.Date: JSValueConstructible {
119+
120+
public static func construct(from value: JSValue) -> Date? {
121+
return value.object
122+
.flatMap { JSDate($0) }
123+
.flatMap { Date($0) }
124+
?? value.number.flatMap { Date(timeIntervalSince1970: $0) }
125+
}
126+
}

Sources/JavaScriptKit/JS Types/JSDate.swift

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,52 @@ public final class JSDate: JSType {
5353
///
5454
/// - Returns: A Number representing the milliseconds elapsed since the UNIX epoch.
5555
public static var now: Double {
56-
guard let function = classObject.now.function,
57-
let timeInterval = function().number
58-
else { fatalError() }
59-
return timeInterval
56+
let function = classObject.now.function.assert()
57+
return function().number.assert()
58+
}
59+
60+
// MARK: - Accessors
61+
62+
/// Converts a date to a string following the ISO 8601 Extended Format.
63+
public func toISOString() -> String {
64+
let function = jsObject.toISOString.function.assert()
65+
return function.apply(this: jsObject).string.assert()
66+
}
67+
68+
/// Returns a string representing the Date using `toISOString()`. Intended for use by JSON.stringify().
69+
public func toJSON() -> String {
70+
let function = jsObject.toJSON.function.assert()
71+
return function.apply(this: jsObject).string.assert()
72+
}
73+
74+
/// Converts a date to a string using the UTC timezone.
75+
public func toUTCString() -> String {
76+
let function = jsObject.toUTCString.function.assert()
77+
return function.apply(this: jsObject).string.assert()
6078
}
6179
}
6280

81+
// MARK: - Constants
82+
6383
internal extension JSDate {
6484

6585
static let classObject = JSObjectRef.global.Date.function!
6686
}
6787

88+
// MARK: - RawRepresentable
89+
90+
extension JSDate: RawRepresentable {
91+
92+
public convenience init(rawValue: Double) {
93+
self.init(timeInterval: rawValue)
94+
}
95+
96+
public var rawValue: Double {
97+
let function = jsObject.valueOf.function.assert("Invalid function \(#function)")
98+
return function.apply(this: jsObject).number.assert()
99+
}
100+
}
101+
68102
// MARK: - CustomStringConvertible
69103

70104
extension JSDate: CustomStringConvertible { }

0 commit comments

Comments
 (0)