Link to home
Start Free TrialLog in
Avatar of CornDog932
CornDog932

asked on

Determine date is weekday or weekend

I need the algorithm to determine if a date is weekday or a weekend.
Avatar of Aggarwal
Aggarwal


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") ;
}
Avatar of Axter
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.

https://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.
Avatar of CornDog932

ASKER

I didn't say this was for Unix!
My Fault !!

CornDog932 - Sorry for trying to help you !!!
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.
Axter,

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

I didn't see your comment...yes, I tried it and it works.
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.
you mean that call to
localtime(&a);
is too easy for you? or you want to write your own fucntion for that?
>>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.
Oops, I am also sorry to posting some comments here
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;
}
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;
}
>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 :)
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...
>Shame it wasn't checked *before* the answer was posted

100% agree
How can we check it ?
How can we check it ?
How can we check it ?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
somehow that question is really strange. an algorithm was asked, than answer with working code was rejected and answer with another code was accepted.
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.

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