Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created March 12, 2025 10:38
Show Gist options
  • Save pookjw/7715f35c1740036d6c2ae2a5ae5d0031 to your computer and use it in GitHub Desktop.
Save pookjw/7715f35c1740036d6c2ae2a5ae5d0031 to your computer and use it in GitHub Desktop.
import SwiftUI
import MachO
import sys_time
struct ContentView: View {
@State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
.onReceive(timer) { input in
let time = withUnsafeTemporaryAllocation(of: timeval.self, capacity: 1) { ptr in
let size = withUnsafeTemporaryAllocation(of: Int.self, capacity: 1) { ptr_2 in
ptr_2.baseAddress!.pointee = MemoryLayout<timeval>.size
assert(sysctlbyname("kern.boottime", ptr.baseAddress!, ptr_2.baseAddress!, nil, 0) == 0)
return ptr_2.baseAddress!.pointee
}
assert(size > 0)
assert(size == MemoryLayout<timeval>.size)
return ptr.baseAddress!.pointee
}
let timebaseInfo = withUnsafeTemporaryAllocation(of: mach_timebase_info_data_t.self, capacity: 1) { ptr in
mach_timebase_info(ptr.baseAddress!)
return ptr.baseAddress!.pointee
}
print(Date(timeIntervalSince1970: TimeInterval(time.tv_sec) + TimeInterval(((mach_absolute_time() * UInt64(timebaseInfo.numer)) / UInt64(timebaseInfo.denom))) / 1.0E9))
print(Date.now)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment