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

# 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
    00086   virtual ~DateTime() {}
    00087
    00089   inline int getYear() const
    00090   {
    00091     int y, m, d;
    00092     getYMD( y, m, d );
    00093     return y;
    00094   }
    00095
    00097   inline int getMonth() const
    00098   {
    00099     int y, m, d;
    00100     getYMD( y, m, d );
    00101     return m;
    00102   }
    00103
    00105   inline int getDay() const
    00106   {
    00107     int y, m, d;
    00108     getYMD( y, m, d );
    00109     return d;
    00110   }
    00111
    00114   inline int getDate() const { return getDay(); }
    00115
    00117   inline int getJulianDate() const { return m_date; }
    00118
    00120   inline int getHour() const
    00121   {
    00122     return m_time / MILLIS_PER_HOUR;
    00123   }
    00124
    00126   inline int getMinute() const
    00127   {
    00128     return (m_time / MILLIS_PER_MIN) % MINUTES_PER_HOUR;
    00129   }
    00130
    00132   inline int getSecond() const
    00133   {
    00134     return (m_time / MILLIS_PER_SEC) % SECONDS_PER_MIN;
    00135   }
    00136
    00138   inline int getMillisecond() const
    00139   {
    00140     return m_time % MILLIS_PER_SEC;
    00141   }
    00142
    00145   inline void getYMD (int& year, int& month, int& day) const
    00146   {
    00147     getYMD( m_date, year, month, day );
    00148   }
    00149
    00152   inline void getHMS( int& hour, int& minute, int& second, int& millis ) const
    00153   {
    00154     int ticks = m_time / MILLIS_PER_SEC;
    00155     hour = ticks / SECONDS_PER_HOUR;
    00156     minute = (ticks / SECONDS_PER_MIN) % MINUTES_PER_HOUR;
    00157     second = ticks % SECONDS_PER_MIN;
    00158     millis = m_time % MILLIS_PER_SEC;
    00159   }
    00160
    00162   inline int getWeekDay() const
    00163   {
    00164     int Y, M, D;
    00165     getYMD (Y, M, D);
    00166     int m = M >= 3 ? M - 2 : M + 10;
    00167     int Yprime = M >= 3 ? Y : Y - 1;
    00168     int y = Yprime % 100;
    00169     int c = Yprime / 100;
    00170     int wd = (D + int (2.6 * m - 0.2) + y + int (y / 4) + int (c / 4) -
    00171               (2 * c)) % 7;
    00172     return 1 + (wd < 0 ? 7 + wd : wd);
    00173   }
    00174
    00177   inline time_t getTimeT() const
    00178   {
    00179     return (SECONDS_PER_DAY * (m_date - JULIAN_19700101) +
    00180             m_time / MILLIS_PER_SEC);
    00181   }
    00182
    00184   tm getTmUtc() const
    00185   {
    00186     int year, month, day;
    00187     int hour, minute, second, millis;
    00188     tm result = { 0 };
    00189
    00190     getYMD( year, month, day );
    00191     getHMS( hour, minute, second, millis );
    00192
    00193     result.tm_year = year - 1900;
    00194     result.tm_mon = month - 1;
    00195     result.tm_mday = day;
    00196     result.tm_hour = hour;
    00197     result.tm_min = minute;
    00198     result.tm_sec = second;
    00199     result.tm_isdst = -1;
    00200
    00201     return result;
    00202   }
    00203
    00205   void setYMD( int year, int month, int day )
    00206   {
    00207     m_date = julianDate( year, month, day );
    00208   }
    00209
    00211   void setHMS( int hour, int minute, int second, int millis )
    00212   {
    00213     m_time = makeHMS( hour, minute, second, millis );
    00214   }
    00215
    00217   void setHour( int hour )
    00218   {
    00219     int old_hour, min, sec, millis;
    00220     getHMS( old_hour, min, sec, millis );
    00221     setHMS( hour, min, sec, millis );
    00222   }
    00223
    00225   void setMinute( int min )
    00226   {
    00227     int hour, old_min, sec, millis;
    00228     getHMS( hour, old_min, sec, millis );
    00229     setHMS( hour, min, sec, millis );
    00230   }
    00231
    00233   void setSecond( int sec )
    00234   {
    00235     int hour, min, old_sec, millis;
    00236     getHMS( hour, min, old_sec, millis );
    00237     setHMS( hour, min, sec, millis );
    00238   }
    00239
    00241   void setMillisecond( int millis )
    00242   {
    00243     int hour, min, sec, old_millis;
    00244     getHMS( hour, min, sec, old_millis );
    00245     setHMS( hour, min, sec, millis );
    00246   }
    00247
    00249   void clearDate()
    00250   {
    00251     m_date = 0;
    00252   }
    00253
    00255   void clearTime()
    00256   {
    00257     m_time = 0;
    00258   }
    00259
    00261   void set( int date, int time ) { m_date = date; m_time = time; }
    00262
    00264   void set( const DateTime& other )
    00265   {
    00266     m_date = other.m_date;
    00267     m_time = other.m_time;
    00268   }
    00269
    00271   void operator+=( int seconds )
    00272   {
    00273     int d = seconds / SECONDS_PER_DAY;
    00274     int s = seconds % SECONDS_PER_DAY;
    00275
    00276     m_date += d;
    00277     m_time += s * MILLIS_PER_SEC;
    00278
    00279     if( m_time > MILLIS_PER_DAY )
    00280     {
    00281       m_date++;
    00282       m_time %= MILLIS_PER_DAY;
    00283     }
    00284     else if( m_time < 0 )
    00285     {
    00286       m_date--;
    00287       m_time += MILLIS_PER_DAY;
    00288     }
    00289   }
    00290
    00293   static int makeHMS( int hour, int minute, int second, int millis )
    00294   {
    00295     return MILLIS_PER_SEC * (SECONDS_PER_HOUR * hour +
    00296                              SECONDS_PER_MIN * minute +
    00297                              second) + millis;
    00298   }
    00299
    00301   static DateTime nowUtc();
    00302
    00304   static DateTime nowLocal();
    00305
    00307   static DateTime fromUtcTimeT( time_t t, int millis = 0 )
    00308   {
    00309     struct tm tm = time_gmtime( &t );
    00310     return fromTm( tm, millis );
    00311   }
    00312
    00313   static DateTime fromLocalTimeT( time_t t, int millis = 0 )
    00314   {
    00315     struct tm tm = time_localtime( &t );
    00316     return fromTm( tm, millis );
    00317   }
    00318
    00321   static DateTime fromTm( const tm& tm, int millis = 0 )
    00322   {
    00323     return DateTime ( julianDate(tm.tm_year + 1900, tm.tm_mon + 1,
    00324                                  tm.tm_mday),
    00325                      makeHMS(tm.tm_hour, tm.tm_min, tm.tm_sec, millis) );
    00326   }
    00327
    00329   static int julianDate( int year, int month, int day )
    00330   {
    00331     int a = (14 - month) / 12;
    00332     int y = year + 4800 - a;
    00333     int m = month + 12 * a - 3;
    00334     return (day + int ((153 * m + 2) / 5) + y * 365 +
    00335             int (y / 4) - int (y / 100) + int (y / 400) - 32045);
    00336   }
    00337
    00339   static void getYMD( int jday, int& year, int& month, int& day )
    00340   {
    00341     int a = jday + 32044;
    00342     int b = (4 * a + 3) / 146097;
    00343     int c = a - int ((b * 146097) / 4);
    00344     int d = (4 * c + 3) / 1461;
    00345     int e = c - int ((1461 * d) / 4);
    00346     int m = (5 * e + 2) / 153;
    00347     day = e - int ((153 * m + 2) / 5) + 1;
    00348     month = m + 3 - 12 * int (m / 10);
    00349     year = b * 100 + d - 4800 + int (m / 10);
    00350   }
    00351 };
    00352
    00353 inline bool operator==( const DateTime& lhs, const DateTime& rhs )
    00354 {
    00355   return lhs.m_date == rhs.m_date && lhs.m_time == rhs.m_time;
    00356 }
    00357
    00358 inline bool operator!=( const DateTime& lhs, const DateTime& rhs )
    00359 {
    00360   return !(lhs == rhs);
    00361 }
    00362
    00363 inline bool operator<( const DateTime& lhs, const DateTime& rhs )
    00364 {
    00365   if( lhs.m_date < rhs.m_date )
    00366     return true;
    00367   else if( lhs.m_date > rhs.m_date )
    00368     return false;
    00369   else if( lhs.m_time < rhs.m_time )
    00370     return true;
    00371   return false;
    00372 }
    00373
    00374 inline bool operator>( const DateTime& lhs, const DateTime& rhs )
    00375 {
    00376   return !(lhs == rhs || lhs < rhs);
    00377 }
    00378
    00379 inline bool operator<=( const DateTime& lhs, const DateTime& rhs )
    00380 {
    00381   return lhs == rhs || lhs < rhs;
    00382 }
    00383
    00384 inline bool operator>=( const DateTime& lhs, const DateTime& rhs )
    00385 {
    00386   return lhs == rhs || lhs > rhs;
    00387 }
    00388
    00391 inline int operator-( const DateTime& lhs, const DateTime& rhs )
    00392 {
    00393   return (DateTime::SECONDS_PER_DAY * (lhs.m_date - rhs.m_date) +
    00394           // Truncate the millis before subtracting
    00395           lhs.m_time / 1000 - rhs.m_time / 1000);
    00396 }
    00397
    00399 class UtcTimeStamp : public DateTime
    00400 {
    00401 public:
    00403   UtcTimeStamp()
    00404   : DateTime( DateTime::nowUtc() ) {}
    00405
    00407   UtcTimeStamp( int hour, int minute, int second, int millisecond = 0 )
    00408   : DateTime( DateTime::nowUtc() )
    00409   {
    00410     setHMS( hour, minute, second, millisecond );
    00411   }
    00412
    00413   UtcTimeStamp( int hour, int minute, int second,
    00414                 int date, int month, int year )
    00415   : DateTime( year, month, date, hour, minute, second, 0 ) {}
    00416
    00417   UtcTimeStamp( int hour, int minute, int second, int millisecond,
    00418                 int date, int month, int year )
    00419   : DateTime( year, month, date, hour, minute, second, millisecond ) {}
    00420
    00421   explicit UtcTimeStamp( time_t time, int millisecond = 0 )
    00422   : DateTime( fromUtcTimeT (time, millisecond) ) {}
    00423
    00424   UtcTimeStamp( const tm* time, int millisecond = 0 )
    00425   : DateTime( fromTm (*time, millisecond) ) {}
    00426
    00427   void setCurrent()
    00428   {
    00429     set( DateTime::nowUtc() );
    00430   }
    00431 };
    00432
    00434 class LocalTimeStamp : public DateTime
    00435 {
    00436 public:
    00438   LocalTimeStamp()
    00439   : DateTime( DateTime::nowLocal() ) {}
    00440
    00442   LocalTimeStamp( int hour, int minute, int second, int millisecond = 0 )
    00443   : DateTime( DateTime::nowLocal() )
    00444   {
    00445     setHMS( hour, minute, second, millisecond );
    00446   }
    00447
    00448   LocalTimeStamp( int hour, int minute, int second,
    00449                 int date, int month, int year )
    00450   : DateTime( year, month, date, hour, minute, second, 0 ) {}
    00451
    00452   LocalTimeStamp( int hour, int minute, int second, int millisecond,
    00453                 int date, int month, int year )
    00454   : DateTime( year, month, date, hour, minute, second, millisecond ) {}
    00455
    00456   explicit LocalTimeStamp( time_t time, int millisecond = 0 )
    00457   : DateTime( fromLocalTimeT (time, millisecond) ) {}
    00458
    00459   LocalTimeStamp( const tm* time, int millisecond = 0 )
    00460   : DateTime( fromTm (*time, millisecond) ) {}
    00461
    00462   void setCurrent()
    00463   {
    00464     set( DateTime::nowLocal() );
    00465   }
    00466 };
    00467
    00469 class UtcTimeOnly : public DateTime
    00470 {
    00471 public:
    00473   UtcTimeOnly()
    00474   {
    00475     setCurrent();
    00476   }
    00477
    00478   UtcTimeOnly( const DateTime& val )
    00479   : DateTime(val)
    00480   {
    00481     clearDate();
    00482   }
    00483
    00484   UtcTimeOnly( int hour, int minute, int second, int millisecond = 0 )
    00485   {
    00486     setHMS( hour, minute, second, millisecond );
    00487   }
    00488
    00489   explicit UtcTimeOnly( time_t time, int millisecond = 0 )
    00490   : DateTime( fromUtcTimeT (time, millisecond) )
    00491   {
    00492     clearDate();
    00493   }
    00494
    00495   UtcTimeOnly( const tm* time, int millisecond = 0 )
    00496   : DateTime( fromTm (*time, millisecond) )
    00497   {
    00498     clearDate();
    00499   }
    00500
    00502   void setCurrent()
    00503   {
    00504     DateTime d = nowUtc();
    00505     m_time = d.m_time;
    00506   }
    00507 };
    00508
    00510 class LocalTimeOnly : public DateTime
    00511 {
    00512 public:
    00514   LocalTimeOnly()
    00515   {
    00516     setCurrent();
    00517   }
    00518
    00519   LocalTimeOnly( const DateTime& val )
    00520   : DateTime(val)
    00521   {
    00522     clearDate();
    00523   }
    00524
    00525   LocalTimeOnly( int hour, int minute, int second, int millisecond = 0 )
    00526   {
    00527     setHMS( hour, minute, second, millisecond );
    00528   }
    00529
    00530   explicit LocalTimeOnly( time_t time, int millisecond = 0 )
    00531   : DateTime( fromLocalTimeT (time, millisecond) )
    00532   {
    00533     clearDate();
    00534   }
    00535
    00536   LocalTimeOnly( const tm* time, int millisecond = 0 )
    00537   : DateTime( fromTm (*time, millisecond) )
    00538   {
    00539     clearDate();
    00540   }
    00541
    00543   void setCurrent()
    00544   {
    00545     DateTime d = nowLocal();
    00546     m_time = d.m_time;
    00547   }
    00548 };
    00549
    00551 class UtcDate : public DateTime
    00552 {
    00553 public:
    00555   UtcDate()
    00556   {
    00557     setCurrent();
    00558   }
    00559
    00560   UtcDate( const DateTime& val )
    00561   : DateTime( val )
    00562   {
    00563     clearTime();
    00564   }
    00565
    00566   UtcDate( int date, int month, int year )
    00567   : DateTime(year, month, date, 0, 0, 0, 0) {}
    00568
    00569   UtcDate( long sec )
    00570   : DateTime( sec / DateTime::SECONDS_PER_DAY, 0 ) {}
    00571
    00572   UtcDate( const tm* time )
    00573   : DateTime( fromTm (*time) )
    00574   {
    00575     clearTime();
    00576   }
    00577
    00579   void setCurrent()
    00580   {
    00581     DateTime d = nowUtc();
    00582     m_date = d.m_date;
    00583   }
    00584 };
    00585
    00587 class LocalDate : public DateTime
    00588 {
    00589 public:
    00591   LocalDate()
    00592   {
    00593     setCurrent();
    00594   }
    00595
    00596   LocalDate( const DateTime& val )
    00597   : DateTime( val )
    00598   {
    00599     clearTime();
    00600   }
    00601
    00602   LocalDate( int date, int month, int year )
    00603   : DateTime(year, month, date, 0, 0, 0, 0) {}
    00604
    00605   LocalDate( long sec )
    00606   : DateTime( sec / DateTime::SECONDS_PER_DAY, 0 ) {}
    00607
    00608   LocalDate( const tm* time )
    00609   : DateTime( fromTm (*time) )
    00610   {
    00611     clearTime();
    00612   }
    00613
    00615   void setCurrent()
    00616   {
    00617     DateTime d = nowLocal();
    00618     m_date = d.m_date;
    00619   }
    00620 };
    00621
    00624 typedef UtcDate UtcDateOnly;
    00625
    00626 typedef std::string STRING;
    00627 typedef char CHAR;
    00628 typedef double PRICE;
    00629 typedef int INT;
    00630 typedef double AMT;
    00631 typedef double QTY;
    00632 typedef std::string CURRENCY;
    00633 typedef std::string MULTIPLEVALUESTRING;
    00634 typedef std::string MULTIPLESTRINGVALUE;
    00635 typedef std::string MULTIPLECHARVALUE;
    00636 typedef std::string EXCHANGE;
    00637 typedef UtcTimeStamp UTCTIMESTAMP;
    00638 typedef bool BOOLEAN;
    00639 typedef std::string LOCALMKTDATE;
    00640 typedef std::string DATA;
    00641 typedef double FLOAT;
    00642 typedef double PRICEOFFSET;
    00643 typedef std::string MONTHYEAR;
    00644 typedef std::string DAYOFMONTH;
    00645 typedef UtcDate UTCDATE;
    00646 typedef UtcDateOnly UTCDATEONLY;
    00647 typedef UtcTimeOnly UTCTIMEONLY;
    00648 typedef int NUMINGROUP;
    00649 typedef double PERCENTAGE;
    00650 typedef int SEQNUM;
    00651 typedef int LENGTH;
    00652 typedef std::string COUNTRY;
    00653 typedef std::string TZTIMEONLY;
    00654 typedef std::string TZTIMESTAMP;
    00655 typedef std::string XMLDATA;
    00656 typedef std::string LANGUAGE;
    00657
    00658 namespace TYPE
    00659 {
    00660 enum Type
    00661 {
    00662   Unknown,
    00663   String,
    00664   Char,
    00665   Price,
    00666   Int,
    00667   Amt,
    00668   Qty,
    00669   Currency,
    00670   MultipleValueString,
    00671   MultipleStringValue,
    00672   MultipleCharValue,
    00673   Exchange,
    00674   UtcTimeStamp,
    00675   Boolean,
    00676   LocalMktDate,
    00677   Data,
    00678   Float,
    00679   PriceOffset,
    00680   MonthYear,
    00681   DayOfMonth,
    00682   UtcDate,
    00683   UtcDateOnly = UtcDate,
    00684   UtcTimeOnly,
    00685   NumInGroup,
    00686   Percentage,
    00687   SeqNum,
    00688   Length,
    00689   Country,
    00690   TzTimeOnly,
    00691   TzTimeStamp,
    00692   XmlData,
    00693   Language
    00694 };
    00695 }
    00696 }
    00697
    00698 #endif //FIX_FIELDTYPES_H