home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / cppk632.exe / Date.cp_ / Date.cp
Encoding:
Text File  |  1998-03-13  |  3.6 KB  |  164 lines

  1. /* EasyCODE(C++) V5.1 01.03.1995 08:03:12
  2. EasyCODE(C++) sample: Write current date, date of yesterday and of tomorrow. */
  3. /* EasyCODE O
  4. If=horizontal
  5. LevelNumbers=no
  6. LineNumbers=no
  7. ScreenFont=Courier,,100,9217,-13,0,400,0,0,0,0,0,0,1,2,1,49
  8. PrinterFont=Courier,,100,2,-42,0,400,0,0,0,0,0,0,2,1,2,49
  9. LastLevelId=9 */
  10.  
  11. /* EasyCODE ( 1
  12.    Date-Class Application */
  13. #include <iostream.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16.  
  17. /* EasyCODE ( 2
  18.    Date */
  19.  
  20. /* EasyCODE C */
  21. class Date
  22.    {
  23.       short       day;             
  24.       short       month;          
  25.       static char *nameofmonth[];   
  26.       short       year;            
  27.       void        check();
  28.    public:
  29.       void set (short day=-1, short mon=-1, short year=-1);
  30.  
  31.       /* EasyCODE ( 3
  32.          get_day */
  33.  
  34.       /* EasyCODE F */
  35.       short get_day()
  36.          {
  37.          return day;
  38.          }
  39.       /* EasyCODE ) */
  40.  
  41.       /* EasyCODE ( 4
  42.          get_month */
  43.  
  44.       /* EasyCODE F */
  45.       const char *get_month()
  46.          {
  47.          return nameofmonth[month-1];
  48.          }
  49.       /* EasyCODE ) */
  50.  
  51.       /* EasyCODE ( 5
  52.          get_year */
  53.  
  54.       /* EasyCODE F */
  55.       short get_year()
  56.          {
  57.          return year;
  58.          }
  59.       /* EasyCODE ) */
  60.       void print();
  61.    };
  62. /* EasyCODE E */
  63. /* EasyCODE ) */
  64. /* EasyCODE < */
  65. // define and initialize global class variable
  66. char* Date::nameofmonth[] = {"January",
  67.                              "February",
  68.                              "March",
  69.                              "April",
  70.                              "May",
  71.                              "June",
  72.                              "July",
  73.                              "August",
  74.                              "September",
  75.                              "October",
  76.                              "November",
  77.                              "December"};
  78. /* EasyCODE > */
  79.  
  80. /* EasyCODE ( 6
  81.    Date::set */
  82.  
  83. /* EasyCODE F */
  84. void Date::set(short d, short m, short y)
  85.    {
  86.    time_t seconds = time((time_t *)0);
  87.    
  88.    struct tm *ptm = localtime(&seconds);
  89.    
  90.    year = (y > 0) ? y : ptm->tm_year + 1900;
  91.    month = (m > -1) ? m : ptm->tm_mon + 1;
  92.    day = (d > -1) ? d : ptm->tm_mday;
  93.    
  94.    check();
  95.    }
  96. /* EasyCODE ) */
  97.  
  98. /* EasyCODE ( 7
  99.    Date::print */
  100.  
  101. /* EasyCODE F */
  102. void Date::print()
  103.    {
  104.    cout << day << ". ";
  105.    cout << nameofmonth[month-1] << " ";
  106.    cout << year << "\n";
  107.    }
  108. /* EasyCODE ) */
  109.  
  110. /* EasyCODE ( 8
  111.    Date::check */
  112.  
  113. /* EasyCODE F */
  114. void Date::check()    // check year, month and day and correct if necessary'
  115.    {
  116. /* EasyCODE < */
  117.    static struct tm tdata = {0};
  118.    
  119.    // write year, month and day into structure
  120.    tdata.tm_year = year - 1900;
  121.    tdata.tm_mon  = month - 1;
  122.    tdata.tm_mday = day;
  123.    tdata.tm_hour = 12;  // noon
  124.    
  125.    // check date, calculate day of the week
  126.    time_t seconds = mktime (&tdata);
  127. /* EasyCODE > */
  128.    if (seconds > -1)
  129.       {
  130.       year  = tdata.tm_year + 1900;
  131.       month = tdata.tm_mon + 1;
  132.       day   = tdata.tm_mday;
  133.       }
  134.    }
  135. /* EasyCODE ) */
  136.  
  137. /* EasyCODE ( 9
  138.    Main program */
  139.  
  140. /* EasyCODE F */
  141. void main()
  142.    {
  143.    Date current_date, date_yesterday, date_tomorrow;
  144.    
  145.    // set current date
  146.    current_date.set();    
  147.    
  148.    // set yesterday's date
  149.    date_yesterday.set(current_date.get_day() - 1);
  150.    
  151.    // set tomorrow's date
  152.    date_tomorrow.set(current_date.get_day() + 1);
  153.    
  154.    
  155.    cout << "Today:  \d";
  156.    current_date.print();
  157.    cout << "Yesterday:\d";
  158.    date_yesterday.print();
  159.    cout << "Tomorrow: \d";
  160.    date_tomorrow.print();
  161.    }
  162. /* EasyCODE ) */
  163. /* EasyCODE ) */
  164.