Skip to content

Commit 5ca7ce2

Browse files
committed
Fixed WebAssembly support
1 parent 85180c2 commit 5ca7ce2

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

Sources/Decoder.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,23 +356,26 @@ private extension TLVDecoder.Decoder {
356356
case .millisecondsSince1970:
357357
let timeInterval = try unboxDouble(data)
358358
return Date(timeIntervalSince1970: timeInterval / 1000)
359+
#if !os(WASI)
359360
case .iso8601:
360361
guard #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
361362
else { fatalError("ISO8601DateFormatter is unavailable on this platform.") }
362363
return try unboxDate(data, using: TLVDateFormatting.iso8601Formatter)
363364
case let .formatted(formatter):
364365
return try unboxDate(data, using: formatter)
366+
#endif
365367
}
366368
}
367369

368-
@inline(__always)
370+
#if !os(WASI)
369371
func unboxDate <T: DateFormatterProtocol> (_ data: Data, using formatter: T) throws -> Date {
370372
let string = try unbox(data, as: String.self)
371373
guard let date = formatter.date(from: string) else {
372374
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Invalid Date string \(string)"))
373375
}
374376
return date
375377
}
378+
#endif
376379
}
377380

378381
// MARK: - Stack

Sources/Encoder.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,22 @@ private extension TLVEncoder.Encoder {
267267
return boxDouble(date.timeIntervalSince1970)
268268
case .millisecondsSince1970:
269269
return boxDouble(date.timeIntervalSince1970 * 1000)
270+
#if !os(WASI)
270271
case .iso8601:
271272
guard #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
272273
else { fatalError("ISO8601DateFormatter is unavailable on this platform.") }
273274
return boxDate(date, using: TLVDateFormatting.iso8601Formatter)
274275
case let .formatted(formatter):
275276
return boxDate(date, using: formatter)
277+
#endif
276278
}
277279
}
278280

281+
#if !os(WASI)
279282
func boxDate <T: DateFormatterProtocol> (_ date: Date, using formatter: T) -> Data {
280283
return box(formatter.string(from: date))
281284
}
285+
#endif
282286
}
283287

284288
// MARK: - Stack

Sources/Format.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ public enum TLVDateFormatting: Equatable {
6868
/// Encodes dates in terms of milliseconds since midnight UTC on January 1, 1970.
6969
case millisecondsSince1970
7070

71+
#if !os(WASI)
7172
/// Formats dates according to the ISO 8601 and RFC 3339 standards.
7273
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
7374
case iso8601
7475

7576
/// Defers formatting settings to a supplied date formatter.
7677
case formatted(DateFormatter)
78+
#endif
7779
}
7880

7981
public extension TLVDateFormatting {
@@ -89,6 +91,14 @@ public typealias TLVDateFormat = TLVDateFormatting
8991

9092
// MARK: - Formatters
9193

94+
#if !os(WASI)
95+
internal protocol DateFormatterProtocol: AnyObject {
96+
97+
func string(from date: Date) -> String
98+
99+
func date(from string: String) -> Date?
100+
}
101+
92102
internal extension TLVDateFormatting {
93103

94104
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
@@ -99,14 +109,8 @@ internal extension TLVDateFormatting {
99109
}()
100110
}
101111

102-
internal protocol DateFormatterProtocol: class {
103-
104-
func string(from date: Date) -> String
105-
106-
func date(from string: String) -> Date?
107-
}
108-
109112
extension DateFormatter: DateFormatterProtocol { }
110113

111114
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
112115
extension ISO8601DateFormatter: DateFormatterProtocol { }
116+
#endif

Tests/TLVCodingTests/TLVCodingTests.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,23 @@ final class TLVCodingTests: XCTestCase {
279279

280280
func testDate() {
281281

282+
var formats: [TLVDateFormatting] = [
283+
.secondsSince1970,
284+
.millisecondsSince1970
285+
]
286+
287+
#if !os(WASI)
282288
let dateFormatter = DateFormatter()
283289
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
284290
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
285291
dateFormatter.calendar = Calendar(identifier: .gregorian)
286292
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
287-
288-
var formats: [TLVDateFormatting] = [.secondsSince1970, .millisecondsSince1970, .formatted(dateFormatter)]
289-
293+
formats.append(.formatted(dateFormatter))
290294
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
291295
formats.append(.iso8601)
292296
}
293-
297+
#endif
298+
294299
let date = Date(timeIntervalSince1970: 60 * 60 * 24 * 365)
295300

296301
for format in formats {

0 commit comments

Comments
 (0)