-
-
Save sturdysturge/1fecdd882c305689cd21b2115afcf4f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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