remora-kit

RemoraKit, shared dive computer engine (Buhlmann ZHL-16C + GF) and SwiftUI components for Open Remora and Open Merou
git clone https://git.schurger.org/remora-kit.git
Log | Files | Refs | README | LICENSE

Format.swift (2394B)


      1 // SPDX-License-Identifier: GPL-2.0-only
      2 import Foundation
      3 
      4 /// Numbers, durations and dates as the apps display them.
      5 public enum Format {
      6     /// Language the interface runs in. The French interface keeps the decimal comma and the date style it
      7     /// always had.
      8     public static let isFrench = Bundle.module.preferredLocalizations.first?.hasPrefix("fr") ?? false
      9     private static let locale: Locale = {
     10         if isFrench { return Locale(identifier: "fr_CA") }
     11         return Locale.current.language.languageCode == .english ? Locale.current : Locale(identifier: "en_US")
     12     }()
     13 
     14     /// Fixed number of decimals, with the decimal separator of the interface language.
     15     public static func decimal(_ value: Double, digits: Int) -> String {
     16         let s = String(format: "%.\(digits)f", value)
     17         return isFrench ? s.replacingOccurrences(of: ".", with: ",") : s
     18     }
     19 
     20     /// Depth in metres, one decimal, without the unit.
     21     public static func depth(_ metres: Double) -> String { decimal(metres, digits: 1) }
     22 
     23     /// Seconds → "12:34" (or "1:02:03").
     24     public static func clock(_ seconds: Double) -> String {
     25         let s = Int(seconds.rounded())
     26         return s >= 3600 ? String(format: "%d:%02d:%02d", s / 3600, s / 60 % 60, s % 60)
     27                          : String(format: "%d:%02d", s / 60, s % 60)
     28     }
     29 
     30     /// Seconds → "42 min".
     31     public static func minutes(_ seconds: Double) -> String { "\(Int((seconds / 60).rounded())) min" }
     32 
     33     /// Waiting time in hours → "3 h 05" (or "45 min").
     34     public static func hours(_ h: Double) -> String {
     35         let total = Int((h * 60).rounded(.up))
     36         return total >= 60 ? "\(total / 60) h \(String(format: "%02d", total % 60))" : "\(total) min"
     37     }
     38 
     39     public static func percent(_ value: Int) -> String { String(localized: "\(value)%", bundle: .module) }
     40 
     41     /// No-decompression limit in minutes, without the unit; nil = unlimited.
     42     public static func ndl(_ minutes: Double?) -> String {
     43         guard let minutes else { return ">99" }
     44         return minutes > 99 ? ">99" : "\(Int(minutes))"
     45     }
     46 
     47     public static func day(_ date: Date) -> String {
     48         date.formatted(.dateTime.day().month(.abbreviated).year().locale(locale))
     49     }
     50 
     51     public static func dayTime(_ date: Date) -> String {
     52         date.formatted(.dateTime.day().month(.abbreviated).hour().minute().locale(locale))
     53     }
     54 }