How to resolve the algorithm Discordian date step by step in the Swift programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Discordian date step by step in the Swift programming language
Table of Contents
Problem Statement
Convert a given date from the Gregorian calendar to the Discordian calendar.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Discordian date step by step in the Swift programming language
Source code in the swift programming language
import Foundation
let monthDays = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
let seasons = ["Chaos", "Discord", "Confusion", "Bureacracy", "The Aftermath"]
let dayNames = ["Sweetmorn", "Boomtime", "Pungenday", "Prickle-Prickle", "Setting Orange"]
let holyDays1 = ["Mungday", "Mojoday", "Syaday", "Zaraday", "Maladay"]
let holyDays2 = ["Chaoflux", "Discoflux", "Confuflux", "Bureflux", "Afflux"]
func discordianDate(date: Date) -> String {
let calendar = Calendar.current
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let discYear = year + 1166
if month == 2 && day == 29 {
return "St. Tib's Day in the YOLD \(discYear)"
}
let dayOfYear = monthDays[month - 1] + day - 1
let season = dayOfYear/73
let weekDay = dayOfYear % 5
let dayOfSeason = 1 + dayOfYear % 73
let ddate = "\(dayNames[weekDay]), day \(dayOfSeason) of \(seasons[season]) in the YOLD \(discYear)"
switch (dayOfSeason) {
case 5:
return ddate + ". Celebrate \(holyDays1[season])!"
case 50:
return ddate + ". Celebrate \(holyDays2[season])!"
default:
return ddate
}
}
func showDiscordianDate(year: Int, month: Int, day: Int) {
let calendar = Calendar.current
let date = calendar.date(from: DateComponents(year: year, month: month, day: day))!
let ddate = discordianDate(date: date)
let format = DateFormatter()
format.dateFormat = "yyyy-MM-dd"
print("\(format.string(from: date)): \(ddate)")
}
showDiscordianDate(year: 2022, month: 1, day: 20)
showDiscordianDate(year: 2020, month: 9, day: 21)
showDiscordianDate(year: 2020, month: 2, day: 29)
showDiscordianDate(year: 2019, month: 7, day: 15)
showDiscordianDate(year: 2025, month: 3, day: 19)
showDiscordianDate(year: 2017, month: 12, day: 8)
You may also check:How to resolve the algorithm Check that file exists step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the NS-HUBASIC programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Elixir programming language
You may also check:How to resolve the algorithm Weird numbers step by step in the JavaScript programming language