Skip to content

Commit ad0695d

Browse files
committed
Don't decode timezone and dates
1 parent 4b71c91 commit ad0695d

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-34
lines changed

Sources/AlphaSwiftage/Public/Model/CurrencyExchangeRate.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public struct CurrencyExchangeRate: Codable, Equatable {
66
public let toCurrencyCode: String
77
public let toCurrencyName: String
88
public let exchangeRate: Decimal
9-
public let lastRefreshed: Date
10-
public let timeZone: TimeZone
9+
public let lastRefreshed: String
10+
public let timeZone: String
1111
public let bidPrice: Decimal
1212
public let askPrice: Decimal
1313

14-
init(fromCurrencyCode: String, fromCurrencyName: String, toCurrencyCode: String, toCurrencyName: String, exchangeRate: Decimal, lastRefreshed: Date, timeZone: TimeZone, bidPrice: Decimal, askPrice: Decimal) {
14+
init(fromCurrencyCode: String, fromCurrencyName: String, toCurrencyCode: String, toCurrencyName: String, exchangeRate: Decimal, lastRefreshed: String, timeZone: String, bidPrice: Decimal, askPrice: Decimal) {
1515
self.fromCurrencyCode = fromCurrencyCode
1616
self.fromCurrencyName = fromCurrencyName
1717
self.toCurrencyCode = toCurrencyCode
@@ -44,23 +44,7 @@ public struct CurrencyExchangeRate: Codable, Equatable {
4444
exchangeRate = try container.decodeUSDecimal(forKey: .exchangeRate)
4545
bidPrice = try container.decodeUSDecimal(forKey: .bidPrice)
4646
askPrice = try container.decodeUSDecimal(forKey: .askPrice)
47-
48-
49-
// Handling TimeZone
50-
let timeZoneString = try container.decode(String.self, forKey: .timeZone)
51-
guard let timeZone = TimeZone(identifier: timeZoneString) else {
52-
throw DecodingError.dataCorruptedError(forKey: .timeZone, in: container, debugDescription: "Time zone string is not a valid identifier.")
53-
}
54-
self.timeZone = timeZone
55-
56-
// Custom decoding for the Date
57-
let dateFormatter = DateFormatter()
58-
dateFormatter.timeZone = timeZone
59-
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
60-
let dateString = try container.decode(String.self, forKey: .lastRefreshed)
61-
guard let date = dateFormatter.date(from: dateString) else {
62-
throw DecodingError.dataCorruptedError(forKey: .lastRefreshed, in: container, debugDescription: "Date string does not match format expected by formatter.")
63-
}
64-
lastRefreshed = date
47+
timeZone = try container.decode(String.self, forKey: .timeZone)
48+
lastRefreshed = try container.decode(String.self, forKey: .lastRefreshed)
6549
}
6650
}

Sources/AlphaSwiftage/Public/Model/Symbol.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct Symbol: Codable, Equatable {
1414
public let region: String
1515
public let marketOpen: String
1616
public let marketClose: String
17-
public let timeZone: TimeZone
17+
public let timeZone: String
1818
public let currency: String
1919
public let matchScore: Float
2020

@@ -39,17 +39,11 @@ public struct Symbol: Codable, Equatable {
3939
marketOpen = try container.decode(String.self, forKey: .marketOpen)
4040
marketClose = try container.decode(String.self, forKey: .marketClose)
4141
currency = try container.decode(String.self, forKey: .currency)
42-
self.matchScore = try container.decodeUSFloat(forKey: .matchScore)
43-
44-
// Handling TimeZone
45-
let timeZoneString = try container.decode(String.self, forKey: .timeZone)
46-
guard let timeZone = TimeZone(identifier: timeZoneString) else {
47-
throw DecodingError.dataCorruptedError(forKey: .timeZone, in: container, debugDescription: "Time zone string is not a valid identifier.")
48-
}
49-
self.timeZone = timeZone
42+
matchScore = try container.decodeUSFloat(forKey: .matchScore)
43+
timeZone = try container.decode(String.self, forKey: .timeZone)
5044
}
5145

52-
init(symbol: String, name: String, type: String, region: String, marketOpen: String, marketClose: String, timeZone: TimeZone, currency: String, matchScore: Float) {
46+
init(symbol: String, name: String, type: String, region: String, marketOpen: String, marketClose: String, timeZone: String, currency: String, matchScore: Float) {
5347
self.symbol = symbol
5448
self.name = name
5549
self.type = type

Tests/AlphaVantageServiceTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ final class AlphaVantageServiceTests: XCTestCase {
4040
toCurrencyCode: target,
4141
toCurrencyName: "Hungarian Forint",
4242
exchangeRate: Decimal(floatLiteral: 393.5),
43-
lastRefreshed: Date(timeIntervalSince1970: 1710096503),
44-
timeZone: TimeZone.gmt,
43+
lastRefreshed: "2024-03-10 18:48:23",
44+
timeZone: "UTC",
4545
bidPrice: Decimal(floatLiteral: 393.49),
4646
askPrice: Decimal(floatLiteral: 393.51))
4747
given(response: .currencyExchangeRate, for: "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=\(base)&to_currency=\(target)&apikey=\(apiKey)")
@@ -62,7 +62,7 @@ final class AlphaVantageServiceTests: XCTestCase {
6262
region: "XETRA",
6363
marketOpen: "08:00",
6464
marketClose: "20:00",
65-
timeZone: TimeZone(secondsFromGMT: 60*60*2)!,
65+
timeZone: "UTC+02",
6666
currency: "EUR",
6767
matchScore: 0.7273)
6868
let service = givenService()

0 commit comments

Comments
 (0)