[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

Determine date is weekday or weekend

I need the algorithm to determine if a date is weekday or a weekend.
0
CornDog932
Asked:
CornDog932
  • 7
  • 5
  • 5
  • +4
1 Solution
 
AggarwalCommented:

on unix /.


#include<time.h>

int main(void)
{
time_t a = time(NULL) ;
struct tm* tmStruct = localtime(&a) ;

if ( tmStruct->tm_wday >= 6 )
    printf("WeekEnd") ;
else
    printf("WeekDay") ;
}
0
 
AxterCommented:
Aggarwal,
The type of answer you posted should have been posted as a comment.

Hi (Aggarwal), welcome to EE.
All of the experts here, for the most part have learn from other experts as
to the proper etiquette for posting answer.

First-class etiquette for posting Answers:
1.  An answer should not be posted as an answer, if other experts have
previously posted possible answers as comments, and/or have already made
contributions to the question.

2.  An answer should not be posted as an answer, if it has a question.
Example: (Is this for windows? If so, here's the answer)

3.  An answer should not be posted as an answer, if it has the phrase "I
think".
Example:  (I think this should work)

4.  Full complete schoolwork answers must not be posted. (EE Policy)

5.  If you inadvertently post an answer after anther expert just post the
same answer as a comment, you should promptly notify the questioner of your
accidental post, and ask the questioner to fairly award the question to the
expert whom fully/best answered the question first.  You might consider
also apologizing to the other expert.

If you feel you have an answer, you can post it as a comment, and the
questioner can convert your comment to an answer if he/she thinks it's
correct or deserving of the awarded points.

There are many experts who never post answers as answer.  Instead, they
post their answers as comments.
If you read the following link, you'll see why this is the preferred method
for many of our valued experts, including myself.

http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp

At the bottom of the above link, see the following section:
"Advice for Experts on Providing Solutions Through Comments or Answers"


Hi (CornDog932):
Feel free to click the [Reject Answer] button near (Answer-poster's)
response, even if it seems like a good answer.
Doing so will increase your chance of obtaining additional input from other
experts.  Later, you can click the [Select Comment as Answer] button on any
response.
0
 
CornDog932Author Commented:
I didn't say this was for Unix!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
AggarwalCommented:
My Fault !!

CornDog932 - Sorry for trying to help you !!!
0
 
AxterCommented:
Aggarwal,
When you lock questioner's question with a bad answer, you really are not helping the questioner.
The questioner has a better chance of getting the most help if you post your answer as a comment.
0
 
AggarwalCommented:
Axter,

Agreed ..my Fault !!!
0
 
MichaelSCommented:
Actually code looks OK, did someone tried it? It sould work under windows too.
0
 
jasonclarkeCommented:
This is a little bit of a harsh rejection, this code works perfectly well on Windows too...
0
 
jasonclarkeCommented:
> Actually code looks OK

I didn't see your comment...yes, I tried it and it works.
0
 
CornDog932Author Commented:
What I'm looking for is an algorithm that can make the determination.
I don't want to use tm structure method.

I seen the algorithm before but I don't remember it.
0
 
MichaelSCommented:
you mean that call to
localtime(&a);
is too easy for you? or you want to write your own fucntion for that?
0
 
CornDog932Author Commented:
>>is too easy for you? or you want to write your own
>>fucntion for that?

I'm looking for the algorithm.  Why is that so hard for you to understand?
If you don't know the algorithm, then please don't waste my time with your comments.
0
 
MichaelSCommented:
Oops, I am also sorry to posting some comments here
0
 
olgatCommented:
I have seen this algorithm somewhere on the Web, so all credits aren't mine. I think it was a sample from the book named "Software Practice and Experience" something like that.

This finction returns absolute # days corresponding to Date assuming Jan 1, 1 B.C is day 1.
Absolute() % 7 = k, where k = 0 is sunday ... k = 6 is saturday and so on.

long Absolute(int nCurDay, int nCurMonth, int nCurYear)
{
    int m = 1;               // start in January;
    int daysBefore = 0;      // count # days before month

    while (m < nCurMonth)
    {
        // tally # of days in preceding months
        daysBefore += DaysInMonth(m,nCurYear);
        m++;
    }

    // Days before this year
    long dayYears = 365 * (nCurYear - 1);

    // Add 1 extra day for each leap year  
    int leapYears = (nCurYear - 1) / 4;     // initial # of leap years

    leapYears -= (nCurYear - 1) / 100;      // subtract years divisible by 100
    leapYears += (nCurYear - 1) / 400;      // add back years divisibly by 400

    return nCurDay + daysBefore + dayYears + leapYears;
}
0
 
olgatCommented:
I have seen this algorithm somewhere on the Web, so all credits aren't mine. I think it was a sample from the book named "Software Practice and Experience" something like that.

This finction returns absolute # days corresponding to Date assuming Jan 1, 1 B.C is day 1.
Absolute() % 7 = k, where k = 0 is sunday ... k = 6 is saturday and so on.

long Absolute(int nCurDay, int nCurMonth, int nCurYear)
{
    int m = 1;               // start in January;
    int daysBefore = 0;      // count # days before month

    while (m < nCurMonth)
    {
        // tally # of days in preceding months
        daysBefore += DaysInMonth(m,nCurYear);
        m++;
    }

    // Days before this year
    long dayYears = 365 * (nCurYear - 1);

    // Add 1 extra day for each leap year  
    int leapYears = (nCurYear - 1) / 4;     // initial # of leap years

    leapYears -= (nCurYear - 1) / 100;      // subtract years divisible by 100
    leapYears += (nCurYear - 1) / 400;      // add back years divisibly by 400

    return nCurDay + daysBefore + dayYears + leapYears;
}
0
 
MichaelSCommented:
>olgat
"please don't waste CornDog932's time with your comments", he is looking for "the algorithm", not for the code which can do it :)
0
 
jasonclarkeCommented:
I would suspect that the only *likely* reason for wanting the algorithm (as opposed to code) is for the sake of homework...

Shame it wasn't checked *before* the answer was posted...
0
 
MichaelSCommented:
>Shame it wasn't checked *before* the answer was posted

100% agree
0
 
olgatCommented:
How can we check it ?
0
 
olgatCommented:
How can we check it ?
0
 
olgatCommented:
How can we check it ?
0
 
nietodCommented:
CornDog932,

    there is one mistake in aggarwal's answer.  The mistake is where he says "on unix".   That answer is 100% standard C/C++ and will work on any platform.

I recommend you accept his annswer and use his answer.

Another answer is to use

int CalJulDat(int yy, int mm, int dd)
{
   int jdn;

   if (mm < 3)
   {
      mm += 12;
      yy -= 1;
   }
   jdn = dd+1720996+(mm+1)*306/10+yy*365 + yy/4 - yy/100 + yy/400;
   return jdn;
}

int main()
{
   int yy=2001;
   int mm = 3
   int dd = 10;
   int jdn = CalJulDat(yy,mm,dd);
   int DOW = (jdn+2) % 7; // Day of week.
   
}

which calculates a julian date.  When you take the modulos of this date by 7 as you see in the last line (DOW) you get the day of the week.  I can't remember if the starting day (0) is sunday or monday, but you can experiment with it to find out.

however using the built-in features seems easier to me.
0
 
MichaelSCommented:
somehow that question is really strange. an algorithm was asked, than answer with working code was rejected and answer with another code was accepted.
0
 
CornDog932Author Commented:
nietod,
>>jdn = dd+1720996+(mm+1)*306/10+yy*365 + yy/4 - yy/100 + yy/400;
Thanks, this line of code is the algorithm I was looking for.


olgat,
Your code may do the same thing but nietod's code and format is what I was looking for.  Thanks for trying.

jasonclarke,
This is not homework.

MichaelS,
Are you an expert here, or is your task to cause trouble, and make stupid comments?
You have been no help here at all.  Your comments are a waste of my time.  So bug off.

0
 
nietodCommented:
And if actually were homework, you'd never get away with using dd+1720996+(mm+1)*306/10+yy*365 + yy/4 - yy/100 + yy/400;

:-)

But personally I would use aggarwal's solution for this.
0
 
MichaelSCommented:
>MichaelS,
>Are you an expert here, or is your task to cause trouble, and make stupid comments?
You have been no help here at all.  Your comments are a waste of my time.  So bug off.

I am here already around 3 years and you are the most friendy person I have ever met :) Hope other people acts the same friendly way to you.
0
 
CornDog932Author Commented:
>>And if actually were homework, you'd never get away with
>>using dd+1720996+(mm+1)*306/10+yy*365 + yy/4
>>- yy/100 + yy/400;

That's right.
I don't understand why other experts would think this is a homework question.  IMHO it does not fit the pattern of a homework question.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 7
  • 5
  • 5
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now