Thursday, March 20, 2014

C++ Program to Find Week Number of a Given Date

The program gives the week number of a Given Date Where Weeks are ISO weeks Starts on Sunday and Ends on Saturday

#include<windows.h>
#include<time.h>
#include<iostream>
#include<conio.h>
#include<tchar.h>
#include <string>
#include<atltime.h>
#include<tchar.h>
using namespace std;
#define SECONDS_IN_A_DAY 86400
#define ConfigIntervalDays 7
int dayofweek(int d, int m, int y);
int getMonthEndDay(int Month,int Year);
bool checkIfLeapYear(int Year);
int WeekofYearFinder(int Day,int Month,int Year);
enum DayOfWeek {Sun=0,Mon,Tue,Wed,Thr,Fri,Sat};
void main()
{
time_t tmNow = time(NULL);
    CTime tmCurrent(tmNow);      
     
      CTime         tmStartOfDay(tmCurrent.GetYear(),tmCurrent.GetMonth(),tmCurrent.GetDay(),1,0,0);
      CTime nPrevEndTime = tmStartOfDay.GetTime();
      CTime nPrevStartTime = tmStartOfDay.GetTime() - ConfigIntervalDays*SECONDS_IN_A_DAY;
 int Year,Month,Day;
      Year=nPrevStartTime.GetYear();
      Month=nPrevStartTime.GetMonth();
      Day=nPrevStartTime.GetDay();
 int i=WeekofYearFinder(Day,Month,Year);
Year= nPrevEndTime.GetYear();
Month=nPrevEndTime.GetMonth();
     Day=nPrevEndTime.GetDay();
 int j=WeekofYearFinder(Day,Month,Year);
 if(i==j)
 {
 cout<<"\n Both Start Time and End Time Belongs to same week";
 cout<<"\n Week:"<<i;
 }
 else
 {
          cout<<"\n Start Time and End Time Belongs to different weeks";
 cout<<"\n Start Time Week:"<<i;
 cout<<"\n End Time Week:"<<j;
 }
 getch();
}

int dayofweek(int d, int m, int y)
{
    static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    y -= m < 3;
    return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}

int getMonthEndDay(int Month,int Year)
{
   int daysInMonth=0;
   switch(Month)
   {
   case  2:
        daysInMonth = checkIfLeapYear(Year) ? 29 : 28;
        break;
   case 4:
   case 6:
   case 9:
   case 11:
         daysInMonth = 30;
         break;
   default:
          daysInMonth = 31;
          break;
   }
  return daysInMonth;
}

bool checkIfLeapYear(int Year)
{
return (!(Year % 4) && (Year % 100)) || !(Year % 400);
}

int WeekofYearFinder(int Day,int Month,int Year)
{
  if(Day<1|| Day>31 ||Month<1||Month>12||Year<1000)
  {
return 0;
  }
   int wofy=0;
   bool breakFlag=false;
   int EndDay;
   int PrevMonthEnd=0;
   int PrevMonthEndDay=0;
   for(int j=1;j<=12;j++)
   {
     EndDay=getMonthEndDay(j,Year);//Get the Day number which Month Ends Eg:31/MM/YYYY
if(j>1)
{
PrevMonthEnd=dayofweek(PrevMonthEndDay,j-1,Year);//Get the Day number which Previous Month Ends Eg:30/MM/YYYY
}
for(int i=1;i<=EndDay;i++)
{
       if(i==1)//check if day is 1/MM/YYYY
  {
  if((dayofweek(i,j,Year)>Sun && !(PrevMonthEnd<=Sat))||j==1)//Check if  Day 1 > sunday and Previous Month not Ends in Sunday
  {
             wofy++;
  }
  else if(dayofweek(i,j,Year)==Sun)//Check if Day 1 is Sunday
      {
           wofy++;
      }
  }
  else if(dayofweek(i,j,Year)==Sun)//Check Which are the other days other than Day 1 falls on Sunday
  {
           wofy++;
  }

      if(i==Day && j==Month)//If Iteration matches to Given Date exits entire loop
 {
 breakFlag=true;
 break;
 }
}
if(breakFlag)
{
break;
}
PrevMonthEndDay=getMonthEndDay(j,Year);
//Sets the Previous Month's End Date Day Number Eg: 30/MM/YYY
   }
return wofy;
}

No comments:

Post a Comment