Szerző Téma: DateDiff hiba (Két nap közti különbség)  (Megtekintve 382 alkalommal)

DateDiff hiba (Két nap közti különbség)
« Dátum: 2014. Január 01. - 22:58:17 »
0 Show voters
Sziasztok!
Problémám támadt a DateDiff-el ami a két nap között eltelt napokat számolja meg.
Eddig minden hibátlanul mûködött (amíg minden dátum 2013 volt), de most hogy 2014 lett nem hajlandó kiszámolni az eltelt napokat.
A kód:
 
#define swap(%1,%2); { %1 = %1 + %2; %2 = %1 - %2; %1 = %1 - %2; }
stock IsLeapYear(year) if((year % 400) == 0 || (((year % 100) != 0) && ((year % 4) == 0))) return 1; else return 0;
stock DateDiff(year1,year2,month1,month2,day1,day2)
{
new days;
new days_of_months[12] =
{
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
};
if(year1 > year2)
{
swap(year1,year2);
swap(month1,month2);
swap(day1,day2);
}
else if(year1 == year2)
{
if(month1 > month2)
{
swap(month1,month2);
swap(day1,day2);
}
else if(month1 == month2)
{
if(day1 == day2) return 0;
else return day1>day2?day1-day2:day2-day1;
}
}
for(new i = year1; i != year2; i++)
{
if(IsLeapYear(i)) days += 366;
else days += 365;
}
for(; month1 != month2; month1++)
{
if(month1 == 2 && IsLeapYear(year2)) days += 29;
else days += (days_of_months[month1-1]);
}
return (days+day2-day1);
}

 
Csináltam az ellenõrzéshez egy teszt scriptet is:
 
#include <a_samp>
public OnFilterScriptInit()
{
new RegiEv, RegiHonap, RegiNap;
RegiEv = 2013;
RegiHonap = 12;
RegiNap = 31;
new MostaniEv, MostaniHonap, MaiNap;
MostaniEv = 2014;
MostaniHonap = 1;
MaiNap = 1;
new ElteltNapok;
ElteltNapok = DateDiff(MostaniEv,RegiEv,MostaniHonap,RegiHonap,MaiNap,RegiNap);
printf(\"%d %d %d - %d %d %d = %d nap a különbség\",RegiEv, RegiHonap, RegiNap, MostaniEv, MostaniHonap, MaiNap, ElteltNapok);
return 1;
}
 
#define swap(%1,%2); { %1 = %1 + %2; %2 = %1 - %2; %1 = %1 - %2; }
stock IsLeapYear(year) if((year % 400) == 0 || (((year % 100) != 0) && ((year % 4) == 0))) return 1; else return 0;
stock DateDiff(year1,year2,month1,month2,day1,day2)
{
new days;
new days_of_months[12] =
{
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
};
if(year1 > year2)
{
swap(year1,year2);
swap(month1,month2);
swap(day1,day2);
}
else if(year1 == year2)
{
if(month1 > month2)
{
swap(month1,month2);
swap(day1,day2);
}
else if(month1 == month2)
{
if(day1 == day2) return 0;
else return day1>day2?day1-day2:day2-day1;
}
}
for(new i = year1; i != year2; i++)
{
if(IsLeapYear(i)) days += 366;
else days += 365;
}
for(; month1 != month2; month1++)
{
if(month1 == 2 && IsLeapYear(year2)) days += 29;
else days += (days_of_months[month1-1]);
}
return (days+day2-day1);
}

 
A tesztelés alatt 2013.12.31 és 2014.1.1 dátummal próbáltam meg kiszámoltatni az eltelt napok számát, de nem futott le, hibát írt ki a crashdetect plugin.
A hiba:
 
Run time error 4: \"Array index out of bounds\"
Accessing element at index 12 past array upper bound 11
AMX backtrace:
#0 000008ec in DateDiff (year1=2013, year2=2014, month1=13, month2=1, day1=31, day2=1) at C:\\tesztszerver\\filterscripts\\DATE.pwn:75
#1 0000017c in public OnFilterScriptInit () at C:\\tesztszerver\\filterscripts\\DATE.pwn:16
[/quote]
Valamiért a 12. hónap helyett 13. hónappal számol, és ezért nem fut le. :S (Pedig 12. hónap van beírva.)
A válaszokat, és a segítséget elõre is köszönöm!
« Utoljára szerkesztve: 2014. Január 02. - 22:27:47 írta DamieN007 »

 

SimplePortal 2.3.7 © 2008-2024, SimplePortal