Skip to content

Instantly share code, notes, and snippets.

@Sreyas-Sreelal
Created January 19, 2017 15:20
Show Gist options
  • Save Sreyas-Sreelal/2bb2a11b9a8c7c6d0efee80640775992 to your computer and use it in GitHub Desktop.
Save Sreyas-Sreelal/2bb2a11b9a8c7c6d0efee80640775992 to your computer and use it in GitHub Desktop.
pawn function to check a date is valid or not with any possible format.
bool:isValidDate(str[])//by Sreyas
{
new count,i,j;
new daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
new parts[3][5],Index_Beg;
new bool:legit;
while(str[i]!='\0')
{
if(str[i] < 48 || str[i] > 57)
{
count++;
strmid(parts[j], str, Index_Beg, i, sizeof(parts[]));
Index_Beg = i+1;
j++;
}
i++;
if(str[i]=='\0')
{
strmid(parts[j], str, Index_Beg, i, sizeof(parts[]));
}
}
if(count != 2) return false;
new number[3];
for(i=0;i<3;i++)
{
number[i] = strval(parts[i]);
if(number[i] == 0)return false;
}
new y,m,d;
y = m = d = -1;
for(i=0;i<3;i++)
{
if(number[i]>31)
{
if(y != -1) return false;
y = i;
}
}
for(i=0;i<3;i++)
{
if(i == y) continue;
if(number[i] < 13 && number[i] > 0)
{
if(m == -1)
{
m = i;
}
else
{
d = i;
}
}
}
if(y==-1)
{
for(i=0;i<3;i++)
{
if(i==d||i==m) continue;
y = i;
}
}
if(d==-1)
{
for(i=0;i<3;i++)
{
if(i==y||i==m) continue;
d = i;
}
}
if(m==-1)
{
for(i=0;i<3;i++)
{
if(i==y||i==d) continue;
m = i;
}
}
if(number[y] % 400 == 0 || (number[y] % 100 != 0 && number[y] % 4 == 0))
daysinmonth[1]=29;
if( number[m]<13 && number[d] <= daysinmonth[number[m]-1])
legit=true;
if (!legit) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment