阳历阴历转换

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阳历阴历转换相关的知识,希望对你有一定的参考价值。

获取阳历对应的阴历日期:

/////////////////////////////////////////////////////////////////////////////////////////////////////
stdcal.h:

#ifndef __STDCAL_H
#define __STDCAL_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define SPLIT "/"
#define LEN_DATE 11

#define START_YEAR 2017
#define END_YEAR 2020

void intostr(char *,int,int);
int intlen(int);

extern unsigned char const cMonInfo[12];

int sumDay(char *date);
void chinaCal(char *year,int sumday,int *);
void getChinaCal(char *,int);

#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////
mystd.c:
#include "stdcal.h"


void intobit(int num)
{
int tnum=num;
int i;
for(i=31;i>=0;i--)
{
int tmpa=tnum;
tmpa=tnum&(1<<i);
((i+1)%8==0&&i!=31)?printf(" %d",tmpa>>i):printf("%d",tmpa>>i);
}
printf("\n");
}

void chrtobit(char chr)
{
char tchr=chr;
int i;
for(i=7;i>=0;i--)
{
char tmpc=tchr;
tmpc=tchr&(1<<i);
printf("%d",tmpc>>i);
}
printf("\n");
}

void strtobit(char *chr)
{
char *pchr=chr;
int i,j;
for(i=0;i<strlen(pchr);i++)
{
for(j=7;j>=0;j--)
{
char tmpp=pchr[i];
tmpp=pchr[i]&(1<<j);
printf("%d",tmpp>>j);
}
printf(" ");
}
printf("\n");
}

int intlen(int num)
{
int tmpn=num;
int len=1;
while(tmpn/=10)
len++;
return len;
}

void intostr(char *dest,int num,int intlen)
{
int tmpn2=num;
char tmpc[intlen+1];
int i=intlen-1;
tmpc[intlen]=‘\0‘;
while(tmpn2)
{
tmpc[i]=48+(tmpn2%10);
if(!(tmpn2/=10))
tmpc[i-1]=40+tmpn2;
i--;
}
memcpy(dest,tmpc,sizeof(tmpc));
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
stdcal.c:

#include "stdcal.h"

unsigned char const cMonInfo[12]={
0x38,0xca,0x2f,
0x5e,0x0a,0x56,
0x48,0x15,0x26,
0x32,0x8F,0x2A
};

int sumDay(char *date)
{
char *indate[3];
struct tm tmptm;
char tmpdate[LEN_DATE];
memcpy(tmpdate,date,strlen(date)+1);
//printf("%s\n",date);

int i=0;
indate[i]=strtok(tmpdate,SPLIT);
while(++i<3)
indate[i]=strtok(NULL,SPLIT);

//printf("%s\n",indate[2]);
tmptm.tm_year=atoi(indate[0])-1900;
tmptm.tm_mon=atoi(indate[1])-1;
tmptm.tm_mday=atoi(indate[2]);
tmptm.tm_hour=0;
tmptm.tm_min=0;
tmptm.tm_sec=0;
tmptm.tm_isdst=-1;
//printf("%s\n",indate[2]);

if(mktime(&tmptm)==-1)
perror("mktime error\n");

//printf("%d\n",tmptm.tm_yday);

return (tmptm.tm_yday+1);
}

void chinaCal(char *year,int sumday,int *cal)
{
int china[2];
china[0]=1;china[1]=1;
char lastyear[11];
int startnum_Mon=(atoi(year)-START_YEAR)*3;
//printf("%d\n",startnum_Mon);
int diffday=(int)(sumday-(cMonInfo[startnum_Mon]>>1));
//printf("sumday:%d\n",sumday);
//printf("(cMonInfo[startnum_Mon]>>1):%d\n",(cMonInfo[startnum_Mon]>>1));
//printf("%d\n",diffday);

if(diffday<0)
{
intostr(lastyear,atoi(year)-1,intlen(atoi(year)-1));
int laststartnum_Mon=(atoi(lastyear)-START_YEAR)*3;
int newdiffday=sumday;

int lastmonDay[13];
lastmonDay[0]=(((cMonInfo[laststartnum_Mon+1]>>4)&0001)==0)?29:30;
lastmonDay[1]=(((cMonInfo[laststartnum_Mon+1]>>3)&00001)==0)?29:30;
lastmonDay[2]=(((cMonInfo[laststartnum_Mon+1]>>2)&000001)==0)?29:30;
lastmonDay[3]=(((cMonInfo[laststartnum_Mon+1]>>1)&0000001)==0)?29:30;
lastmonDay[4]=(((cMonInfo[laststartnum_Mon+1])&00000001)==0)?29:30;
lastmonDay[5]=(((cMonInfo[laststartnum_Mon+2]>>7))==0)?29:30;
lastmonDay[6]=(((cMonInfo[laststartnum_Mon+2]>>6)&01)==0)?29:30;
lastmonDay[7]=(((cMonInfo[laststartnum_Mon+2]>>5)&001)==0)?29:30;
lastmonDay[8]=(((cMonInfo[laststartnum_Mon+2]>>4)&0001)==0)?29:30;
lastmonDay[9]=(((cMonInfo[laststartnum_Mon+2]>>3)&00001)==0)?29:30;
lastmonDay[10]=(((cMonInfo[laststartnum_Mon+2]>>2)&000001)==0)?29:30;
lastmonDay[11]=(((cMonInfo[laststartnum_Mon+2]>>1)&0000001)==0)?29:30;
lastmonDay[12]=(((cMonInfo[laststartnum_Mon+2])&00000001)==0)?29:30;

lastyear[4]=‘/‘;lastyear[5]=‘1‘;lastyear[6]=‘2‘;lastyear[7]=‘/‘;lastyear
[8]=‘3‘;lastyear[9]=‘1‘;lastyear[10]=‘\0‘;
chinaCal(lastyear,sumDay(&lastyear[0]),china);

if(newdiffday>(lastmonDay[china[0]-1]-china[1]))
{
newdiffday-=lastmonDay[china[0]-1]-china[1];
++china[0];china[1]=0;
}
//printf("%d/%d\n",china[0],china[1]);
//printf("%d\n",newdiffday);
while(newdiffday>0)
{
if(newdiffday>=lastmonDay[china[0]])
{
newdiffday-=lastmonDay[china[0]];
china[0]++;
}else{
china[1]+=newdiffday;
newdiffday=0;
}
}

//memcpy(cal,china,sizeof(china));
printf("%d/%d\n",china[0],china[1]);

exit(0);

}

int isleap=(int)((cMonInfo[startnum_Mon+1])>>5);
//printf("%d\n",diffday);
//printf("isleap:%d\n",isleap);
int monDay[13];
monDay[0]=(((cMonInfo[startnum_Mon+1]>>4)&0001)==0)?29:30;
monDay[1]=(((cMonInfo[startnum_Mon+1]>>3)&00001)==0)?29:30;
monDay[2]=(((cMonInfo[startnum_Mon+1]>>2)&000001)==0)?29:30;
monDay[3]=(((cMonInfo[startnum_Mon+1]>>1)&0000001)==0)?29:30;
monDay[4]=(((cMonInfo[startnum_Mon+1])&00000001)==0)?29:30;
monDay[5]=(((cMonInfo[startnum_Mon+2]>>7))==0)?29:30;
monDay[6]=(((cMonInfo[startnum_Mon+2]>>6)&01)==0)?29:30;
monDay[7]=(((cMonInfo[startnum_Mon+2]>>5)&001)==0)?29:30;
monDay[8]=(((cMonInfo[startnum_Mon+2]>>4)&0001)==0)?29:30;
monDay[9]=(((cMonInfo[startnum_Mon+2]>>3)&00001)==0)?29:30;
monDay[10]=(((cMonInfo[startnum_Mon+2]>>2)&000001)==0)?29:30;
monDay[11]=(((cMonInfo[startnum_Mon+2]>>1)&0000001)==0)?29:30;
monDay[12]=(((cMonInfo[startnum_Mon+2])&00000001)==0)?29:30;
//printf("monDay:%d\n",monDay[8]);

int i=0,isfin=1;
//printf("diffday:%d\n",diffday);
while(diffday>0)
{
//printf("diffday:%d\n",diffday);
//printf("now chinaYear:%d\n",chinaYear);
//printf("now chinaMon:%d\n",chinaMon);
if(isleap==i&&isfin==1)
{
//printf("leap_monDay[%d]:%d\n\n",i,monDay[12]);
if(diffday>=monDay[12])
{
isfin=0;
diffday-=monDay[12];
++china[0];
continue;
}else{
china[1]+=diffday;
diffday=0;
break;
}
}
if(diffday>=monDay[i])
{
if(isleap==i+1)
{
//printf("monDay[%d]:%d\n\n",i,monDay[i]);
diffday-=monDay[i];
}else{
++china[0];
//printf("monDay[%d]:%d\n\n",i+1,monDay[i]);
diffday-=monDay[i];
}
}else if(diffday<monDay[i]){
china[1]+=diffday;
diffday=0;
break;
}
++i;
}

//printf("%d/%d\n",chinaYear,chinaMon);
//china[0]=chinaYear;
//china[1]=chinaMon;

//return china;
memcpy(cal,china,sizeof(china));
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
execal.c:
#include "stdcal.h"

int main(int argc,char *argv[])
{
char *indate[3];
int china[2];
char tmpdate[LEN_DATE];
memcpy(tmpdate,argv[1],strlen(argv[1])+1);

int i=0;
indate[i]=strtok(tmpdate,SPLIT);
while(++i<3)
indate[i]=strtok(NULL,SPLIT);

chinaCal(indate[0],sumDay(argv[1]),china);
//getChinaCal(indate[0],sumDay(argv[1]));
printf("%d/%d\n",china[0],china[1]);

return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

makefile:
obj=execal.o stdcal.o mystd.o
cal:$(obj)
cc -o cal $(obj)
execal.o:execal.c stdcal.h
cc -c execal.c
stdcal.o:stdcal.c stdcal.h
cc -c stdcal.c
mystd.o:mystd.c stdcal.h
cc -c mystd.c
clean:
rm $(obj)

/////////////////////////////////////////////////////////////////////////////////////////////////////

 

以上是关于阳历阴历转换的主要内容,如果未能解决你的问题,请参考以下文章

如何利用自定义函数把阳历转换成阴历

阳历阴历转换

阳历转换成阴历PHP实现详解

PHP日期操作类代码-农历-阳历转换闰年计算天数等

PHP日期操作类代码-农历-阳历转换闰年计算天数等

Python如何实现阳历转阴历的方法分享