From: <ai...@us...> - 2009-02-09 21:01:51
|
Revision: 9480 http://plplot.svn.sourceforge.net/plplot/?rev=9480&view=rev Author: airwin Date: 2009-02-09 21:01:38 +0000 (Mon, 09 Feb 2009) Log Message: ----------- AWI for Tony Allen. Fix bugs in negative years and negative Julian dates for breakDownMJD and associated helper and extra functions. Modified Paths: -------------- trunk/lib/qsastime/qsastime.c trunk/lib/qsastime/qsastime_extra.c Modified: trunk/lib/qsastime/qsastime.c =================================================================== --- trunk/lib/qsastime/qsastime.c 2009-02-09 10:47:34 UTC (rev 9479) +++ trunk/lib/qsastime/qsastime.c 2009-02-09 21:01:38 UTC (rev 9480) @@ -79,6 +79,7 @@ year100 = year - 1; year400 = year - 1; } + if((forceJulian == 0 && (year < 1582 || (year == 1582 && month < 9) || (year == 1582 && month == 9 && day < 15))) || forceJulian == 1) { /* count leap years on proleptic Julian Calendar */ @@ -147,6 +148,7 @@ { static char *dow = {"Wed\0Thu\0Fri\0Sat\0Sun\0Mon\0Tue"}; int d = MJD->base_day % 7; + if(d < 0) d += 7; return &(dow[d*4]); } @@ -154,6 +156,7 @@ { static char *dow = {"Wednesday\0Thursday\0\0Friday\0\0\0\0Saturday\0\0Sunday\0\0\0\0Monday\0\0\0\0Tuesday"}; int d = MJD->base_day % 7; + if(d < 0) d += 7; return &(dow[d*10]); } @@ -190,33 +193,37 @@ j = MJD->base_day + extra_days; - if( j <= -678943) { + if( j < -678943) { - /* BCE dates */ + /* Negative CE dates */ - j += 678943; - if( j > 0) - { - /* must be in year BCE 1 (CE year 0) */ - year = 0; - /* subtract nothing from j */ - } - else - { - /* negative years */ - year = (int) ((float)j / 365.25) -1; - doy = j - (int)(year * 365.25); - } + j += 678943; - } + /* negative years */ + year = (int) ((float)(j-365) / 365.25); + doy = j +1 - year * 365.25; + + } + else if( j < -678577) { + + /* CE = 0, BCE = 1 dates */ + + j += 678943; + + /* negative years */ + year = 0; + doy = j +1; + + } else if( j < -100840 || forceJulian == 1) { /* Julian Dates */ j += 678943; year = (int) ((float)j / 365.25); - - doy = j - (int)(year * 365.25); + lastyear = year - 1; + doy = j - year * 365 - lastyear / 4; + } else @@ -255,23 +262,22 @@ j = MJD->base_day + extra_days; - if( j <= -678943) { + if( j <= -678577) { /* BCE dates */ j += 678943; - if( j > 0) - { - /* must be in year BCE 1 (CE year 0) */ - *year = 0; - /* subtract nothing from j */ - } - else - { - /* negative years */ - *year = (int) ((float)j / 365.25) -1; - j = j - (int)((*year) * 365.25); - } + if( j>=0) + { + *year = 0; + j++; + } + else + { + /* negative years */ + *year = (int) ((float)(j-365) / 365.25); + j = j +1 - *year * 365.25; + } /* j is now always positive */ *month = -1; @@ -287,8 +293,6 @@ } else { - /* put this year's leap day back as it is done here */ - j++; while(j > MonthStartDOY_L[*month +1]) { (*month)++; @@ -304,7 +308,8 @@ *year = (int) ((float)j / 365.25); - j = j - (int)(*year * 365.25); + lastyear = *year - 1; + j = j - *year * 365 - lastyear / 4; *month = -1; if(*year%4 != 0) @@ -318,8 +323,6 @@ } else { - /* put leap day back for this year as done here */ - j++; while(j > MonthStartDOY_L[*month + 1]) { (*month)++; Modified: trunk/lib/qsastime/qsastime_extra.c =================================================================== --- trunk/lib/qsastime/qsastime_extra.c 2009-02-09 10:47:34 UTC (rev 9479) +++ trunk/lib/qsastime/qsastime_extra.c 2009-02-09 21:01:38 UTC (rev 9480) @@ -55,6 +55,7 @@ /* parse off year */ y = strtol(&(ISOstring[startAt]), NULL, 10); + if(ISOstring[startAt] == '-' || ISOstring[startAt] == '+') startAt++; startAt += 5; if(startAt > len) return 1; @@ -100,7 +101,7 @@ /* MJD for Jan 1 0000 (correctly Jan 01, BCE 1) is - 678943, count from there */ /* negative CE (AD) years convert to BCE (BC) as BCE = 1 - CE, e.g. 2 BCE = -1 CE */ - leaps = year / 4 - 1 ; /* (note leaps is negative here and year 0 (1 BCE) was a leap year */ + leaps = (year -4) / 4; /* (note leaps is negative here and year 0 (1 BCE) was a leap year */ MJD->base_day = year * 365 + leaps + doy - 678943; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |