Skip to content

Commit

Permalink
update code, fixed bugs convert Sun to Lunar Calendar.
Browse files Browse the repository at this point in the history
  • Loading branch information
vantoan8x committed Jun 30, 2013
1 parent cd5e0e1 commit f869a69
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 24 deletions.
4 changes: 2 additions & 2 deletions SunLunar.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct
- (TimeSL) convertLunarToSun:(TimeSL)date timeZone:(int)timeZone;

// Convert Lunar to Sun calendar
- (TimeSL) convertLunarToSun:(int)day month:(int)month year:(int)year timeZone:(int)timeZone;
- (TimeSL) convertLunarToSun:(int)day month:(int)month year:(int)year lunarLeap:(int)lunarLeap timeZone:(int)timeZone;

// Get Date Components
- (TimeSL) getDateComponentsBy:(NSDate*)date;
Expand All @@ -48,7 +48,7 @@ typedef struct
- (TimeSL) getNextDayBy:(int)day month:(int)month year:(int)year;

// Get count day of a Month
- (int) getNumberDayOfMonth:(int)month;
- (int) getNumberDayOfMonth:(int)month year:(int)year;

// get NSDate by Time separated numbers
- (NSDate*) getDateTimeBy:(int)day month:(int)month year:(int)year hour:(int)hour minute:(int)minute second:(int)second;
Expand Down
125 changes: 103 additions & 22 deletions SunLunar.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,35 @@ - (void) test
TimeSL t = [self convertSunToLunar:29 month:6 year:2013 timeZone:[self getLocalTimeZoneNumber]];
NSLog(@"Lunar day : %d %d %d", t.day, t.month, t.year);

t = [self convertLunarToSun:5 month:5 year:t.year timeZone:[self getLocalTimeZoneNumber]];
t = [self convertLunarToSun:5 month:5 year:t.year lunarLeap:1 timeZone:[self getLocalTimeZoneNumber]];
NSLog(@"Sun day : %d %d %d", t.day, t.month, t.year);

t = [self getToday];
NSLog(@"Today : %d %d %d", t.day, t.month, t.year);

for(int i=1; i<37; i++)
{
t = [self addSomeMonthsTo:10 year:2013 addMonths:i];
NSLog(@"Add Months %d, %d/%d", i, t.month, t.year);
}

for(int i=1; i<37; i++)
{
t = [self addSomeMonthsTo:10 year:2013 addMonths:-i];
NSLog(@"Subtract Months %d, %d/%d", i, t.month, t.year);
}

for(int i=1; i<367; i++)
{
t = [self addSomeDaysTo:1 month:11 year:2013 addDays:i];
NSLog(@"Add days %d, %d/%d/%d", i, t.day, t.month, t.year);
}

for(int i=1; i<367; i++)
{
t = [self addSomeDaysTo:1 month:11 year:2013 addDays:i];
NSLog(@"Subtract days %d, %d/%d/%d", i, t.day, t.month, t.year);
}
}

- (int) getYearWeekBy:(NSDate*)date
Expand Down Expand Up @@ -100,12 +124,21 @@ - (NSDate*) getDateTimeBy:(int)day month:(int)month year:(int)year hour:(int)hou
return d;
}

- (int) getNumberDayOfMonth:(int)month;
- (int) getNumberDayOfMonth:(int)month year:(int)year;
{
if((month == 2) || (month == 4) || (month == 6) || (month == 9) || (month == 11))
if((month == 4) || (month == 6) || (month == 9) || (month == 11))
{
return 30;
}
else if(month == 2)
{
if(year%4)
{
return 28;
}

return 29;
}

return 31;
}
Expand All @@ -123,7 +156,7 @@ - (TimeSL) addSomeDaysTo:(int)day month:(int)month year:(int)year addDays:(int)a
{
if(addDays > 0)
{
int numDayInMonth = [self getNumberDayOfMonth:t.month];
int numDayInMonth = [self getNumberDayOfMonth:t.month year:t.year];
while(t.day > numDayInMonth)
{
t.month++;
Expand All @@ -134,7 +167,7 @@ - (TimeSL) addSomeDaysTo:(int)day month:(int)month year:(int)year addDays:(int)a
}

t.day -= numDayInMonth;
numDayInMonth = [self getNumberDayOfMonth:t.month];
numDayInMonth = [self getNumberDayOfMonth:t.month year:t.year];
}
}
else
Expand All @@ -148,7 +181,7 @@ - (TimeSL) addSomeDaysTo:(int)day month:(int)month year:(int)year addDays:(int)a
t.month = 12;
}

