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

FIX::DateTime Struct Reference
[User]

Date and Time stored as a Julian day number and number of milliseconds since midnight. More...

#include <FieldTypes.h>

Inheritance diagram for FIX::DateTime:

Inheritance graph
[legend]
List of all members.

Public Types

enum  {
  SECONDS_PER_DAY = 86400, SECONDS_PER_HOUR = 3600, SECONDS_PER_MIN = 60, MINUTES_PER_HOUR = 60,
  MILLIS_PER_DAY = 86400000, MILLIS_PER_HOUR = 3600000, MILLIS_PER_MIN = 60000, MILLIS_PER_SEC = 1000,
  JULIAN_19700101 = 2440588
}
 Magic numbers. More...


Public Member Functions

 DateTime ()
 Default constructor - initializes to zero.

 DateTime (int date, int time)
 Construct from a Julian day number and time in millis.

 DateTime (int year, int month, int day, int hour, int minute, int second, int millis)
 Construct from the specified components.

int getYear () const
 Return the year portion of the date.

int getMonth () const
 Return the month (1-12) portion of the date.

int getDay () const
 Return the day of the month portion of the date.

int getDate () const
 Another name for the day of the month.

int getJulianDate () const
 Return the internal julian date.

int getHour () const
 Return the hour portion of the time (0-23).

int getMinute () const
 Return the minute portion of the time (0-59).

int getSecond () const
 Return the second portion of the time (0-59).

int getMillisecond () const
 Return the millisecond portion of the time.

void getYMD (int &year, int &month, int &day) const
 Load the referenced values with the year, month and day portions of the date in a single operation.

void getHMS (int &hour, int &minute, int &second, int &millis) const
 Load the referenced values with the hour, minute, second and millisecond portions of the time in a single operation.

int getWeekDay () const
 Calculate the weekday of the date (Sunday is 1, Saturday is 7).

time_t getTimeT () const
 Convert the DateTime to a time_t.

tm getTmUtc () const
 Convert the DateTime to a struct tm which is in UTC.

void setYMD (int year, int month, int day)
 Set the date portion of the DateTime.

void setHMS (int hour, int minute, int second, int millis)
 Set the time portion of the DateTime.

void setHour (int hour)
 Set the hour portion of the time.

void setMinute (int min)
 Set the minute portion of the time.

void setSecond (int sec)
 Set the seconds portion of the time.

void setMillisecond (int millis)
 Set the millisecond portion of the time.

void clearDate ()
 Clear the date portion of the DateTime.

void clearTime ()
 Clear the time portion of the DateTime.

void set (int date, int time)
 Set the internal date and time members.

void set (const DateTime &other)
 Initialize from another DateTime.

void operator+= (int seconds)
 Add a number of seconds to this.


Static Public Member Functions

int makeHMS (int hour, int minute, int second, int millis)
 Helper method to convert a broken down time to a number of milliseconds since midnight.

DateTime now ()
 Return the current wall-clock time as a DateTime.

DateTime fromTimeT (time_t t, int millis=0)
 Convert a time_t and optional milliseconds to a DateTime.

DateTime fromTm (const tm &tm, int millis=0)
 Convert a tm and optional milliseconds to a DateTime.

int julianDate (int year, int month, int day)
 Helper method to calculate a Julian day number.

void getYMD (int jday, int &year, int &month, int &day)
 Convert a Julian day number to a year, month and day.


Public Attributes

int m_date
int m_time

Detailed Description

Date and Time stored as a Julian day number and number of milliseconds since midnight.

Does not perform any timezone calculations. All magic numbers and related calculations have been taken from:

See also:
http://www.faqs.org/faqs/calendars.faq

http://scienceworld.wolfram.com/astronomy/JulianDate.html

http://scienceworld.wolfram.com/astronomy/GregorianCalendar.html

http://scienceworld.wolfram.com/astronomy/Weekday.html

Author:
Caleb Epstein <caleb.epstein at gmail dot com>

Definition at line 50 of file FieldTypes.h.


Member Enumeration Documentation

anonymous enum
 

Magic numbers.

Enumeration values:
SECONDS_PER_DAY 
SECONDS_PER_HOUR 
SECONDS_PER_MIN 
MINUTES_PER_HOUR 
MILLIS_PER_DAY 
MILLIS_PER_HOUR 
MILLIS_PER_MIN 
MILLIS_PER_SEC 
JULIAN_19700101 

