Index  Source Files  Annotated Class List  Alphabetical Class List  Class Hierarchy  Graphical Class Hierarchy   
 

/home/orenmnero/autobuild/quickfix/src/C++/FieldTypes.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /****************************************************************************
00004 ** Copyright (c) quickfixengine.org  All rights reserved.
00005 **
00006 ** This file is part of the QuickFIX FIX Engine
00007 **
00008 ** This file may be distributed under the terms of the quickfixengine.org
00009 ** license as defined by quickfixengine.org and appearing in the file
00010 ** LICENSE included in the packaging of this file.
00011 **
00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 **
00015 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00016 **
00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00018 ** not clear to you.
00019 **
00020 ****************************************************************************/
00021 
00022 #ifndef FIX_FIELDTYPES_H
00023 #define FIX_FIELDTYPES_H
00024 
00025 #ifdef _MSC_VER
00026 #pragma warning( disable : 4503 4355 4786 4290 )
00027 #endif
00028 
00029 #include "Utility.h"
00030 #include <string>
00031 #include <time.h>
00032 
00033 namespace FIX
00034 {
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 struct DateTime 
00051 {
00052   int m_date;
00053   int m_time;
00054 
00056   enum 
00057   {
00058     SECONDS_PER_DAY = 86400,
00059     SECONDS_PER_HOUR = 3600,
00060     SECONDS_PER_MIN = 60,
00061     MINUTES_PER_HOUR = 60,
00062 
00063     MILLIS_PER_DAY = 86400000,
00064     MILLIS_PER_HOUR = 3600000,
00065     MILLIS_PER_MIN = 60000,
00066     MILLIS_PER_SEC = 1000,
00067 
00068     // time_t epoch (1970-01-01) as a Julian date
00069     JULIAN_19700101 = 2440588
00070   };
00071 
00073   DateTime () : m_date (0), m_time (0) {}
00074 
00076   DateTime (int date, int time) : m_date (date), m_time (time) {}
00077 
00079   DateTime( int year, int month, int day,
00080             int hour, int minute, int second, int millis ) 
00081   {
00082     m_date = julianDate( year, month, day );
00083     m_time = makeHMS( hour, minute, second, millis );
00084   }
00085 
00087   inline int getYear() const 
00088   {
00089     int y, m, d;
00090     getYMD( y, m, d );
00091     return y;
00092   }
00093     
00095   inline int getMonth() const 
00096   {
00097     int y, m, d;
00098     getYMD( y, m, d );
00099     return m;
00100   }
00101 
00103   inline int getDay() const 
00104   {
00105     int y, m, d;
00106     getYMD( y, m, d );
00107     return d;
00108   }
00109 
00112   inline int getDate() const { return getDay(); }
00113 
00115   inline int getJulianDate() const { return m_date; }
00116 
00118   inline int getHour() const 
00119   {
00120     return m_time / MILLIS_PER_HOUR;
00121   }
00122 
00124   inline int getMinute() const 
00125   {
00126     return (m_time / MILLIS_PER_MIN) % MINUTES_PER_HOUR;
00127   }
00128 
00130   inline int getSecond() const 
00131   {
00132     return (m_time / MILLIS_PER_SEC) % SECONDS_PER_MIN;
00133   }
00134 
00136   inline int getMillisecond() const 
00137   {
00138     return m_time % MILLIS_PER_SEC;
00139   }
00140 
00143   inline void getYMD (int& year, int& month, int& day) const 
00144   {
00145     getYMD( m_date, year, month, day );
00146   }
00147 
00150   inline void getHMS( int& hour, int& minute, int& second, int& millis ) const 
00151   {
00152     int ticks = m_time / MILLIS_PER_SEC;
00153     hour = ticks / SECONDS_PER_HOUR;
00154     minute = (ticks / SECONDS_PER_MIN) % MINUTES_PER_HOUR;
00155     second = ticks % SECONDS_PER_MIN;
00156     millis = m_time % MILLIS_PER_SEC;
00157   }
00158 
00160   inline int getWeekDay() const 
00161   {
00162     int Y, M, D;
00163     getYMD (Y, M, D);
00164     int m = M >= 3 ? M - 2 : M + 10;
00165     int Yprime = M >= 3 ? Y : Y - 1;
00166     int y = Yprime % 100;
00167     int c = Yprime / 100;
00168     int wd = (D + int (2.6 * m - 0.2) + y + int (y / 4) + int (c / 4) -
00169               (2 * c)) % 7;
00170     return 1 + (wd < 0 ? 7 + wd : wd);
00171   }
00172 
00175   inline time_t getTimeT() const 
00176   {
00177     return (SECONDS_PER_DAY * (m_date - JULIAN_19700101) +
00178             m_time / MILLIS_PER_SEC);
00179   }
00180 
00182   tm getTmUtc() const 
00183   {
00184     int year, month, day;
00185     int hour, minute, second, millis;
00186     tm result = { 0 };
00187 
00188     getYMD( year, month, day );
00189     getHMS( hour, minute, second, millis );
00190 
00191     result.tm_year = year - 1900;
00192     result.tm_mon = month - 1;
00193     result.tm_mday = day;
00194     result.tm_hour = hour;
00195     result.tm_min = minute;
00196     result.tm_sec = second;
00197     result.tm_isdst = -1;
00198 
00199     return result;
00200   }
00201 
00203   void setYMD( int year, int month, int day ) 
00204   {
00205     m_date = julianDate( year, month, day );
00206   }
00207 
00209   void setHMS( int hour, int minute, int second, int millis )
00210   {
00211     m_time = makeHMS( hour, minute, second, millis );
00212   }
00213 
00215   void setHour( int hour )
00216   {
00217     int old_hour, min, sec, millis;
00218     getHMS( old_hour, min, sec, millis );
00219     setHMS( hour, min, sec, millis );
00220   }
00221 
00223   void setMinute( int min )
00224   {
00225     int hour, old_min, sec, millis;
00226     getHMS( hour, old_min, sec, millis );
00227     setHMS( hour, min, sec, millis );
00228   }
00229 
00231   void setSecond( int sec )
00232   {
00233     int hour, min, old_sec, millis;
00234     getHMS( hour, min, old_sec, millis );
00235     setHMS( hour, min, sec, millis );
00236   }
00237 
00239   void setMillisecond( int millis )
00240   {
00241     int hour, min, sec, old_millis;
00242     getHMS( hour, min, sec, old_millis );
00243     setHMS( hour, min, sec, millis );
00244   }
00245 
00247   void clearDate() 
00248   {
00249     m_date = 0;
00250   }
00251 
00253   void clearTime() 
00254   {
00255     m_time = 0;
00256   }
00257 
00259   void set( int date, int time ) { m_date = date; m_time = time; }
00260 
00262   void set( const DateTime& other ) 
00263   {
00264     m_date = other.m_date;
00265     m_time = other.m_time;
00266   }
00267 
00269   void operator+=( int seconds ) 
00270   {
00271     int d = seconds / SECONDS_PER_DAY;
00272     int s = seconds % SECONDS_PER_DAY;
00273 
00274     m_date += d;
00275     m_time += s * MILLIS_PER_SEC;
00276 
00277     if( m_time > MILLIS_PER_DAY )
00278     {
00279       m_date++;
00280       m_time %= MILLIS_PER_DAY;
00281     }
00282     else if( m_time < 0 )
00283     {
00284       m_date--;
00285       m_time += MILLIS_PER_DAY;
00286     }
00287   }
00288 
00291   static int makeHMS( int hour, int minute, int second, int millis )
00292   {
00293     return MILLIS_PER_SEC * (SECONDS_PER_HOUR * hour +
00294                              SECONDS_PER_MIN * minute +
00295                              second) + millis;
00296   }
00297 
00299   static DateTime now();
00300 
00302   static DateTime fromTimeT( time_t t, int millis = 0 ) 
00303   {
00304     struct tm tm = time_gmtime( &t );
00305     return fromTm( tm, millis );
00306   }
00307 
00310   static DateTime fromTm( const tm& tm, int millis = 0 )
00311   {
00312     return DateTime ( julianDate(tm.tm_year + 1900, tm.tm_mon + 1,
00313                                  tm.tm_mday),
00314                      makeHMS(tm.tm_hour, tm.tm_min, tm.tm_sec, millis) );
00315   }
00316 
00318   static int julianDate( int year, int month, int day )
00319   {
00320     int a = (14 - month) / 12;
00321     int y = year + 4800 - a;
00322     int m = month + 12 * a - 3;
00323     return (day + int ((153 * m + 2) / 5) + y * 365 +
00324             int (y / 4) - int (y / 100) + int (y / 400) - 32045);
00325   }
00326 
00328   static void getYMD( int jday, int& year, int& month, int& day )
00329   {
00330     int a = jday + 32044;
00331     int b = (4 * a + 3) / 146097;
00332     int c = a - int ((b * 146097) / 4);
00333     int d = (4 * c + 3) / 1461;
00334     int e = c - int ((1461 * d) / 4);
00335     int m = (5 * e + 2) / 153;
00336     day = e - int ((153 * m + 2) / 5) + 1;
00337     month = m + 3 - 12 * int (m / 10);
00338     year = b * 100 + d - 4800 + int (m / 10);
00339   }
00340 };
00341 
00342 inline bool operator==( const DateTime& lhs, const DateTime& rhs )
00343 {
00344   return lhs.m_date == rhs.m_date && lhs.m_time == rhs.m_time;
00345 }
00346 
00347 inline bool operator!=( const DateTime& lhs, const DateTime& rhs )
00348 {
00349   return !(lhs == rhs);
00350 }
00351 
00352 inline bool operator<( const DateTime& lhs, const DateTime& rhs )
00353 {
00354   if( lhs.m_date < rhs.m_date )
00355     return true;
00356   else if( lhs.m_date > rhs.m_date )
00357     return false;
00358   else if( lhs.m_time < rhs.m_time )
00359     return true;
00360   return false;
00361 }
00362 
00363 inline bool operator>( const DateTime& lhs, const DateTime& rhs )
00364 {
00365   return !(lhs == rhs || lhs < rhs);
00366 }
00367 
00368 inline bool operator<=( const DateTime& lhs, const DateTime& rhs )
00369 {
00370   return lhs == rhs || lhs < rhs;
00371 }
00372 
00373 inline bool operator>=( const DateTime& lhs, const DateTime& rhs )
00374 {
00375   return lhs == rhs || lhs > rhs;
00376 }
00377 
00380 inline int operator-( const DateTime& lhs, const DateTime& rhs )
00381 {
00382   return (DateTime::SECONDS_PER_DAY * (lhs.m_date - rhs.m_date) +
00383           // Truncate the millis before subtracting
00384           lhs.m_time / 1000 - rhs.m_time / 1000);
00385 }
00386 
00387 class UtcTimeOnly;
00388 class UtcDate;
00389 
00391 class UtcTimeStamp : public DateTime
00392 {
00393 public:
00395   UtcTimeStamp()
00396   : DateTime( DateTime::now() ) {}
00397 
00399   UtcTimeStamp( int hour, int minute, int second, int millisecond = 0 )
00400   : DateTime( DateTime::now () )
00401   {
00402     setHMS( hour, minute, second, millisecond );
00403   }
00404 
00405 
00406   UtcTimeStamp( int hour, int minute, int second,
00407                 int date, int month, int year )
00408   : DateTime( year, month, date, hour, minute, second, 0 ) {}
00409 
00410   UtcTimeStamp( int hour, int minute, int second, int millisecond,
00411                 int date, int month, int year )
00412   : DateTime( year, month, date, hour, minute, second, millisecond ) {}
00413 
00414   UtcTimeStamp( time_t time, int millisecond = 0 )
00415   : DateTime( fromTimeT (time, millisecond) ) {}
00416 
00417   UtcTimeStamp( const tm* time, int millisecond = 0 )
00418   : DateTime( fromTm (*time, millisecond) ) {}
00419 
00420   void setCurrent() 
00421   {
00422     set( DateTime::now() );
00423   }
00424 };
00425 
00427 class UtcTimeOnly : public DateTime
00428 {
00429 public:
00431   UtcTimeOnly()
00432   {
00433     setCurrent();
00434   }
00435 
00436   UtcTimeOnly( const DateTime& val )
00437   : DateTime(val)
00438   {
00439     clearDate();
00440   }
00441 
00442   UtcTimeOnly( int hour, int minute, int second, int millisecond = 0 )
00443   {
00444     setHMS( hour, minute, second, millisecond );
00445   }
00446 
00447   UtcTimeOnly( time_t time, int millisecond = 0 )
00448   : DateTime( fromTimeT (time, millisecond) )
00449   {
00450     clearDate();
00451   }
00452 
00453   UtcTimeOnly( const tm* time, int millisecond = 0 )
00454   : DateTime( fromTm (*time, millisecond) )
00455   {
00456     clearDate();
00457   }
00458 
00460   void setCurrent()
00461   {
00462     DateTime d = now();
00463     m_time = d.m_time;
00464   }
00465 };
00466 
00468 class UtcDate : public DateTime
00469 {
00470 public:
00472   UtcDate()
00473   {
00474     setCurrent();
00475   }
00476 
00477   UtcDate( const DateTime& val )
00478   : DateTime( val )
00479   {
00480     clearTime();
00481   }
00482 
00483   UtcDate( int date, int month, int year )
00484   : DateTime(year, month, date, 0, 0, 0, 0) {}
00485 
00486   UtcDate( long sec )
00487   : DateTime( sec / DateTime::SECONDS_PER_DAY, 0 ) {}
00488 
00489   UtcDate( const tm* time )
00490   : DateTime( fromTm (*time) )
00491   {
00492     clearTime();
00493   }
00494 
00496   void setCurrent()
00497   {
00498     DateTime d = now();
00499     m_date = d.m_date;
00500   }
00501 };
00504 typedef UtcDate UtcDateOnly;
00505 
00506 typedef std::string STRING;
00507 typedef char CHAR;
00508 typedef double PRICE;
00509 typedef int INT;
00510 typedef double AMT;
00511 typedef double QTY;
00512 typedef std::string CURRENCY;
00513 typedef std::string MULTIPLEVALUESTRING;
00514 typedef std::string EXCHANGE;
00515 typedef UtcTimeStamp UTCTIMESTAMP;
00516 typedef bool BOOLEAN;
00517 typedef std::string LOCALMKTDATE;
00518 typedef std::string DATA;
00519 typedef double FLOAT;
00520 typedef double PRICEOFFSET;
00521 typedef std::string MONTHYEAR;
00522 typedef std::string DAYOFMONTH;
00523 typedef UtcDate UTCDATE;
00524 typedef UtcDateOnly UTCDATEONLY;
00525 typedef UtcTimeOnly UTCTIMEONLY;
00526 typedef int NUMINGROUP;
00527 typedef double PERCENTAGE;
00528 typedef int SEQNUM;
00529 typedef int LENGTH;
00530 typedef std::string COUNTRY;
00531 
00532 namespace TYPE
00533 {
00534 enum Type
00535 {
00536   Unknown,
00537   String,
00538   Char,
00539   Price,
00540   Int,
00541   Amt,
00542   Qty,
00543   Currency,
00544   MultipleValueString,
00545   Exchange,
00546   UtcTimeStamp,
00547   Boolean,
00548   LocalMktDate,
00549   Data,
00550   Float,
00551   PriceOffset,
00552   MonthYear,
00553   DayOfMonth,
00554   UtcDate,
00555   UtcDateOnly = UtcDate,
00556   UtcTimeOnly,
00557   NumInGroup,
00558   Percentage,
00559   SeqNum,
00560   Length,
00561   Country
00562 };
00563 }
00564 }
00565 
00566 #endif //FIX_FIELDTYPES_H

Generated on Mon Jul 24 19:36:28 2006 for QuickFIX by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2001