首页 > 代码库 > DateAndTime

DateAndTime

  1 //DatesAndTime.cpp  2   3 /*  4 Copyright 2000-2004 The VCF Project.  5 Please see License.txt in the top level directory  6 where you installed the VCF.  7 */  8   9  10 #include "vcf/FoundationKit/FoundationKit.h" 11  12 /** 13 include the DateTime header - this must come *after* your include 14 of the FoundationKit. 15 */ 16 #include "vcf/FoundationKit/DateTime.h" 17  18  19  20 using namespace VCF; 21  22 int main( int argc, char** argv ){ 23  24     FoundationKit::init( argc, argv ); 25  26     /** 27     retrieve the current time 28     */ 29     DateTime currentTime = DateTime::now(); 30  31     /** 32     Output the date time instance using the Object::toString method 33     */ 34  35     String timeStr = currentTime.toString(); 36     System::println( Format("currentTime: %ls") % timeStr.c_str() ); 37  38  39     /** 40     Modify the date 41     */ 42  43     currentTime.setDate( 2012, 11, 23 ); 44  45     /** 46     Output the date time instance using the StringUtils::format() method 47     */ 48  49     System::println( Format("currentTime from StringUtils::format():\n\t%ls") 50                     % StringUtils::format( currentTime, "Day %#j in the year %Y, week %#U, %A day %#d of %B month %#m" ).c_str() ); 51  52     /** 53     Output the time portion of the date time instance using the StringUtils::format() method 54     */ 55  56     System::println( Format("currentTime from StringUtils::format():\n\t%ls") 57                     % StringUtils::format( currentTime, "%H:%M:%S.%s" ).c_str() ); 58  59     /** 60     Modify the time, hours, minutes and seconds 61     */ 62     currentTime.setTime( 9, 45, 12 ); 63  64     System::println( Format("currentTime from StringUtils::format():\n\t%ls") 65                     % StringUtils::format( currentTime, "%H:%M:%S.%s" ).c_str() ); 66  67  68     /** 69     Modify the date and time in one call - this allows for milliseconds resolutions 70     */ 71     currentTime.set( 1982, 5, 21, 18, 23, 10, 456 ); 72  73     System::println( Format("currentTime from StringUtils::format():\n\t%ls") 74                     % StringUtils::format( currentTime, "%Y/%m/%d-%H:%M:%S.%s" ).c_str() ); 75  76  77     /** 78     Demonstrate what happens when setting the date/time with bad data 79     */ 80     try { 81         currentTime.setDate( 2003, 45, 12 ); 82     } 83     catch ( std::exception& e ) { 84         System::println( Format("Setting the date/time failed due to : %s") % e.what() ); 85     } 86  87     try { 88         currentTime.setTime( 2003, 45, 12 ); 89     } 90     catch ( std::exception& e ) { 91         System::println( Format("Setting the date/time failed due to : %s") % e.what() ); 92     } 93  94     DateTime dt1(2003,12,23); 95     DateTime dt2(2003,12,23); 96  97     /** 98     Compare a date 99     */100 101     if ( dt1 == dt2 ) {102         System::println( Format("Date {%ls} equals date {%ls}")103                         % dt1.toString().c_str() % dt2.toString().c_str() );104     }105 106     /**107     increment the dates value108     */109     dt2.incrHour( 10 );110 111     if ( dt1 < dt2 ) {112         System::println( Format("Date {%ls} is earlier than date {%ls}")113                         % dt1.toString().c_str() % dt2.toString().c_str() );114     }115 116     /**117     decrement the dates value118     */119     dt2.decrYear( 10 );120 121     if ( dt1 > dt2 ) {122         System::println( Format("Date {%ls} is later than date {%ls}")123                         % dt1.toString().c_str() % dt2.toString().c_str() );124     }125 126 127     /**128     Iterate dates given a starting point129     */130     DateTime xmas2002(2002, 12, 25);131     DateTime::Iterator<ByYear> iterator = xmas2002;132     while ( *iterator != DateTime(2012, 12, 25) ) {133         System::println( StringUtils::format( *iterator, "Christmas is on a %A in %Y" ) );134         iterator ++;135     }136 137     DateTime dt3(1998, 8, 1, 16, 30, 0 );138     DateTime now = DateTime::now();139 140     DateTimeSpan howLong = dt3 - now;141 142 143     System::println( Format("Holy cow! I‘ve been married for:\n\t%d years, %d months, %d days, %d hours, and %d minutes,\n\tor for a total of %s seconds!")144                     %    howLong.getYears()145                     %    howLong.getMonths()146                     %    howLong.getDays()147                     %    howLong.getHours()148                     %    howLong.getMinutes()149                     %    System::getCurrentThreadLocale()->toString( howLong.getTotalSeconds() ) );150 151 152     dt3.setMilliseconds( 0 );153     howLong = now - dt3;154     System::println( Format("now() - 0:\n\t%d years, %d months, %d days, %d hours, and %d minutes,\n\tor for a total of %s seconds!")155                     %    howLong.getYears()156                     %    howLong.getMonths()157                     %    howLong.getDays()158                     %    howLong.getHours()159                     %    howLong.getMinutes()160                     %    System::getCurrentThreadLocale()->toString( howLong.getTotalSeconds() ) );161 162     DateTimeSpan period1( 0 );163     System::print( Format("period1( %d ): " ) % period1.getTotalMilliseconds() );164     howLong = period1;165     System::println( Format("\n\t%d years, %d months, %d days, %d hours, and %d minutes,\n\tor for a total of %s seconds!")166                     %    howLong.getYears()167                     %    howLong.getMonths()168                     %    howLong.getDays()169                     %    howLong.getHours()170                     %    howLong.getMinutes()171                     %    System::getCurrentThreadLocale()->toString( howLong.getTotalSeconds() ) );172     173     DateTimeSpan period2( 1000000 );174     System::print( Format("period2( %d ): " ) % period2.getTotalMilliseconds() );175     howLong = period2;176     System::println( Format("\n\t%d years, %d months, %d days, %d hours, and %d minutes,\n\tor for a total of %s seconds!")177                     %    howLong.getYears()178                     %    howLong.getMonths()179                     %    howLong.getDays()180                     %    howLong.getHours()181                     %    howLong.getMinutes()182                     %    System::getCurrentThreadLocale()->toString( howLong.getTotalSeconds() ) );183 184 185     /**186     The following demonstrates how to save a date to a output stream187     and how to read it back in from an input stream. These188     show how to read/write the date time as an object, obviously189     there are alternate ways of store it.190     */191 192     DateTime storeMe( 1977, 10, 3, 19, 23, 12 );193 194     {195         FileOutputStream fs( "datetime.out" );196 197         fs << &storeMe;198 199         System::println( Format("storeMe (%ls) saved!")%  storeMe.toString().c_str() );200     }201 202     DateTime loadMe;203     System::println( Format("loadMe is equal to %s") % loadMe.toString() );204 205     {206         FileInputStream fs( "datetime.out" );207 208         fs >> static_cast<VCF::Persistable*>(&loadMe);209 210         System::println( Format("loadMe (%s) loaded!") % loadMe.toString() );211     }212 213 214     FoundationKit::terminate();215     return 0;216 }217 218 219 /**220 $Id: DatesAndTime.cpp 2977 2007-01-04 14:59:37Z kdmix $221 */

 

DateAndTime