int numberInMonth = [self getNumberDayOfMonth:t.month];
int numberInMonth = [self getNumberDayOfMonth:t.month year:t.year];
t.day += numberInMonth;
}
}
Expand All @@ -172,10 +205,13 @@ - (TimeSL) addSomeMonthsTo:(TimeSL)day addMonths:(int)addMonths
}
else
{
ads = -ads;
int dYear = (ads-1)/12;
ads = ads-dYear*12;
t.month += (12-ads);
t.month += ads;

if(t.month < 1)
{
t.year += (t.month/12 - 1);
t.month = 12 + t.month%12;
}
}
}

Expand All @@ -198,7 +234,7 @@ - (TimeSL) getNextDayBy:(int)day month:(int)month year:(int)year
{
TimeSL t = {++day, month, year};

int numDayInMonth = [self getNumberDayOfMonth:t.month];
int numDayInMonth = [self getNumberDayOfMonth:t.month year:t.year];
if(t.day > numDayInMonth)
{
t.day -= numDayInMonth;
Expand Down Expand Up @@ -226,7 +262,8 @@ - (TimeSL) getPreviousDayBy:(int)day month:(int)month year:(int)year

if(t.day <= 0)
{
t.day = [self getNumberDayOfMonth:--t.month];
t = [self addSomeMonthsTo:t addMonths:-1];
t.day = [self getNumberDayOfMonth:t.month year:t.year];

if(t.month <= 0)
{
Expand All @@ -240,14 +277,14 @@ - (TimeSL) getPreviousDayBy:(int)day month:(int)month year:(int)year

- (TimeSL) getLunarNextDay:(int)day month:(int)month year:(int)year
{
TimeSL sDay = [self convertLunarToSun:day month:month year:YES timeZone:[self getLocalTimeZoneNumber]];
TimeSL sDay = [self convertLunarToSun:day month:month year:YES lunarLeap:1 timeZone:[self getLocalTimeZoneNumber]];
TimeSL sNextDay = [self getNextDayBy:sDay.day month:sDay.month year:sDay.year];
return [self convertSunToLunar:sNextDay.day month:sNextDay.month year:sNextDay.year timeZone:[self getLocalTimeZoneNumber]];
}

- (TimeSL) getLunarPreviousDay:(int)day month:(int)month year:(int)year
{
TimeSL sDay = [self convertLunarToSun:day month:month year:YES timeZone:[self getLocalTimeZoneNumber]];
TimeSL sDay = [self convertLunarToSun:day month:month year:YES lunarLeap:1 timeZone:[self getLocalTimeZoneNumber]];
TimeSL sNextDay = [self getPreviousDayBy:sDay.day month:sDay.month year:sDay.year];
return [self convertSunToLunar:sNextDay.day month:sNextDay.month year:sNextDay.year timeZone:[self getLocalTimeZoneNumber]];
}
Expand Down Expand Up @@ -330,7 +367,7 @@ - (TimeSL)jdToDate:(long long)jd

- (int) getNewMoonDay:(CGFloat)k timeZone:(int)timeZone
{
CGFloat T, T2, T3, dr, Jd1, M, Mpr, F, C1, deltat, JdNew;
double T, T2, T3, dr, Jd1, M, Mpr, F, C1, deltat, JdNew;

T = k/1236.85; // Time in Julian centuries from 1900 January 0.5
T2 = T * T;
Expand Down Expand Up @@ -360,14 +397,14 @@ - (int) getNewMoonDay:(CGFloat)k timeZone:(int)timeZone

JdNew = Jd1 + C1 - deltat;

return (int)(JdNew + 0.5 + timeZone/24);
return (int)(JdNew + 0.5 + timeZone/24.0);
}

- (int) getSunLongitude:(CGFloat)jdn timeZone:(int)timeZone
{
CGFloat T, T2, dr, M, L0, DL, L;

T = (jdn - 2451545.5 - timeZone/24) / 36525; // Time in Julian centuries from 2000-01-01 12:00:00 GMT
T = (jdn - 2451545.5 - timeZone/24.0) / 36525; // Time in Julian centuries from 2000-01-01 12:00:00 GMT
T2 = T*T;
dr = M_PI/180; // degree to radian
M = 357.52910 + 35999.05030*T - 0.0001559*T2 - 0.00000048*T*T2; // mean anomaly, degree
Expand Down Expand Up @@ -416,18 +453,62 @@ - (int) getLeapMonthOffset:(CGFloat)a11 timeZone:(int)timeZone
return i-1;
}


- (int) getLunarLeapFromSunDate:(int)day month:(int)month year:(int)year timeZone:(int)timeZone
{
CGFloat k, dayNumber, monthStart, a11, b11, lunarLeap;

dayNumber = [self jdFromDate:day month:month year:year];

k = (int)((dayNumber - 2415021.076998695) / 29.530588853);
monthStart = [self getNewMoonDay:k+1 timeZone:timeZone];
if (monthStart > dayNumber)
{
monthStart = [self getNewMoonDay:k timeZone:timeZone];
}

a11 = [self getLunarMonth11:year timeZone:timeZone];
b11 = a11;
if (a11 >= monthStart)
{
a11 = [self getLunarMonth11:year-1 timeZone:timeZone];
}
else
{
b11 = [self getLunarMonth11:year+1 timeZone:timeZone];
}

lunarLeap = 0;
int diff = (int)((monthStart - a11)/29);
if (b11 - a11 > 365)
{
CGFloat leapMonthDiff = [self getLeapMonthOffset:a11 timeZone:timeZone];
if (diff >= leapMonthDiff)
{
if (diff == leapMonthDiff)
{
lunarLeap = 1;
}
}
}

return lunarLeap;
}

- (TimeSL) convertSunToLunar:(TimeSL)date timeZone:(int)timeZone
{
return [self convertSunToLunar:date.day month:date.month year:date.year timeZone:timeZone];
}

- (TimeSL) convertSunToLunar:(int)day month:(int)month year:(int)year timeZone:(int)timeZone
{
CGFloat k, dayNumber, monthStart, a11, b11, lunarDay, lunarMonth, lunarYear, lunarLeap;

int k, dayNumber, monthStart, a11, b11, lunarDay, lunarMonth, lunarYear, lunarLeap;

dayNumber = [self jdFromDate:day month:month year:year];

k = (int)((dayNumber - 2415021.076998695) / 29.530588853);

monthStart = [self getNewMoonDay:k+1 timeZone:timeZone];
if (monthStart > dayNumber)
{
Expand Down Expand Up @@ -480,10 +561,10 @@ - (TimeSL) convertSunToLunar:(int)day month:(int)month year:(int)year timeZone:(

- (TimeSL) convertLunarToSun:(TimeSL)date timeZone:(int)timeZone
{
return [self convertLunarToSun:date.day month:date.month year:date.year timeZone:timeZone];
return [self convertLunarToSun:date.day month:date.month year:date.year lunarLeap:1 timeZone:timeZone];
}

- (TimeSL) convertLunarToSun:(int)day month:(int)month year:(int)year timeZone:(int)timeZone
- (TimeSL) convertLunarToSun:(int)day month:(int)month year:(int)year lunarLeap:(int)lunarLeap timeZone:(int)timeZone
{

CGFloat k, a11, b11, off, leapOff, leapMonth, monthStart;
Expand Down Expand Up @@ -514,12 +595,12 @@ - (TimeSL) convertLunarToSun:(int)day month:(int)month year:(int)year timeZone:(
leapMonth += 12;
}

if (month != leapMonth)
if ((lunarLeap != 0) && (month != leapMonth))
{
TimeSL t = {0,0,0};
return t;
}
else if (off >= leapOff)
else if ((lunarLeap != 0) && (off >= leapOff))
{
off += 1;
}
Expand Down

0 comments on commit f869a69

Please sign in to comment.