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.
virtual ~DateTime ()
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

static 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.
static DateTime nowUtc ()
 Return the current wall-clock time as a utc DateTime.
static DateTime nowLocal ()
 Return the current wall-clock time as a local DateTime.
static DateTime fromUtcTimeT (time_t t, int millis=0)
 Convert a time_t and optional milliseconds to a DateTime.
static DateTime fromLocalTimeT (time_t t, int millis=0)
static DateTime fromTm (const tm &tm, int millis=0)
 Convert a tm and optional milliseconds to a DateTime.
static int julianDate (int year, int month, int day)
 Helper method to calculate a Julian day number.
static 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.

Enumerator:
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.

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.

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   }

virtual FIX::DateTime::~DateTime (  )  [inline, virtual]

Definition at line 86 of file FieldTypes.h.

00086 {}


Member Function Documentation

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

Clear the date portion of the DateTime.

Definition at line 249 of file FieldTypes.h.

References m_date.

Referenced by FIX::LocalTimeOnly::LocalTimeOnly(), and FIX::UtcTimeOnly::UtcTimeOnly().

00250   {
00251     m_date = 0;
00252   }

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

Clear the time portion of the DateTime.

Definition at line 255 of file FieldTypes.h.

References m_time.

Referenced by FIX::LocalDate::LocalDate(), and FIX::UtcDate::UtcDate().

00256   {
00257     m_time = 0;
00258   }

static DateTime FIX::DateTime::fromLocalTimeT ( time_t  t,
int  millis = 0 
) [inline, static]

Definition at line 313 of file FieldTypes.h.

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

Referenced by nowLocal().

00314   {
00315     struct tm tm = time_localtime( &t );
00316     return fromTm( tm, millis );
00317   }

static 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 321 of file FieldTypes.h.

References DateTime(), julianDate(), and makeHMS().

Referenced by fromLocalTimeT(), and fromUtcTimeT().

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   }

static DateTime FIX::DateTime::fromUtcTimeT ( time_t  t,
int  millis = 0 
) [inline, static]

Convert a time_t and optional milliseconds to a DateTime.

Definition at line 307 of file FieldTypes.h.

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

Referenced by nowUtc().

00308   {
00309     struct tm tm = time_gmtime( &t );
00310     return fromTm( tm, millis );
00311   }

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 114 of file FieldTypes.h.

References getDay().

00114 { return getDay(); }

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

Return the day of the month portion of the date.

Definition at line 105 of file FieldTypes.h.

References getYMD().

Referenced by getDate().

00106   {
00107     int y, m, d;
00108     getYMD( y, m, d );
00109     return d;
00110   }

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 152 of file FieldTypes.h.

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

Referenced by FIX::UtcTimeOnlyConvertor::convert(), FIX::UtcTimeStampConvertor::convert(), getTmUtc(), setHour(), setMillisecond(), setMinute(), and setSecond().

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   }

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

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

Definition at line 120 of file FieldTypes.h.

References m_time, and MILLIS_PER_HOUR.

Referenced by FIX::SessionFactory::create().

00121   {
00122     return m_time / MILLIS_PER_HOUR;
00123   }

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

Return the internal julian date.

Definition at line 117 of file FieldTypes.h.

References m_date.

Referenced by FIX::TimeRange::isInSameRange().

00117 { return m_date; }

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

Return the millisecond portion of the time.

Definition at line 138 of file FieldTypes.h.

References m_time, and MILLIS_PER_SEC.

00139   {
00140     return m_time % MILLIS_PER_SEC;
00141   }

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

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

Definition at line 126 of file FieldTypes.h.

References m_time, MILLIS_PER_MIN, and MINUTES_PER_HOUR.

Referenced by FIX::SessionFactory::create().

00127   {
00128     return (m_time / MILLIS_PER_MIN) % MINUTES_PER_HOUR;
00129   }

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

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

Definition at line 97 of file FieldTypes.h.

References getYMD().

00098   {
00099     int y, m, d;
00100     getYMD( y, m, d );
00101     return m;
00102   }

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

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

