Calculate constellation based on date

Posted by albatros21 on Thu, 05 Dec 2019 02:23:00 +0100

In app, when displaying personal information, sometimes the server only returns one birthday, and then displays the corresponding constellation name and icon in the personal information. In this case, the client needs to calculate according to the birthday itself, so how to calculate is very simple.

  1. Declare a constellation structure, which contains constellation code and constellation name.
    Code is used to identify a constellation. You can set different constellation pictures according to code, and display the corresponding localized strings according to code when internationalization is needed.
struct JPSConstellation {
    //Constellation code
    var code: Int
    //Constellation name
    var name: String
}
  1. Calculate the constellation according to the date
func constellationWith(date: Date) -> JPSConstellation? {
        guard let calendar = NSCalendar(identifier: NSCalendar.Identifier.gregorian) else {
            return nil
        }
        let components = calendar.components([.month, .day], from: date)
        let month = components.month!
        let day = components.day!
        
        
        // The month is calculated as a number of 100 times the month
        let mmdd = month * 100 + day;
        var constellation: JPSConstellation?
        
        if ((mmdd >= 321 && mmdd <= 331) ||
            (mmdd >= 401 && mmdd <= 419)) {
            constellation = JPSConstellation(code: 1, name: "Aries")
            
        } else if ((mmdd >= 420 && mmdd <= 430) ||
            (mmdd >= 501 && mmdd <= 520)) {
            constellation = JPSConstellation(code: 2, name: "Taurus")
            
        } else if ((mmdd >= 521 && mmdd <= 531) ||
            (mmdd >= 601 && mmdd <= 621)) {
            constellation = JPSConstellation(code: 3, name: "Gemini")
            
        } else if ((mmdd >= 622 && mmdd <= 630) ||
            (mmdd >= 701 && mmdd <= 722)) {
            constellation = JPSConstellation(code: 4, name: "Cancer")
            
        } else if ((mmdd >= 723 && mmdd <= 731) ||
            (mmdd >= 801 && mmdd <= 822)) {
            constellation = JPSConstellation(code: 5, name: "Leo")
            
        } else if ((mmdd >= 823 && mmdd <= 831) ||
            (mmdd >= 901 && mmdd <= 922)) {
            constellation = JPSConstellation(code: 6, name: "Virgo")
            
        } else if ((mmdd >= 923 && mmdd <= 930) ||
            (mmdd >= 1001 && mmdd <= 1023)) {
            constellation = JPSConstellation(code: 7, name: "Libra")
            
        } else if ((mmdd >= 1024 && mmdd <= 1031) ||
            (mmdd >= 1101 && mmdd <= 1122)) {
            constellation = JPSConstellation(code: 8, name: "Scorpio")
            
        } else if ((mmdd >= 1123 && mmdd <= 1130) ||
            (mmdd >= 1201 && mmdd <= 1221)) {
            constellation = JPSConstellation(code: 9, name: "Sagittarius")
            
        } else if ((mmdd >= 1222 && mmdd <= 1231) ||
            (mmdd >= 101 && mmdd <= 119)) {
            constellation = JPSConstellation(code: 10, name: "Capricornus")
            
        } else if ((mmdd >= 120 && mmdd <= 131) ||
            (mmdd >= 201 && mmdd <= 218)) {
            constellation = JPSConstellation(code: 11, name: "Aquarius")
            
        } else if ((mmdd >= 219 && mmdd <= 229) ||
            (mmdd >= 301 && mmdd <= 320)) {
            //Considering that there are 29 days in leap year in February
            constellation = JPSConstellation(code: 12, name: "Pisces")
            
        }else{
            print(mmdd)
            print("Date error")
            constellation = nil
        }
        return constellation
    }
  1. Finally, you can print and verify it
override func viewDidLoad() {
        super.viewDidLoad()
        
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy/MM/dd"
        let date = formatter.date(from: "1995/1/11")
  
        let constellation = constellationWith(date: date!)
        print(constellation?.name as Any)
}