ccyymmdd.cpp

00001 
00002 
00003 
00004 
00005 #include <iostream>
00006 #include "NewDateTime.h"
00007 #include "boost/date_time/gregorian/formatters.hpp"
00008 #include "boost/date_time/gregorian/parsers.hpp"
00009 #include "boost/date_time/posix_time/posix_time.hpp"
00010 #include "boost/date_time/c_local_time_adjustor.hpp"
00011 
00012 using namespace boost::gregorian;
00013 using namespace std;
00014 
00015 using namespace boost::posix_time;
00016 
00017 NEWDATETIMEFNC char ccyymmdd_error_buffer[256];
00018 
00019 NEWDATETIMEFNC void STDCALL copy_to_error_buffer( exception& e) {
00020   if (strlen( e.what()) > 256) {
00021     memcpy( ccyymmdd_error_buffer, e.what(), 254);
00022     ccyymmdd_error_buffer[254]=0;
00023   } else {
00024     strcpy( ccyymmdd_error_buffer, e.what());
00025   }
00026 }
00027 
00028 NEWDATETIMEFNC int STDCALL day_diff( int iso_lhs, int iso_rhs) {
00029   ostringstream ss;
00030   ss << iso_lhs;
00031 
00032   try {
00033     date dt_lhs( date_from_iso_string( ss.str()));
00034 
00035     ss.str( "");
00036     ss << iso_rhs;
00037     try {
00038       date dt_rhs( date_from_iso_string( ss.str()));
00039   
00040       date_duration df = dt_lhs - dt_rhs;
00041 
00042       return df.days();
00043 
00044     } catch (exception &e) {
00045       copy_to_error_buffer( e);
00046       return -1;
00047     }
00048   } catch (exception &e) {
00049     copy_to_error_buffer( e);
00050     return -1;
00051   }
00052 }
00053 
00054 NEWDATETIMEFNC int STDCALL serial_time( int iso_lhs) {
00055   return day_diff( iso_lhs, 19800101);
00056 }
00057 
00058 NEWDATETIMEFNC int STDCALL week_day( int iso_lhs) {
00059   int wday =  day_diff( iso_lhs, 19800106) % 7;
00060   if (wday < 0) wday += 7;
00061   return wday;
00062 }
00063   
00064 #if defined( DEBUGALONE)
00065 
00066 int main() {
00067   int lhs = 20070420, rhs = 20070127;
00068   
00069   int period = day_diff( lhs, rhs);
00070 
00071   cout << "day_diff( 20070420, 20070127) = " << period << endl;
00072 
00073   period = serial_time( 20080211);
00074   cout << "serial_time( 20080211) = " << period << endl;
00075 
00076   period = week_day( 20080211);
00077   cout << "serial_time( 20080211) = " << period << endl;
00078 
00079   try {
00080     period = serial_time( 99999999);
00081   } catch (exception & e) {
00082     cout << "exception caught = {" << e.what() << "}" << endl;
00083   }
00084 
00085   cout << "serial_time( 99999999) = " << period << endl;
00086   cout << "ccyymmdd_error_buffer={" << ccyymmdd_error_buffer << "}" << endl;
00087 
00088   return 0;
00089 }
00090 
00091 #endif

Generated on Fri Jan 7 12:36:18 2011 for public_options by  doxygen 1.5.1