Skip to content

Instantly share code, notes, and snippets.

@IgorMuzyka
Created February 19, 2019 17:13
Show Gist options
  • Save IgorMuzyka/c9b2792d65619676cf3f0209e5545377 to your computer and use it in GitHub Desktop.
Save IgorMuzyka/c9b2792d65619676cf3f0209e5545377 to your computer and use it in GitHub Desktop.
import Foundation
struct Weed {
public let birthDate: Date
private let calendar = Calendar.current
private let yearLength = 420
private var today: Date {
return calendar.startOfDay(for: Date())
}
public var birthDays: [Date] {
var birthDay = 0
var birthDays = [Int]()
while birthDay < daysSinceBirth {
birthDay += yearLength
birthDays.append(birthDay)
}
return birthDays.map { days in
calendar.date(byAdding: .day, value: days , to: birthDate)!
}
}
public var nextBirthDay: Date {
return calendar.date(byAdding: .day, value: yearLength, to: birthDays.last!)!
}
public var daysSinceBirth: Int {
return calendar.dateComponents(Set(arrayLiteral: .day), from: birthDate, to: today).day!
}
public var age: Int {
return Int(daysSinceBirth / yearLength)
}
init(birhtDate: Date) {
self.birthDate = calendar.startOfDay(for: birhtDate)
}
init(birthDate: String) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
self.birthDate = dateFormatter.date(from: birthDate)!
}
}
let weed = Weed(birthDate: "1993-04-10T00:00:00+0000")
print(weed.age)
print(weed.nextBirthDay)
print(weed.birthDate)
weed.birthDays.forEach { birthDay in
print(birthDay)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment