美文网首页
ios 获取24节气的方法 (1901-2100)

ios 获取24节气的方法 (1901-2100)

作者: 一1二2三3 | 来源:发表于2017-11-10 10:42 被阅读42次

    ios 获取24节气的方法 (1901-2100)

    调用getSolartermsMsg 传入公历年月日即可

    #define baseYear  1901
    
    @property (nonatomic, strong) NSCalendar *gregorianCalendar;
    @property (nonatomic, strong) NSArray *sectionalTermMap;
    @property (nonatomic, strong) NSArray *sectionalTermYear;
    @property (nonatomic, strong) NSArray *principleTermMap;
    @property (nonatomic, strong) NSArray *principleTermYear;
    @property (nonatomic, strong) NSArray *principleTermNames;
    @property (nonatomic, strong) NSArray *sectionalTermNames;
    
    
    - (NSString *)getSolartermsMsg:(NSInteger)gregorianYear gregorianMonth:(NSInteger)gregorianMonth gregorianDate:(NSInteger)gregorianDate{
        int sectionalTerm = [self sectionalTerm:gregorianYear mother:gregorianMonth];
        int principleTerm = [self principleTerm:gregorianYear mother:gregorianMonth];
        NSString *str = @"";
        if (gregorianDate == sectionalTerm) {
            str = self.sectionalTermNames[gregorianMonth - 1];
        } else if (gregorianDate == principleTerm) {
            str = self.principleTermNames[gregorianMonth - 1];
        }
        return str;
    }
    
    - (int)sectionalTerm:(NSInteger)y mother:(NSInteger)m{
        if (y < 1901 || y > 2100){
            return 0;
        }
        NSInteger index = 0;
        NSInteger ry = y - baseYear + 1;
        while (ry >= [self.sectionalTermYear[m - 1][index] intValue]) {
            index++;
        }
        int term = [self.sectionalTermMap[m - 1][4 * index + ry % 4] intValue];
        if ((ry == 121) && (m == 4)){
            term = 5;
        }
        if ((ry == 132) && (m == 4)){
            term = 5;
        }
        if ((ry == 194) && (m == 6)){
            term = 6;
        }
        return term;
    }
    - (int)principleTerm:(NSInteger)y mother:(NSInteger)m{
        if (y < 1901 || y > 2100){
            return 0;
        }
        int index = 0;
        NSInteger ry = y - baseYear + 1;
        while (ry >= [self.principleTermYear[m - 1][index] intValue]) {
            index++;
        }
        int term = [self.principleTermMap[m - 1][4 * index + ry % 4] intValue];
        if ((ry == 171) && (m == 3)){
            term = 21;
        }
        if ((ry == 181) && (m == 5)){
            term = 21;
        }
        return term;
    }
    -(NSCalendar *)gregorianCalendar{
        if (!_gregorianCalendar) {
            _gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        }
        return _gregorianCalendar;
    }
    -(NSArray *)principleTermNames{
        if (!_principleTermNames) {
            _principleTermNames = @[@"大寒", @"雨水", @"春分", @"谷雨", @"小满", @"夏至", @"大暑", @"处暑", @"秋分", @"霜降", @"小雪", @"冬至"];
        }
        return _principleTermNames;
    }
    -(NSArray *)sectionalTermNames{
        if (!_sectionalTermNames) {
            _sectionalTermNames = @[@"小寒", @"立春", @"惊蛰", @"清明", @"立夏", @"芒种", @"小暑", @"立秋", @"白露", @"寒露", @"立冬", @"大雪"];
        }
        return _sectionalTermNames;
    }
    -(NSArray *)sectionalTermMap{
        if (!_sectionalTermMap) {
            _sectionalTermMap = @[
                                  @[@(7), @(6), @(6), @(6), @(6), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(5), @(5), @(5), @(5), @(5), @(4), @(5), @(5)],
                                  @[@(5), @(4), @(5), @(5), @(5), @(4), @(4), @(5), @(5), @(4), @(4), @(4), @(4), @(4), @(4), @(4), @(4), @(3), @(4), @(4), @(4), @(3), @(3), @(4), @(4), @(3), @(3), @(3)],
                                  @[@(6), @(6), @(6), @(7), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(5)],
                                  @[@(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(4), @(4), @(5), @(5), @(4), @(4), @(4), @(5), @(4), @(4), @(4), @(4), @(5)],
                                  @[@(6), @(6), @(6), @(7), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(5)],
                                  @[@(6), @(6), @(7), @(7), @(6), @(6), @(6), @(7), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(5)],
                                  @[@(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(6), @(6), @(6), @(7), @(7)],
                                  @[@(8), @(8), @(8), @(9), @(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(7)],
                                  @[@(8), @(8), @(8), @(9), @(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(7)],
                                  @[@(9), @(9), @(9), @(9), @(8), @(9), @(9), @(9), @(8), @(8), @(9), @(9), @(8), @(8), @(8), @(9), @(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(8)],
                                  @[@(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(7)],
                                  @[@(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(6), @(6), @(6), @(7), @(7)]
                                  ];
        }
        return _sectionalTermMap;
    }
    
    - (NSArray *)sectionalTermYear{
        if (!_sectionalTermYear) {
            _sectionalTermYear = @[
                                   @[@13, @49, @85, @117, @149, @185, @201, @250, @250],
                                   @[@13, @45, @81, @117, @149, @185, @201, @250, @250],
                                   @[@13, @48, @84, @112, @148, @184, @200, @201, @250],
                                   @[@13, @45, @76, @108, @140, @172, @200, @201, @250],
                                   @[@13, @44, @72, @104, @132, @168, @200, @201, @250],
                                   @[@5, @33, @68, @96, @124, @152, @188, @200, @201],
                                   @[@29, @57, @85, @120, @148, @176, @200, @201, @250],
                                   @[@13, @48, @76, @104, @132, @168, @196, @200, @201],
                                   @[@25, @60, @88, @120, @148, @184, @200, @201, @250],
                                   @[@16, @44, @76, @108, @144, @172, @200, @201, @250],
                                   @[@28, @60, @92, @124, @160, @192, @200, @201, @250],
                                   @[@17, @53, @85, @124, @156, @188, @200, @201, @250]
                                   ];
        }
        return _sectionalTermYear;
    }
    -(NSArray *)principleTermMap{
        if (!_principleTermMap) {
            _principleTermMap = @[
                                  @[@21, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @20, @20, @20, @20, @20, @19, @20, @20, @20, @19, @19, @20],
                                  @[@20, @19, @19, @20, @20, @19, @19, @19, @19, @19, @19, @19, @19, @18, @19, @19, @19, @18, @18, @19, @19, @18, @18, @18, @18, @18, @18, @18],
                                  @[@21, @21, @21, @22, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @21, @20, @20, @20, @20, @19, @20, @20, @20, @20],
                                  @[@20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @21, @20, @20, @20, @20, @19, @20, @20, @20, @19, @19, @20, @20, @19, @19, @19, @20, @20],
                                  @[@21, @22, @22, @22, @21, @21, @22, @22, @21, @21, @21, @22, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @21, @21],
                                  @[@22, @22, @22, @22, @21, @22, @22, @22, @21, @21, @22, @22, @21, @21, @21, @22, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @21],
                                  @[@23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @22, @22, @22, @22, @23],
                                  @[@23, @24, @24, @24, @23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @23],
                                  @[@23, @24, @24, @24, @23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @23],
                                  @[@24, @24, @24, @24, @23, @24, @24, @24, @23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @23],
                                  @[@23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @22, @22, @22, @22, @21, @22, @22, @22, @21, @21, @22, @22, @22],
                                  @[@22, @22, @23, @23, @22, @22, @22, @23, @22, @22, @22, @22, @21, @22, @22, @22, @21, @21, @22, @22, @21, @21, @21, @22, @21, @21, @21, @21, @22]];
        }
        return _principleTermMap;
    }
    -(NSArray *)principleTermYear{
        if (!_principleTermYear) {
            _principleTermYear = @[
                                   @[@13, @45, @81, @113, @149, @185, @201],
                                   @[@21, @57, @93, @125, @161, @193, @201],
                                   @[@21, @56, @88, @120, @152, @188, @200, @201],
                                   @[@21, @49, @81, @116, @144, @176, @200, @201],
                                   @[@17, @49, @77, @112, @140, @168, @200, @201],
                                   @[@28, @60, @88, @116, @148, @180, @200, @201],
                                   @[@25, @53, @84, @112, @144, @172, @200, @201],
                                   @[@29, @57, @89, @120, @148, @180, @200, @201],
                                   @[@17, @45, @73, @108, @140, @168, @200, @201],
                                   @[@28, @60, @92, @124, @160, @192, @200, @201],
                                   @[@16, @44, @80, @112, @148, @180, @200, @201],
                                   @[@17, @53, @88, @120, @156, @188, @200, @201]
                                   ];
        }
        return _principleTermYear;
    }
    

    相关文章

      网友评论

          本文标题:ios 获取24节气的方法 (1901-2100)

          本文链接:https://www.haomeiwen.com/subject/tvtbmxtx.html