Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : Groupe Ka — app iOS native7// File : Shared/Format.swift8// Module : Utilities9// Repository : https://git.spboucher.ai/groupe-ka-ios10// Project Website : Not specified11// Created : 2026-12-0312// Last Modified : 2026-12-0313//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation4243/// Écriture des nombres et des dates en français du Québec. Une valeur absente n'est jamais remplacée :44/// les fonctions rendent `nil` et l'écran n'affiche rien.45enum Fmt {46 static let locale = Locale(identifier: "fr_CA")47 private static let fuseau = TimeZone(identifier: "America/Toronto") ?? .current4849 /// « 50 375 », « 1 751,5 »50 static func nombre(_ v: Double, decimales: Int = 0) -> String {51 let t = v.formatted(.number.locale(locale).precision(.fractionLength(0...decimales)).grouping(.automatic))52 // pas de « −0 » : une valeur qui s'arrondit à zéro s'écrit zéro53 if t.hasPrefix("-"), !t.contains(where: { $0.isNumber && $0 != "0" }) { return String(t.dropFirst()) }54 return t.replacingOccurrences(of: "-", with: "−")55 }5657 /// décimales utiles selon la grandeur : 100,16 garde ses décimales, 50 375 n'en a pas58 static func nombreAuto(_ v: Double) -> String {59 let a = abs(v)60 if a >= 1000 || v == v.rounded() { return nombre(v) }61 return nombre(v, decimales: a >= 100 ? 2 : (a >= 10 ? 1 : 2))62 }6364 static func entier(_ v: Double?) -> String? { v.map { nombre($0) } }6566 /// « +3,2 % », « −0,5 % », « 0 % »67 static func variation(_ v: Double?) -> String? {68 guard let v else { return nil }69 let t = nombre(abs(v), decimales: 1)70 if t == "0" { return "0 %" }71 return (v > 0 ? "+" : "−") + t + " %"72 }7374 static func pct(_ v: Double?, decimales: Int = 1) -> String? { v.map { nombre($0, decimales: decimales) + " %" } }7576 /// « 50,4 k », « 1,2 M » : graduations d'axes seulement77 static func compact(_ v: Double) -> String {78 let a = abs(v)79 if a >= 1_000_000 { return nombre(v / 1_000_000, decimales: 1) + " M" }80 if a >= 10000 { return nombre(v / 1000, decimales: a >= 100_000 ? 0 : 1) + " k" }81 return nombreAuto(v)82 }8384 /// valeur d'un indicateur avec son unité : « 1 751 $ », « 91,5 % », « 78,2 /100 », « 50 375 »85 static func valeur(_ v: JSONValeur?, unite: String?) -> String? {86 guard let v else { return nil }87 let u = unite?.trimmingCharacters(in: .whitespaces) ?? ""88 switch v {89 case .nombre(let n):90 let t = nombreAuto(n)91 return u.isEmpty ? t : (u.hasPrefix("/") ? t + " " + u : t + " " + u)92 case .texte(let s):93 let s = s.trimmingCharacters(in: .whitespaces)94 return s.isEmpty ? nil : (u.isEmpty || s.hasSuffix(u) ? s : s + " " + u)95 case .bool(let b): return b ? "oui" : "non"96 default: return nil97 }98 }99100 /// cellule de tableau101 static func cellule(_ v: JSONValeur) -> String {102 switch v {103 case .nombre(let n): nombreAuto(n)104 case .texte(let s): s105 case .bool(let b): b ? "oui" : "non"106 default: ""107 }108 }109110 /// « 3 déc. », « 3 déc. 2026 »111 static func jour(_ d: Date, annee: Bool = false) -> String {112 var s = Date.FormatStyle(locale: locale, timeZone: fuseau).day().month(.abbreviated)113 if annee { s = s.year() }114 return d.formatted(s)115 }116117 static func jour(_ texte: String?, annee: Bool = false) -> String? { Dates.jour(texte).map { jour($0, annee: annee) } }118119 /// « 18 h 12 »120 static func heure(_ d: Date) -> String {121 let c = Calendar.current.dateComponents(in: TimeZone.current, from: d)122 return String(format: "%d h %02d", c.hour ?? 0, c.minute ?? 0)123 }124125 /// « à l'instant », « il y a 12 min », « il y a 3 h », « hier à 18 h 12 », « le 2 déc. à 9 h 05 »126 static func ilYA(_ d: Date, maintenant: Date = Date()) -> String {127 let s = maintenant.timeIntervalSince(d)128 if s < 60 { return "à l'instant" }129 if s < 3600 { return "il y a \(Int(s / 60)) min" }130 if s < 6 * 3600 { return "il y a \(Int(s / 3600)) h" }131 let cal = Calendar.current132 if cal.isDate(d, inSameDayAs: maintenant) { return "aujourd'hui à \(heure(d))" }133 if let hier = cal.date(byAdding: .day, value: -1, to: maintenant), cal.isDate(d, inSameDayAs: hier) { return "hier à \(heure(d))" }134 return "le \(jour(d)) à \(heure(d))"135 }136137 /// délai en millisecondes : « 8 ms », « 1,2 s »138 static func delai(_ ms: Double?) -> String? {139 guard let ms else { return nil }140 return ms >= 1000 ? nombre(ms / 1000, decimales: 1) + " s" : nombre(ms) + " ms"141 }142}143