home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
linuxmafia.com 2016
/
linuxmafia.com.tar
/
linuxmafia.com
/
pub
/
palmos
/
happydays-src-1.37.tar.gz
/
happydays-src-1.37.tar
/
happydays-1.37
/
sol2lun.c
< prev
next >
Wrap
C/C++ Source or Header
|
2000-04-01
|
11KB
|
322 lines
/*
HappyDays - A Birthdate displayer for the PalmPilot
Copyright (C) 1999-2000 JaeMok Jeong
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <PalmOS.h>
#include "calendar.h"
Char convertIndex[163][13] = {
/*1881*/
1, 2, 1, 2, 1, 2, 2, 3, 2, 2, 1, 2, 1,
1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 0,
1, 1, 2, 1, 1, 2, 1, 2, 2, 2, 1, 2, 0,
2, 1, 1, 2, 1, 3, 2, 1, 2, 2, 1, 2, 2,
2, 1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 0,
2, 1, 2, 1, 2, 1, 1, 2, 1, 2, 1, 2, 0,
2, 2, 1, 2, 3, 2, 1, 1, 2, 1, 2, 1, 2,
2, 1, 2, 2, 1, 2, 1, 1, 2, 1, 2, 1, 0,
2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2, 0,
1, 2, 3, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2,
/*1891*/
1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 2, 0,
1, 1, 2, 1, 1, 2, 3, 2, 2, 1, 2, 2, 2,
1, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 0,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0,
1, 2, 3, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2,
1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 0,
2, 1, 2, 1, 1, 2, 3, 2, 1, 2, 2, 2, 1,
2, 1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 0,
1, 2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 0,
1, 2, 2, 1, 2, 3, 1, 2, 1, 1, 2, 2, 1,
2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 0,
1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 0,
/*2031*/
2, 1, 2, 3, 2, 1, 2, 2, 1, 2, 1, 2, 1,
2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 0,
1, 2, 1, 1, 2, 1, 2, 3, 2, 2, 2, 1, 2,
1, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 0,
2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 0,
2, 2, 1, 2, 1, 1, 4, 1, 1, 2, 1, 2, 2,
2, 2, 1, 2, 1, 1, 2, 1, 1, 2, 1, 2, 0,
2, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2, 1, 0,
2, 2, 1, 2, 2, 3, 2, 1, 2, 1, 2, 1, 1,
2, 1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 0,
/*2041*/
2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 2, 0,
1, 2, 3, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2,
1, 2, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 0
};
Int16 month[12] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//
// solarDay : the solar day to be converted
// leapyes : leap yes or no(0/1)
//
int sol2lun(int syear, int smonth, int sday, DateTimeType *lunar, int *leapyes)
{
int lyear, lmonth, lday; /* calculated lunar date */
int m1, m2, i, j, jcount, ll, m0;
int dt[163];
long td, td0, td1, td2, k11;
if ( syear < 1881 || syear > 2043 ) return -1;
if ( smonth < 1 || smonth > 12 ) return -1;
if ( sday < 1 || sday > 31 ) return -1;
if ( syear == 1881 && smonth == 1 && sday < 30 ) return -1;
for(i=0; i<163; i++) {
dt[i] = 0;
for(j=0; j<12; j++) {
switch(convertIndex[i][j])
{
case 1:
case 3:
dt[i] = dt[i] + 29;
break;
case 2:
case 4:
dt[i] = dt[i] + 30;
}
}
switch(convertIndex[i][12])
{
case 0:
break;
case 1:
case 3:
dt[i] = dt[i] + 29;
break;
case 2:
case 4:
dt[i] = dt[i] + 30;
break;
}
}
/* 1. 1. 1. - 1910. 2. 10. */
td1 = 1880*365L + 1880/4 - 1880/100 + 1880/400 + 30;
/* ## 1. 1. 1. - syear. smonth. sday. ## */
k11 = (long)(syear-1);
td2 = k11*365L + k11/4L - k11/100L + k11/400L;
ll = syear%400==0 || (syear%100!=0 && syear%4==0);
if(ll) month[1] = 29;
else month[1] = 28;
if( sday > month[smonth-1] ) { /* Input */
return -1;
}
for(i=0; i<smonth-1; i++) td2 = td2 + (long)month[i];
td2 = td2 + (long)sday;
/* ## 1881. 1. 30. - syear. smonth. sday. ## */
td = td2 - td1 + 1;
/* ## Lunar Year Caculation ## */
td0 = (long)dt[0];
for(i=0; i<163; i++) {
if( td <= td0 ) break;
td0 = td0 + (long)dt[i+1];
}
lyear = i + 1881; /* Calculated Lunar Year */
/* ## Lunar Month Calculation ## */
td0 = td0 - (long)dt[i];
td = td - td0;
if(convertIndex[i][12] != 0) jcount = 13;
else jcount = 12;
m2 = 0;
for(j=0; j<jcount; j++) {
if( convertIndex[i][j] <=2 ) m2++;
if( convertIndex[i][j] <=2 ) m1 = convertIndex[i][j] + 28;
else m1 = convertIndex[i][j] + 26;
if( td <= (long)m1 ) break;
td = td - (long)m1;
}
m0 = j;
lmonth = m2; /* Calculated Lunar Month */
lday = td; /* Calculated Lunar Day */
lunar->year = lyear; lunar->month = lmonth; lunar->day = lday;
if( (int)convertIndex[lyear-1881][12] != 0 &&
(int)convertIndex[lyear-1881][m0] > 2 ) *leapyes = 1;
else *leapyes = 0;
return 0;
}