Definition at line 56 of file FieldTypes.h.

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   };


Constructor & Destructor Documentation

FIX::DateTime::DateTime  )  [inline]
 

Default constructor - initializes to zero.

Definition at line 73 of file FieldTypes.h.

References m_date, and m_time.

Referenced by fromTm().

00073 : m_date (0), m_time (0) {}

FIX::DateTime::DateTime int  date,
int  time
[inline]
 

Construct from a Julian day number and time in millis.

Definition at line 76 of file FieldTypes.h.

References m_date, and m_time.

00076 : m_date (date), m_time (time) {}

FIX::DateTime::DateTime int  year,
int  month,
int  day,
int  hour,
int  minute,
int  second,
int  millis
[inline]
 

Construct from the specified components.

Definition at line 79 of file FieldTypes.h.

References julianDate(), m_date, m_time, and makeHMS().

00081   {
00082     m_date = julianDate( year, month, day );
00083     m_time = makeHMS( hour, minute, second, millis );
00084   }


Member Function Documentation

void FIX::DateTime::clearDate  )  [inline]
 

Clear the date portion of the DateTime.

Definition at line 247 of file FieldTypes.h.

References m_date.

Referenced by FIX::UtcTimeOnly::UtcTimeOnly().

00248   {
00249     m_date = 0;
00250   }

void FIX::DateTime::clearTime  )  [inline]
 

Clear the time portion of the DateTime.

Definition at line 253 of file FieldTypes.h.

References m_time.

Referenced by FIX::UtcDate::UtcDate().

00254   {
00255     m_time = 0;
00256   }

DateTime FIX::DateTime::fromTimeT time_t  t,
int  millis = 0
[inline, static]
 

Convert a time_t and optional milliseconds to a DateTime.

Definition at line 302 of file FieldTypes.h.

References fromTm(), and FIX::time_gmtime().

Referenced by now().

00303   {
00304     struct tm tm = time_gmtime( &t );
00305     return fromTm( tm, millis );
00306   }

DateTime FIX::DateTime::fromTm const tm &  tm,
int  millis = 0
[inline, static]
 

Convert a tm and optional milliseconds to a DateTime.

Note:
the tm structure is assumed to contain a date specified in UTC

Definition at line 310 of file FieldTypes.h.

References DateTime(), and julianDate().

Referenced by fromTimeT().

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   }

int FIX::DateTime::getDate  )  const [inline]
 

Another name for the day of the month.

Bad name, but used because of the legacy UtcTimeStamp interface

Definition at line 112 of file FieldTypes.h.

References getDay().

00112 { return getDay(); }

int FIX::DateTime::getDay  )  const [inline]
 

Return the day of the month portion of the date.

Definition at line 103 of file FieldTypes.h.

References getYMD().

Referenced by FIX::SessionFactory::create(), and getDate().

00104   {
00105     int y, m, d;
00106     getYMD( y, m, d );
00107     return d;
00108   }

void FIX::DateTime::getHMS int &  hour,
int &  minute,
int &  second,
int &  millis
const [inline]
 

Load the referenced values with the hour, minute, second and millisecond portions of the time in a single operation.

Definition at line 150 of file FieldTypes.h.

References m_time, MILLIS_PER_SEC, MINUTES_PER_HOUR, SECONDS_PER_HOUR, and SECONDS_PER_MIN.

Referenced by getTmUtc(), setHour(), setMillisecond(), setMinute(), and setSecond().

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   }

int FIX::DateTime::getHour  )  const [inline]
 

Return the hour portion of the time (0-23).

Definition at line 118 of file FieldTypes.h.

References m_time, and MILLIS_PER_HOUR.

00119   {
00120     return m_time / MILLIS_PER_HOUR;
00121   }

int FIX::DateTime::getJulianDate  )  const [inline]
 

Return the internal julian date.

Definition at line 115 of file FieldTypes.h.

References m_date.

Referenced by FIX::SessionTime::isSameSession().

00115 { return m_date; }

int FIX::DateTime::getMillisecond  )  const [inline]
 

Return the millisecond portion of the time.

Definition at line 136 of file FieldTypes.h.

References m_time, and MILLIS_PER_SEC.

00137   {
00138     return m_time % MILLIS_PER_SEC;
00139   }

int FIX::DateTime::getMinute  )  const [inline]
 

Return the minute portion of the time (0-59).

Definition at line 124 of file FieldTypes.h.

References m_time, MILLIS_PER_MIN, and MINUTES_PER_HOUR.

00125   {
00126     return (m_time / MILLIS_PER_MIN) % MINUTES_PER_HOUR;
00127   }

int FIX::DateTime::getMonth  )  const [inline]
 

Return the month (1-12) portion of the date.

Definition at line 95 of file FieldTypes.h.

References getYMD().

00096   {
00097     int y, m, d;
00098     getYMD( y, m, d );
00099     return m;
00100   }

int FIX::DateTime::getSecond  )  const [inline]
 

Return the second portion of the time (0-59).

Definition at line 130 of file FieldTypes.h.

References m_time, MILLIS_PER_SEC, and SECONDS_PER_MIN.

00131   {
00132     return (m_time / MILLIS_PER_SEC) % SECONDS_PER_MIN;
00133   }

time_t FIX::DateTime::getTimeT  )  const [inline]
 

Convert the DateTime to a time_t.

Note that this operation can overflow on 32-bit platforms when we go beyond year 2038.

Definition at line 175 of file FieldTypes.h.

References JULIAN_19700101, m_date, m_time, MILLIS_PER_SEC, and SECONDS_PER_DAY.

00176   {
00177     return (SECONDS_PER_DAY * (m_date - JULIAN_19700101) +
00178             m_time / MILLIS_PER_SEC);
00179   }

tm FIX::DateTime::getTmUtc  )  const [inline]
 

Convert the DateTime to a struct tm which is in UTC.

Definition at line 182 of file FieldTypes.h.

References getHMS(), and getYMD().

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   }

int FIX::DateTime::getWeekDay  )  const [inline]
 

Calculate the weekday of the date (Sunday is 1, Saturday is 7).

Definition at line 160 of file FieldTypes.h.

References getYMD().

Referenced by FIX::SessionTime::isSameSession(), and FIX::SessionTime::isSessionTime().

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   }

int FIX::DateTime::getYear  )  const [inline]
 

Return the year portion of the date.

Definition at line 87 of file FieldTypes.h.

References getYMD().

00088   {
00089     int y, m, d;
00090     getYMD( y, m, d );
00091     return y;
00092   }

void FIX::DateTime::getYMD int  jday,
int &  year,
int &  month,
int &  day
[inline, static]
 

Convert a Julian day number to a year, month and day.

Definition at line 328 of file FieldTypes.h.

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   }

void FIX::DateTime::getYMD int &  year,
int &  month,
int &  day
const [inline]
 

Load the referenced values with the year, month and day portions of the date in a single operation.

Definition at line 143 of file FieldTypes.h.

References m_date.

Referenced by getDay(), getMonth(), getTmUtc(), getWeekDay(), and getYear().

00144   {
00145     getYMD( m_date, year, month, day );
00146   }

int FIX::DateTime::julianDate int  year,
int  month,
int  day
[inline, static]
 

Helper method to calculate a Julian day number.

Definition at line 318 of file FieldTypes.h.

Referenced by DateTime(), fromTm(), and setYMD().

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   }

int FIX::DateTime::makeHMS int  hour,
int  minute,
int  second,
int  millis
[inline, static]
 

Helper method to convert a broken down time to a number of milliseconds since midnight.

Definition at line 291 of file FieldTypes.h.

References MILLIS_PER_SEC, SECONDS_PER_HOUR, and SECONDS_PER_MIN.

Referenced by DateTime(), and setHMS().

00292   {
00293     return MILLIS_PER_SEC * (SECONDS_PER_HOUR * hour +
00294                              SECONDS_PER_MIN * minute +
00295                              second) + millis;
00296   }

DateTime FIX::DateTime::now  )  [static]
 

Return the current wall-clock time as a DateTime.

Definition at line 36 of file FieldTypes.cpp.

References fromTimeT().

Referenced by FIX::UtcDate::setCurrent(), and FIX::UtcTimeOnly::setCurrent().

00037 {
00038 #if defined (HAVE_FTIME)
00039     timeb tb;
00040     ftime (&tb);
00041     return fromTimeT (tb.time, tb.millitm);
00042 #elif defined (_POSIX_SOURCE)
00043     struct timeval tv;
00044     gettimeofday (&tv, 0);
00045     return fromTimeT (tv.tv_sec, tv.tv_usec / 1000);
00046 #else
00047     return fromTimeT (::time (0), 0);
00048 #endif
00049 }

void FIX::DateTime::operator+= int  seconds  )  [inline]
 

Add a number of seconds to this.

Definition at line 269 of file FieldTypes.h.

References m_date, m_time, MILLIS_PER_DAY, MILLIS_PER_SEC, and SECONDS_PER_DAY.

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   }

void FIX::DateTime::set const DateTime other  )  [inline]
 

Initialize from another DateTime.

Definition at line 262 of file FieldTypes.h.

References m_date, and m_time.

00263   {
00264     m_date = other.m_date;
00265     m_time = other.m_time;
00266   }

void FIX::DateTime::set int  date,
int  time
[inline]
 

Set the internal date and time members.

Definition at line 259 of file FieldTypes.h.

References m_date, and m_time.

Referenced by FIX::UtcTimeStamp::setCurrent().

00259 { m_date = date; m_time = time; }

void FIX::DateTime::setHMS int  hour,
int  minute,
int  second,
int  millis
[inline]
 

Set the time portion of the DateTime.

Definition at line 209 of file FieldTypes.h.

References m_time, and makeHMS().

Referenced by setHour(), setMillisecond(), setMinute(), setSecond(), FIX::UtcTimeOnly::UtcTimeOnly(), and FIX::UtcTimeStamp::UtcTimeStamp().

00210   {
00211     m_time = makeHMS( hour, minute, second, millis );
00212   }

void FIX::DateTime::setHour int  hour  )  [inline]
 

Set the hour portion of the time.

Definition at line 215 of file FieldTypes.h.

References getHMS(), and setHMS().

00216   {
00217     int old_hour, min, sec, millis;
00218     getHMS( old_hour, min, sec, millis );
00219     setHMS( hour, min, sec, millis );
00220   }

void FIX::DateTime::setMillisecond int  millis  )  [inline]
 

Set the millisecond portion of the time.

Definition at line 239 of file FieldTypes.h.

References getHMS(), and setHMS().

00240   {
00241     int hour, min, sec, old_millis;
00242     getHMS( hour, min, sec, old_millis );
00243     setHMS( hour, min, sec, millis );
00244   }

void FIX::DateTime::setMinute int  min  )  [inline]
 

Set the minute portion of the time.

Definition at line 223 of file FieldTypes.h.

References getHMS(), and setHMS().

00224   {
00225     int hour, old_min, sec, millis;
00226     getHMS( hour, old_min, sec, millis );
00227     setHMS( hour, min, sec, millis );
00228   }

void FIX::DateTime::setSecond int  sec  )  [inline]
 

Set the seconds portion of the time.

Definition at line 231 of file FieldTypes.h.

References getHMS(), and setHMS().

00232   {
00233     int hour, min, old_sec, millis;
00234     getHMS( hour, min, old_sec, millis );
00235     setHMS( hour, min, sec, millis );
00236   }

void FIX::DateTime::setYMD int  year,
int  month,
int  day
[inline]
 

Set the date portion of the DateTime.

Definition at line 203 of file FieldTypes.h.

References julianDate(), and m_date.

00204   {
00205     m_date = julianDate( year, month, day );
00206   }


Member Data Documentation

int FIX::DateTime::m_date
 

Definition at line 52 of file FieldTypes.h.

Referenced by clearDate(), DateTime(), getJulianDate(), getTimeT(), getYMD(), operator+=(), set(), FIX::UtcDate::setCurrent(), and setYMD().

int FIX::DateTime::m_time
 

Definition at line 53 of file FieldTypes.h.

Referenced by clearTime(), DateTime(), getHMS(), getHour(), getMillisecond(), getMinute(), getSecond(), getTimeT(), operator+=(), set(), FIX::UtcTimeOnly::setCurrent(), and setHMS().


The documentation for this struct was generated from the following files:
Generated on Mon Jul 24 19:36:43 2006 for QuickFIX by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2001