From 37c8b915c7e348e55c16bbd963b470de642e46ac Mon Sep 17 00:00:00 2001
From: jgzhou <996155731@qq.com>
Date: Fri, 09 Apr 2021 02:01:41 +0000
Subject: [PATCH] 优化

---
 Lunar/Lunar/Tool/Lunar.h              |    5 +-
 Lunar/Lunar.xcodeproj/project.pbxproj |    8 ++++
 Lunar/Lunar/Tool/Lunar.m              |   48 ++++++++++++++++++++----
 Lunar/Lunar/ViewController.m          |    4 +-
 4 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/Lunar/Lunar.xcodeproj/project.pbxproj b/Lunar/Lunar.xcodeproj/project.pbxproj
index 724e58b..ff1aca5 100644
--- a/Lunar/Lunar.xcodeproj/project.pbxproj
+++ b/Lunar/Lunar.xcodeproj/project.pbxproj
@@ -42,6 +42,13 @@
 /* End PBXFrameworksBuildPhase section */
 
 /* Begin PBXGroup section */
+		076D1B56261713D800087D45 /* Util */ = {
+			isa = PBXGroup;
+			children = (
+			);
+			path = Util;
+			sourceTree = "<group>";
+		};
 		07F51C1A2615C1A600D30158 = {
 			isa = PBXGroup;
 			children = (
@@ -86,6 +93,7 @@
 		07F51C462615C33A00D30158 /* Tool */ = {
 			isa = PBXGroup;
 			children = (
+				076D1B56261713D800087D45 /* Util */,
 				07F51C472615C34A00D30158 /* Lunar.h */,
 				07F51C482615C34A00D30158 /* Lunar.m */,
 			);
diff --git a/Lunar/Lunar/Tool/Lunar.h b/Lunar/Lunar/Tool/Lunar.h
index 4b98249..04a2525 100644
--- a/Lunar/Lunar/Tool/Lunar.h
+++ b/Lunar/Lunar/Tool/Lunar.h
@@ -164,6 +164,7 @@
 + (instancetype)fromYmd:(NSInteger)lunarYear :(NSInteger)lunarMonth :(NSInteger)lunarDay;
 + (instancetype)fromYmdHms:(NSInteger)lunarYear :(NSInteger)lunarMonth :(NSInteger)lunarDay :(NSInteger)hour :(NSInteger)minute :(NSInteger)second;
 + (instancetype)fromDate:(NSDate *)date;
++ (instancetype)fromSolar:(Solar *)solar;
 + (CGFloat)nutationLon2:(CGFloat)t;
 + (CGFloat)eLon:(CGFloat)t :(NSInteger)n;
 + (CGFloat)gxcSunLon:(CGFloat)t;
@@ -411,8 +412,8 @@
 @property (nonatomic, assign) NSInteger BASE_DAY_GAN_ZHI_INDEX;
 @property (nonatomic, assign) NSInteger BASE_MONTH_ZHI_INDEX;
 @property (nonatomic, assign) NSInteger BASE_WEEK_INDEX;
-@property (nonatomic, strong) NSArray *LEAP_MONTH_YEAR;
-@property (nonatomic, strong) NSArray *LUNAR_MONTH;
+@property (nonatomic, strong) NSArray *LEAP_MONTH_YEAR;// 闰年表
+@property (nonatomic, strong) NSArray *LUNAR_MONTH;// 闰月表
 @property (nonatomic, strong) NSArray *XUN;
 @property (nonatomic, strong) NSArray *XUN_KONG;
 @property (nonatomic, strong) NSArray *LIU_YAO;
diff --git a/Lunar/Lunar/Tool/Lunar.m b/Lunar/Lunar/Tool/Lunar.m
index 62d8613..d380a3f 100644
--- a/Lunar/Lunar/Tool/Lunar.m
+++ b/Lunar/Lunar/Tool/Lunar.m
@@ -121,6 +121,11 @@
 + (instancetype)fromDate:(NSDate *)date
 {
     Solar *solar = [Solar fromDate:date];
+    return [self fromSolar:solar];
+}
+
++ (instancetype)fromSolar:(Solar *)solar
+{
     NSInteger y = solar.year;
     NSInteger m = solar.month;
     NSInteger d = solar.day;
@@ -128,16 +133,19 @@
     NSInteger startYear = [SolarUtil share].BASE_YEAR + 99;
     NSInteger startMonth = 1;
     NSInteger startDay = 1;
+    
     NSInteger lunarYear = [LunarUtil share].BASE_YEAR + 99;
     NSInteger lunarMonth = 11;
     NSInteger lunarDay = 25;
+    
     if (y < 2000) {
-      startYear = [SolarUtil share].BASE_YEAR;
-      startMonth = [SolarUtil share].BASE_MONTH;
-      startDay = [SolarUtil share].BASE_DAY;
-      lunarYear = [LunarUtil share].BASE_YEAR;
-      lunarMonth = [LunarUtil share].BASE_MONTH;
-      lunarDay = [LunarUtil share].BASE_DAY;
+        startYear = [SolarUtil share].BASE_YEAR;
+        startMonth = [SolarUtil share].BASE_MONTH;
+        startDay = [SolarUtil share].BASE_DAY;
+        
+        lunarYear = [LunarUtil share].BASE_YEAR;
+        lunarMonth = [LunarUtil share].BASE_MONTH;
+        lunarDay = [LunarUtil share].BASE_DAY;
     }
     
     NSInteger diff = 0;
@@ -2832,18 +2840,36 @@
     NSInteger d = 30;
     
     if (1 <= month && month <= 8) {
+        
+        if (2*index >= [LunarUtil share].LUNAR_MONTH.count) {
+            NSLog(@"getDaysOfMonth 数组越界:%@", @(month));
+            return d;
+        }
+                
         NSInteger v = [[LunarUtil share].LUNAR_MONTH[2*index] integerValue];
         NSInteger l = month - 1;
         if (((v >> l) & 0x01) == 1) {
             d = 29;
         }
     } else if (9 <= month && month <= 12) {
+        
+        if (2*index >= [LunarUtil share].LUNAR_MONTH.count) {
+            NSLog(@"getDaysOfMonth 数组越界:%@", @(month));
+            return d;
+        }
+        
         NSInteger v = [[LunarUtil share].LUNAR_MONTH[2*index+1] integerValue];
         NSInteger l = month - 9;
         if (((v >> l) & 0x01) == 1) {
             d = 29;
         }
     } else {
+        
+        if (2*index+1 >= [LunarUtil share].LUNAR_MONTH.count) {
+            NSLog(@"getDaysOfMonth 数组越界:%@", @(month));
+            return d;
+        }
+        
         NSInteger v = [[LunarUtil share].LUNAR_MONTH[2*index+1] integerValue];
         v = (v >> 4) & 0x0F;
         if (v != labs(month)) {
@@ -2870,7 +2896,13 @@
 + (NSInteger)getLeapMonth:(NSInteger)year
 {
     NSInteger index = year - [LunarUtil share].BASE_YEAR + [LunarUtil share].BASE_INDEX;
-    NSInteger v = [[LunarUtil share].LUNAR_MONTH[2*index + 1] integerValue];
+    
+    if (2*index+1 >= [LunarUtil share].LUNAR_MONTH.count) {
+        NSLog(@"getLeapMonth 数组越界:%@", @(year));
+        return 0;
+    }
+    
+    NSInteger v = [[LunarUtil share].LUNAR_MONTH[2*index+1] integerValue];
     v = (v >> 4) & 0x0F;
     return v;
 }
@@ -5040,7 +5072,7 @@
 
 - (Lunar *)getLunar
 {
-    return [Lunar fromDate:_calendar];
+    return [Lunar fromSolar:self];
 }
 
 /**
diff --git a/Lunar/Lunar/ViewController.m b/Lunar/Lunar/ViewController.m
index 34998ed..a45261e 100644
--- a/Lunar/Lunar/ViewController.m
+++ b/Lunar/Lunar/ViewController.m
@@ -20,8 +20,8 @@
     
     self.view.backgroundColor = [UIColor redColor];
     
-//    Solar *d = [Solar fromYmdHms:2021 :4 :1 :0 :0 :0];
-    Solar *d = [Solar fromDate:[NSDate new]];
+    Solar *d = [Solar fromYmdHms:1901 :1 :1 :0 :0 :0];
+//    Solar *d = [Solar fromDate:[NSDate new]];
     NSLog(@"公历:%@", [d toFullString]);
     
     Lunar *l = [d getLunar];

--
Gitblit v1.9.1