Skip to content

Instantly share code, notes, and snippets.

@madelinecr
Last active April 20, 2022 00:27
Show Gist options
  • Save madelinecr/d6ad824c26a48df2a12df32e7e8d05bf to your computer and use it in GitHub Desktop.
Save madelinecr/d6ad824c26a48df2a12df32e7e8d05bf to your computer and use it in GitHub Desktop.
DIT Time Date Extension w/ SwiftUI Display
//
// ContentView.swift
// Decimal Time
//
// Created by Madeline Cray on 4/16/22.
//
import SwiftUI
let DESECS_IN_FRACTIONAL_MS = 0.864;
struct ContentView: View {
@State var timer = Timer.publish(every: DESECS_IN_FRACTIONAL_MS, on: .main, in: .common).autoconnect()
@State var formattedTime: String = ""
var body: some View {
Text(formattedTime)
.padding()
.onReceive(timer) { _ in
formattedTime = Date.now.currentDitTimeString
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
/* LICENCE:
Public Domain
Originally by: s0 2022-03-27
Modified by: Madeline Cray 2022-04-19
*/
import Foundation
let MS_PER_SEC: UInt = 1000
let MS_PER_MIN: UInt = (MS_PER_SEC * 60)
let MS_PER_HOUR: UInt = (MS_PER_MIN * 60)
let S_PER_HOUR: UInt = 60 * 60
let DESECS_PER_DESIM: UInt = 100
let DESIMS_PER_DECA: UInt = 100
let DECAS_PER_DAY: UInt = 10
let DESECS_PER_DECA: UInt = (DESECS_PER_DESIM * DESIMS_PER_DECA)
let DESECS_PER_DAY: UInt = (DESECS_PER_DECA * DECAS_PER_DAY)
// 86400 seconds in a day; 100000 desecs in a day; therefore 864 milliseconds per desec, computed by compiler.
let MS_PER_DESEC: UInt = ((1000 * 86400)/DESECS_PER_DAY)
extension Date {
var currentDitTimeString: String {
let currentTime = self.timeIntervalSinceReferenceDate
let currentUTC12Milliseconds = UInt((currentTime - (12*60*60)).truncatingRemainder(dividingBy: 86400) * 1000)
let currentDitDesecs = currentUTC12Milliseconds / MS_PER_DESEC
let s = String(format: "%.0u.%02u.%02u",
(currentDitDesecs / DESECS_PER_DECA) % DECAS_PER_DAY,
(currentDitDesecs / DESECS_PER_DESIM) % DESIMS_PER_DECA,
currentDitDesecs % DESECS_PER_DESIM
)
return s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment