home *** CD-ROM | disk | FTP | other *** search
- /*
- * Example 1: classes RWTime and RWDate
- *
- * $Header: E:/vcs/toolexam/example1.cpv 1.1 01 Apr 1992 16:51:10 keffer $
- *
- ****************************************************************************
- *
- * Rogue Wave Software, Inc.
- * P.O. Box 2328
- * Corvallis, OR 97339
- * Voice: (503) 754-2311 FAX: (503) 757-7350
- *
- * Copyright (C) 1989, 1990, 1991. This software is subject to copyright
- * protection under the laws of the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/toolexam/example1.cpv $
- *
- * Rev 1.1 01 Apr 1992 16:51:10 keffer
- * Now includes <rw/xxx.h>
- *
- */
-
- // Include the header files for the classes RWDate and RWTime:
- #include <rw/rwdate.h>
- #include <rw/rwtime.h>
- #include <rw/rstream.h>
-
- #ifdef __TURBOC__
- # include <dos.h>
- #else
- # ifdef __ZTC__
- # include <time.h>
- # else
- # if defined(MSC_BACKEND) || defined(__HIGHC__) /* These compilers do not have sleep() */
- STARTWRAP
- # include <time.h>
- ENDWRAP
- # else
- STARTWRAP
- # include <libc.h>
- ENDWRAP
- # endif
- # endif
- #endif
-
- const int sleepTime = 5;
-
- main()
- {
-
- /**************************************************************/
- /* RWDate */
- /**************************************************************/
-
- // Construct a RWDate with the current date.
- RWDate d1;
-
- cout << "Print the date using various formats.\n";
-
- cout << "First, the default:\n";
- cout << d1 << "\n\n";
-
- cout << "Now using \"terse\":\n";
- d1.setPrintOption(RWDate::terse);
- cout << d1 << "\n\n";
-
- cout << "Now using \"numbers\":\n";
- d1.setPrintOption(RWDate::numbers);
- cout << d1 << "\n\n";
-
- cout << "Now using \"europeanNumbers\":\n";
- d1.setPrintOption(RWDate::europeanNumbers);
- cout << d1 << "\n\n";
-
- cout << "Now using \"european\":\n";
- d1.setPrintOption(RWDate::european);
- cout << d1 << NL;
-
- // reset to normal:
- d1.setPrintOption(RWDate::normal);
-
- cout << "\nNow print some information about the date:\n";
- cout << "Day of the year: " << d1.day() << NL;
- cout << "Day of the month: " << d1.dayOfMonth() << NL;
- cout << "Name of the month: " << d1.nameOfMonth() << NL;
- cout << "The year: " << d1.year() << NL;
-
- cout << "Construct the date 4/12/1842...";
- RWDate d2(12, "April", 1842);
- cout << "done. Print it out:\n";
- cout << d2 << NL;
-
- // Was this year a leap year?
- if ( d2.leap() )
- cout << "A leap year!\n";
- else
- cout << "Not a leap year.\n";
-
- // Add some days to a RWDate:
- cout << "Add 37 days to this date, then print again:\n";
- d2 += 37;
- cout << d2 << NL;
-
- RWDate inputDate;
-
- while(1) {
- cout << "Enter a date in one of the following formats:\n";
- cout << " dd-mmm-yy (e.g. 10-MAR-86);\n";
- cout << " mm/dd/yy (e.g. 3/10/86);\n";
- cout << " mmm dd, yyyy (e.g. March 10, 1986).\n";
- cout << "Invalid dates will be flagged.\nUse EOF to terminate (^Z on the PC; usually ^D on Unix).\n";
-
- cin.clear(); // Clear any corrupt dates
- cin >> inputDate; // Get the date
-
- if( cin.eof() ) break; // Check for EOF
-
- if( inputDate.isValid() )
- cout << "You entered:\t" << inputDate << NL;
- else {
- cout << "Not a valid date.\n";
- }
- }
-
- /**************************************************************/
- /* RWTime */
- /**************************************************************/
-
- cout << "\nWell, that was fun. Now let's exercise RWTime.\n\n";
- cout << "Construct a RWTime with the current time...";
- RWTime t1;
-
- cout << "done. Now print it out.\n";
- cout << t1 << NL;
-
- cout << "Convert to a date, then print it out:\n";
- cout << RWDate(t1) << NL;
-
- cout << "Return some information about the time:\n";
- cout << "The hour is " << t1.hour() << NL;
- cout << "The hour (GMT) is " << t1.hourGMT() << NL;
- cout << "The minute is: " << t1.minute() << NL;
- cout << "The second is: " << t1.second() << NL;
-
- cout << "Hang on while I go to sleep for a few seconds...";
- cout.flush();
-
- #if defined(MSC_BACKEND) || defined(__HIGHC__)
- time_t start, end;
- time(&start);
- do {
- time(&end);
- } while ( end - start < (time_t)sleepTime ) ;
- #else
- sleep(sleepTime);
- #endif
-
- cout << "back again.\n\nNow construct a new time.\n";
-
- // Construct new RWTime with current time:
- RWTime t2;
-
- // Print it out:
- cout << t2 << NL;
-
- return 0;
- }
-