home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / DATEDEMO.CPP < prev    next >
Text File  |  1997-07-05  |  9KB  |  233 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /*
  4.  * This file is part of PB-Lib C/C++ Library
  5.  *
  6.  * Copyright (c) 1995, 1996 Branislav L. Slantchev
  7.  * A Product of Silicon Creations, Inc.
  8.  *
  9.  * This class is hereby donated to the SNIPPETS collection (maintained
  10.  * by Bob Stout). You are granted the right to use the code contained
  11.  * herein free of charge as long as you keep this copyright notice intact.
  12.  *
  13.  * Contact: 73023.262@compuserve.com
  14. */
  15. #include "date.hpp"
  16. #include <iostream.h>
  17.  
  18. ostream& operator<<(ostream &stream, zDate::moon_phase phase)
  19. {
  20.       switch( phase ){
  21.             case zDate::new_moon: stream << "new"; break;
  22.             case zDate::waxing_crescent: stream << "waxing crescent"; break;
  23.             case zDate::first_quater: stream << "first quater"; break;
  24.             case zDate::waxing_gibbous: stream << "waxing gibbous"; break;
  25.             case zDate::full_moon: stream << "full"; break;
  26.             case zDate::waning_gibbous: stream << "waning gibbous"; break;
  27.             case zDate::third_quater: stream << "third quater"; break;
  28.             case zDate::waning_crescent: stream << "waning crescent"; break;
  29.             default: stream << "--error--"; break;
  30.       }
  31.       return stream;
  32. }
  33.  
  34. ostream& operator<<(ostream &stream, const zDate &date)
  35. {
  36.       stream << date.Day() << "-" << (int)date.Month() << "-" << date.Year();
  37.       return stream;
  38. }
  39.  
  40. ostream& operator<<(ostream &stream, zDate::week_day day)
  41. {
  42.       switch( day ){
  43.             case zDate::sun: stream << "Sunday"; break;
  44.             case zDate::mon: stream << "Monday"; break;
  45.             case zDate::tue: stream << "Tuesday"; break;
  46.             case zDate::wed: stream << "Wednesday"; break;
  47.             case zDate::thu: stream << "Thursday"; break;
  48.             case zDate::fri: stream << "Friday"; break;
  49.             case zDate::sat: stream << "Saturday"; break;
  50.             default        : stream << "--error--"; break;
  51.       }
  52.       return stream;
  53. }
  54.  
  55. void test()
  56. {
  57.       cout << " zDate Class Demo \n\n";
  58.  
  59.       // default constructor, Jan 1 0000
  60.       zDate a;
  61.       cout << a << endl;
  62.       // Various versions of the constructors
  63.       zDate x(zDate::oct,20,1962);
  64.       cout << x << endl;
  65.       // constructor with a julian
  66.       zDate z( 2450000L );
  67.       cout << z << endl;
  68.       // make a date with system date (tests copy constructor)
  69.       zDate s(zDate::Today());
  70.       cout << s << endl;
  71.       // init with the day of year
  72.       zDate y(33, 1996);
  73.       cout << y << endl;
  74.       // init from current system time
  75.       time_t secs_now = time(NULL);
  76.       zDate n(localtime(&secs_now));
  77.       cout << n << endl;
  78.  
  79.       // using date addition and subtraction
  80.       zDate adder = x + 10;
  81.       cout << adder << endl;
  82.       adder = adder - 25;
  83.       cout << adder << endl;
  84.  
  85.       //using subtraction of two date objects
  86.       zDate a1(zDate::Today());
  87.       zDate a2 = a1 + 14;
  88.       cout << (a1 - a2) << endl;
  89.       cout << (a2 += 10) << endl;
  90.  
  91.       a1++;
  92.       cout << "Tommorrow= " << a1 << endl;
  93.  
  94.       a1 = zDate(zDate::jul, 14, 1991);
  95.       cout << "a1 (7-14-91) < a2 (" << a2
  96.              << ")? ==> " << ((a1 < a2) ? "TRUE" : "FALSE") << endl;
  97.       cout << "a1 (7-14-91) > a2 ("<< a2
  98.              << ")? ==> " << ((a1 > a2) ? "TRUE" : "FALSE") << endl;
  99.       cout << "a1 (7-14-91) < 8-01-91 ? ==> "
  100.              << ((a1 < zDate(zDate::aug, 1, 1991)) ? "TRUE" : "FALSE") << endl;
  101.       cout << "a1 (7-14-91) > 8-01-91 ? ==> "
  102.              << ((a1 > zDate(zDate::aug, 1, 1991)) ? "TRUE" : "FALSE") << endl;
  103.       cout << "a1 (7-14-91) == 7-14-91 ? ==> "
  104.              << ((a1==zDate(zDate::jul, 14, 1991)) ? "TRUE" : "FALSE") << endl;
  105.       zDate a3 = a1;
  106.  
  107.       cout << "a1 (" << a1 << ") == a3 (" << a3
  108.              << ") ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << endl;
  109.       zDate a4 = a1;
  110.       ++a4;
  111.       cout << "a1 ("<< a1 <<") == a4 (" << a4
  112.              << ") ? ==> " << ((a1==a4) ? "TRUE" : "FALSE") << endl;
  113.  
  114.       zDate a5(zDate::Today());
  115.       cout << "Today is: " << a5 << endl;
  116.       a4 = zDate::Today();
  117.       cout << "Today (a4) is: " << a4 << endl;
  118.  
  119.       cout << "Today + 4 is: " << (a4 += 4) << endl;
  120.       a4 = zDate::Today();
  121.       cout << "Today - 4 is: " << (a4 -= 4) << endl;
  122.       cout << "=========== Leap Year Test ===========\n";
  123.       a1 = zDate(zDate::jan, 15, 1992);
  124.       cout << a1 << "\t" << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
  125.       cout << "\t" << "day of year:  " << a1.DayOfYear() << endl;
  126.  
  127.       a1 = zDate(zDate::feb, 16, 1993);
  128.       cout << a1 << "\t" << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
  129.       cout << "\t" << "day of year:  " << a1.DayOfYear() << endl;
  130.  
  131.       zDate v4(zDate::Today());
  132.       cout << "---------- Add Stuff -----------\n";
  133.       cout << "Start => " << v4 << endl;
  134.       cout << "Add  4 Weeks  => " << v4.AddWeeks(4) << endl;
  135.       cout << "Sub 52 Weeks  => " << v4.AddWeeks(-52)  << endl;
  136.       cout << "Add  2 Years  => " << v4.AddYears(2)    << endl;
  137.  
  138.       cout << flush;
  139.  
  140.       cout << "---------- Misc Stuff -----------\n";
  141.       cout << "The date aboves' day of the month is => " << v4.Day() << endl;
  142.       cout << "There are " << v4.DaysInMonth() << " days in this month.\n";
  143.       cout << "This day happens to be " << v4.DayOfWeek() << " day of week" << endl;
  144.       cout << "on the " << v4.WeekOfYear() << " week of the year," << endl;
  145.       cout << "on the " << v4.WeekOfMonth() << " week of the month, " << endl;
  146.       cout << "which is the "<< (int)v4.Month() << "nth month in the year.\n";
  147.       cout << "The year alone is " << v4.Year() << endl;
  148.       cout << "And this is the " << v4.DayOfYear() << " day of year" << endl;
  149.       cout << "of a year with " << v4.DaysInYear() << " days in it" << endl;
  150.       cout << "which makes exatcly " << v4.WeeksInYear() << " weeks" << endl;
  151.  
  152.       zDate birthday(zDate::jul, 16, 1973);
  153.       cout << "The age test: i was born on " << birthday
  154.              << " which makes me " << v4.Age(birthday) << " years old" << endl;
  155.  
  156.       zDate       D2(zDate::jul, 4, 1776);
  157.       int         I1 = 4;
  158.  
  159.       cout << "Before: I1 = " << I1 << ",  D2 = " << D2 << endl;
  160.       cout << "---------- Postfix '++' test -----------\n";
  161.       cout << "Test : I1++ = " << I1++ << ",  D2++ = " << D2++ << endl;
  162.       cout << "After: I1   = " << I1 << ",  D2   = " << D2 << endl;
  163.  
  164.       cout << "---------- Prefix '++' test -----------\n";
  165.       cout << "Test : ++I1 = " << ++I1 << ",  ++D2 = " << ++D2 << endl;
  166.       cout << "After:   I1 = " << I1 << ",    D2 = " << D2 << endl;
  167.  
  168.       cout << "---------- Postfix '--' test -----------\n";
  169.       cout << "Test : I1-- = " << I1-- << ",  D2-- = " << D2-- << endl;
  170.       cout << "After: I1   = " << I1 << ",  D2   = " << D2 << endl;
  171.  
  172.       cout << "---------- Prefix '--' test -----------\n";
  173.       cout << "Test : --I1 = " << --I1 << ",  --D2 = " << --D2 << endl;
  174.       cout << "After:   I1 = " << I1 << ",    D2 = " << D2 << endl;
  175.  
  176.       cout << "Last day of this year is dayno "
  177.              << zDate(zDate::dec, 31, 1996).DayOfYear() << endl;
  178.       cout << "Last day of prev year is dayno "
  179.              << zDate(zDate::dec, 31, 1995).DayOfYear() << endl;
  180.  
  181.       cout << "Today the moon is " << zDate::Today().MoonPhase() << endl;
  182.  
  183.       zDate today = zDate::Today();
  184.  
  185.       cout << "DST for " << today.Year() << " starts on " << today.BeginDST()
  186.              << " and ends on " << today.EndDST() << endl;
  187.       cout << "Today, " << today << ", DST is "
  188.              << (today.IsDST() ? "" : "not") << "in effect" << endl;
  189.  
  190.       zDate date1(zDate::aug, 31, 1996);
  191.       cout << "Adding 6 months to " << date1 << " results in "
  192.              << date1.AddMonths(6) << endl;
  193.  
  194.       zDate date2(zDate::mar, 31, 1996);
  195.       cout << "Subtracting 1 month from " << date2 << " results in "
  196.              << date2.AddMonths(-1) << endl;
  197.  
  198.       zDate date3(zDate::jul, 4, 1776);
  199.       cout << "Adding 2400 months to " << date3 << " results in "
  200.              << date3.AddMonths(2400) << endl;
  201.  
  202.       cout << "Today's day number is " << zDate::Today().DayNumber() << endl;
  203.  
  204.       zDate date4(zDate::feb, 29, 1996);
  205.       cout << date4 << " subtract two years = " << date4.AddYears(-2) << endl;
  206.  
  207.       cout << "In 1996, DST began on " << zDate::BeginDST(1996) << endl;
  208.  
  209.       zDate date5(zDate::sep, 26, 1996);
  210.       cout << "Moon phase on " << date5 << " was " << date5.MoonPhase() << endl;
  211.  
  212.       zDate date6(zDate::oct, 3, 1996);
  213.       cout << date6 << " + 55 days is " << (date6 + 55) << endl;
  214.  
  215.       zDate date7(zDate::oct, 4, 1996);
  216.       cout << date7 << " + 217 days is ";
  217.       date7 += 217;
  218.       cout << date7 << endl;
  219.       date7 = zDate(zDate::oct, 4, 1996);
  220.       cout << "Same date - (-217) days is ";
  221.       date7 -= -217;
  222.       cout << date7 << endl;
  223.  
  224.       cout << "For 1996, Easter is on " << zDate::Easter(1996) << endl;
  225. }
  226.  
  227.  
  228. void
  229. main()
  230. {
  231.       test();
  232. };
  233.