Last active
May 4, 2024 09:13
-
-
Save Codelaby/097df0b07f226f8362db560ce7f35ffe to your computer and use it in GitHub Desktop.
Binary Clock System in Swift
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 Foundation | |
struct BinaryClock { | |
private(set) var hours24: [Int] = Array(repeating: 0, count: 6) | |
private(set) var hours12: [Int] = Array(repeating: 0, count: 4) | |
private(set) var minutes: [Int] = Array(repeating: 0, count: 6) | |
private(set) var seconds: [Int] = Array(repeating: 0, count: 6) | |
mutating func update(withSeconds seconds: TimeInterval) { | |
let totalSeconds = Int(seconds) | |
// Binary representation for hours in 24-hour format | |
let hour24 = totalSeconds / 3600 | |
for i in 0..<6 { | |
hours24[i] = (hour24 >> i) & 1 | |
} | |
// Calculate hours in 12-hour format | |
var hour12 = hour24 % 12 | |
if hour12 == 0 { | |
hour12 = 12 | |
} | |
for i in 0..<4 { | |
hours12[i] = (hour12 >> i) & 1 | |
} | |
// Binary representation for minutes | |
let minute = totalSeconds / 60 % 60 | |
for i in 0..<6 { | |
minutes[i] = (minute >> i) & 1 | |
} | |
// Binary representation for seconds | |
let second = totalSeconds % 60 | |
let secondInt = Int(second) // Convertir a entero | |
for i in 0..<6 { | |
self.seconds[i] = (secondInt >> i) & 1 | |
} | |
} | |
func binaryToDecimal(_ binaryArray: [Int]) -> Int { | |
var decimal = 0 | |
for i in 0..<binaryArray.count { | |
decimal += binaryArray[i] * Int(pow(2.0, Double(i))) | |
} | |
return decimal | |
} | |
} | |
// Define una hora específica (por ejemplo, 12:34:56) | |
let calendar = Calendar.current | |
var dateComponents = DateComponents() | |
dateComponents.hour = 13 | |
dateComponents.minute = 30 | |
dateComponents.second = 59 | |
// Crea un objeto Date con la hora especificada | |
guard let date = calendar.date(from: dateComponents) else { | |
fatalError("No se pudo crear el objeto Date") | |
} | |
// Instancia BinaryClock | |
var binaryClock = BinaryClock() | |
// Obtiene los segundos desde la medianoche | |
let secondsSinceMidnight = date.timeIntervalSince(calendar.startOfDay(for: date)) | |
// Actualiza BinaryClock con los segundos obtenidos y la hora en formato de 12 horas | |
let hour12 = calendar.component(.hour, from: date) % 12 | |
binaryClock.update(withSeconds: secondsSinceMidnight) | |
// Mostrar los resultados | |
print("Binary Clock") | |
print("24h: \(binaryClock.hours24) = \(binaryClock.binaryToDecimal(binaryClock.hours24))") | |
print("12h: \(binaryClock.hours12). = \(binaryClock.binaryToDecimal(binaryClock.hours12))") | |
print("m. : \(binaryClock.minutes) = \(binaryClock.binaryToDecimal(binaryClock.minutes))") | |
print("s. : \(binaryClock.seconds) = \(binaryClock.binaryToDecimal(binaryClock.seconds))") |
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 Foundation | |
struct BinaryClock { | |
private(set) var hours24: [Bool] = Array(repeating: false, count: 6) | |
private(set) var hours12: [Bool] = Array(repeating: false, count: 4) | |
private(set) var minutes: [Bool] = Array(repeating: false, count: 6) | |
private(set) var seconds: [Bool] = Array(repeating: false, count: 6) | |
mutating func update(withSeconds seconds: TimeInterval) { | |
let totalSeconds = Int(seconds) | |
// Binary representation for hours in 24-hour format | |
let hour24 = totalSeconds / 3600 | |
for i in 0..<6 { | |
hours24[i] = ((hour24 >> i) & 1) == 1 | |
} | |
// Calculate hours in 12-hour format | |
var hour12 = hour24 % 12 | |
if hour12 == 0 { | |
hour12 = 12 | |
} | |
for i in 0..<4 { | |
hours12[i] = ((hour12 >> i) & 1) == 1 | |
} | |
// Binary representation for minutes | |
let minute = totalSeconds / 60 % 60 | |
for i in 0..<6 { | |
minutes[i] = ((minute >> i) & 1) == 1 | |
} | |
// Binary representation for seconds | |
let second = totalSeconds % 60 | |
let secondInt = Int(second) // Convertir a entero | |
for i in 0..<6 { | |
self.seconds[i] = ((secondInt >> i) & 1) == 1 | |
} | |
} | |
func binaryToDecimal(_ binaryArray: [Bool]) -> Int { | |
var decimal = 0 | |
for (index, bit) in binaryArray.enumerated() { | |
decimal += bit ? (1 << index) : 0 | |
} | |
return decimal | |
} | |
func boolToInt(_ bool: Bool) -> Int { | |
return bool ? 1 : 0 | |
} | |
} | |
// Define una hora específica (por ejemplo, 12:34:56) | |
let calendar = Calendar.current | |
var dateComponents = DateComponents() | |
dateComponents.hour = 23 | |
dateComponents.minute = 30 | |
dateComponents.second = 59 | |
// Crea un objeto Date con la hora especificada | |
guard let date = calendar.date(from: dateComponents) else { | |
fatalError("No se pudo crear el objeto Date") | |
} | |
// Instancia BinaryClock | |
var binaryClock = BinaryClock() | |
// Obtiene los segundos desde la medianoche | |
let secondsSinceMidnight = date.timeIntervalSince(calendar.startOfDay(for: date)) | |
// Actualiza BinaryClock con los segundos obtenidos y la hora en formato de 12 horas | |
let hour12 = calendar.component(.hour, from: date) % 12 | |
binaryClock.update(withSeconds: secondsSinceMidnight) | |
// Mostrar los resultados | |
print("Binary Clock") | |
print("24h: \(binaryClock.hours24.map { binaryClock.boolToInt($0) }) = \(binaryClock.binaryToDecimal(binaryClock.hours24))") | |
print("12h: \(binaryClock.hours12.map { binaryClock.boolToInt($0) }) = \(binaryClock.binaryToDecimal(binaryClock.hours12))") | |
print("m. : \(binaryClock.minutes.map { binaryClock.boolToInt($0) }) = \(binaryClock.binaryToDecimal(binaryClock.minutes))") | |
print("s. : \(binaryClock.seconds.map { binaryClock.boolToInt($0) }) = \(binaryClock.binaryToDecimal(binaryClock.seconds))") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment