#include "solarlunar.h"
#define LEAP(year) ( ((year%400 == 0) || (year%4 == 0 && year%100 != 0)) ? 1:0 )
#define ABS(num) ( (num) < 0 ? (-(num)):(num) )
#define PRINT(i) printf("%d\n", (i))
/**************************
for solar calendar date
**************************/
/********************************
solar calendar format:
In internal show: solar calendar date -1.12.31 is -1L, and 1.1.1 is 0L.
and other date adds 1 or minus 1 one by one, as follows:
..., -n,-n+1,...,-1,0,1,...,n...
why choose such format, the main reason is that SOLAR CALENDAR VARIABLE can support
the internal operation +,-,<,>,<=,>=,==.
*********************************/
static unsigned int solar_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
static unsigned int reverse_solar_month[12]={31,30,31,30,31,31,30,31,30,31,28,31};
solar_calendar solar_creat_date(const int year, const unsigned int month, const unsigned int day)
{
solar_calendar so = 0L; /*attention 0L is 1.1.1, -1L is -1.12.31 */
unsigned int days = 0;
int i, absyear;
/* input checking... */
if( year == 0 || /* In solar calendar, there is no Year 0 */
(month < 1) || (month > 12) ||
(day < 1) || (day > 31) ) {
return 1L;
}
absyear = ABS(year);
so += (absyear-1)*365;
for(i = 1; i < absyear; i++) {
if(LEAP(i))
so++;
}
for(i = 1; i < month; i++) {
days += solar_month[i-1];
}
if(month >2 && LEAP(absyear))
days++;
days += day;
year > 0 ? (so += days) : (so = -( so + (365+LEAP(absyear)-days+1) ));
/* attention: 0L is 1.1.1, all solar more than zero reduce 1. */
return so > 0 ? so-1 : so ;
}
int solar_get_year(const solar_calendar solar_date)
{
int year = 1;
solar_calendar temp;
temp = (solar_date >= 0) ? solar_date+1 : ABS(solar_date);
//error: year = solar_date /365.
while(temp > (365u + LEAP(year))) {
temp -= (365u + LEAP(year));
year++;
}
return solar_date >= 0 ? year : -year;
}
unsigned int solar_get_month(const solar_calendar solar_date)
{
int year = 1;
unsigned int month = 1u;
unsigned int *month_day;
solar_calendar temp;
temp = (solar_date >= 0) ? solar_date+1 : ABS(solar_date);
while(temp > (365u + LEAP(year))) {
temp -= (365u + LEAP(year));
year++;
}
month_day = solar_date > 0 ? solar_month : reverse_solar_month;
if(LEAP(year))
solar_date >= 0 ? (month_day[1] = 29) : (month_day[10] = 29);
while(temp > month_day[month-1]) {
temp -= month_day[month-1];
month++;
}
solar_date >= 0 ? (month_day[1] = 28) : (month_day[10] = 28);
return solar_date >= 0 ? month : 13-month;
}
unsigned int solar_get_day(const solar_calendar solar_date)
{
int year = 1;
unsigned int month = 1u;
unsigned int *month_day;
solar_calendar temp;
temp = (solar_date >= 0) ? solar_date+1 : ABS(solar_date);
while(temp > (365u + LEAP(year))) {
temp -= (365u + LEAP(year));
year++;
}
month_day = (solar_date >= 0 ? solar_month : reverse_solar_month);
if(LEAP(year))
solar_date >= 0 ? (month_day[1] = 29) : (month_day[10] = 29);
while(temp > month_day[month-1]) {
temp -= month_day[month-1];
month++;
}
if(solar_date < 0)
temp = month_day[month-1]-temp+1;
solar_date >= 0 ? (month_day[1] = 28) : (month_day[10] = 28);
return temp;
}
unsigned int solar_get_days(const int year, unsigned int month)
{
return solar_month[month-1] + ( month == 2 ? LEAP(ABS(year)) : 0 );
}
weeks solar_weeks_day(const solar_calendar solar_date)
{
/* 1900/1/1 is monday. */
return solar_date >= 0 ?
(weeks)(solar_date%7 + 1) :
(weeks)(8 - ((ABS(solar_date)-1)%7 + 1));
}
boolean is_leap_year(const int year)
{
return LEAP(ABS(year));
}
/**************************
for lunar calendar date.
**************************/
/********************************
lunar calendar format:
base date: 1899.12.1 (in solar calendar date: 1900.1.1).
top date: 2099.12.30
In internal show: lunar calendar date 1899.12.1 is 1L.
and other date adds 1 one by one, as follows:
1,...,n...
why choose such format, the main reason is that SOLAR CALENDAR VARIABLE can support
the internal operation +,-,<,>,<=,>=,==.
*********************************/
static struct { unsigned int year;
unsigned int leap_month; /* leap month(0 for no) */
unsigned int month[13]; /*days in month(0: 29days,1:30days, incude leap month) */
}lunar_table[] = {
1900, 8,0,1,0,0,1,0,1,1,0,1,1,0,1,
1901, 0,0,1,0,0,1,0,1,0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0,1,0,1,0,
1933, 5,0,1,1,0,1,1,0,1,0,1,0,0,1,
1934, 0,0,1,0,1,1,0,1,0,1,1,0,1,0,
1935, 0,0,0,1,0,1,0,1,1,0,1,1,0,0,
1936, 3,1,0,0,1,0,0,1,1,0,1,1,1,0,
1937, 0,1,0,0,1,0,0,1,0,1,1,1,0,0,
1938, 7,1,1,0,0,1,0,0,1,0,1,1,0,1,
1939, 0,1,1,0,0,1,0,0,1,0,1,0,1,0,
1940, 0,1,1,0,1,0,1,0,0,1,0,1,0,0,
1941, 6,1,1,0,1,1,0,1,0,0,1,0,1,0,
1942, 0,1,0,1,1,0,1,0,1,0,1,0,1,0,
1943, 0,0,1,0,1,0,1,1,0,1,0,1,0,0,
1944, 4,1,0,1,0,1,0,1,0,1,1,0,1,1,
1945, 0,0,0,1,0,0,1,0,1,1,1,0,1,0,
1946, 0,1,0,0,1,0,0,1,0,1,1,0,1,0,
1947, 2,1,1,0,0,1,0,0,1,0,1,0,1,1,
1948, 0,1,0,1,0,1,0,0,1,0,1,0,1,0,
1949, 7,1,0,1,1,0,1,0,0,1,0,1,0,1,
1950, 0,0,1,1,0,1,1,0,0,1,0,1,0,0,
1951, 0,1,0,1,1,0,1,0,1,0,1,0,1,0,
1952, 5,0,1,0,1,0,1,0,1,1,0,1,0,1,
1953, 0,0,1,0,0,1,1,0,1,1,0,1,0,0,
1954, 0,1,0,1,0,0,1,0,1,1,0,1,1,0,
1955, 3,0,1,0,1,0,0,1,0,1,0,1,1,1,
1956, 0,0,1,0,1,0,0,1,0,1,0,1,1,0,
1957, 8,1,0,1,0,1,0,0
没有合适的资源?快使用搜索试试~ 我知道了~
大范围阴阳历转换,包含源程序。
共4个文件
c:2个
h:1个
exe:1个
4星 · 超过85%的资源 需积分: 10 55 下载量 153 浏览量
2007-10-02
15:44:58
上传
评论 1
收藏 13KB RAR 举报
温馨提示
使用SOLARLUNAR库实现的阴阳历转换。<br>范围: 1899..12.1 ~ 2099.12.30
资源推荐
资源详情
资源评论
收起资源包目录
solu.rar (4个子文件)
solu
sl.exe 34KB
source
main.c 2KB
solarlunar.h 3KB
solarlunar.c 22KB
共 4 条
- 1
资源评论
- zhuxianjun2011-11-15谢谢,好像没有节气的提示
- lindir2016-07-04基本没注释
- 南国时代2013-11-01挺好,不过不能用
luodongshui
- 粉丝: 6
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功