Start New Discussion within our Software Development Community

Dear my friends
This code take base of time details as second , minute and hour .
it also take base of date details as day , month and year .
Then it take length of time and date details .
Finaly these information is process by program and it shows result.
It is not very much accurate , because some of month has 31 or 29
days!!!
I wrote it by Borland 5.02 .

Good Luck!

//Time & Date computing
//programming by : Erfan Nasoori
//Mail : ketn68@yahoo.com
//Date of send 2009/1/12

#include<iostream.h>
#include<conio.h>

typedef int date;     // global type define

	class Date{

                    private:
			        date b_day;
				date b_mnt;
				date b_year;
				date day;
				date mnt;
				date year;

				date b_hor;
				date b_min;
				date b_sec;
				date hor;
				date min;
				date sec;

                    public:

                            void set_getdate();

		  };



void Date :: set_getdate()
{
 Date d;

 d.hor = d.min = d.sec = d.day = d.mnt = d.year = 0;

  cout<<"Enter base of second(from 0_59) : ";
  cin>>d.b_sec;

 while(d.b_sec < 0  ||  d.b_sec > 59)
 {
	cout<<"Its incorrect!\n"
	    <<"Try again(from 1_59): ";
	cin>>d.b_sec;
 }


  cout<<"Enter base of minute(from 0_59) : ";
  cin>>d.b_min;

 while(d.b_min < 0  ||  d.b_min > 59)
 {
	cout<<"Its incorrect!\n"
	    <<"Try again(from 0_59): ";
	cin>>d.b_min;
 }


  cout<<"Enter base of hour(from 0_23) : ";
  cin>>d.b_hor;

 while(d.b_hor < 0  ||  d.b_hor > 23)
 {
	cout<<"Its incorrect!\n"
	    <<"Try again(from 0_24): ";
	cin>>d.b_hor;
 }



 cout<<"Enter base of day(from 1_30) : ";
 cin>>d.b_day;

 while(d.b_day < 1  ||  d.b_day > 30)
 {
	cout<<"Its incorrect!\n"
	    <<"Try again(from 1_30): ";
	cin>>d.b_day;
 }


 cout<<"Enter base of month(from 1_12) : ";
 cin>>d.b_mnt;

 while(d.b_mnt < 1  ||  d.b_mnt > 12)
 {
	cout<<"Its incorrect!\n"
	    <<"Try again(from 1_12): ";
	cin>>d.b_mnt;
 }


 cout<<"Enter base of year(from 2000_2999) : ";
 cin>>d.b_year;

 while(d.b_year < 2000  ||  d.b_year > 2999)
 {
	cout<<"Its incorrect!\n"
	    <<"Try again(from 2000_2999): ";
	cin>>d.b_year;
 }

cout<<"\n\n_______________________________________________________________\n";
 Date x;

 cout<<"\n\nEnter length of second : ";
 cin>>x.sec;

 date dy = 0;
 date minute = 0;
 date hour = 0;
 date month = 0;
 date yer = 0;

 if(x.sec < 0  &&  x.sec > -60)
 {
	d.sec += (x.sec + 60);
	--minute;
 }

 else if( x.sec <= -60  &&  x.sec > -3600)
 {
	minute += (x.sec / 60);
	d.sec  += (x.sec % 60)+60;
	--hour;
 }

 else if(x.sec <= -3600  &&  x.sec > -86400)
 {
	hour   += (x.sec / 3600);
	minute += (x.sec % 3600)/60;
	d.sec  += ((x.sec % 3600)%60)+60;
	--dy;
 }

 else if(x.sec <= -86400)
 {
	dy     += (x.sec / 86400);
	hour   += (x.sec % 86400)/3600;
	minute += ((x.sec % 86400)%3600)/60;
	d.sec  += (((x.sec % 86400)%3600)%60)+60;
	--month;
 }

 else if(x.sec >= 0  &&  x.sec < 60)
	d.sec += x.sec;

 else if(x.sec >= 60  &&  x.sec < 3600)
 {
	d.min += (x.sec / 60);
	d.sec += (x.sec % 60);
 }

 else if(x.sec >= 3600  &&  x.sec < 86400)
 {
	d.hor += (x.sec / 3600);
	d.min += (x.sec % 3600)/60;
	d.sec += (x.sec % 3600)%60;
 }

 else
 {
	d.day += (x.sec / 86400);
	d.hor += (x.sec % 86400)/3600;
	d.min += ((x.sec % 86400)%3600)/60;
	d.sec += ((x.sec % 86400)%3600)%60;
 }
/////end of second getting ///////////////////////////

 cout<<"\n\nEnter length of minute : ";
 cin>>x.min;

	x.min += minute;

 if(x.min < 0  &&  x.min > -60)
 {
	d.min += (x.min + 60);
	--hour;
 }

 else if(x.min <= -60  &&  x.min > -1440)
 {
	hour  += (x.min / 60);
	d.min += (x.min % 60)+60;
	--dy;
 }

 else if(x.min <= -1440  &&  x.min > -43200)
 {
	dy    += (x.min / 1440);
	hour  += (x.min % 1440)/60;
	d.min += ((x.min % 1440)%60)+60;
	--month ;
 }

 else if(x.min <= -43200)
 {
	month += (x.min / 43200);
	dy    += (x.min % 43200)/1440;
	hour  += ((x.min % 43200)%1440)/60;
	d.min += (((x.min % 43200)%1440)%60)+60;
	--yer ;
  }

 else if(x.min >=0  && x.min < 60)
	d.min += x.min;

 else if(x.min >= 60  &&  x.min < 1440)
 {
	d.hor += (x.min /60);
	d.min += (x.min % 60);
 }

 else if(x.min >= 1440  &&  x.min < 43200)
 {
	d.day += (x.min / 1440);
	d.hor += (x.min % 1440)/60;
	d.min += ((x.min % 1440)%60);
 }

 else
 {
	d.mnt += (x.min / 43200);
	d.day += (x.min % 43200)/1440;
	d.hor += ((x.min % 43200)%1440)/60;
	d.min += (((x.min % 43200)%1440)%60);
 }
/////end of lenth of minute getting /////////////////////

 cout<<"\n\nEnter length of hour : ";
 cin>>x.hor;

 x.hor += hour;

 if(x.hor < 0  &&  x.hor > -24)
 {
	d.hor += (x.hor + 24);
	--dy;
 }

 else if(x.hor <= -24  &&  x.hor > -720)
 {
	dy    += (x.hor / 24);
	d.hor += (x.hor % 24)+24;
	--month;
 }

 else if(x.hor <= -720  &&  x.hor > -8760)
 {
	month += (x.hor / 720);
	dy    += (x.hor % 720)/24;
	d.hor += ((x.hor % 720)%24)+24;
	--yer;
 }

 else if(x.hor <= -8760)
 {
	yer   += (x.hor / 8760);
	month += (x.hor % 8760)/720;
	dy    += ((x.hor % 8760)%720)/24;
	d.hor += (((x.hor % 8760)%720)%24)+24;
 }

 else if(x.hor >= 0  &&  x.hor < 24)
	d.hor += x.hor;

 else if(x.hor >= 24  &&  x.hor < 720)
 {
	d.day += (x.hor / 24);
	d.hor += (x.hor % 24);
 }

 else if(x.hor >= 720  &&  x.hor < 8760)
 {
	d.mnt += (x.hor / 720);
	d.day += (x.hor % 720)/24;
	d.hor += (x.hor % 720)%24;
 }

 else if(x.hor >= 8760)
 {
	d.year += (x.hor / 8760);
	d.mnt  += (x.hor % 8760)/720;
	d.day  += ((x.hor % 8760)%720)/24;
	d.hor  += (((x.hor % 8760)%720)%24);
 }
/////end of getting length of hour////////////////////////


 cout<<"\nNow enter length of day : ";
 cin>>x.day;

 x.day += dy;

 if(x.day < 0  &&  x.day > -30)
 {
	d.day += (x.day + 30);
	--month;
 }

 else if(x.day <= -30  &&  x.day > -365)
 {
	month += (x.day / 30);
	x.day += (x.day % 30)+30;
	--yer;
 }

 else if(x.day <= -365)
 {
	yer   += (x.day / 365);
	month += (x.day % 365)/30;
	d.day += ((x.day % 365)%30)+30;
 }

 else if(x.day >= 0  &&  x.day <= 30)
	d.day += x.day;

 else if(x.day > 30  &&  x.day < 365)
 {
	d.mnt += (x.day / 30);
	d.day += (x.day % 30);
 }

 else
 {
	d.year += (x.day / 365);
	d.mnt  += (x.day % 365)/30;
	d.day  += (x.day % 365)%30;
 }
 /////end of getting length of day////////////////


 cout<<"\nEnter length of month : ";
 cin>>x.mnt;
 x.mnt += month;

 if(x.mnt < 0  &&  x.mnt > -12)
 {
	d.mnt  += (x.mnt + 12);
	--yer;
 }

 else if(x.mnt < -12)
 {
	yer   += (x.mnt / 12);
	d.mnt += (x.mnt % 12)+12;
 }

 else if(x.mnt >= 0  &&  x.mnt <= 12)
	d.mnt += x.mnt;

 else
 {
	d.year += (x.mnt / 12);
	d.mnt  += (x.mnt % 12);
 }
 /////end of getting length of month///////////////


 cout<<"\nEnter length of year : ";
 cin>>x.year;
 x.year += yer;
 d.year += x.year;
 /////end of getting length of year///////////////


 d.sec += d.b_sec;
 if(d.sec >= 60)
 {
	(d.min += (d.sec / 60));
	(d.sec  = (d.sec % 60));
 }
/////end of second setting////////////////////////

 d.min += d.b_min;
 if(d.min >= 60)
 {
	(d.hor += (d.min / 60));
	(d.min  = (d.min % 60));
 }
/////end of minute setting////////////////////////

 d.hor += d.b_hor;
 if(d.hor >= 24)
 {
	(d.day += (d.hor / 24));
	(d.hor  = (d.hor % 24));
 }
/////end of hour setting/////////////////////////

 d.day += d.b_day;
 if(d.day > 30)
 {
	(d.mnt += (d.day / 30));
	(d.day  = (d.day % 30));
 }
 /////end of day setting/////////////////////////


 d.mnt += d.b_mnt;
 if(d.mnt > 12)
 {
	(d.year += (d.mnt / 12));
	(d.mnt   = (d.mnt % 12));
 }
 /////end of month setting///////////////////////


 d.year += d.b_year;   //end of year setting


 ////Date display//////////////////////////////
  char MONTH[13][12]={"January","February","March","April"
                     ,"May","June","July","August"
                     ,"September","October","November","December"};

 cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
	  <<"\n\nDate is =  "
	  <<d.year
	  <<" / "
	  <<MONTH[d.mnt-1]
	  <<" / "
	  <<d.day
	  <<endl
	  <<"\nTime is =  "
	  <<d.hor
	  <<" : "
	  <<d.min
	  <<" : "
	  <<d.sec
	  <<endl;
}

//////////////////main function////////////////////////////////

void main()
{
 Date x;
 x.set_getdate();
 getch();
}