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 / lun2sol.c < prev    next >
C/C++ Source or Header  |  2000-04-01  |  3KB  |  110 lines

  1. /*
  2. HappyDays - A Birthdate displayer for the PalmPilot
  3. Copyright (C) 1999-2000 JaeMok Jeong
  4.  
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18. */
  19.  
  20. #include <PalmOS.h>
  21. #include "calendar.h"
  22.  
  23. //  lunayDay : the lunar day to be converted
  24. //  leapyes  : leap yes or no(0/1)
  25. //
  26. int lun2sol(int lyear, int lmonth, int lday, int leapyes,
  27.                   DateTimeType *solar)
  28. {
  29.     int syear, smonth, sday;    /* calculated solar date */
  30.     int m1, m2, n2;
  31.     int i, j, leap;
  32.     long td;
  33.       
  34.     if ( lyear < 1881 || lyear > 2043 ) return -1;
  35.     if ( lmonth < 1 || lmonth > 12 ) return -1;
  36.     if ( lday < 1 || lday > 30 ) return -1;
  37.                                               
  38.     if(leapyes == 1)
  39.     {
  40.         if( convertIndex[lyear-1881][12] < 1 ) return -1;
  41.         if( convertIndex[lyear-1881][lmonth] < 3 ) return -1;
  42.         if( lday > convertIndex[lyear-1881][lmonth]+26 ) return -1;
  43.     }
  44.  
  45.     j = lmonth-1;
  46.  
  47.     if( leapyes == 0 )
  48.     {
  49.         for(i = 1; i <= 12; i++) if(convertIndex[lyear-1881][i-1] > 2 ) j++;
  50.         if(lday > convertIndex[lyear-1881][j]+28) return -1;
  51.     }
  52.  
  53.     m1 = -1;
  54.     td = 0L;
  55.  
  56.     if (lyear != 1881) {
  57.         m1 = lyear - 1882;
  58.         for (i=0; i<=m1; i++) {
  59.             for (j=0; j<13; j++) td = td + (long)convertIndex[i][j];
  60.             if (convertIndex[i][12]==0 ) td = td + 336L;
  61.             else               td = td + 362L;
  62.         }
  63.     }
  64.  
  65.     m1++;
  66.     n2 = lmonth-1;
  67.     m2 = -1;
  68.  
  69.     do {
  70.         m2++;
  71.         if ( convertIndex[m1][m2] > 2 ) {
  72.             td = td + 26L + (long)convertIndex[m1][m2];
  73.             n2++;
  74.         }
  75.         else if (m2==n2) break;
  76.         else td = td + 28L + (long)convertIndex[m1][m2];
  77.     } while(1);
  78.  
  79.     if (leapyes != 0) td = td + 28L + (long)convertIndex[m1][m2];
  80.     td = td + (long)lday + 29L;
  81.     m1 = 1880;
  82.  
  83.     do {
  84.         m1++;
  85.         leap = m1%400==0 || (m1%100!=0 && m1%4==0);
  86.         if (leap) m2=366;
  87.         else      m2=365;
  88.         if (td <= (long)m2) break;
  89.         td = td - (long)m2;
  90.     } while(1);
  91.  
  92.     syear = m1;
  93.     month[1] = m2 - 337;
  94.     m1 = 0;
  95.  
  96.     do {
  97.         m1++;
  98.         if ( td <= (long)month[m1-1] ) break;
  99.         td = td - (long)month[m1-1];
  100.     } while(1);
  101.  
  102.     smonth = m1;
  103.     sday = (int)td;
  104.     
  105.     solar->year = syear; solar->month = smonth;
  106.     solar->day =  sday;
  107.  
  108.     return 0;
  109. }
  110.