subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

Units.swift (4325B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 import SubsurfaceLog
      4 
      5 /// Display of quantities: the logbook is always metric, conversion only happens on screen.
      6 struct Units: Equatable {
      7     var imperial = false
      8 
      9     private func number(_ value: Double, _ digits: Int) -> String {
     10         value.formatted(.number.precision(.fractionLength(digits)))
     11     }
     12 
     13     var depthUnit: String { imperial ? "ft" : "m" }
     14     var temperatureUnit: String { imperial ? "°F" : "°C" }
     15     var pressureUnit: String { imperial ? "psi" : "bar" }
     16 
     17     func depthValue(_ meters: Double) -> Double { imperial ? meters * 3.28084 : meters }
     18     func metersFromDisplay(_ value: Double) -> Double { imperial ? value / 3.28084 : value }
     19     func temperatureValue(_ celsius: Double) -> Double { imperial ? celsius * 9 / 5 + 32 : celsius }
     20     func celsiusFromDisplay(_ value: Double) -> Double { imperial ? (value - 32) * 5 / 9 : value }
     21     func pressureValue(_ bar: Double) -> Double { imperial ? bar * 14.5038 : bar }
     22     func barFromDisplay(_ value: Double) -> Double { imperial ? value / 14.5038 : value }
     23     var weightUnit: String { imperial ? "lb" : "kg" }
     24     func weightValue(_ kilograms: Double) -> Double { imperial ? kilograms * 2.20462 : kilograms }
     25     func kilogramsFromDisplay(_ value: Double) -> Double { imperial ? value / 2.20462 : value }
     26 
     27     func depth(_ meters: Double) -> String { "\(number(depthValue(meters), imperial ? 0 : 1)) \(depthUnit)" }
     28     func temperature(_ celsius: Double) -> String { "\(number(temperatureValue(celsius), 0)) \(temperatureUnit)" }
     29     func pressure(_ bar: Double) -> String { "\(number(pressureValue(bar), 0)) \(pressureUnit)" }
     30     func weight(_ kilograms: Double) -> String {
     31         imperial ? "\(number(kilograms * 2.20462, 0)) lb" : "\(number(kilograms, 1)) kg"
     32     }
     33     func volume(_ liters: Double) -> String { "\(number(liters, 1)) L" }
     34     func sac(_ litersPerMinute: Double) -> String {
     35         imperial ? "\(number(litersPerMinute * 0.0353147, 2)) \(String(localized: "cu ft/min"))" : "\(number(litersPerMinute, 1)) L/min"
     36     }
     37 
     38     /// "45 min", "1 h 05"
     39     static func duration(_ seconds: Int) -> String {
     40         let minutes = Int((Double(seconds) / 60).rounded())
     41         if minutes < 60 { return "\(minutes) min" }
     42         return "\(minutes / 60) h " + String(format: "%02d", minutes % 60)
     43     }
     44 
     45     /// "12:34" for the profile cursor.
     46     static func clock(_ seconds: Int) -> String { "\(seconds / 60):" + String(format: "%02d", seconds % 60) }
     47 
     48     static func totalTime(_ seconds: Int) -> String {
     49         let hours = seconds / 3600
     50         let minutes = (seconds % 3600) / 60
     51         return hours > 0 ? "\(hours) h " + String(format: "%02d", minutes) : "\(minutes) min"
     52     }
     53 }
     54 
     55 /// Logbook times are local and "naive": they are shown as they are, without a time zone.
     56 enum DiveDate {
     57     private static let utc = TimeZone(identifier: "UTC")!
     58 
     59     static func full(_ dive: Dive) -> String {
     60         dive.date.formatted(Date.FormatStyle(date: .complete, time: .shortened, timeZone: utc))
     61     }
     62 
     63     static func day(_ dive: Dive) -> String {
     64         dive.date.formatted(Date.FormatStyle(timeZone: utc).day().month(.abbreviated).year())
     65     }
     66 
     67     static func short(_ dive: Dive) -> String {
     68         dive.date.formatted(Date.FormatStyle(timeZone: utc).weekday(.abbreviated).day().month(.abbreviated))
     69     }
     70 
     71     static func time(_ dive: Dive) -> String {
     72         dive.date.formatted(Date.FormatStyle(timeZone: utc).hour().minute())
     73     }
     74 
     75     static func monthTitle(_ when: Int) -> String {
     76         Date(timeIntervalSince1970: TimeInterval(when)).formatted(Date.FormatStyle(timeZone: utc).month(.wide).year()).capitalizedFirst
     77     }
     78 
     79     static func year(_ when: Int) -> Int { NaiveTime.components(when).year ?? 0 }
     80     static func monthKey(_ when: Int) -> String {
     81         let c = NaiveTime.components(when)
     82         return String(format: "%04d-%02d", c.year ?? 0, c.month ?? 0)
     83     }
     84 
     85     /// Date chosen in a picker (device time zone) → naive logbook time, seconds set to zero.
     86     static func naive(from date: Date) -> Int {
     87         let local = Int(date.timeIntervalSince1970) + TimeZone.current.secondsFromGMT(for: date)
     88         return local - local % 60
     89     }
     90 }
     91 
     92 extension String {
     93     var capitalizedFirst: String { prefix(1).uppercased() + dropFirst() }
     94 }