Skip to content

Instantly share code, notes, and snippets.

@aoenth
Created February 24, 2020 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoenth/231f86d547fa670402f5ec1d5a6579f3 to your computer and use it in GitHub Desktop.
Save aoenth/231f86d547fa670402f5ec1d5a6579f3 to your computer and use it in GitHub Desktop.
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