Advertisement
poohitan

date.h

Feb 21st, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <Windows.h>
  4. using namespace std;
  5. class Date {
  6.     unsigned day;
  7.     unsigned month;
  8.     unsigned year;
  9.     unsigned hour;
  10.     unsigned minute;
  11.     unsigned second;
  12.     unsigned millisecond;
  13. public:
  14.     Date (): day(0), month(0), year(0), hour(0), minute(0), second(0), millisecond(0) {}
  15.     Date (unsigned da, unsigned mo, unsigned ye, unsigned ho, unsigned mi, unsigned se, unsigned mil):
  16.     day(da), month(mo), year(ye), hour(ho), minute(mi), second(se), millisecond(mil) {}
  17.     Date now() {
  18.         SYSTEMTIME st;
  19.         GetSystemTime(&st);
  20.         day=st.wDay;
  21.         month=st.wMonth;
  22.         year=st.wYear;
  23.         hour=st.wHour;
  24.         minute=st.wMinute;
  25.         second=st.wSecond;
  26.         millisecond=st.wMilliseconds;
  27.         return *this;
  28.     }
  29.     friend ostream & operator << (ostream & os, const Date & d) {
  30.         if (d.day<10) cout<<"0";
  31.         cout<<d.day<<"/";
  32.         if (d.month<10) cout<<"0";
  33.         cout<<d.month<<"/";
  34.         cout<<d.year<<" ";
  35.         if (d.hour<10) cout<<"0";
  36.         cout<<d.hour<<":";
  37.         if (d.minute<10) cout<<"0";
  38.         cout<<d.minute<<":";
  39.         if (d.second<10) cout<<"0";
  40.         cout<<d.second<<"."<<d.millisecond;
  41.         return os;
  42.     }
  43.     friend istream & operator >> (istream & is, Date & d) {
  44.         cout<<"Enter day: ";
  45.         is>>d.day;
  46.         cout<<"Enter month: ";
  47.         is>>d.month;
  48.         cout<<"Enter year: ";
  49.         is>>d.year;
  50.         cout<<"Enter hour: ";
  51.         is>>d.hour;
  52.         cout<<"Enter minute: ";
  53.         is>>d.minute;
  54.         cout<<"Enter second: ";
  55.         is>>d.second;
  56.         cout<<"Enter millisecond: ";
  57.         is>>d.millisecond;
  58.         return is;
  59.     }
  60.     int operator - (const Date & d) {
  61.         int res=0;
  62.         res+=this->day-d.day;
  63.         res+=(this->month-d.month)*30;
  64.         res+=(this->year-d.year)*365;
  65.         return res;
  66.     }
  67.     void print_time() const {
  68.         if (hour<10) cout<<"0";
  69.         cout<<hour<<":";
  70.         if (minute<10) cout<<"0";
  71.         cout<<minute<<":";
  72.         if (second<10) cout<<"0";
  73.         cout<<second<<"."<<millisecond<<endl;
  74.     }
  75.     void get_date(string s="") {
  76.         if (s=="") cin>>s;
  77.         unsigned n=s.length();
  78.         unsigned i=0;
  79.         while (i<n) {
  80.             switch (s[i]) {
  81.                 case 'd':
  82.                 case 'D': {
  83.                     if (day<10) cout<<"0";
  84.                     cout<<day;
  85.                     i+=2;
  86.                 } break;
  87.                 case 'M': {
  88.                     switch (month) {
  89.                     case 1: cout<<"Jan"; break;
  90.                     case 2: cout<<"Feb"; break;
  91.                     case 3: cout<<"Mar"; break;
  92.                     case 4: cout<<"Apr"; break;
  93.                     case 5: cout<<"May"; break;
  94.                     case 6: cout<<"Jun"; break;
  95.                     case 7: cout<<"Jul"; break;
  96.                     case 8: cout<<"Aug"; break;
  97.                     case 9: cout<<"Sep"; break;
  98.                     case 10: cout<<"Oct"; break;
  99.                     case 11: cout<<"Nov"; break;
  100.                     case 12: cout<<"Dec"; break;
  101.                     default: break;
  102.                     }
  103.                     i+=2;
  104.                 } break;
  105.                 case 'm': {
  106.                     if (month<10) cout<<"0";
  107.                     cout<<month;
  108.                     i+=2;
  109.                 } break;
  110.                 case 'y':
  111.                 case 'Y': {
  112.                     if ((s[i+2]=='Y')||(s[i+2]=='y')) {
  113.                         cout<<year;
  114.                         i+=4;
  115.                     }
  116.                     else {
  117.                         double a1=year;
  118.                         double a2=100;
  119.                         double a3=(int)(a1/100);
  120.                         cout<<(a1/a2-a3)*100;
  121.                         i+=2;
  122.                     }
  123.                 } break;
  124.                 case '.': {
  125.                     cout<<".";
  126.                     i+=1;
  127.                 } break;
  128.                 case '/': {
  129.                     cout<<"/";
  130.                     i+=1;
  131.                 } break;
  132.                 case '-': {
  133.                     cout<<"-";
  134.                     i+=1;
  135.                 } break;
  136.                 case ' ': {
  137.                     cout<<" ";
  138.                     i+=1;
  139.                 } break;
  140.                 default: i+=1; break;
  141.             }
  142.         }
  143.         cout<<endl;
  144.     }
  145.     void get_time(string s="") {
  146.         if (s=="") cin>>s;
  147.         unsigned n=s.length();
  148.         unsigned i=0;
  149.         bool ampm=false;
  150.         while (i<n) {
  151.             switch (s[i]) {
  152.                 case 'h': {
  153.                     if (hour>12) {
  154.                         if ((hour-12)<10) cout<<"0";
  155.                         cout<<hour-12;
  156.                     }
  157.                     else {
  158.                         if (hour<10) cout<<"0";
  159.                         cout<<hour;
  160.                     }
  161.                     ampm=true;
  162.                     i+=2;
  163.                 } break;
  164.                 case 'H': {
  165.                     if (hour<10) cout<<"0";
  166.                     cout<<hour;
  167.                     i+=2;
  168.                 } break;
  169.                 case 'm':
  170.                 case 'M': {
  171.                     if (minute<10) cout<<"0";
  172.                     cout<<minute;
  173.                     i+=2;
  174.                 } break;
  175.                 case 's':
  176.                 case 'S': {
  177.                     if (second<10) cout<<"0";
  178.                     cout<<second;
  179.                     i+=2;
  180.                 } break;
  181.                 default: break;
  182.             }
  183.             if (i<n) cout<<":";
  184.         }
  185.         if ((ampm==true)&&(hour<12)) cout<<" AM";
  186.             else if ((ampm==true)&&(hour>=12)) cout<<" PM";
  187.             cout<<endl;
  188.     }
  189.     unsigned get_day() const {return day;}
  190.     unsigned get_month() const {return month;}
  191.     unsigned get_year() const {return year;}
  192.     unsigned get_hour() const {return hour;}
  193.     unsigned get_minute() const {return minute;}
  194.     unsigned get_second() const {return second;}
  195.     unsigned get_millisecond() const {return millisecond;}
  196.     void set_day(unsigned d) {day=d;}
  197.     void set_month(unsigned m) {month=m;}
  198.     void set_year(unsigned y) {year=y;}
  199.     void set_hour(unsigned h) {hour=h;}
  200.     void set_minute(unsigned m) {minute=m;}
  201.     void set_second(unsigned s) {second=s;}
  202.     void set_millisecond(unsigned m) {millisecond=m;}
  203. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement