SA-MP Forums Archive
Checking weekdays - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Checking weekdays (/showthread.php?tid=461176)



Checking weekdays - siemka321 - 31.08.2013

So, I found a little code that could show what day of the week it is.

Код:
GetWeekDay(day=0, month=0, year=0)
{
  if (!day)
    getdate(year, month, day);

  new
    weekday_str[10],
    j,
    e
  ;

  if (month <= 2)
  {
    month += 12;
    --year;
  }

  j = year % 100;
  e = year / 100;

  switch ((day + (month+1)*26/10 + j + j/4 + e/4 - 2*e) % 7)
  {
    case 0: weekday_str = "Saturday";
    case 1: weekday_str = "Sunday";
    case 2: weekday_str = "Monday";
    case 3: weekday_str = "Tuesday";
    case 4: weekday_str = "Wednesday";
    case 5: weekday_str = "Thursday";
    case 6: weekday_str = "Friday";
  }

  return weekday_str;
}
The code works nice, but I want to make something that checks for a specific weekday. Anybody have any ideas on how to do this?


Re: Checking weekdays - Rapgangsta - 31.08.2013

What do you mean exactly? An example?


Re: Checking weekdays - siemka321 - 31.08.2013

Quote:
Originally Posted by Rapgangsta
Посмотреть сообщение
What do you mean exactly? An example?
You see, I want to make a pickup. And when you pick it up, it checks if the weekday is saturday. I can do the pickup but the checking part is a little bit confusing. I tried doing if(weekday_str == "Saturday"), but it gives me a problem that the variable is undefined. I tried defining it at the top instead of GetWeekDay(day=0, month=0, year=0), it gives me a warning.


Re: Checking weekdays - Rapgangsta - 31.08.2013

You can't compare strings in that way, you should pass the current date in the stock and then return it as a string, and for comparing two string you should use strcmp
https://sampwiki.blast.hk/wiki/Strcmp

so the code becomes
Код:
new Year, Month, Day;
getdate(Year, Month, Day);
if(!strcmp(GetWeekDay(Day, Month, Yeaer), "Saturday"), 1)
{
printf("Today is Saturday!");
}
(You shold have only the GetWeekDay stock)


Re: Checking weekdays - siemka321 - 31.08.2013

Quote:
Originally Posted by Rapgangsta
Посмотреть сообщение
You can't compare strings in that way, you should pass the current date in the stock and then return it as a string, and for comparing two string you should use strcmp
https://sampwiki.blast.hk/wiki/Strcmp

so the code becomes
Код:
new Year, Month, Day;
getdate(Year, Month, Day);
if(!strcmp(GetWeekDay(Day, Month, Yeaer), "Saturday"), 1)
{
printf("Today is Saturday!");
}
(You shold have only the GetWeekDay stock)


Thanks mate! You really helped me out on this one!

EDIT: Spoke out too soon got a warning:

warning 206: redundant test: constant expression is non-zero


Re: Checking weekdays - Rapgangsta - 31.08.2013

Oh sorry, i forgot a ' ) '

Код:
new Year, Month, Day;
getdate(Year, Month, Day);
if(!strcmp(GetWeekDay(Day, Month, Yeaer), "Saturday"), 1))
{
printf("Today is Saturday!");
}



Re: Checking weekdays - siemka321 - 31.08.2013

Quote:
Originally Posted by Rapgangsta
Посмотреть сообщение
Oh sorry, i forgot a ' ) '

Код:
new Year, Month, Day;
getdate(Year, Month, Day);
if(!strcmp(GetWeekDay(Day, Month, Yeaer), "Saturday"), 1))
{
printf("Today is Saturday!");
}
Now it gave me an error:

warning 206: redundant test: constant expression is non-zero
error 029: invalid expression, assumed zero


Re: Checking weekdays - Rapgangsta - 31.08.2013

Sorry i made some confusing with braces, now it compiles

Код:
new Year, Month, Day;
getdate(Year, Month, Day);
if(!strcmp(GetWeekDay(Day, Month, Yeaer), "Saturday",true))
{
printf("Today is Saturday!");
}



Re: Checking weekdays - siemka321 - 01.09.2013

Quote:
Originally Posted by Rapgangsta
Посмотреть сообщение
Sorry i made some confusing with braces, now it compiles

Код:
new Year, Month, Day;
getdate(Year, Month, Day);
if(!strcmp(GetWeekDay(Day, Month, Yeaer), "Saturday",true))
{
printf("Today is Saturday!");
}
It compiles without any errors or warnings!

Just tested it. It works nice! Thanks, man. You really helped me out.