OS X 10.6.5 upgrade
11/11/2010, 12:23 - Apple
Everything is fine.

發表回應 發表回應 ( 446預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3.1 / 361 )

陽曆轉農曆及四柱八字程式更新
09/27/2010, 10:36 - Open Source
上次的轉換程式,轉換出來的四柱八字,和黃曆上所記的不同,經過研究之後,發現農曆的年干支轉換,是發生在立春,而非農曆正月初一。而每月的干支轉換是發生在該月份的第一個節氣,而非每月的第一日。造成這個問題,是因為在四柱八字轉參考了紫微斗數的四柱轉換方式,紫微斗數的轉換方式出較簡單直接,而農曆則需要考慮節氣轉換。而節氣是和太陽運行照射的角度有關。感謝JamDing的指正,並提供測試方法。

LunarCalendar.h

//
// LunarCalendar.h
// LunarCal
//
// Created by Tasuka Hsu on 09/07/2010.
// Copyleft 2010 http://Tasuka.IDV.TW.
//

#import <Foundation/Foundation.h>

@interface LunarCalendar : NSObject {
NSNumber *SolarYear; //陽曆年 1900-2100
NSNumber *SolarMonth; //陽曆月 1-12
NSNumber *SolarDay; //陽曆日 1-31
NSNumber *SolarHour; //陽曆時 0-23
}

-(void)setSolarYear:(int) year month:(int)month day:(int)day hour:(int)hour;
-(void)setSolarYear:(int) year month:(int)month day:(int)day;
-(void)setSolarYear:(int) year;
-(void)setSolarMonth:(int) month;
-(void)setSolarDay:(int) day;
-(void)setSolarHour:(int) hour;

-(int)solarYear; //陽曆年 1900-2100
-(int)solarMonth; //陽曆月 1-12
-(int)solarDay; //陽曆日 1-31
-(int)solarHour; //陽曆時 0-23
-(int)weekDay; //星期 0-6

-(int)lunarYear; //農曆年 1900-2100
-(int)lunarMonth; //農曆月 1-12
-(int)lunarDay; //農曆日 1-30
-(int)lunarHour; //農曆時 0-11

-(NSString *)lunarHourChinese; //農曆時
-(NSDate *)spring:(int)year; //春分
-(NSDate *)spring:(int)year index:(int)idx;
-(NSDate *)springStart:(int)year; //立春

-(BOOL)isLeap; //閏年

-(NSString *)chineseYear; //農曆干支年
-(NSString *)chineseMonth; //農曆干支月
-(NSString *)chineseDay; //農曆干支日
-(NSString *)chineseHour; //農曆干支時
-(NSString *)chineseYearTitle; //紀元
-(NSString *)chineseYearAnimal; //生肖

-(NSArray *)Gan; //天干表
-(NSArray *)Zhi; //地支表
-(NSArray *)Animals; //生肖表
-(NSArray *)solarTerms; //二十四節氣表

-(int)solarTerm:(int)year index:(int)idx; //節氣
-(void)lunarCalendar; //計算陽曆轉換農曆

@end



LunarCalendar.m

//
// LunarCalendar.m
// LunarCal
//
// Created by Tasuka Hsu on 09/07/2010.
// Copyleft 2010 http://Tasuka.IDV.TW.
// This class is based on http://s.o4u.com/host/blog/calendar/calendar.htm Java Script code

#import "LunarCalendar.h"

@interface LunarCalendar (Private)
NSArray *lunarInfo;
NSArray *solarMonths;
NSArray *Gan;
NSArray *Zhi;
NSArray *Animals;
NSArray *solarTerms;
NSArray *solarTermsSunAngle;
NSArray *solarTermsRate;
NSArray *solarTermsDayIdx;

NSNumber *week;
NSNumber *LunarYear;
NSNumber *LunarMonth;
NSNumber *LunarDay;
NSNumber *LunarHour;

NSString *ChineseYear;
NSString *ChineseMonth;
NSString *ChineseDay;
NSString *ChineseHour;
NSString *ChineseYearTitle;
NSString *ChineseYearAnimal;

int dayCyclical;
BOOL NightHour;
BOOL Leap;

-(int)lunarYearDays:(int)year;
-(int)leapDays:(int)year;
-(int)leapMonth:(int)year;
-(int)monthDays:(int)year: month:(int)month;
-(int)solarDays:(int)year: month:(int)month;

-(NSString *)cyclical:(int)number;
-(NSDate *)lunarCalc;

#define MoonCycle 29.53059
#define EarthCycle 365.2422

@end

@implementation LunarCalendar
-(id)init
{
//農曆資料表
lunarInfo=[NSArray arrayWithObjects:
[NSNumber numberWithInt:0x4bd8],[NSNumber numberWithInt:0x4ae0],[NSNumber numberWithInt:0xa570],
[NSNumber numberWithInt:0x54d5],[NSNumber numberWithInt:0xd260],[NSNumber numberWithInt:0xd950],
[NSNumber numberWithInt:0x5554],[NSNumber numberWithInt:0x56af],[NSNumber numberWithInt:0x9ad0],
[NSNumber numberWithInt:0x55d2],[NSNumber numberWithInt:0x4ae0],[NSNumber numberWithInt:0xa5b6],
[NSNumber numberWithInt:0xa4d0],[NSNumber numberWithInt:0xd250],[NSNumber numberWithInt:0xd295],
[NSNumber numberWithInt:0xb54f],[NSNumber numberWithInt:0xd6a0],[NSNumber numberWithInt:0xada2],
[NSNumber numberWithInt:0x95b0],[NSNumber numberWithInt:0x4977],[NSNumber numberWithInt:0x497f],
[NSNumber numberWithInt:0xa4b0],[NSNumber numberWithInt:0xb4b5],[NSNumber numberWithInt:0x6a50],
[NSNumber numberWithInt:0x6d40],[NSNumber numberWithInt:0xab54],[NSNumber numberWithInt:0x2b6f],
[NSNumber numberWithInt:0x9570],[NSNumber numberWithInt:0x52f2],[NSNumber numberWithInt:0x4970],
[NSNumber numberWithInt:0x6566],[NSNumber numberWithInt:0xd4a0],[NSNumber numberWithInt:0xea50],
[NSNumber numberWithInt:0x6a95],[NSNumber numberWithInt:0x5adf],[NSNumber numberWithInt:0x2b60],
[NSNumber numberWithInt:0x86e3],[NSNumber numberWithInt:0x92ef],[NSNumber numberWithInt:0xc8d7],
[NSNumber numberWithInt:0xc95f],[NSNumber numberWithInt:0xd4a0],[NSNumber numberWithInt:0xd8a6],
[NSNumber numberWithInt:0xb55f],[NSNumber numberWithInt:0x56a0],[NSNumber numberWithInt:0xa5b4],
[NSNumber numberWithInt:0x25df],[NSNumber numberWithInt:0x92d0],[NSNumber numberWithInt:0xd2b2],
[NSNumber numberWithInt:0xa950],[NSNumber numberWithInt:0xb557],[NSNumber numberWithInt:0x6ca0],
[NSNumber numberWithInt:0xb550],[NSNumber numberWithInt:0x5355],[NSNumber numberWithInt:0x4daf],
[NSNumber numberWithInt:0xa5b0],[NSNumber numberWithInt:0x4573],[NSNumber numberWithInt:0x52bf],
[NSNumber numberWithInt:0xa9a8],[NSNumber numberWithInt:0xe950],[NSNumber numberWithInt:0x6aa0],
[NSNumber numberWithInt:0xaea6],[NSNumber numberWithInt:0xab50],[NSNumber numberWithInt:0x4b60],
[NSNumber numberWithInt:0xaae4],[NSNumber numberWithInt:0xa570],[NSNumber numberWithInt:0x5260],
[NSNumber numberWithInt:0xf263],[NSNumber numberWithInt:0xd950],[NSNumber numberWithInt:0x5b57],
[NSNumber numberWithInt:0x56a0],[NSNumber numberWithInt:0x96d0],[NSNumber numberWithInt:0x4dd5],
[NSNumber numberWithInt:0x4ad0],[NSNumber numberWithInt:0xa4d0],[NSNumber numberWithInt:0xd4d4],
[NSNumber numberWithInt:0xd250],[NSNumber numberWithInt:0xd558],[NSNumber numberWithInt:0xb540],
[NSNumber numberWithInt:0xb6a0],[NSNumber numberWithInt:0x95a6],[NSNumber numberWithInt:0x95bf],
[NSNumber numberWithInt:0x49b0],[NSNumber numberWithInt:0xa974],[NSNumber numberWithInt:0xa4b0],
[NSNumber numberWithInt:0xb27a],[NSNumber numberWithInt:0x6a50],[NSNumber numberWithInt:0x6d40],
[NSNumber numberWithInt:0xaf46],[NSNumber numberWithInt:0xab60],[NSNumber numberWithInt:0x9570],
[NSNumber numberWithInt:0x4af5],[NSNumber numberWithInt:0x4970],[NSNumber numberWithInt:0x64b0],
[NSNumber numberWithInt:0x74a3],[NSNumber numberWithInt:0xea50],[NSNumber numberWithInt:0x6b58],
[NSNumber numberWithInt:0x5ac0],[NSNumber numberWithInt:0xab60],[NSNumber numberWithInt:0x96d5],
[NSNumber numberWithInt:0x92e0],[NSNumber numberWithInt:0xc960],[NSNumber numberWithInt:0xd954],
[NSNumber numberWithInt:0xd4a0],[NSNumber numberWithInt:0xda50],[NSNumber numberWithInt:0x7552],
[NSNumber numberWithInt:0x56a0],[NSNumber numberWithInt:0xabb7],[NSNumber numberWithInt:0x25d0],
[NSNumber numberWithInt:0x92d0],[NSNumber numberWithInt:0xcab5],[NSNumber numberWithInt:0xa950],
[NSNumber numberWithInt:0xb4a0],[NSNumber numberWithInt:0xbaa4],[NSNumber numberWithInt:0xad50],
[NSNumber numberWithInt:0x55d9],[NSNumber numberWithInt:0x4ba0],[NSNumber numberWithInt:0xa5b0],
[NSNumber numberWithInt:0x5176],[NSNumber numberWithInt:0x52bf],[NSNumber numberWithInt:0xa930],
[NSNumber numberWithInt:0x7954],[NSNumber numberWithInt:0x6aa0],[NSNumber numberWithInt:0xad50],
[NSNumber numberWithInt:0x5b52],[NSNumber numberWithInt:0x4b60],[NSNumber numberWithInt:0xa6e6],
[NSNumber numberWithInt:0xa4e0],[NSNumber numberWithInt:0xd260],[NSNumber numberWithInt:0xea65],
[NSNumber numberWithInt:0xd530],[NSNumber numberWithInt:0x5aa0],[NSNumber numberWithInt:0x76a3],
[NSNumber numberWithInt:0x96d0],[NSNumber numberWithInt:0x4afb],[NSNumber numberWithInt:0x4ad0],
[NSNumber numberWithInt:0xa4d0],[NSNumber numberWithInt:0xd0b6],[NSNumber numberWithInt:0xd25f],
[NSNumber numberWithInt:0xd520],[NSNumber numberWithInt:0xdd45],[NSNumber numberWithInt:0xb5a0],
[NSNumber numberWithInt:0x56d0],[NSNumber numberWithInt:0x55b2],[NSNumber numberWithInt:0x49b0],
[NSNumber numberWithInt:0xa577],[NSNumber numberWithInt:0xa4b0],[NSNumber numberWithInt:0xaa50],
[NSNumber numberWithInt:0xb255],[NSNumber numberWithInt:0x6d2f],[NSNumber numberWithInt:0xada0],
[NSNumber numberWithInt:0x4b63],[NSNumber numberWithInt:0x937f],[NSNumber numberWithInt:0x49f8],
[NSNumber numberWithInt:0x4970],[NSNumber numberWithInt:0x64b0],[NSNumber numberWithInt:0x68a6],
[NSNumber numberWithInt:0xea5f],[NSNumber numberWithInt:0x6b20],[NSNumber numberWithInt:0xa6c4],
[NSNumber numberWithInt:0xaaef],[NSNumber numberWithInt:0x92e0],[NSNumber numberWithInt:0xd2e3],
[NSNumber numberWithInt:0xc960],[NSNumber numberWithInt:0xd557],[NSNumber numberWithInt:0xd4a0],
[NSNumber numberWithInt:0xda50],[NSNumber numberWithInt:0x5d55],[NSNumber numberWithInt:0x56a0],
[NSNumber numberWithInt:0xa6d0],[NSNumber numberWithInt:0x55d4],[NSNumber numberWithInt:0x52d0],
[NSNumber numberWithInt:0xa9b8],[NSNumber numberWithInt:0xa950],[NSNumber numberWithInt:0xb4a0],
[NSNumber numberWithInt:0xb6a6],[NSNumber numberWithInt:0xad50],[NSNumber numberWithInt:0x55a0],
[NSNumber numberWithInt:0xaba4],[NSNumber numberWithInt:0xa5b0],[NSNumber numberWithInt:0x52b0],
[NSNumber numberWithInt:0xb273],[NSNumber numberWithInt:0x6930],[NSNumber numberWithInt:0x7337],
[NSNumber numberWithInt:0x6aa0],[NSNumber numberWithInt:0xad50],[NSNumber numberWithInt:0x4b55],
[NSNumber numberWithInt:0x4b6f],[NSNumber numberWithInt:0xa570],[NSNumber numberWithInt:0x54e4],
[NSNumber numberWithInt:0xd260],[NSNumber numberWithInt:0xe968],[NSNumber numberWithInt:0xd520],
[NSNumber numberWithInt:0xdaa0],[NSNumber numberWithInt:0x6aa6],[NSNumber numberWithInt:0x56df],
[NSNumber numberWithInt:0x4ae0],[NSNumber numberWithInt:0xa9d4],[NSNumber numberWithInt:0xa4d0],
[NSNumber numberWithInt:0xd150],[NSNumber numberWithInt:0xf252],[NSNumber numberWithInt:0xd520],nil];

//陽曆月天數表
solarMonths=[NSArray arrayWithObjects:[NSNumber numberWithInt:31],[NSNumber numberWithInt:28],
[NSNumber numberWithInt:31],[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],
[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],[NSNumber numberWithInt:31],
[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],[NSNumber numberWithInt:30],
[NSNumber numberWithInt:31],nil];

//天干表
Gan=[NSArray arrayWithObjects:@"甲",@"乙",@"丙",@"丁",@"戊",@"己",@"庚",@"辛",@"壬",@"癸",nil];

//地支表
Zhi=[NSArray arrayWithObjects:@"子",@"丑",@"寅",@"卯",@"辰",@"巳",@"午",@"未",@"申",@"酉",@"戌",@"亥",nil];

//生肖表
Animals=[NSArray arrayWithObjects:@"鼠",@"牛",@"虎",@"兔",@"龍",@"蛇",@"馬",@"羊",@"猴",@"雞",@"狗",@"豬",nil];

//節氣表
solarTerms=[NSArray arrayWithObjects:
@"小寒",@"大寒",@"立春",@"雨水",@"驚蟄",@"春分",
@"清明",@"穀雨",@"立夏",@"小滿",@"芒種",@"夏至",
@"小暑",@"大暑",@"立秋",@"處暑",@"白露",@"秋分",
@"寒露",@"霜降",@"立冬",@"小雪",@"大雪",@"冬至",nil];

//節氣計算值表
solarTermsRate=[NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.242778],[NSNumber numberWithFloat:0.242765],
[NSNumber numberWithFloat:0.242713],[NSNumber numberWithFloat:0.242627],
[NSNumber numberWithFloat:0.242512],[NSNumber numberWithFloat:0.242377],
[NSNumber numberWithFloat:0.242231],[NSNumber numberWithFloat:0.242083],
[NSNumber numberWithFloat:0.241945],[NSNumber numberWithFloat:0.241825],
[NSNumber numberWithFloat:0.241731],[NSNumber numberWithFloat:0.241669],
[NSNumber numberWithFloat:0.241642],[NSNumber numberWithFloat:0.241654],
[NSNumber numberWithFloat:0.241703],[NSNumber numberWithFloat:0.241786],
[NSNumber numberWithFloat:0.241898],[NSNumber numberWithFloat:0.242032],
[NSNumber numberWithFloat:0.242179],[NSNumber numberWithFloat:0.242328],
[NSNumber numberWithFloat:0.242469],[NSNumber numberWithFloat:0.242592],
[NSNumber numberWithFloat:0.242689],[NSNumber numberWithFloat:0.242752],nil];

//節氣計算index值表
solarTermsDayIdx=[NSArray arrayWithObjects:
[NSNumber numberWithFloat:6.3811],[NSNumber numberWithFloat:21.1046],
[NSNumber numberWithFloat:4.8693],[NSNumber numberWithFloat:19.7062],
[NSNumber numberWithFloat:6.3968],[NSNumber numberWithFloat:21.4471],
[NSNumber numberWithFloat:5.6280],[NSNumber numberWithFloat:20.9375],
[NSNumber numberWithFloat:6.3771],[NSNumber numberWithFloat:21.9300],
[NSNumber numberWithFloat:6.5733],[NSNumber numberWithFloat:22.2747],
[NSNumber numberWithFloat:8.0091],[NSNumber numberWithFloat:23.7317],
[NSNumber numberWithFloat:8.4102],[NSNumber numberWithFloat:24.0125],
[NSNumber numberWithFloat:8.5186],[NSNumber numberWithFloat:23.8896],
[NSNumber numberWithFloat:9.1414],[NSNumber numberWithFloat:24.2487],
[NSNumber numberWithFloat:8.2396],[NSNumber numberWithFloat:23.1189],
[NSNumber numberWithFloat:7.9152],[NSNumber numberWithFloat:22.6587],nil];

solarTermsSunAngle=[NSArray arrayWithObjects:
[NSNumber numberWithInt:285],[NSNumber numberWithInt:300],
[NSNumber numberWithInt:315],[NSNumber numberWithInt:330],
[NSNumber numberWithInt:345],[NSNumber numberWithInt:0],
[NSNumber numberWithInt:15],[NSNumber numberWithInt:30],
[NSNumber numberWithInt:45],[NSNumber numberWithInt:60],
[NSNumber numberWithInt:75],[NSNumber numberWithInt:90],
[NSNumber numberWithInt:105],[NSNumber numberWithInt:120],
[NSNumber numberWithInt:135],[NSNumber numberWithInt:150],
[NSNumber numberWithInt:165],[NSNumber numberWithInt:180],
[NSNumber numberWithInt:195],[NSNumber numberWithInt:210],
[NSNumber numberWithInt:225],[NSNumber numberWithInt:240],
[NSNumber numberWithInt:255],[NSNumber numberWithInt:270],nil];

return self;
}

-(void)dealloc
{
[super dealloc];
}

-(int)lunarYearDays:(int)year //農曆年天數
{
int i,sum=348;

for(i=0x8000;i>0x8;i>>=1){
sum+=(([[lunarInfo objectAtIndex:(year-1900)] intValue]&i)?1:0);
}

return (sum+[self leapDays:year]);
}

-(int)leapDays:(int)year //農曆閏年天數
{
if([self leapMonth:year]){
return ((([[lunarInfo objectAtIndex:(year-1899)] intValue]&0xf)==0xf)?30:29);
}else{
return 0;
}
}

-(int)leapMonth:(int)year //農曆閏月
{
int lm=([[lunarInfo objectAtIndex:(year-1900)] intValue]&0xf);
return ((lm==0xf)?0:lm);
}

-(int)monthDays:(int)year month:(int)month //農曆每月天數
{
return ([[lunarInfo objectAtIndex:(year-1900)] intValue]&(0x10000>>(month))?30:29);
}

-(int)solarDays:(int)year month:(int)month //陽曆月天數
{
if(month==2){
return ((((year%4)==0)&&((year%100)!=0)||((year%400)==0))?29:28);
}else{
return [[solarMonths objectAtIndex:month-1] intValue];
}
}

-(NSString *)cyclical:(int)number //干支轉換
{
return [[Gan objectAtIndex:(number%10)] stringByAppendingString:[Zhi objectAtIndex:(number%12)]];
}

-(NSDate *)spring:(int)year //春分日期
{
return [self spring:year index:0];
}

-(NSDate *)springStart:(int)year //立春日期
{
return [self spring:year index:1];
}

-(NSDate *)spring:(int)year index:(int)idx
{
NSDateComponents *dayTerm2Comps=[[NSDateComponents alloc] init];

if(index==0){ //春分
[dayTerm2Comps setDay:[self solarTerm:year index:5]];
[dayTerm2Comps setMonth:3];
}else{ //立春
[dayTerm2Comps setDay:[self solarTerm:year index:2]];
[dayTerm2Comps setMonth:2];
}

[dayTerm2Comps setYear:year];

NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dayTerm2=[gregorian dateFromComponents:dayTerm2Comps];

[dayTerm2Comps release];
[gregorian release];

return dayTerm2;
}

-(NSDate *)lunarCalc //農曆轉換計算
{
int i,leap,temp=0,year=0;
long offset=0;

NSDateComponents *baseDayComps=[[NSDateComponents alloc] init]; //農曆基準年
NSDateComponents *solarDayComps=[[NSDateComponents alloc] init];
NSDateComponents *chineseDayBaseComps=[[NSDateComponents alloc] init]; //干支轉換基準年

[baseDayComps setDay:31];
[baseDayComps setMonth:1];
[baseDayComps setYear:1900];

[chineseDayBaseComps setDay:1];
[chineseDayBaseComps setMonth:1];
[chineseDayBaseComps setYear:1900];

if(SolarYear!=nil && SolarMonth!=nil && SolarDay!=nil){
[solarDayComps setYear:[SolarYear intValue]];
[solarDayComps setMonth:[SolarMonth intValue]];
[solarDayComps setDay:[SolarDay intValue]];
}

NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *baseDay=[gregorian dateFromComponents:baseDayComps];
NSDate *solarDayTemp=[gregorian dateFromComponents:solarDayComps];
NSDate *chineseDayTemp=[gregorian dateFromComponents:chineseDayBaseComps];

week=[NSNumber numberWithInt:[[solarDayTemp descriptionWithCalendarFormat:@"%w" timeZone:nil locale:nil] intValue]];

NSTimeInterval timeDifference=[solarDayTemp timeIntervalSinceDate:baseDay]; //和基準年的日差
offset=timeDifference/86400;

NSTimeInterval chineseTimeDifference=[solarDayTemp timeIntervalSinceDate:chineseDayTemp];
dayCyclical=(chineseTimeDifference/86400)+10;

for(i=1900;(i<2100 && offset>0);i++){
temp=[self lunarYearDays:i];
offset-=temp;
}
if(offset<0){
offset+=temp;
i--;
}
year=i;

leap=[self leapMonth:i];
Leap=NO;

for(i=1;(i<13 && offset>0);i++){
if(leap>0 && i==(leap+1) && Leap==NO){
i--;
Leap=YES;
temp=[self leapDays:year];
}else{
temp=[self monthDays:year month:i];
}
if(Leap==YES && i==(leap+1)){
Leap=NO;
}
offset-=temp;
}

if(offset==0 && leap>0 && i==leap+1){
if(Leap){
Leap=NO;
}else{
Leap=YES;
i--;
}
}

if(offset<0){
offset+=temp;
i--;
}

NSDateComponents *lunarComps=[[NSDateComponents alloc] init];
[lunarComps setYear:year];
[lunarComps setMonth:i];
[lunarComps setDay:offset+1];
[lunarComps setHour:[SolarHour intValue]];

NSDate *lunarDate=[gregorian dateFromComponents:lunarComps];

[chineseDayBaseComps release];
[baseDayComps release];
[solarDayComps release];
[gregorian release];
[lunarComps release];

return lunarDate;
}

-(void)lunarCalendar //農曆轉換格式
{
int year,month,hour,sYear,sMonth,sDay;

//若沒輸入完整陽曆年月日,則取現在時間
if(SolarYear==nil || SolarMonth==nil || SolarDay==nil){
NSDate *nowDate=[NSDate date];
[self setSolarYear:[[nowDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil] intValue]];
[self setSolarMonth:[[nowDate descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil] intValue]];
[self setSolarDay:[[nowDate descriptionWithCalendarFormat:@"%d" timeZone:nil locale:nil] intValue]];
[self setSolarHour:[[nowDate descriptionWithCalendarFormat:@"%H" timeZone:nil locale:nil] intValue]];
}

NSDate *lunarDate=[self lunarCalc];

LunarYear=[NSNumber numberWithInt:[[lunarDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil] intValue]];
LunarMonth=[NSNumber numberWithInt:[[lunarDate descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil] intValue]];
LunarDay=[NSNumber numberWithInt:[[lunarDate descriptionWithCalendarFormat:@"%d" timeZone:nil locale:nil] intValue]];

hour=([[lunarDate descriptionWithCalendarFormat:@"%H" timeZone:nil locale:nil] intValue]+1);

//農曆子時 23-1 所以一天會有兩個子時
NightHour=(hour>23)?YES:NO;
hour=(hour>23)?0:hour/2;
LunarHour=[NSNumber numberWithInt:hour];

year=[self lunarYear];
month=[self lunarMonth];

sYear=[self solarYear];
sMonth=[self solarMonth];
sDay=[self solarDay];

if(year>1911){
ChineseYearTitle=[NSString stringWithString:@"民國"];
if(year-1911==1){
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:@"元年"];
}else{
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:[NSString stringWithFormat:@"%d年",year-1911]];
}
}else{
if(year>1874 && year<1909){
ChineseYearTitle=[NSString stringWithString:@"光緒"];
if(year-1874==1){
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:@"元年"];
}else{
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:[NSString stringWithFormat:@"%d年",year-1874]];
}
}else{
if(year>1908 && year<1912){
ChineseYearTitle=[NSString stringWithString:@"宣統"];
if(year-1908==1){
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:@"元年"];
}else{
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:[NSString stringWithFormat:@"%d年",year-1908]];
}
}
}
}

//農曆在立春就換干支年並在每月第一個節氣就轉換干支月,而紫微斗數則是直接由農曆轉換。兩者轉換出來的年柱和月柱會有不同
int springTerm=[self solarTerm:sYear index:2];
int firstTerm=[self solarTerm:sYear index:(sMonth-1)*2]; //每月第一個節氣

if(sMonth<2){
ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+12)]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)]];
}else{
if(sMonth==2 && sDay==springTerm){
if(sYear==year){
ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+12+1)]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)]];
}else{
ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36+1]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+12+2)]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)+1]];
}
}else{
if(sMonth==2 && sDay<springTerm){
ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+12+1)]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)]];
}else{
if(sYear>year){
ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36+1]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+12+2)]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)+1]];
}else{
if(sMonth==month){
ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+12)]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)]];
}else{
ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+12+1)]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)]];
}
}
}
}
}

ChineseDay=[NSString stringWithString:[self cyclical:dayCyclical]];

hour=(((((dayCyclical%10)%5)*2)+[self lunarHour])%10);
hour=NightHour?((hour+12)%10):hour;
ChineseHour=[NSString stringWithString:[Gan objectAtIndex:hour]];

ChineseHour=[ChineseHour stringByAppendingString:[Zhi objectAtIndex:[self lunarHour]]];

if([self isLeap]){
ChineseMonth=[ChineseMonth stringByAppendingString:@"(閏)"];
}
}

-(void)setSolarYear:(int) year month:(int)month day:(int)day hour:(int)hour
{
[self setSolarYear:year];
[self setSolarMonth:month];
[self setSolarDay:day];
[self setSolarHour:hour];
}

-(void)setSolarYear:(int) year month:(int)month day:(int)day
{
[self setSolarYear:year];
[self setSolarMonth:month];
[self setSolarDay:day];
}

-(void)setSolarYear:(int) year
{
if(year>1873||year<2100){
SolarYear=[NSNumber numberWithInt:year];
}
}

-(void)setSolarMonth:(int) month
{
if(month>0||month<=12){
SolarMonth=[NSNumber numberWithInt:month];
}
}

-(void)setSolarDay:(int) day
{
if(day>0||day<=31){
SolarDay=[NSNumber numberWithInt:day];
}
}

-(void)setSolarHour:(int) hour
{
if(hour>=0||hour<=23){
SolarHour=[NSNumber numberWithInt:hour];
}
}

-(int)solarYear
{
return [SolarYear intValue];
}

-(int)solarMonth
{
return [SolarMonth intValue];
}

-(int)solarDay
{
return [SolarDay intValue];
}

-(int)solarHour
{
return [SolarHour intValue];
}

-(int)weekDay
{
return [week intValue];
}

-(int)lunarYear
{
return [LunarYear intValue];
}

-(int)lunarMonth
{
return [LunarMonth intValue];
}

-(int)lunarDay
{
return [LunarDay intValue];
}

-(int)lunarHour
{
return [LunarHour intValue];
}

-(NSString *)lunarHourChinese
{
return [NSString stringWithString:[Zhi objectAtIndex:[self lunarHour]]];
}

-(BOOL)isLeap
{
return(Leap?YES:NO);
}

-(int)solarTerm:(int)year index:(int)idx //節氣,從小寒開始算起
{
// D+A*Y-L Y西元年份後兩位,L閏年數。
return floor([[solarTermsDayIdx objectAtIndex:idx] floatValue]+ \
[[solarTermsRate objectAtIndex:idx] floatValue]* \
(year-1900)-((year-1900)/4));
}

-(NSString *)chineseYear
{
return ChineseYear;
}

-(NSString *)chineseMonth
{
return ChineseMonth;
}

-(NSString *)chineseDay
{
return ChineseDay;
}

-(NSString *)chineseHour
{
return ChineseHour;
}

-(NSString *)chineseYearTitle
{
return ChineseYearTitle;
}

-(NSString *)chineseYearAnimal
{
return ChineseYearAnimal;
}

-(NSArray *)Gan
{
return Gan;
}

-(NSArray *)Zhi
{
return Zhi;
}

-(NSArray *)Animals
{
return Animals;
}
-(NSArray *)solarTerms
{
return solarTerms;
}
@end



LunarCal.m

//#define TEST0
//#define TEST1
//#define TEST2
//#define TEST21
//#define TEST22
//#define TEST3
//#define TEST4
//#define TEST41
//#define TEST5

#import <Foundation/Foundation.h>
#import "LunarCalendar.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

LunarCalendar *lunarCal=[[LunarCalendar alloc] init];

#ifdef TEST0
NSLog(@"辛卯年庚子月辛酉日");
[lunarCal setSolarYear:2012 month:1 day:1 hour:23];
#endif

#ifdef TEST1
NSLog(@"壬辰年己酉月丙戌日");
[lunarCal setSolarYear:2012 month:9 day:22 hour:23];
#endif

#ifdef TEST2
NSLog(@"己丑年丁丑月甲申日");
[lunarCal setSolarYear:2010 month:2 day:3];
#endif

#ifdef TEST21
NSLog(@"庚寅年戊寅月乙酉日");
[lunarCal setSolarYear:2010 month:2 day:4];
#endif

#ifdef TEST22
NSLog(@"庚寅年戊寅月丙戌日");
[lunarCal setSolarYear:2010 month:2 day:5 hour:10];
#endif

#ifdef TEST3
NSLog(@"乙卯年己卯月癸酉日");
[lunarCal setSolarYear:1975 month:3 day:28 hour:2];
#endif

#ifdef TEST4
NSLog(@"辛亥年庚寅月庚申日");
[lunarCal setSolarYear:1971 month:2 day:4 hour:2];
#endif

#ifdef TEST41
NSLog(@"辛亥年庚寅月辛酉日");
[lunarCal setSolarYear:1971 month:2 day:5 hour:2];
#endif

#ifdef TEST5
NSLog(@"辛亥年壬辰月辛巳日");
[lunarCal setSolarYear:1971 month:4 day:26 hour:2];
#endif

[lunarCal lunarCalendar];
NSLog(@"陽曆:%d-%d-%d 星期%d %d時",[lunarCal solarYear],[lunarCal solarMonth],[lunarCal solarDay], \
[lunarCal weekDay],[lunarCal solarHour]);
NSLog(@"農曆:%d-%d-%d",[lunarCal lunarYear],[lunarCal lunarMonth],[lunarCal lunarDay]);
NSLog(@"農曆:%@%d月%d日 %@時 %@年%@月%@日%@時",[lunarCal chineseYearTitle],[lunarCal lunarMonth], \
[lunarCal lunarDay],[lunarCal lunarHourChinese], \
[lunarCal chineseYear],[lunarCal chineseMonth],[lunarCal chineseDay],[lunarCal chineseHour]);
NSLog(@"生肖屬%@",[lunarCal chineseYearAnimal]);

[lunarCal release];
[pool drain];
return 0;
}



註:修正生肖也需要在立春同步改變問題。
3 回應 3 回應 ( 21725預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 2.9 / 346 )

巧合
09/16/2010, 21:16 - 有的沒的
看到自已的aNobii書櫃,書本有500本,作者也正好有500個人,真巧啊。

發表回應 發表回應 ( 464預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3 / 379 )

Port Objective-C Code from XCode to Linux
09/16/2010, 12:43 - Open Source
為了將在XCode下寫的程式搬到Linux下,
因為是用Objective-C和Apple的Class object,但又不想重寫.
所以就利用一點時間試了一下,
看有沒有方法可以快速的搬移,而且最好不要改原始程式.
又因為不想用復雜的GNUstep環境和重寫GNUmakefile,
但是GNUstep下己經有很多class object己經寫好,而且和Apple的相容,不用都對不起自己.
所以一開始是想將需要用到的GNUstep的class程式碼,一個一個的include叫用.
結果發現這個方法,是個非常笨的方法,因為要一個一個class的.m和.h都去找出來,
且class又會叫用另外的class,這樣永無直境的include,和原來的要求不合.
後來想到GNUstep己經將class object全都包好現成的library,而且header也己經放好,
若只是include header,並在link時使用GNUstep的library,
應該可以跳過GNUstep的環境和重寫GNUmakefile.

經過測試,發現可行,只要重寫一個Makefile就可以了,
而做好的程式可以直接在Linux下使用.
目前遇到的問題是unicode中文字在X-Window下顯示出來是亂碼的問題,
這看來是沒有找到對的字形所致,和Compile或執行無關.

以下是對以前寫的易經卜卦程式所寫的Makefile.
其中先看是XCode(因為XCode只可以在OS X下執行)或不是,而有不同的參數設定.
是OS X的XCode就只要在Link時指定framework就好,
而若不是就指定GNUstep的安裝路徑及使用的library.
這次只用到Foundation的部份,下次再來試GUI的部份.
不過因為最近XCode一直在改版,並加新的class,
所以有些class在GNUstep下還沒有實做出來,在post時要注意.


#
OS=$(shell uname)
CC=gcc
#
PROGRAM=rand
DARWIN=Darwin
FRAMEWORK=Foundation
#
INCLUDE_DIR=/usr/include/GNUstep
LIBS_DIR=/usr/lib
#
ifneq ($(OS),Darwin)
OBJCFLAGS=-fconstant-string-class=NSConstantString
OBJCFLAGS+=-fgnu-runtime
OBJCFLAGS+=-I$(INCLUDE_DIR)
#
LDFLAGS=-L$(LIBS_DIR)
LDFLAGS+=-lobjc
LDFLAGS+=-lgnustep-base
#LDFLAGS+=-lgnustep-gui
else
LDFLAGS=-framework $(FRAMEWORK)
endif
#
RM=rm -fr
#
all: $(PROGRAM)

$(PROGRAM): rand.m Yi randGen pokwa
$(CC) $(OBJCFLAGS) $(LDFLAGS) rand.m Yi.o randGen.o pokwa.o \
-o $(PROGRAM)

Yi: Yi.m Yi.h
$(CC) $(OBJCFLAGS) -c Yi.m -o Yi.o

randGen: randGen.m randGen.h
$(CC) $(OBJCFLAGS) -c randGen.m -o randGen.o

pokwa: pokwa.m pokwa.h
$(CC) $(OBJCFLAGS) -c pokwa.m -o pokwa.o

clean:
@$(RM) *.o $(PROGRAM)



發表回應 發表回應 ( 641預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3 / 462 )

陽曆轉農曆的程式
09/10/2010, 12:28 - Open Source
寫了個Objective-C的陽曆轉農曆程式,順便包含四柱八字轉換,生肖及春分計算。

LunarCalendar.h

//
// LunarCalendar.h
// LunarCal
//
// Created by Tasuka Hsu on 09/07/2010.
// Copyleft 2010 http://Tasuka.IDV.TW.
//

#import <Foundation/Foundation.h>

@interface LunarCalendar : NSObject {
NSNumber *SolarYear;
NSNumber *SolarMonth;
NSNumber *SolarDay;
NSNumber *SolarHour;
}

-(void)setSolarYear:(int) year month:(int)month day:(int)day hour:(int)hour;
-(void)setSolarYear:(int) year month:(int)month day:(int)day;
-(void)setSolarYear:(int) year;
-(void)setSolarMonth:(int) month;
-(void)setSolarDay:(int) day;
-(void)setSolarHour:(int) hour;

-(int)solarYear;
-(int)solarMonth;
-(int)solarDay;
-(int)solarHour;

-(int)weekDay;

