Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <Windows.h>
- using namespace std;
- class Date {
- unsigned day;
- unsigned month;
- unsigned year;
- unsigned hour;
- unsigned minute;
- unsigned second;
- unsigned millisecond;
- public:
- Date (): day(0), month(0), year(0), hour(0), minute(0), second(0), millisecond(0) {}
- Date (unsigned da, unsigned mo, unsigned ye, unsigned ho, unsigned mi, unsigned se, unsigned mil):
- day(da), month(mo), year(ye), hour(ho), minute(mi), second(se), millisecond(mil) {}
- Date now() {
- SYSTEMTIME st;
- GetSystemTime(&st);
- day=st.wDay;
- month=st.wMonth;
- year=st.wYear;
- hour=st.wHour;
- minute=st.wMinute;
- second=st.wSecond;
- millisecond=st.wMilliseconds;
- return *this;
- }
- friend ostream & operator << (ostream & os, const Date & d) {
- if (d.day<10) cout<<"0";
- cout<<d.day<<"/";
- if (d.month<10) cout<<"0";
- cout<<d.month<<"/";
- cout<<d.year<<" ";
- if (d.hour<10) cout<<"0";
- cout<<d.hour<<":";
- if (d.minute<10) cout<<"0";
- cout<<d.minute<<":";
- if (d.second<10) cout<<"0";
- cout<<d.second<<"."<<d.millisecond;
- return os;
- }
- friend istream & operator >> (istream & is, Date & d) {
- cout<<"Enter day: ";
- is>>d.day;
- cout<<"Enter month: ";
- is>>d.month;
- cout<<"Enter year: ";
- is>>d.year;
- cout<<"Enter hour: ";
- is>>d.hour;
- cout<<"Enter minute: ";
- is>>d.minute;
- cout<<"Enter second: ";
- is>>d.second;
- cout<<"Enter millisecond: ";
- is>>d.millisecond;
- return is;
- }
- int operator - (const Date & d) {
- int res=0;
- res+=this->day-d.day;
- res+=(this->month-d.month)*30;
- res+=(this->year-d.year)*365;
- return res;
- }
- void print_time() const {
- if (hour<10) cout<<"0";
- cout<<hour<<":";
- if (minute<10) cout<<"0";
- cout<<minute<<":";
- if (second<10) cout<<"0";
- cout<<second<<"."<<millisecond<<endl;
- }
- void get_date(string s="") {
- if (s=="") cin>>s;
- unsigned n=s.length();
- unsigned i=0;
- while (i<n) {
- switch (s[i]) {
- case 'd':
- case 'D': {
- if (day<10) cout<<"0";
- cout<<day;
- i+=2;
- } break;
- case 'M': {
- switch (month) {
- case 1: cout<<"Jan"; break;
- case 2: cout<<"Feb"; break;
- case 3: cout<<"Mar"; break;
- case 4: cout<<"Apr"; break;
- case 5: cout<<"May"; break;
- case 6: cout<<"Jun"; break;
- case 7: cout<<"Jul"; break;
- case 8: cout<<"Aug"; break;
- case 9: cout<<"Sep"; break;
- case 10: cout<<"Oct"; break;
- case 11: cout<<"Nov"; break;
- case 12: cout<<"Dec"; break;
- default: break;
- }
- i+=2;
- } break;
- case 'm': {
- if (month<10) cout<<"0";
- cout<<month;
- i+=2;
- } break;
- case 'y':
- case 'Y': {
- if ((s[i+2]=='Y')||(s[i+2]=='y')) {
- cout<<year;
- i+=4;
- }
- else {
- double a1=year;
- double a2=100;
- double a3=(int)(a1/100);
- cout<<(a1/a2-a3)*100;
- i+=2;
- }
- } break;
- case '.': {
- cout<<".";
- i+=1;
- } break;
- case '/': {
- cout<<"/";
- i+=1;
- } break;
- case '-': {
- cout<<"-";
- i+=1;
- } break;
- case ' ': {
- cout<<" ";
- i+=1;
- } break;
- default: i+=1; break;
- }
- }
- cout<<endl;
- }
- void get_time(string s="") {
- if (s=="") cin>>s;
- unsigned n=s.length();
- unsigned i=0;
- bool ampm=false;
- while (i<n) {
- switch (s[i]) {
- case 'h': {
- if (hour>12) {
- if ((hour-12)<10) cout<<"0";
- cout<<hour-12;
- }
- else {
- if (hour<10) cout<<"0";
- cout<<hour;
- }
- ampm=true;
- i+=2;
- } break;
- case 'H': {
- if (hour<10) cout<<"0";
- cout<<hour;
- i+=2;
- } break;
- case 'm':
- case 'M': {
- if (minute<10) cout<<"0";
- cout<<minute;
- i+=2;
- } break;
- case 's':
- case 'S': {
- if (second<10) cout<<"0";
- cout<<second;
- i+=2;
- } break;
- default: break;
- }
- if (i<n) cout<<":";
- }
- if ((ampm==true)&&(hour<12)) cout<<" AM";
- else if ((ampm==true)&&(hour>=12)) cout<<" PM";
- cout<<endl;
- }
- unsigned get_day() const {return day;}
- unsigned get_month() const {return month;}
- unsigned get_year() const {return year;}
- unsigned get_hour() const {return hour;}
- unsigned get_minute() const {return minute;}
- unsigned get_second() const {return second;}
- unsigned get_millisecond() const {return millisecond;}
- void set_day(unsigned d) {day=d;}
- void set_month(unsigned m) {month=m;}
- void set_year(unsigned y) {year=y;}
- void set_hour(unsigned h) {hour=h;}
- void set_minute(unsigned m) {minute=m;}
- void set_second(unsigned s) {second=s;}
- void set_millisecond(unsigned m) {millisecond=m;}
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement