Skip to content

Instantly share code, notes, and snippets.

@aoenth
Created Feb 24, 2020
Embed
What would you like to do?
func normalDate(fromYear year: Int, month: Int, day: Int) -> Date {
var normalCalendarDateComponents = DateComponents()
normalCalendarDateComponents.year = year
normalCalendarDateComponents.month = month
normalCalendarDateComponents.day = day
let normalCalendar = Calendar(identifier: .gregorian)
let normalDate = normalCalendar.date(from: normalCalendarDateComponents)!
return normalDate
}
func zodiacFrom(date normalDate: Date) -> String {
let chineseDateString = convertToChineseDate(from: normalDate)
return zodiacFrom(chineseDate: chineseDateString)
}
func convertToChineseDate(from normalDate: Date) -> String {
let chineseCalendar = Calendar(identifier: .chinese)
let formatter = DateFormatter()
formatter.calendar = chineseCalendar
formatter.dateStyle = .full
let chineseDate = formatter.string(from: normalDate)
return chineseDate
}
func zodiacFrom(chineseDate: String) -> String {
let branchExtracted = extractBranchFrom(chineseDate: chineseDate)
if let zodiac = branchNameToZodiac(branchExtracted) {
return zodiac
}
fatalError("Cannot convert \(chineseDate) to a zodiac sign")
}
func extractBranchFrom(chineseDate: String) -> String {
guard let hyphen = chineseDate.firstIndex(of: "-") else {
fatalError("\(chineseDate) is not correctly formatted, use DateFormatter.Style.full")
}
let startIndex = chineseDate.index(after: hyphen)
let endIndex = chineseDate.index(chineseDate.endIndex, offsetBy: -2)
let branchExtracted = chineseDate[startIndex ... endIndex]
return String(branchExtracted)
}
func branchNameToZodiac(_ branch: String) -> String? {
let dict = [
"zi": "rat",
"chou": "ox",
"yin": "tiger",
"mao": "rabbit",
"chen": "dragon",
"si": "snake",
"wu": "horse",
"wei": "goat",
"shen": "monkey",
"you": "rooster",
"xu": "dog",
"hai": "pig"
]
return dict[branch]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment