Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
FIX::DateTime Struct Reference

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]

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. More...
 
 DateTime (int date, int time)
 Construct from a Julian day number and time in millis. More...
 
 DateTime (int year, int month, int day, int hour, int minute, int second, int millis)
 Construct from the specified components. More...
 
virtual ~DateTime ()
 
int getYear () const
 Return the year portion of the date. More...
 
int getMonth () const
 Return the month (1-12) portion of the date. More...
 
int getDay () const
 Return the day of the month portion of the date. More...
 
int getDate () const
 Another name for the day of the month. More...
 
int getJulianDate () const
 Return the internal julian date. More...
 
int getHour () const
 Return the hour portion of the time (0-23) More...
 
int getMinute () const
 Return the minute portion of the time (0-59) More...
 
int getSecond () const
 Return the second portion of the time (0-59) More...
 
int getMillisecond () const
 Return the millisecond portion of the time. More...
 
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. More...
 
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. More...
 
int getWeekDay () const
 Calculate the weekday of the date (Sunday is 1, Saturday is 7) More...
 
time_t getTimeT () const
 Convert the DateTime to a time_t. More...
 
tm getTmUtc () const
 Convert the DateTime to a struct tm which is in UTC. More...
 
void setYMD (int year, int month, int day)
 Set the date portion of the DateTime. More...
 
void setHMS (int hour, int minute, int second, int millis)
 Set the time portion of the DateTime. More...
 
void setHour (int hour)
 Set the hour portion of the time. More...
 
void setMinute (int min)
 Set the minute portion of the time. More...
 
void setSecond (int sec)
 Set the seconds portion of the time. More...
 
void setMillisecond (int millis)
 Set the millisecond portion of the time. More...
 
void clearDate ()
 Clear the date portion of the DateTime. More...
 
void clearTime ()
 Clear the time portion of the DateTime. More...
 
void set (int date, int time)
 Set the internal date and time members. More...
 
void set (const DateTime &other)
 Initialize from another DateTime. More...
 
void operator+= (int seconds)
 Add a number of seconds to this. More...
 

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

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.

Constructor & Destructor Documentation

FIX::DateTime::DateTime ( )
inline

Default constructor - initializes to zero.

Definition at line 73 of file FieldTypes.h.

Referenced by fromTm().

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

76 : 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().

81  {
82  m_date = julianDate( year, month, day );
83  m_time = makeHMS( hour, minute, second, millis );
84  }
static int julianDate(int year, int month, int day)
Helper method to calculate a Julian day number.
Definition: FieldTypes.h:329
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.
Definition: FieldTypes.h:293
virtual FIX::DateTime::~DateTime ( )
inlinevirtual

Definition at line 86 of file FieldTypes.h.

86 {}

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().

250  {
251  m_date = 0;
252  }
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().

256  {
257  m_time = 0;
258  }
static DateTime FIX::DateTime::fromLocalTimeT ( time_t  t,
int  millis = 0 
)
inlinestatic

Definition at line 313 of file FieldTypes.h.

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

Referenced by nowLocal().

314  {
315  struct tm tm = time_localtime( &t );
316  return fromTm( tm, millis );
317  }
tm time_localtime(const time_t *t)
Definition: Utility.cpp:377
static DateTime fromTm(const tm &tm, int millis=0)
Convert a tm and optional milliseconds to a DateTime.
Definition: FieldTypes.h:321
static DateTime FIX::DateTime::fromTm ( const tm &  tm,
int  millis = 0 
)
inlinestatic

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().

322  {
323  return DateTime ( julianDate(tm.tm_year + 1900, tm.tm_mon + 1,
324  tm.tm_mday),
325  makeHMS(tm.tm_hour, tm.tm_min, tm.tm_sec, millis) );
326  }
static int julianDate(int year, int month, int day)
Helper method to calculate a Julian day number.
Definition: FieldTypes.h:329
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.
Definition: FieldTypes.h:293
DateTime()
Default constructor - initializes to zero.
Definition: FieldTypes.h:73
static DateTime FIX::DateTime::fromUtcTimeT ( time_t  t,
int  millis = 0 
)
inlinestatic

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().

