forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDate.swift
More file actions
127 lines (93 loc) · 3.46 KB
/
Date.swift
File metadata and controls
127 lines (93 loc) · 3.46 KB
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
//
// Date.swift
// JavaScriptKit
//
// Created by Alsey Coleman Miller on 6/4/20.
//
import SwiftFoundation
#if arch(wasm32)
// MARK: - Time
internal extension JSDate {
/// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.
static var timeIntervalSinceReferenceDate: SwiftFoundation.TimeInterval {
return JSDate.now - Date.timeIntervalBetween1970AndReferenceDate
}
}
public extension SwiftFoundation.Date {
/// Returns a `Date` initialized to the current date and time.
init() {
self.init(timeIntervalSinceReferenceDate: JSDate.timeIntervalSinceReferenceDate)
}
/// Returns a `Date` initialized relative to the current date and time by a given number of seconds.
init(timeIntervalSinceNow: SwiftFoundation.TimeInterval) {
self.init(timeIntervalSinceReferenceDate: timeIntervalSinceNow + JSDate.timeIntervalSinceReferenceDate)
}
/**
The time interval between the date and the current date and time.
If the date is earlier than the current date and time, the this property’s value is negative.
- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSince1970`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
var timeIntervalSinceNow: SwiftFoundation.TimeInterval {
return self.timeIntervalSinceReferenceDate - JSDate.timeIntervalSinceReferenceDate
}
/**
The interval between the date object and 00:00:00 UTC on 1 January 1970.
This property’s value is negative if the date object is earlier than 00:00:00 UTC on 1 January 1970.
- SeeAlso: `timeIntervalSince(_:)`
- SeeAlso: `timeIntervalSinceNow`
- SeeAlso: `timeIntervalSinceReferenceDate`
*/
var timeIntervalSince1970: SwiftFoundation.TimeInterval {
return self.timeIntervalSinceReferenceDate + Date.timeIntervalBetween1970AndReferenceDate
}
}
public extension SwiftFoundation.Date {
/// The interval between 00:00:00 UTC on 1 January 2001 and the current date and time.
static var _timeIntervalSinceReferenceDate: SwiftFoundation.TimeInterval {
// FIXME: Compiler error
return JSDate.timeIntervalSinceReferenceDate
}
}
// MARK: - CustomStringConvertible
extension SwiftFoundation.Date: CustomStringConvertible {
public var description: String {
return JSDate(self).toUTCString()
}
}
// MARK: - CustomDebugStringConvertible
extension SwiftFoundation.Date: CustomDebugStringConvertible {
public var debugDescription: String {
return description
}
}
#endif
// MARK: - JS Value
public extension JSDate {
convenience init(_ date: SwiftFoundation.Date) {
self.init(timeInterval: date.timeIntervalSince1970)
}
}
public extension SwiftFoundation.Date {
init(_ date: JSDate) {
self.init(timeIntervalSince1970: date.rawValue)
}
}
// MARK: - JSValueConvertible
extension SwiftFoundation.Date: JSValueConvertible {
public func jsValue() -> JSValue {
let date = JSDate(self)
assert(date.rawValue == timeIntervalSince1970)
return date.jsValue()
}
}
// MARK: - JSValueConstructible
extension SwiftFoundation.Date: JSValueConstructible {
public static func construct(from value: JSValue) -> Date? {
return value.object
.flatMap { JSDate($0) }
.flatMap { Date($0) }
?? value.number.flatMap { Date(timeIntervalSince1970: $0) }
}
}