Files
eqn.ios/Sources/Earthquake Network/Models/EQNUtility+Extensions.swift
T
2021-03-14 15:12:16 +01:00

65 lines
2.0 KiB
Swift

//
// EQNUtility+Extension.swift
// Earthquake Network
//
// Created by Busi Andrea on 08/10/2020.
// Copyright © 2020 Earthquake Network. All rights reserved.
//
import Foundation
extension EQNUtility {
private static var dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss d-MMM"
return formatter
}()
// MARK: - Public
/// Convert coordinates in degrees
/// - Parameter coordinate: The latitude and longitude associated with a location
/// - Returns: Formatted coordinates, like
class func coordinateString(coordinate: CLLocationCoordinate2D) -> String {
var latSeconds = Int(coordinate.latitude * 3600)
let latDegrees = latSeconds / 3600
latSeconds = abs(latSeconds % 3600)
let latMinutes = latSeconds / 60
latSeconds %= 60
var longSeconds = Int(coordinate.longitude * 3600)
let longDegrees = longSeconds / 3600
longSeconds = abs(longSeconds % 3600)
let longMinutes = longSeconds / 60
longSeconds %= 60
return String(format:"%d°%d'%d\"%@ lat %d°%d'%d\"%@ lon",
abs(latDegrees),
latMinutes,
latSeconds, latDegrees >= 0 ? "N" : "S",
abs(longDegrees),
longMinutes,
longSeconds,
longDegrees >= 0 ? "E" : "W" )
}
@objc
class func formattedTimeDifference(from: Date, to: Date = Date()) -> String {
let diffComponents = Calendar.current.dateComponents([.day, .hour, .minute], from: from, to: to)
let days = diffComponents.day ?? 0
let hours = diffComponents.hour ?? 0
let minutes = diffComponents.minute ?? 0
if days > 0 {
return "\(days)d"
} else if hours > 0 {
return "\(hours)h"
}
return "\(minutes)m"
}
class func formattedDate(from date: Date) -> String {
Self.dateFormatter.string(from: date)
}
}