308  {
309  struct tm tm = time_gmtime( &t );
310  return fromTm( tm, millis );
311  }
tm time_gmtime(const time_t *t)
Definition: Utility.cpp:361
static DateTime fromTm(const tm &tm, int millis=0)
Convert a tm and optional milliseconds to a DateTime.
Definition: FieldTypes.h:321
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().

114 { return getDay(); }
int getDay() const
Return the day of the month portion of the date.
Definition: FieldTypes.h:105
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().

106  {
107  int y, m, d;
108  getYMD( y, m, d );
109  return d;
110  }
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...
Definition: FieldTypes.h:145
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::UtcTimeStampConvertor::convert(), FIX::UtcTimeOnlyConvertor::convert(), getTmUtc(), setHour(), setMillisecond(), setMinute(), and setSecond().

153  {
154  int ticks = m_time / MILLIS_PER_SEC;
155  hour = ticks / SECONDS_PER_HOUR;
156  minute = (ticks / SECONDS_PER_MIN) % MINUTES_PER_HOUR;
157  second = ticks % SECONDS_PER_MIN;
158  millis = m_time % MILLIS_PER_SEC;
159  }
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().

121  {
122  return m_time / MILLIS_PER_HOUR;
123  }
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().

117 { 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.

139  {
140  return m_time % MILLIS_PER_SEC;
141  }
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().

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().

98  {
99  int y, m, d;
100  getYMD( y, m, d );
101  return m;
102  }
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...
Definition: FieldTypes.h:145
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().

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.

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

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().

185  {
186  int year, month, day;
187  int hour, minute, second, millis;
188  tm result = { 0 };
189 
190  getYMD( year, month, day );
191  getHMS( hour, minute, second, millis );
192 
193  result.tm_year = year - 1900;
194  result.tm_mon = month - 1;
195  result.tm_mday = day;
196  result.tm_hour = hour;
197  result.tm_min = minute;
198  result.tm_sec = second;
199  result.tm_isdst = -1;
200 
201  return result;
202  }
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...
Definition: FieldTypes.h:145
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 si...
Definition: FieldTypes.h:152
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().

163  {
164  int Y, M, D;
165  getYMD (Y, M, D);
166  int m = M >= 3 ? M - 2 : M + 10;
167  int Yprime = M >= 3 ? Y : Y - 1;
168  int y = Yprime % 100;
169  int c = Yprime / 100;
170  int wd = (D + int (2.6 * m - 0.2) + y + int (y / 4) + int (c / 4) -
171  (2 * c)) % 7;
172  return 1 + (wd < 0 ? 7 + wd : wd);
173  }
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...
Definition: FieldTypes.h:145
int FIX::DateTime::getYear ( ) const
inline

Return the year portion of the date.

Definition at line 89 of file FieldTypes.h.

References getYMD().

90  {
91  int y, m, d;
92  getYMD( y, m, d );
93  return y;
94  }
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...
Definition: FieldTypes.h:145
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().

146  {
147  getYMD( m_date, year, month, day );
148  }
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...
Definition: FieldTypes.h:145
static void FIX::DateTime::getYMD ( int  jday,
int &  year,
int &  month,
int &  day 
)
inlinestatic

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

Definition at line 339 of file FieldTypes.h.

340  {
341  int a = jday + 32044;
342  int b = (4 * a + 3) / 146097;
343  int c = a - int ((b * 146097) / 4);
344  int d = (4 * c + 3) / 1461;
345  int e = c - int ((1461 * d) / 4);
346  int m = (5 * e + 2) / 153;
347  day = e - int ((153 * m + 2) / 5) + 1;
348  month = m + 3 - 12 * int (m / 10);
349  year = b * 100 + d - 4800 + int (m / 10);
350  }
static int FIX::DateTime::julianDate ( int  year,
int  month,
int  day 
)
inlinestatic

Helper method to calculate a Julian day number.

Definition at line 329 of file FieldTypes.h.

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

330  {
331  int a = (14 - month) / 12;
332  int y = year + 4800 - a;
333  int m = month + 12 * a - 3;
334  return (day + int ((153 * m + 2) / 5) + y * 365 +
335  int (y / 4) - int (y / 100) + int (y / 400) - 32045);
336  }
static int FIX::DateTime::makeHMS ( int  hour,
int  minute,
int  second,
int  millis 
)
inlinestatic

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().

294  {
295  return MILLIS_PER_SEC * (SECONDS_PER_HOUR * hour +
296  SECONDS_PER_MIN * minute +
297  second) + millis;
298  }
DateTime FIX::DateTime::nowLocal ( )
static

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

Definition at line 49 of file FieldTypes.cpp.

References fromLocalTimeT().

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

50 {
51 #if defined( HAVE_FTIME )
52  timeb tb;
53  ftime (&tb);
54  return fromLocalTimeT( tb.time, tb.millitm );
55 #elif defined( _POSIX_SOURCE )
56  struct timeval tv;
57  gettimeofday (&tv, 0);
58  return fromLocalTimeT( tv.tv_sec, tv.tv_usec / 1000 );
59 #else
60  return fromLocalTimeT( ::time (0), 0 );
61 #endif
62 }
static DateTime fromLocalTimeT(time_t t, int millis=0)
Definition: FieldTypes.h:313
DateTime FIX::DateTime::nowUtc ( )
static

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

Definition at line 34 of file FieldTypes.cpp.

References fromUtcTimeT().

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

35 {
36 #if defined( HAVE_FTIME )
37  timeb tb;
38  ftime (&tb);
39  return fromUtcTimeT (tb.time, tb.millitm);
40 #elif defined( _POSIX_SOURCE )
41  struct timeval tv;
42  gettimeofday (&tv, 0);
43  return fromUtcTimeT( tv.tv_sec, tv.tv_usec / 1000 );
44 #else
45  return fromUtcTimeT( ::time (0), 0 );
46 #endif
47 }
static DateTime fromUtcTimeT(time_t t, int millis=0)
Convert a time_t and optional milliseconds to a DateTime.
Definition: FieldTypes.h:307
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.

272  {
273  int d = seconds / SECONDS_PER_DAY;
274  int s = seconds % SECONDS_PER_DAY;
275 
276  m_date += d;
277  m_time += s * MILLIS_PER_SEC;
278 
279  if( m_time > MILLIS_PER_DAY )
280  {
281  m_date++;
283  }
284  else if( m_time < 0 )
285  {
286  m_date--;
288  }
289  }
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.

Referenced by FIX::UtcTimeStamp::setCurrent(), and FIX::LocalTimeStamp::setCurrent().

261 { m_date = date; m_time = time; }
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.

265  {
266  m_date = other.m_date;
267  m_time = other.m_time;
268  }
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().

212  {
213  m_time = makeHMS( hour, minute, second, millis );
214  }
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.
Definition: FieldTypes.h:293
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().

218  {
219  int old_hour, min, sec, millis;
220  getHMS( old_hour, min, sec, millis );
221  setHMS( hour, min, sec, millis );
222  }
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 si...
Definition: FieldTypes.h:152
void setHMS(int hour, int minute, int second, int millis)
Set the time portion of the DateTime.
Definition: FieldTypes.h:211
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().

242  {
243  int hour, min, sec, old_millis;
244  getHMS( hour, min, sec, old_millis );
245  setHMS( hour, min, sec, millis );
246  }
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 si...
Definition: FieldTypes.h:152
void setHMS(int hour, int minute, int second, int millis)
Set the time portion of the DateTime.
Definition: FieldTypes.h:211
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().

226  {
227  int hour, old_min, sec, millis;
228  getHMS( hour, old_min, sec, millis );
229  setHMS( hour, min, sec, millis );
230  }
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 si...
Definition: FieldTypes.h:152
void setHMS(int hour, int minute, int second, int millis)
Set the time portion of the DateTime.
Definition: FieldTypes.h:211
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().

234  {
235  int hour, min, old_sec, millis;
236  getHMS( hour, min, old_sec, millis );
237  setHMS( hour, min, sec, millis );
238  }
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 si...
Definition: FieldTypes.h:152
void setHMS(int hour, int minute, int second, int millis)
Set the time portion of the DateTime.
Definition: FieldTypes.h:211
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.

206  {
207  m_date = julianDate( year, month, day );
208  }
static int julianDate(int year, int month, int day)
Helper method to calculate a Julian day number.
Definition: FieldTypes.h:329

Member Data Documentation

int FIX::DateTime::m_date
int FIX::DateTime::m_time

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

Generated on Sat Mar 29 2014 15:13:34 for QuickFIX by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2001