17.01.2017, 00:36
(
Последний раз редактировалось Lordzy; 17.01.2017 в 02:58.
)
Quote:
isValidDate
Okay this function is created on the behalf of this discussion.It validates a date which is passed as string to the function.It doesn't need any delimiter or format to specify actually.The function will identify date,month and year automatically. Parameters: str - date in string PHP код:
|
------
An even more faster version (1.5 times) of the above IsValidDate that accepts any date-format and separators.
(non-sscanf version)
IsValidDate - Returns true on valid dates, false for invalid ones.
Parameters:
dStr[] - Date as string.
size = sizeof(dStr)) - String size. (optional, default value = sizeof(dStr) )
pawn Код:
stock bool:IsValidDate(dStr[], size = sizeof(dStr)) {
new
monthDays[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
},
temp_YMD[3],
temp_Pos = 0,
temp_ePos = 0,
temp_Index = 0,
temp_shortStr[5]
;
do {
while(dStr[temp_ePos] >= 48 && dStr[temp_ePos] <= 57) {
if(++temp_ePos >= size) {
temp_ePos = size - 1;
break;
}
}
strmid(temp_shortStr, dStr, temp_Pos, temp_ePos, sizeof(temp_shortStr));
temp_YMD[temp_Index++] = strval(temp_shortStr);
temp_Pos = ++temp_ePos;
}
while(dStr[temp_ePos] != '\0' && temp_Index < 3);
temp_ePos = temp_Pos = -1;
for(temp_Index = 0; temp_Index < 3; temp_Index++) {
if(temp_YMD[temp_Index] > 1750) {
if((temp_YMD[temp_Index] % 400) == 0 || ((temp_YMD[temp_Index] % 100) != 0 && (temp_YMD[temp_Index] % 4) == 0))
monthDays[1] = 29;
}
else if(temp_YMD[temp_Index] > 12)
temp_Pos = temp_Index;
else
if(temp_ePos == -1)
temp_ePos = temp_Index;
else
temp_Pos = temp_Index;
}
if(temp_ePos == -1 || temp_Pos == -1)
return false;
if(temp_YMD[temp_ePos] < 1 || temp_YMD[temp_ePos] > 12)
return false;
if(temp_YMD[temp_Pos] < 1 || temp_YMD[temp_Pos] > monthDays[temp_YMD[temp_ePos] - 1])
return false;
return true;
}
Parameters:
dStr[] - Date, as string.
pawn Код:
stock bool:IsValidDate(dStr[]) {
new
monthDays[12] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
},
temp_YMD[3],
temp_Pos = 0,
temp_ePos = 0,
temp_Index = 0
;
//Thought about implementing SSCANF_QUIET, but it requires spaces to work "currently".
for(temp_Index = strlen(dStr) - 1; temp_Index != 0; temp_Index--) {
if(!(dStr[temp_Index] >= 48 && dStr[temp_Index] <= 57))
dStr[temp_Index] = ' ';
}
if(sscanf(dStr, "iii", temp_YMD[0], temp_YMD[1], temp_YMD[2]))
return false;
temp_ePos = temp_Pos = -1;
for(temp_Index = 0; temp_Index < 3; temp_Index++) {
if(temp_YMD[temp_Index] > 1750) {
if((temp_YMD[temp_Index] % 400) == 0 || ((temp_YMD[temp_Index] % 100) != 0 && (temp_YMD[temp_Index] % 4) == 0))
monthDays[1] = 29;
}
else if(temp_YMD[temp_Index] > 12)
temp_Pos = temp_Index;
else
if(temp_ePos == -1)
temp_ePos = temp_Index;
else
temp_Pos = temp_Index;
}
if(temp_ePos == -1 || temp_Pos == -1)
return false;
if(temp_YMD[temp_ePos] < 1 || temp_YMD[temp_ePos] > 12)
return false;
if(temp_YMD[temp_Pos] < 1 || temp_YMD[temp_Pos] > monthDays[temp_YMD[temp_ePos] - 1])
return false;
return true;
}