Solved

Determine date is weekday or weekend

Posted on 2001-08-06
27
491 Views
Last Modified: 2011-10-03
I need the algorithm to determine if a date is weekday or a weekend.
0
Comment
Question by:CornDog932
  • 7
  • 5
  • 5
  • +4
27 Comments
 
LVL 1

Expert Comment

by:Aggarwal
Comment Utility

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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
 

Author Comment

by:CornDog932
Comment Utility
I didn't say this was for Unix!
0
 
LVL 1

Expert Comment

by:Aggarwal
Comment Utility
My Fault !!

CornDog932 - Sorry for trying to help you !!!
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
 
LVL 1

Expert Comment

by:Aggarwal
Comment Utility
Axter,

Agreed ..my Fault !!!
0
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
Actually code looks OK, did someone tried it? It sould work under windows too.
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
This is a little bit of a harsh rejection, this code works perfectly well on Windows too...
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
> Actually code looks OK

I didn't see your comment...yes, I tried it and it works.
0
 

Author Comment

by:CornDog932
Comment Utility
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
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
you mean that call to
localtime(&a);
is too easy for you? or you want to write your own fucntion for that?
0
 

Author Comment

by:CornDog932
Comment Utility
>>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
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
Oops, I am also sorry to posting some comments here
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Expert Comment

by:olgat
Comment Utility
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
 
LVL 1

Expert Comment

by:olgat
Comment Utility
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
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
>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
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
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
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
>Shame it wasn't checked *before* the answer was posted

100% agree
0
 
LVL 1

Expert Comment

by:olgat
Comment Utility
How can we check it ?
0
 
LVL 1

Expert Comment

by:olgat
Comment Utility
How can we check it ?
0
 
LVL 1

Expert Comment

by:olgat
Comment Utility
How can we check it ?
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
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
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
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
 

Author Comment

by:CornDog932
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
 
LVL 6

Expert Comment

by:MichaelS
Comment Utility
>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
 

Author Comment

by:CornDog932
Comment Utility
>>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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now