-(int)lunarYear;
-(int)lunarMonth;
-(int)lunarDay;
-(int)lunarHour;

-(NSString *)lunarHourChinese;
-(NSDate *)spring:(int)year;

-(BOOL)isLeap;

-(NSString *)chineseYear;
-(NSString *)chineseMonth;
-(NSString *)chineseDay;
-(NSString *)chineseHour;
-(NSString *)chineseYearTitle;
-(NSString *)chineseYearAnimal;

-(void)lunarCalendar;

@end


LunarCalendar.m

//
// LunarCalendar.m
// LunarCal
//
// Created by Tasuka Hsu on 09/07/2010.
// Copyleft 2010 http://Tasuka.IDV.TW.
// This class is based on http://s.o4u.com/host/blog/calendar/calendar.htm Java Script code

#import "LunarCalendar.h"

@interface LunarCalendar (Private)
NSArray *lunarInfo;
NSArray *solarMonth;
NSArray *Gan;
NSArray *Zhi;
NSArray *Animals;

NSNumber *week;
NSNumber *LunarYear;
NSNumber *LunarMonth;
NSNumber *LunarDay;
NSNumber *LunarHour;

NSString *ChineseYear;
NSString *ChineseMonth;
NSString *ChineseDay;
NSString *ChineseHour;
NSString *ChineseYearTitle;
NSString *ChineseYearAnimal;

int dayCyclical;
BOOL NightHour;
BOOL Leap;

-(int)lunarYearDays:(int)year;
-(int)leapDays:(int)year;
-(int)leapMonth:(int)year;
-(int)monthDays:(int)year: month:(int)month;
-(int)solarDays:(int)year: month:(int)month;

-(NSString *)cyclical:(int)number;
-(NSDate *)lunarCalc;

@end

@implementation LunarCalendar
-(id)init
{
lunarInfo=[NSArray arrayWithObjects:
[NSNumber numberWithInt:0x4bd8],[NSNumber numberWithInt:0x4ae0],[NSNumber numberWithInt:0xa570],
[NSNumber numberWithInt:0x54d5],[NSNumber numberWithInt:0xd260],[NSNumber numberWithInt:0xd950],
[NSNumber numberWithInt:0x5554],[NSNumber numberWithInt:0x56af],[NSNumber numberWithInt:0x9ad0],
[NSNumber numberWithInt:0x55d2],[NSNumber numberWithInt:0x4ae0],[NSNumber numberWithInt:0xa5b6],
[NSNumber numberWithInt:0xa4d0],[NSNumber numberWithInt:0xd250],[NSNumber numberWithInt:0xd295],
[NSNumber numberWithInt:0xb54f],[NSNumber numberWithInt:0xd6a0],[NSNumber numberWithInt:0xada2],
[NSNumber numberWithInt:0x95b0],[NSNumber numberWithInt:0x4977],[NSNumber numberWithInt:0x497f],
[NSNumber numberWithInt:0xa4b0],[NSNumber numberWithInt:0xb4b5],[NSNumber numberWithInt:0x6a50],
[NSNumber numberWithInt:0x6d40],[NSNumber numberWithInt:0xab54],[NSNumber numberWithInt:0x2b6f],
[NSNumber numberWithInt:0x9570],[NSNumber numberWithInt:0x52f2],[NSNumber numberWithInt:0x4970],
[NSNumber numberWithInt:0x6566],[NSNumber numberWithInt:0xd4a0],[NSNumber numberWithInt:0xea50],
[NSNumber numberWithInt:0x6a95],[NSNumber numberWithInt:0x5adf],[NSNumber numberWithInt:0x2b60],
[NSNumber numberWithInt:0x86e3],[NSNumber numberWithInt:0x92ef],[NSNumber numberWithInt:0xc8d7],
[NSNumber numberWithInt:0xc95f],[NSNumber numberWithInt:0xd4a0],[NSNumber numberWithInt:0xd8a6],
[NSNumber numberWithInt:0xb55f],[NSNumber numberWithInt:0x56a0],[NSNumber numberWithInt:0xa5b4],
[NSNumber numberWithInt:0x25df],[NSNumber numberWithInt:0x92d0],[NSNumber numberWithInt:0xd2b2],
[NSNumber numberWithInt:0xa950],[NSNumber numberWithInt:0xb557],[NSNumber numberWithInt:0x6ca0],
[NSNumber numberWithInt:0xb550],[NSNumber numberWithInt:0x5355],[NSNumber numberWithInt:0x4daf],
[NSNumber numberWithInt:0xa5b0],[NSNumber numberWithInt:0x4573],[NSNumber numberWithInt:0x52bf],
[NSNumber numberWithInt:0xa9a8],[NSNumber numberWithInt:0xe950],[NSNumber numberWithInt:0x6aa0],
[NSNumber numberWithInt:0xaea6],[NSNumber numberWithInt:0xab50],[NSNumber numberWithInt:0x4b60],
[NSNumber numberWithInt:0xaae4],[NSNumber numberWithInt:0xa570],[NSNumber numberWithInt:0x5260],
[NSNumber numberWithInt:0xf263],[NSNumber numberWithInt:0xd950],[NSNumber numberWithInt:0x5b57],
[NSNumber numberWithInt:0x56a0],[NSNumber numberWithInt:0x96d0],[NSNumber numberWithInt:0x4dd5],
[NSNumber numberWithInt:0x4ad0],[NSNumber numberWithInt:0xa4d0],[NSNumber numberWithInt:0xd4d4],
[NSNumber numberWithInt:0xd250],[NSNumber numberWithInt:0xd558],[NSNumber numberWithInt:0xb540],
[NSNumber numberWithInt:0xb6a0],[NSNumber numberWithInt:0x95a6],[NSNumber numberWithInt:0x95bf],
[NSNumber numberWithInt:0x49b0],[NSNumber numberWithInt:0xa974],[NSNumber numberWithInt:0xa4b0],
[NSNumber numberWithInt:0xb27a],[NSNumber numberWithInt:0x6a50],[NSNumber numberWithInt:0x6d40],
[NSNumber numberWithInt:0xaf46],[NSNumber numberWithInt:0xab60],[NSNumber numberWithInt:0x9570],
[NSNumber numberWithInt:0x4af5],[NSNumber numberWithInt:0x4970],[NSNumber numberWithInt:0x64b0],
[NSNumber numberWithInt:0x74a3],[NSNumber numberWithInt:0xea50],[NSNumber numberWithInt:0x6b58],
[NSNumber numberWithInt:0x5ac0],[NSNumber numberWithInt:0xab60],[NSNumber numberWithInt:0x96d5],
[NSNumber numberWithInt:0x92e0],[NSNumber numberWithInt:0xc960],[NSNumber numberWithInt:0xd954],
[NSNumber numberWithInt:0xd4a0],[NSNumber numberWithInt:0xda50],[NSNumber numberWithInt:0x7552],
[NSNumber numberWithInt:0x56a0],[NSNumber numberWithInt:0xabb7],[NSNumber numberWithInt:0x25d0],
[NSNumber numberWithInt:0x92d0],[NSNumber numberWithInt:0xcab5],[NSNumber numberWithInt:0xa950],
[NSNumber numberWithInt:0xb4a0],[NSNumber numberWithInt:0xbaa4],[NSNumber numberWithInt:0xad50],
[NSNumber numberWithInt:0x55d9],[NSNumber numberWithInt:0x4ba0],[NSNumber numberWithInt:0xa5b0],
[NSNumber numberWithInt:0x5176],[NSNumber numberWithInt:0x52bf],[NSNumber numberWithInt:0xa930],
[NSNumber numberWithInt:0x7954],[NSNumber numberWithInt:0x6aa0],[NSNumber numberWithInt:0xad50],
[NSNumber numberWithInt:0x5b52],[NSNumber numberWithInt:0x4b60],[NSNumber numberWithInt:0xa6e6],
[NSNumber numberWithInt:0xa4e0],[NSNumber numberWithInt:0xd260],[NSNumber numberWithInt:0xea65],
[NSNumber numberWithInt:0xd530],[NSNumber numberWithInt:0x5aa0],[NSNumber numberWithInt:0x76a3],
[NSNumber numberWithInt:0x96d0],[NSNumber numberWithInt:0x4afb],[NSNumber numberWithInt:0x4ad0],
[NSNumber numberWithInt:0xa4d0],[NSNumber numberWithInt:0xd0b6],[NSNumber numberWithInt:0xd25f],
[NSNumber numberWithInt:0xd520],[NSNumber numberWithInt:0xdd45],[NSNumber numberWithInt:0xb5a0],
[NSNumber numberWithInt:0x56d0],[NSNumber numberWithInt:0x55b2],[NSNumber numberWithInt:0x49b0],
[NSNumber numberWithInt:0xa577],[NSNumber numberWithInt:0xa4b0],[NSNumber numberWithInt:0xaa50],
[NSNumber numberWithInt:0xb255],[NSNumber numberWithInt:0x6d2f],[NSNumber numberWithInt:0xada0],
[NSNumber numberWithInt:0x4b63],[NSNumber numberWithInt:0x937f],[NSNumber numberWithInt:0x49f8],
[NSNumber numberWithInt:0x4970],[NSNumber numberWithInt:0x64b0],[NSNumber numberWithInt:0x68a6],
[NSNumber numberWithInt:0xea5f],[NSNumber numberWithInt:0x6b20],[NSNumber numberWithInt:0xa6c4],
[NSNumber numberWithInt:0xaaef],[NSNumber numberWithInt:0x92e0],[NSNumber numberWithInt:0xd2e3],
[NSNumber numberWithInt:0xc960],[NSNumber numberWithInt:0xd557],[NSNumber numberWithInt:0xd4a0],
[NSNumber numberWithInt:0xda50],[NSNumber numberWithInt:0x5d55],[NSNumber numberWithInt:0x56a0],
[NSNumber numberWithInt:0xa6d0],[NSNumber numberWithInt:0x55d4],[NSNumber numberWithInt:0x52d0],
[NSNumber numberWithInt:0xa9b8],[NSNumber numberWithInt:0xa950],[NSNumber numberWithInt:0xb4a0],
[NSNumber numberWithInt:0xb6a6],[NSNumber numberWithInt:0xad50],[NSNumber numberWithInt:0x55a0],
[NSNumber numberWithInt:0xaba4],[NSNumber numberWithInt:0xa5b0],[NSNumber numberWithInt:0x52b0],
[NSNumber numberWithInt:0xb273],[NSNumber numberWithInt:0x6930],[NSNumber numberWithInt:0x7337],
[NSNumber numberWithInt:0x6aa0],[NSNumber numberWithInt:0xad50],[NSNumber numberWithInt:0x4b55],
[NSNumber numberWithInt:0x4b6f],[NSNumber numberWithInt:0xa570],[NSNumber numberWithInt:0x54e4],
[NSNumber numberWithInt:0xd260],[NSNumber numberWithInt:0xe968],[NSNumber numberWithInt:0xd520],
[NSNumber numberWithInt:0xdaa0],[NSNumber numberWithInt:0x6aa6],[NSNumber numberWithInt:0x56df],
[NSNumber numberWithInt:0x4ae0],[NSNumber numberWithInt:0xa9d4],[NSNumber numberWithInt:0xa4d0],
[NSNumber numberWithInt:0xd150],[NSNumber numberWithInt:0xf252],[NSNumber numberWithInt:0xd520],nil];

solarMonth=[NSArray arrayWithObjects:[NSNumber numberWithInt:31],[NSNumber numberWithInt:28],[NSNumber numberWithInt:31],
[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],
[NSNumber numberWithInt:31],[NSNumber numberWithInt:30],[NSNumber numberWithInt:31],[NSNumber numberWithInt:30],
[NSNumber numberWithInt:31],nil];

Gan=[NSArray arrayWithObjects:@"甲",@"乙",@"丙",@"丁",@"戊",@"己",@"庚",@"辛",@"壬",@"癸",nil];
Zhi=[NSArray arrayWithObjects:@"子",@"丑",@"寅",@"卯",@"辰",@"巳",@"午",@"未",@"申",@"酉",@"戌",@"亥",nil];
Animals=[NSArray arrayWithObjects:@"鼠",@"牛",@"虎",@"兔",@"龍",@"蛇",@"馬",@"羊",@"猴",@"雞",@"狗",@"豬",nil];

return self;
}

