Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active May 23, 2022 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/1fecdd882c305689cd21b2115afcf4f3 to your computer and use it in GitHub Desktop.
Save sturdysturge/1fecdd882c305689cd21b2115afcf4f3 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@State var days = 0
@State var hours = 0
@State var minutes = 0
@State var seconds = 0
static let wwdc = Date(timeIntervalSince1970: 1654534800)
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Text("\(days) days")
.font(.largeTitle.monospacedDigit())
Text("\(hours) hours")
.font(.title.monospacedDigit())
Text("\(minutes) minutes")
.font(.title2.monospacedDigit())
Text("\(seconds) seconds")
.font(.title3.monospacedDigit())
}
.onReceive(timer) { input in
let difference = Self.wwdc.timeIntervalSince1970 - input.timeIntervalSince1970
let doubleDays = difference / 86400
let truncatedDays = Int(doubleDays)
days = truncatedDays
let doubleHours = Double((doubleDays - Double(truncatedDays)) * 24)
let truncatedHours = Int(doubleHours)
hours = Int(doubleHours)
let doubleMinutes = Double((doubleHours - Double(truncatedHours)) * 60)
let truncatedMinutes = Int(doubleMinutes)
minutes = truncatedMinutes
let doubleSeconds = Double((doubleMinutes - Double(truncatedMinutes)) * 60)
let truncatedSeconds = Int(doubleSeconds)
seconds = truncatedSeconds
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment