从2个不同的字典中以timeIntervalSince1970格式计算日期的双精度数

时间:2018-12-13 01:32:36

标签: swift date dictionary sum

我有2个字典:

bought: [Int64: [String:String]]是用户购买加密货币的时间

可读词典:

2018-12-04 17:41:48
["amount": "5000.0", "symbol": "MOBI"]
2018-12-06 13:52:44
["amount": "1.5", "symbol": "ETH"]
2018-12-06 13:53:47
["amount": "200.0", "symbol": "XRP"]
2018-12-08 12:01:04
["amount": "2.0", "symbol": "ETH"]

原始字典:

[1543945308:["amount": "5000.0", "symbol": "MOBI"],
1544104364:["amount": "1.5", "symbol": "ETH"],
1544104427:["amount": "200.0", "symbol": "XRP"],
1544270464:["amount": "2.0", "symbol": "ETH"]]

pricesForPreviousDays:[Int64:[String:Double]]是将加密货币加进购买后所采用的加密货币的价格

可读词典:

2018-12-04 23:59:59
["MOBI": 0.01481]
2018-12-05 23:59:59 
["MOBI": 0.01243]
2018-12-06 23:59:59 
["MOBI": 0.01179, "XRP": 0.3074, "ETH": 90.43]
2018-12-07 23:59:59 
["MOBI": 0.01114, "XRP": 0.3023, "ETH": 93.61]
2018-12-08 23:59:59 
["MOBI": 0.01125, "XRP": 0.3074, "ETH": 91.44]

原始字典:

[1543967999:["MOBI": 0.01481],
1544054399:["MOBI": 0.01243],
1544140799:["MOBI": 0.01179, "XRP": 0.3074, "ETH": 90.43],
1544227199:["MOBI": 0.01114, "XRP": 0.3023, "ETH": 93.61],
1544313599:["MOBI": 0.01125, "XRP": 0.3074, "ETH": 91.44]]

我必须计算用户当天的金额。 示例字典moneyForTheDay[Int64:Double],其中Int64是一天的结尾(我有Date扩展名是一天的结尾),而Double是当天的全部金额。

因此,对于第一天(2018年12月4日),代码必须计算MOBI amount * MOBI price = 5000 * 0.01481 = ?

第三天(2018-12-06)用户添加了XRP和ETH,因此代码必须计算MOBI amount * MOBI price + XRP amount * XRP price + ETH amount * ETH price

注意:第五天(2018-12-08)用户又购买了2 ETH,因此代码必须从2018-12-06获得ETH并自动进行总金额{{1} },因此2018-12-08的ETH数量必须为3.5,且公式相同1.5+2 = 3.5

我尝试过:

MOBI amount * MOBI price + XRP amount * XRP price + ETH amount * ETH price

但是计算不正确,我也不知道为什么...

1 个答案:

答案 0 :(得分:1)

我决定在这里将日期作为简单的格式化字符串使用,以方便比较日期。为此,我有一个简单的实用程序函数来转换int日期

func parseToDateString(_ intDate: Int64) -> String {
    let formatter = ISO8601DateFormatter()
    formatter.formatOptions = [.withFullDate]
    let date = Date(timeIntervalSince1970: TimeInterval(intDate))
    return formatter.string(from: date)
}

对于实际计算,我决定首先创建一种事件链,以便知道每个日期拥有哪种加密货币,然后按日期顺序遍历密钥pricesForPreviousDays并查找拥有的货币和价格在价格集合中以计算每天拥有的货币的每日价值,该货币由价格数组中的日期范围间接定义。

typealias CryptoTuple = (symbol: String, amount: Double)
var cryptosPerDay = [String: [CryptoTuple]]()

var prev = ""
bought.keys.sorted(by: <).forEach( { key in
    let date = parseToDateString(key)
    if let buy = bought[key], let symbol = buy["symbol"], let amountStr = buy["amount"], let amount = Double(amountStr) {
        if prev != date {
            cryptosPerDay[date] = [(symbol, amount)]
            if let old = cryptosPerDay[prev] {
                cryptosPerDay[date]?.append(contentsOf: old)
            }
            prev = date
        } else  {
            cryptosPerDay[date]?.append((symbol, amount))
        }
    }
})

var last = [CryptoTuple]()
pricesForPreviousDays.keys.sorted(by: <).forEach( { intDate in
    let date = parseToDateString(intDate)
    let position = cryptosPerDay[date] ?? last

    var totalPerDay = 0.0
    position.forEach( {
        let symbol = $0.symbol
        let amount = $0.amount

        if let item = pricesForPreviousDays.first(where: { parseToDateString($0.key) == date && $0.value.keys.contains(symbol) }) {
            totalPerDay += amount * item.value[symbol]!
        }
    })
    last = position
    print("\(date) - \(totalPerDay)")
})

输出为

  

2018-12-04-74.05
  2018-12-05-62.15
  2018-12-06-256.07500000000005
  2018-12-07-256.575
  2018-12-08-437.77