-(void)dealloc
{
[super dealloc];
}

-(int)lunarYearDays:(int)year
{
int i,sum=348;

for(i=0x8000;i>0x8;i>>=1){
sum+=(([[lunarInfo objectAtIndex:(year-1900)] intValue]&i)?1:0);
}

return (sum+[self leapDays:year]);
}

-(int)leapDays:(int)year
{
if([self leapMonth:year]){
return ((([[lunarInfo objectAtIndex:(year-1899)] intValue]&0xf)==0xf)?30:29);
}else{
return 0;
}
}
-(int)leapMonth:(int)year
{
int lm=([[lunarInfo objectAtIndex:(year-1900)] intValue]&0xf);
return ((lm==0xf)?0:lm);
}

-(int)monthDays:(int)year month:(int)month
{
return ([[lunarInfo objectAtIndex:(year-1900)] intValue]&(0x10000>>month)?30:29);
}

-(int)solarDays:(int)year month:(int)month
{
NSNumber *j;

if(month==1){
return ((((year%4)==0)&&((year%100)!=0)||((year%400)==0))?29:28);
}else{
return [[solarMonth objectAtIndex:month] intValue];
}
}

-(NSString *)cyclical:(int)number
{
return [[Gan objectAtIndex:(number%10)] stringByAppendingString:[Zhi objectAtIndex:(number%12)]];
}

-(NSDate *)spring:(int)year
{
if(year>2001&&year<2099){
int y=floor((year-2000)*0.2422+20.646)-((year-2000)/4);

if(year==2084){
y++;
}

NSDateComponents *dayTerm2Comps=[[NSDateComponents alloc] init];

[dayTerm2Comps setDay:y];
[dayTerm2Comps setMonth:3];
[dayTerm2Comps setYear:year];

NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *dayTerm2=[gregorian dateFromComponents:dayTerm2Comps];

[dayTerm2Comps release];
[gregorian release];

return dayTerm2;
}
return nil;
}

-(NSDate *)lunarCalc
{
int i,leap,temp=0,year=0;
long offset=0;

NSDateComponents *baseDayComps=[[NSDateComponents alloc] init];
NSDateComponents *solarDayComps=[[NSDateComponents alloc] init];
NSDateComponents *chineseDayBaseComps=[[NSDateComponents alloc] init];

[baseDayComps setDay:31];
[baseDayComps setMonth:1];
[baseDayComps setYear:1900];

[chineseDayBaseComps setDay:1];
[chineseDayBaseComps setMonth:1];
[chineseDayBaseComps setYear:1900];

if(SolarYear!=nil && SolarMonth!=nil && SolarDay!=nil){
[solarDayComps setYear:[SolarYear intValue]];
[solarDayComps setMonth:[SolarMonth intValue]];
[solarDayComps setDay:[SolarDay intValue]];
}

NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDate *baseDay=[gregorian dateFromComponents:baseDayComps];
NSDate *solarDayTemp=[gregorian dateFromComponents:solarDayComps];
NSDate *chineseDayTemp=[gregorian dateFromComponents:chineseDayBaseComps];

week=[NSNumber numberWithInt:[[solarDayTemp descriptionWithCalendarFormat:@"%w" timeZone:nil locale:nil] intValue]];

NSTimeInterval timeDifference=[solarDayTemp timeIntervalSinceDate:baseDay];
offset=timeDifference/86400;

NSTimeInterval chineseTimeDifference=[solarDayTemp timeIntervalSinceDate:chineseDayTemp];
dayCyclical=(chineseTimeDifference/86400)+10;

for(i=1900;(i<2100 && offset>0);i++){
temp=[self lunarYearDays:i];
offset-=temp;
}
if(offset<0){
offset+=temp;
i--;
}
year=i;

leap=[self leapMonth:i];
Leap=NO;

for(i=0;(i<12 && offset>0);i++){
if(leap>0 && i==(leap+1) && [self isLeap]==NO){
i--;
Leap=YES;
temp=[self leapDays:year];
}else{
temp=[self monthDays:year month:i];
}
if([self isLeap]==YES && i==(leap+1)){
[self isLeap]==NO;
}
offset-=temp;
}

if(offset==0 && leap>0 && i==leap+1){
if([self isLeap]==YES){
Leap=NO;
}else{
Leap=YES;
i--;
}
}
if(offset<0){
offset+=temp;
i--;
}

NSDateComponents *lunarComps=[[NSDateComponents alloc] init];
[lunarComps setYear:year];
[lunarComps setMonth:i+1];
[lunarComps setDay:offset+1];
[lunarComps setHour:[SolarHour intValue]];

NSDate *lunarDate=[gregorian dateFromComponents:lunarComps];

[chineseDayBaseComps release];
[baseDayComps release];
[solarDayComps release];
[gregorian release];
[lunarComps release];

return lunarDate;
}

-(void)lunarCalendar
{
int year,month,hour;

if(SolarYear==nil || SolarMonth==nil || SolarDay==nil){
NSDate *nowDate=[NSDate date];
[self setSolarYear:[[nowDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil] intValue]];
[self setSolarMonth:[[nowDate descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil] intValue]];
[self setSolarDay:[[nowDate descriptionWithCalendarFormat:@"%d" timeZone:nil locale:nil] intValue]];
[self setSolarHour:[[nowDate descriptionWithCalendarFormat:@"%H" timeZone:nil locale:nil] intValue]];
}

NSDate *lunarDate=[self lunarCalc];

LunarYear=[NSNumber numberWithInt:[[lunarDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil] intValue]];
LunarMonth=[NSNumber numberWithInt:[[lunarDate descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil] intValue]];
LunarDay=[NSNumber numberWithInt:[[lunarDate descriptionWithCalendarFormat:@"%d" timeZone:nil locale:nil] intValue]];

hour=([[lunarDate descriptionWithCalendarFormat:@"%H" timeZone:nil locale:nil] intValue]+1);

NightHour=(hour>23)?YES:NO;
hour=(hour>23)?0:hour/2;
LunarHour=[NSNumber numberWithInt:hour];

year=[self lunarYear];
month=[self lunarMonth];

if(year>1911){
ChineseYearTitle=[NSString stringWithString:@"民國"];
if(year-1911==1){
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:@"元年"];
}else{
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:[NSString stringWithFormat:@"%d年",year-1911]];
}
}else{
if(year>1874 && year<1909){
ChineseYearTitle=[NSString stringWithString:@"光緒"];
if(year-1874==1){
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:@"元年"];
}else{
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:[NSString stringWithFormat:@"%d年",year-1874]];
}
}else{
if(year>1908 && year<1912){
ChineseYearTitle=[NSString stringWithString:@"宣統"];
if(year-1908==1){
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:@"元年"];
}else{
ChineseYearTitle=[ChineseYearTitle stringByAppendingString:[NSString stringWithFormat:@"%d年",year-1908]];
}
}
}
}

ChineseYear=[NSString stringWithString:[self cyclical:year-1900+36]];
ChineseMonth=[NSString stringWithString:[self cyclical:((year-1900)*12+month+13)]];
ChineseDay=[NSString stringWithString:[self cyclical:dayCyclical]];
ChineseYearAnimal=[NSString stringWithString:[Animals objectAtIndex:((year-4)%12)]];

hour=(((((dayCyclical%10)%5)*2)+[self lunarHour])%10);
hour=NightHour?((hour+12)%10):hour;
ChineseHour=[NSString stringWithString:[Gan objectAtIndex:hour]];

ChineseHour=[ChineseHour stringByAppendingString:[Zhi objectAtIndex:[self lunarHour]]];

if([self isLeap]){
ChineseMonth=[ChineseMonth stringByAppendingString:@"(閏)"];
}
}

-(void)setSolarYear:(int) year month:(int)month day:(int)day hour:(int)hour
{
[self setSolarYear:year];
[self setSolarMonth:month];
[self setSolarDay:day];
[self setSolarHour:hour];
}

-(void)setSolarYear:(int) year month:(int)month day:(int)day
{
[self setSolarYear:year];
[self setSolarMonth:month];
[self setSolarDay:day];
}

-(void)setSolarYear:(int) year
{
if(year>1873||year<2100){
SolarYear=[NSNumber numberWithInt:year];
}
}

-(void)setSolarMonth:(int) month
{
if(month>0||month<=12){
SolarMonth=[NSNumber numberWithInt:month];
}
}

-(void)setSolarDay:(int) day
{
if(day>0||day<=31){
SolarDay=[NSNumber numberWithInt:day];
}
}

-(void)setSolarHour:(int) hour
{
if(hour>=0||hour<=23){
SolarHour=[NSNumber numberWithInt:hour];
}
}

-(int)solarYear
{
return [SolarYear intValue];
}

-(int)solarMonth
{
return [SolarMonth intValue];
}

-(int)solarDay
{
return [SolarDay intValue];
}

-(int)solarHour
{
return [SolarHour intValue];
}

-(int)weekDay
{
return [week intValue];
}

-(int)lunarYear
{
return [LunarYear intValue];
}

-(int)lunarMonth
{
return [LunarMonth intValue];
}

-(int)lunarDay
{
return [LunarDay intValue];
}

-(int)lunarHour
{
return [LunarHour intValue];
}

-(NSString *)lunarHourChinese
{
return [NSString stringWithString:[Zhi objectAtIndex:[self lunarHour]]];
}

-(BOOL)isLeap
{
return(Leap?YES:NO);
}

-(NSString *)chineseYear
{
return ChineseYear;
}

-(NSString *)chineseMonth
{
return ChineseMonth;
}

-(NSString *)chineseDay
{
return ChineseDay;
}

-(NSString *)chineseHour
{
return ChineseHour;
}

-(NSString *)chineseYearTitle
{
return ChineseYearTitle;
}

-(NSString *)chineseYearAnimal
{
return ChineseYearAnimal;
}

@end


LunarCal.m


//#define TEST1
//#define TEST2

#import <Foundation/Foundation.h>
#import "LunarCalendar.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

LunarCalendar *lunarCal=[[LunarCalendar alloc] init];

#ifdef TEST1
[lunarCal setSolarYear:2010 month:9 day:10];
#endif

#ifdef TEST2
[lunarCal setSolarYear:2012 month:9 day:22 hour:23];
#endif

[lunarCal lunarCalendar];
NSLog(@"陽曆:%d-%d-%d 星期%d %d時",[lunarCal solarYear],[lunarCal solarMonth],[lunarCal solarDay], \
[lunarCal weekDay],[lunarCal solarHour]);
NSLog(@"農曆:%d-%d-%d",[lunarCal lunarYear],[lunarCal lunarMonth],[lunarCal lunarDay]);
NSLog(@"農曆:%@%d月%d日 %@時 %@年%@月%@日%@時",[lunarCal chineseYearTitle],[lunarCal lunarMonth], \
[lunarCal lunarDay],[lunarCal lunarHourChinese], \
[lunarCal chineseYear],[lunarCal chineseMonth],[lunarCal chineseDay],[lunarCal chineseHour]);
NSLog(@"生肖屬%@",[lunarCal chineseYearAnimal]);

[lunarCal release];
[pool drain];
return 0;
}


附註:因為在計算上的問題,沒有測試到,經 JamDing 提醒,發現在計算農曆二月的日數會有30天的問題。所以有改正,並請使用以下的 patch!

LunarCalendar.h.patch

--- old.h 2010-09-23 11:57:29.000000000 +0800
+++ new.h 2010-09-23 10:08:40.000000000 +0800
@@ -1,5 +1,3 @@
-unarCalendar.h
-
//
// LunarCalendar.h
// LunarCal
@@ -48,6 +46,10 @@ unarCalendar.h
-(NSString *)chineseYearTitle;
-(NSString *)chineseYearAnimal;

+-(NSArray *)Gan;
+-(NSArray *)Zhi;
+-(NSArray *)Animals;
+
-(void)lunarCalendar;

@end


LunarCalendar.m.patch


--- old.m 2010-09-23 11:15:15.000000000 +0800
+++ new.m 2010-09-23 11:22:12.000000000 +0800
@@ -164,12 +164,10 @@

-(int)solarDays:(int)year month:(int)month
{
- NSNumber *j;
-
- if(month==1){
+ if(month==2){
return ((((year%4)==0)&&((year%100)!=0)||((year%400)==0))?29:28);
}else{
- return [[solarMonth objectAtIndex:month] intValue];
+ return [[solarMonth objectAtIndex:month-1] intValue];
}
}

@@ -254,28 +252,29 @@
leap=[self leapMonth:i];
Leap=NO;

- for(i=0;(i<12 && offset>0);i++){
- if(leap>0 && i==(leap+1) && [self isLeap]==NO){
+ for(i=1;(i<13 && offset>0);i++){
+ if(leap>0 && i==(leap+1) && Leap==NO){
i--;
Leap=YES;
temp=[self leapDays:year];
}else{
temp=[self monthDays:year month:i];
}
- if([self isLeap]==YES && i==(leap+1)){
- [self isLeap]==NO;
+ if(Leap==YES && i==(leap+1)){
+ Leap=NO;
}
offset-=temp;
}

if(offset==0 && leap>0 && i==leap+1){
- if([self isLeap]==YES){
+ if(Leap){
Leap=NO;
}else{
Leap=YES;
i--;
}
}
+
if(offset<0){
offset+=temp;
i--;
@@ -283,7 +282,7 @@

NSDateComponents *lunarComps=[[NSDateComponents alloc] init];
[lunarComps setYear:year];
- [lunarComps setMonth:i+1];
+ [lunarComps setMonth:i];
[lunarComps setDay:offset+1];
[lunarComps setHour:[SolarHour intValue]];

@@ -496,4 +495,18 @@
return ChineseYearAnimal;
}

-@end
\ No newline at end of file
+-(NSArray *)Gan
+{
+ return Gan;
+}
+
+-(NSArray *)Zhi
+{
+ return Zhi;
+}
+
+-(NSArray *)Animals
+{
+ return Animals;
+}
+@end


原來之前的也不算是錯的,只是用到紫微斗數的四柱算法,直接用農曆年月日時換算四柱。而其實農曆是要配合十二個節氣來變動調整年柱和月柱的,而每個節氣不大都不是一個月的第一天,所以調整就比較多了。

11 回應 11 回應 ( 6125預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3.1 / 573 )

我很懶,我是懶惰的攻城屍
09/03/2010, 16:11 - 生活
很多人,尤其是當主管的,一聽到有人自我介紹說"我很懶",馬上想到這個人是笨蛋嗎?事實上會這樣說的人,第一他真的是個笨蛋,二他真的懶,三他是個懶惰的笨蛋。不過會敢這樣說的人,真的是笨蛋嗎?也許吧。但是不是說"科技始終來自於人性"嗎?人懶是天生的,懶的人,能睡,絕不會醒著,能躺,絕不會坐著,能坐,絕不會站著,才會有需求,渴望如何可以讓生活、工作用更懶的方式達成。不然就不會有遙控器讓人不用從沙發站起來去切換電視頻道;不會有電腦可以用試算表去自動算出報表,而不是每個月底自已用心算、算盤、計算機計算出所要的報表。人懶不一定代表不動、不工作。反而是為了要達到"懶"這個需求,所以要開始研究,如何滿足想要懶的需求,如何達成。等到達成了目標,這時一定會有新的需求,以讓自已可以再更懶一點,而一再重覆研究。所以懶的人,才會有動力去研究做事的方法,讓自已可以更懶。

工作有很多方式,像在工廠生產線工作的人,只需要照標準工作流程的方式,照順序一步一步的作,從來沒有想過工作流程是否可以改善,因為他很勤勞,將工作方式照步驟完全清楚的實現,讓工作很順利的進行,沒有發生問題。可是他也可能因為很懶,拼命研究步驟和工作流程,所以發現工作的流程有問題,進一步研究是不是有還可以改善空間,將工作步驟、流程改進。這不是將工作的流程偷工減料的那種懶,偷工減料,自行減少步驟那種懶,是懶得研究、懶得動腦、懶得觀察,只是想偷懶而已。而是真的去觀察、研究,發現問題,並加以改進。

在研發單位工作的人,經常要作很多測試,不管是軟體或硬體,在開發過程中,會經由很多的測試,來發現及證明設計是否正確,是否工作正常。所以很多的研發"攻城屍"們,除了開發新產品功能之外,每天要花很多時間在作功能測試。這些測試,是經由人工測試,每個測試都是很簡單的接接線,設定測試儀器,按按鍵之類就可以完成。但是因經常需要換測試設定,結果最花的時間,竟然是在換測試方式及設定。不過,沒有辦法,全世界都這樣,因為每個測試都很簡單,很快可以試完,所以"攻城屍"們沒有想過要如何改善,就這樣日復一日的人工測試下去,可是因為工作排定Schedule有需要完成的時間,但是功能測試項目也很多、也一定要做。"攻城屍"們為了要如期完成工作,所以就因為時間壓力,而勤勞的加班,以便完成工作。

如果其中有一位"攻城屍"很懶,不甘天天加班,加到地老天荒,但公司說因為是責任制,自已工作沒做完,是自已自願留下來加班的,常然沒有加班費。這位"攻城屍"就只能暗幹在心的,"自動自發"的加班,讓自已的時薪被稀釋、降低。結果在加班測試的空檔,研究了一下功能測試項目,以及測試步驟,和測試時會用到的儀器設備,發現了有些測試儀器可以將上次的測試設定存檔,下次只要直接叫用就可以了,而且可以經由某些介面下指令控制,不一定要用GUI加Mouse。所以,他就再利用"空檔的空檔時間"(這是什麼時間,請自行想像。),總之,他自已作了一個可以將一些常用的測試步驟、測試儀器的設定自動執行的程式,所以他將一部份的測試工作交給測試程式執行,等執行完之後再看結果,自已再去忙著改硬體線路、寫程式等等,原來每天要做的正常工作。然後,他可以每天減少加班的時間,“提早"下班,維持好自已的時薪在一定水準之上。而這位懶惰的"攻城屍"發現自動測試程式很好用,可是遇到需要改硬體接線時,還是要人來照顧,沒辦法自動。這樣每天還是要加班,雖然加班時數減少了,但加班就是加班啊。為了加班,有男女朋友、老婆、小孩的,沒有辦法善盡為人男女朋友、先生、父母的責任。沒有男女朋友的,當然就更慘了,因為根本沒時間去認識異性朋友啊,可是人生苦短啊,一生就要在沒有異性,或只能在辦公室的兩朵目前沒人採的花之中,擇一而採,來盡人生應盡的責任。

這位懶惰的"攻城屍",為了向自已人生應盡的責任負責,就研究想利用一些額外的硬體設備,可以經由程式控制,取代人工接線,在需要更改接線方式時,自動配合測試項目。當然在做這個研究時,不免又要動用"空檔"時間啦。經過一段時間之後,他已經可以經由電腦自動控制,測試軟體和硬體以及測試儀器的組合,他每天可以在正常的下班時間離開公司,因為他現在將重覆測試的工作交給機器代勞了,所以他就可以再更懶一點,回家善盡男女朋友、先生、父母的人生應盡的責任,順便若還有空閒時間,再研究一下有沒有別的技術,可以再讓自已再更加懶惰,而這位"攻城屍"也經由這樣子的研究進步,而變成了每天可以準時上下班的"工程師"了。其他的"攻城屍"們,自然很羨慕,起而效尤,想辦法將"懶惰的工程師"的懶惰方法抄過來,也讓自已懶一點,不用再勤勞的加班,順便保護好自已的時薪在水準之上。

當然這時他"勤勞的主管",也可能因為看到這位"懶惰的工程師",每天準時走人,工作還是做得好好的,所以就又再交辦新的工作進來。這時怎辦呢?這位工程師又要變回勤勞的"攻城屍"了嗎?還是再發揮為人一定要懶的精神,再開始研究,並利用上次的研究成果,修改之後,再讓自已回到"懶惰工程師"的行列。所以說懶惰不一定是壞事,而是進步的動力。下次聽到有人說"我很懶"時,要問清楚,他是個當"勤勞攻城屍"還是"懶惰工程師"的料。
2 回應 2 回應 ( 1805預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3.1 / 377 )

火星像是滿月那般大
08/25/2010, 11:30 - 想到的
最近從網路收到以下訊息:

在八月份開始的夜空裡,火星將是星空中最亮的行星。
用肉眼觀看,火星的體積像是滿月那般大。
當火星來到距離地球不到 34.65M英里[三千四百六十五萬英里]的時後,
上述現象將在今年的八月27日出現。
在今年2010的八月27日凌晨12:30時後,千萬別忘了觀察這[兩個滿月]的景象。
下一次火星再次來到拒離地球這麼近的時刻,將是西元2287年了。

火星"體積"像月亮一樣大,用"體積"就己經怪怪的了。
例如太陽比月球大395倍,而和地球的距離也正好是395倍,
所以在地球看來太陽和月亮是一樣大。

查資料計算了一下:
月球平均半徑1737KM
火星平均半徑3389KM
己知月球距離地球384000KM
若火星要和月球一樣大的話,距離比應會和半徑比值相同。

3389/1737=X/384000
X=1.95106505469199769718*384000=748545.76856649395509499136KM
也就是說火星要在地球看來和月球一樣大,距地球的距離要在 748575KM。這距離也太近了吧!



火星的近日點是 206620000 KM
地球的遠日點是 152097701 KM
最可能的兩星接近距離是 54522299KM 遠大於 X 5.73971138006656605401 倍。
而34650000Mile大約是5544000KM。就是稱為"火星大衝",也就是地球的遠日點和火星的近點同時出現。

所以就算是假設地球的遠日點和火星的近日點在同一時間出現,
最接近點看到的火星也只可能會是
54522299/384000=141.98567447916666666667
和月球半徑比值是1.95,大約是2:1,所以可能是70分之一月球大小吧!
若看到月球有70公分直徑,那火星就只有一公分直徑。
不過一公分的火星己經非常大就是了,不過前提是月亮要有70公分大耶!
若看到的月亮只有35公分大,那火星就變成0.5公分了,和別的星星比起來,還是很大就是了。

火星大衝的日期,上次是2003年8月27日,是六千年來最近點。這個日期一樣是8月27日,應該是這個傳言的由來吧!

PS: 還有村上春樹的1Q84有提到兩個月亮! 但是真實和幻境的兩個境界重疊!

請參考


火星
月球
地球
火星衝
發表回應 發表回應 ( 610預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3 / 262 )

下班後留的時間長,代表工作沒有效率
08/06/2010, 10:54 - 生活
下班後留在辦公室的時間長,代表工作沒有效率.可以在工作時間內作完的,就不要留下來作. 工作時間長,代表你的"時薪"在下降中.

假設每個人每天工作八小時,一個月工作二十二天,月薪三萬.時薪是 30000/(8x22)= 170.46元/小時
若每天工作十二小時,時薪變成 113.64元/小時
若每天用十四小時工作的話,時薪變成 97.4元/小時

和7-11打工,每小時100-110沒差太多,不過7-11打工壓力小多了,而且不會開太多會議!

同樣可以算開會兩小時的成本,若十個人一起共渡一段會議時光
總工時是20小時,170.46*20=3409.2元.
若每週開會2次就變成3409.2*2=6818.4元.
而且開會時工作暫停,所以要乘2變成13636元了
開會成本很貴的!

因為開會時大都只有一個人在講,其他人聽,若每個人都使用十分鐘,再加上各十分鐘在開場及總結,就需要(10+2)*10=120分鐘=2小時,總工時變成10*2=20小時.長時間開會,再加上注意力的問題,及內容有時會飄移變成三姑六婆閒話家常,會議會變得更長.

但若是換成先由主持人和每個人單獨談十分鐘,總工時就變成(10*9)*2=180分鐘=1.5小時,1.5小時花費大約是255.69元.又因為是一對一,三姑六婆閒話家常就變少了,所以會議時間應會更短.

20小時對上1.5小時,加上因會議延後的工作量,一來一回至少要算兩倍.變成相差40:3=13.3倍耶!我想讀過小學,數學的基本四則運算還記得的人,應該都看得出來如何作比較好吧!

重點是減少了開會的時間,就有更多的時間可以工作,輸出變高,就不用在下班後還留下來了,除非是要作樣子,假裝很認真,事實上不是沒東西好作,就是在浪費生命的話,就是例外,不在此限了.

下班就回家,上班準時,努力工作,努力玩樂,少開會就好了!
詳細的可以看別開會了,趕快做!:一對一的無會議高效率管理這本書.
發表回應 發表回應 ( 766預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3 / 406 )

Arduino簡介
07/21/2010, 12:48 - Open Source
Arduino簡介 PDF 檔案
發表回應 發表回應 ( 796預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 2.9 / 397 )

OS X 10.6.4 內的程式還有PowerPC的...
07/02/2010, 09:15 - Apple
MacBook Pro跑的程式,現在大都是Intel 64bit的格式了,不過還是有些是32bit的,更好玩的是還有PowerPC的。Apple在對舊程式的相容能力,在這部份就沒有話說的了。

發表回應 發表回應 ( 1907預覽 )   |  [ 0 引用 ]   |  permalink   |   ( 3 / 386 )


<<開始 <前一頁 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 下一頁> 最後>>