VC阴阳历转化与二十四节气

地公转轨道是一个以太阳为一个中心点的椭圆。
以近日点为0度,将地球公转所扫过的角度每十五度的界点所在日期便是一个节气。


要计算某年某个节气在这一年的日期。需要获得这个节气在这一年经历的时间。于是就得下面这个数组:

static const int termInfo[] = {
0 ,21208 ,42467 ,63836 ,85337 ,107014,
128867,150921,173149,195551,218072,240693,
263343,285989,308563,331033,353350,375494,
397447,419210,440795,462224,483532,504758
};
这个数组每一个元素是各个节气距离一年起点的分钟数。由于是椭圆,所以数据不太规则,不是等差数列。

另一个重要的数据是1900年一月六日两点五分的正小寒点的天数。

static const double x_1900_1_6_2_5 = 693966.08680556;

再有一个常量是地球公转周期毫秒:

static const doubleONE_YEAR_LOOP = 365.2422;

函数如下:

UINT calendar::sTerm(UINT y,UINT n)
{

double a;

a = ( ONE_YEAR_LOOP *(y-1900) + sTermInfo[n]*60000 ) + x_1900_1_6_2_5*60000;

return a/(1000*60*60*24);

}

UINT calendar::sTerm(UINT ye,UINT n)
{
//节气校准数据,精确到分
static const int termInfo[] = {
0 ,21208 ,42467 ,63836 ,85337 ,107014,
128867,150921,173149,195551,218072,240693,
263343,285989,308563,331033,353350,375494,
397447,419210,440795,462224,483532,504758
};
//每个月底一年过去的天数
static const UINT mdays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}; //
long temp = long(x_1900_1_6_2_5+365.2422*(ye-1900)+termInfo[n]/(60.0*24)); //过去的总天数

INT days;
UINT diff, y ,m, d;

days = 100 * (temp - temp/(3652425L/(3652425L-3652400L)) );
y = days / 36524;
days %= 36524;
m = 1 + days/3044; /* 月:[1..12] */
d = 1 + (days%3044)/100; /*日: [1..31] */

diff =y*365+y/4-y/100+y/400+mdays[m-1]+d-((m<=2&&((y&3)==0)&&((y%100)!=0||y%400==0))) - temp;

if( diff > 0 && diff >= d ) /* ~0.5% */
{
if( m == 1 )
{
--y; m = 12;
d = 31 - ( diff - d );
}
else
{
d = mdays[m-1] - ( diff - d );
if( --m == 2 )
d += ((y&3)==0) && ((y%100)!=0||y%400==0);
}
}
else
{
if( (d -= diff) > mdays[m] ) /* ~1.6% */
{
if( m == 2 )
{
if(((y&3)==0) && ((y%100)!=0||y%400==0))
{
if( d != 29 )
m = 3 , d -= 29;
}
else
{
m = 3 , d -= 28;
}
}
else
{
d -= mdays[m];
if( m++ == 12 )
++y , m = 1;
}
}
}

return d; //日期,由于一些特殊的需要,我只返回的D,M是月

你可能感兴趣的:(vc++)