Checking weekdays
#1

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?
Reply
#2

What do you mean exactly? An example?
Reply
#3

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.
Reply
#4

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)
Reply
#5

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
Reply
#6

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!");
}
Reply
#7

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
Reply
#8

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!");
}
Reply
#9

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)