home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / 4 < prev    next >
Encoding:
Text File  |  1992-04-07  |  4.6 KB  |  170 lines

  1. /*
  2.  * Example 1: classes RWTime and RWDate 
  3.  *
  4.  * $Header:   E:/vcs/toolexam/example1.cpv   1.1   01 Apr 1992 16:51:10   keffer  $
  5.  *
  6.  ****************************************************************************
  7.  *
  8.  * Rogue Wave Software, Inc.
  9.  * P.O. Box 2328
  10.  * Corvallis, OR 97339
  11.  * Voice: (503) 754-2311    FAX: (503) 757-7350
  12.  *
  13.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  14.  * protection under the laws of the United States and other countries.
  15.  *
  16.  ***************************************************************************
  17.  *
  18.  * $Log:   E:/vcs/toolexam/example1.cpv  $
  19.  * 
  20.  *    Rev 1.1   01 Apr 1992 16:51:10   keffer
  21.  * Now includes <rw/xxx.h>
  22.  * 
  23.  */
  24.  
  25. // Include the header files for the classes RWDate and RWTime:
  26. #include <rw/rwdate.h>
  27. #include <rw/rwtime.h>
  28. #include <rw/rstream.h>
  29.  
  30. #ifdef __TURBOC__
  31. # include <dos.h>
  32. #else
  33. #  ifdef __ZTC__
  34. #    include <time.h>
  35. #  else
  36. #    if defined(MSC_BACKEND) || defined(__HIGHC__) /* These compilers do not have sleep() */
  37.        STARTWRAP
  38. #      include <time.h>
  39.        ENDWRAP
  40. #    else
  41.        STARTWRAP
  42. #      include <libc.h>
  43.        ENDWRAP
  44. #    endif
  45. #  endif
  46. #endif
  47.  
  48. const int sleepTime = 5;
  49.  
  50. main()
  51. {
  52.  
  53.   /**************************************************************/
  54.   /*                       RWDate                               */
  55.   /**************************************************************/
  56.   
  57.   // Construct a RWDate with the current date.
  58.   RWDate d1;
  59.  
  60.   cout << "Print the date using various formats.\n";
  61.  
  62.   cout << "First, the default:\n";
  63.   cout << d1 << "\n\n";
  64.  
  65.   cout << "Now using \"terse\":\n";
  66.   d1.setPrintOption(RWDate::terse); 
  67.   cout << d1 << "\n\n";
  68.  
  69.   cout << "Now using \"numbers\":\n";
  70.   d1.setPrintOption(RWDate::numbers); 
  71.   cout << d1 << "\n\n";
  72.  
  73.   cout << "Now using \"europeanNumbers\":\n";
  74.   d1.setPrintOption(RWDate::europeanNumbers); 
  75.   cout << d1 << "\n\n";
  76.  
  77.   cout << "Now using \"european\":\n";
  78.   d1.setPrintOption(RWDate::european); 
  79.   cout << d1 << NL;
  80.  
  81.   // reset to normal:
  82.   d1.setPrintOption(RWDate::normal); 
  83.  
  84.   cout << "\nNow print some information about the date:\n";
  85.   cout << "Day of the year:   " << d1.day() << NL;
  86.   cout << "Day of the month:  " << d1.dayOfMonth() << NL;
  87.   cout << "Name of the month: " << d1.nameOfMonth() << NL;
  88.   cout << "The year:          " << d1.year() << NL;
  89.   
  90.   cout << "Construct the date 4/12/1842...";
  91.   RWDate d2(12, "April", 1842);
  92.   cout << "done.  Print it out:\n";
  93.   cout << d2 << NL;
  94.  
  95.   // Was this year a leap year?
  96.   if ( d2.leap() ) 
  97.         cout << "A leap year!\n";
  98.   else
  99.         cout << "Not a leap year.\n";
  100.   
  101.   // Add some days to a RWDate:
  102.   cout << "Add 37 days to this date, then print again:\n";
  103.   d2 += 37;
  104.   cout << d2 << NL;
  105.    
  106.   RWDate inputDate;
  107.  
  108.   while(1) {
  109.     cout << "Enter a date in one of the following formats:\n";
  110.     cout << "  dd-mmm-yy    (e.g. 10-MAR-86);\n";
  111.     cout << "  mm/dd/yy     (e.g. 3/10/86);\n";
  112.     cout << "  mmm dd, yyyy (e.g. March 10, 1986).\n";
  113.     cout << "Invalid dates will be flagged.\nUse EOF to terminate (^Z on the PC; usually ^D on Unix).\n";
  114.  
  115.     cin.clear();        // Clear any corrupt dates
  116.     cin >> inputDate;        // Get the date
  117.  
  118.     if( cin.eof() ) break;    // Check for EOF
  119.  
  120.     if( inputDate.isValid() )
  121.       cout << "You entered:\t" << inputDate << NL;
  122.     else {
  123.       cout << "Not a valid date.\n";
  124.     }
  125.   }
  126.  
  127.   /**************************************************************/
  128.   /*                       RWTime                               */
  129.   /**************************************************************/
  130.  
  131.   cout << "\nWell, that was fun.  Now let's exercise RWTime.\n\n";
  132.   cout << "Construct a RWTime with the current time...";
  133.   RWTime t1;
  134.  
  135.   cout << "done.  Now print it out.\n";
  136.   cout << t1 << NL;
  137.  
  138.   cout << "Convert to a date, then print it out:\n";
  139.   cout << RWDate(t1) << NL;
  140.  
  141.   cout << "Return some information about the time:\n";
  142.   cout << "The hour is       " << t1.hour()    << NL;
  143.   cout << "The hour (GMT) is " << t1.hourGMT() << NL;
  144.   cout << "The minute is:    " << t1.minute()  << NL;
  145.   cout << "The second is:    " << t1.second()  << NL;
  146.  
  147.   cout << "Hang on while I go to sleep for a few seconds...";
  148.   cout.flush();
  149.  
  150. #if defined(MSC_BACKEND) || defined(__HIGHC__)
  151.   time_t start, end;
  152.   time(&start);
  153.   do {
  154.     time(&end);
  155.   } while ( end - start < (time_t)sleepTime ) ;
  156. #else
  157.   sleep(sleepTime);
  158. #endif
  159.  
  160.   cout << "back again.\n\nNow construct a new time.\n";
  161.  
  162.   // Construct new RWTime with current time:
  163.   RWTime t2;
  164.  
  165.   // Print it out:
  166.   cout << t2 << NL;
  167.  
  168.   return 0;
  169. }
  170.