Definition at line 132 of file FieldTypes.h.

References m_time, MILLIS_PER_SEC, and SECONDS_PER_MIN.

Referenced by FIX::SessionFactory::create().

00133   {
00134     return (m_time / MILLIS_PER_SEC) % SECONDS_PER_MIN;
00135   }

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 177 of file FieldTypes.h.

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

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

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

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

Definition at line 184 of file FieldTypes.h.

References getHMS(), and getYMD().

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   }

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

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

Definition at line 162 of file FieldTypes.h.

References getYMD().

Referenced by FIX::TimeRange::isInRange(), and FIX::TimeRange::isInSameRange().

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   }

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

Return the year portion of the date.

Definition at line 89 of file FieldTypes.h.

References getYMD().

00090   {
00091     int y, m, d;
00092     getYMD( y, m, d );
00093     return y;
00094   }

static 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 339 of file FieldTypes.h.

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   }

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 145 of file FieldTypes.h.

References m_date.

Referenced by FIX::UtcTimeStampConvertor::convert(), getDay(), getMonth(), getTmUtc(), getWeekDay(), and getYear().

00146   {
00147     getYMD( m_date, year, month, day );
00148   }

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

Helper method to calculate a Julian day number.

Definition at line 329 of file FieldTypes.h.

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

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   }

static 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 293 of file FieldTypes.h.

References MILLIS_PER_SEC, SECONDS_PER_HOUR, and SECONDS_PER_MIN.

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

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

DateTime FIX::DateTime::nowLocal (  )  [static]

Return the current wall-clock time as a local DateTime.

Definition at line 50 of file FieldTypes.cpp.

References fromLocalTimeT().

Referenced by FIX::LocalDate::setCurrent(), FIX::LocalTimeOnly::setCurrent(), and FIX::LocalTimeStamp::setCurrent().

00051 {
00052 #if defined( HAVE_FTIME )
00053     timeb tb;
00054     ftime (&tb);
00055     return fromLocalTimeT( tb.time, tb.millitm );
00056 #elif defined( _POSIX_SOURCE )
00057     struct timeval tv;
00058     gettimeofday (&tv, 0);
00059     return fromLocalTimeT( tv.tv_sec, tv.tv_usec / 1000 );
00060 #else
00061     return fromLocalTimeT( ::time (0), 0 );
00062 #endif
00063 }

DateTime FIX::DateTime::nowUtc (  )  [static]

Return the current wall-clock time as a utc DateTime.

Definition at line 35 of file FieldTypes.cpp.

References fromUtcTimeT().

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

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

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

Add a number of seconds to this.

Definition at line 271 of file FieldTypes.h.

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

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   }

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

Initialize from another DateTime.

Definition at line 264 of file FieldTypes.h.

References m_date, and m_time.

00265   {
00266     m_date = other.m_date;
00267     m_time = other.m_time;
00268   }

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

Set the internal date and time members.

Definition at line 261 of file FieldTypes.h.

References m_date, and m_time.

00261 { 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 211 of file FieldTypes.h.

References m_time, and makeHMS().

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

00212   {
00213     m_time = makeHMS( hour, minute, second, millis );
00214   }

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

Set the hour portion of the time.

Definition at line 217 of file FieldTypes.h.

References getHMS(), and setHMS().

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

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

Set the millisecond portion of the time.

Definition at line 241 of file FieldTypes.h.

References getHMS(), and setHMS().

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

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

Set the minute portion of the time.

Definition at line 225 of file FieldTypes.h.

References getHMS(), and setHMS().

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

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

Set the seconds portion of the time.

Definition at line 233 of file FieldTypes.h.

References getHMS(), and setHMS().

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

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

Set the date portion of the DateTime.

Definition at line 205 of file FieldTypes.h.

References julianDate(), and m_date.

00206   {
00207     m_date = julianDate( year, month, day );
00208   }


Member Data Documentation


The documentation for this struct was generated from the following files:

Generated on Mon Apr 5 21:00:04 2010 for QuickFIX by doxygen 1.6.1 written by Dimitri van Heesch, © 1997-2001