Public Types | Public Member Functions | Static Public Member Functions | Public Attributes
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]

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.

  {
    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,

    // time_t epoch (1970-01-01) as a Julian date
    JULIAN_19700101 = 2440588
  };

Constructor & Destructor Documentation

Default constructor - initializes to zero.

Definition at line 73 of file FieldTypes.h.

Referenced by fromTm().

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

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

  {
    m_date = julianDate( year, month, day );
    m_time = makeHMS( hour, minute, second, millis );
  }
virtual FIX::DateTime::~DateTime ( ) [inline, virtual]

Definition at line 86 of file FieldTypes.h.

{}

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

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

  {
    m_time = 0;
  }
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().

  {
    struct tm tm = time_localtime( &t );
    return fromTm( tm, millis );
  }
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().

  {
    return DateTime ( julianDate(tm.tm_year + 1900, tm.tm_mon + 1,
                                 tm.tm_mday),
                     makeHMS(tm.tm_hour, tm.tm_min, tm.tm_sec, millis) );
  }
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().

  {
    struct tm tm = time_gmtime( &t );
    return fromTm( tm, millis );
  }
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().

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

  {
    int y, m, d;
    getYMD( y, m, d );
    return d;
  }
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().

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

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

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

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

  {
    int y, m, d;
    getYMD( y, m, d );
    return m;
  }
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().

  {
    int year, month, day;
    int hour, minute, second, millis;
    tm result = { 0 };

    getYMD( year, month, day );
    getHMS( hour, minute, second, millis );

    result.tm_year = year - 1900;
    result.tm_mon = month - 1;
    result.tm_mday = day;
    result.tm_hour = hour;
    result.tm_min = minute;
    result.tm_sec = second;
    result.tm_isdst = -1;

    return result;
  }
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().

  {
    int Y, M, D;
    getYMD (Y, M, D);
    int m = M >= 3 ? M - 2 : M + 10;
    int Yprime = M >= 3 ? Y : Y - 1;
    int y = Yprime % 100;
    int c = Yprime / 100;
    int wd = (D + int (2.6 * m - 0.2) + y + int (y / 4) + int (c / 4) -
              (2 * c)) % 7;
    return 1 + (wd < 0 ? 7 + wd : wd);
  }
int FIX::DateTime::getYear ( ) const [inline]

Return the year portion of the date.

Definition at line 89 of file FieldTypes.h.

References getYMD().

  {
    int y, m, d;
    getYMD( y, m, d );
    return y;
  }
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().

  {
    getYMD( m_date, year, month, day );
  }
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.

  {
    int a = jday + 32044;
    int b = (4 * a + 3) / 146097;
    int c = a - int ((b * 146097) / 4);
    int d = (4 * c + 3) / 1461;
    int e = c - int ((1461 * d) / 4);
    int m = (5 * e + 2) / 153;
    day = e - int ((153 * m + 2) / 5) + 1;
    month = m + 3 - 12 * int (m / 10);
    year = b * 100 + d - 4800 + int (m / 10);
  }
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().

  {
    int a = (14 - month) / 12;
    int y = year + 4800 - a;
    int m = month + 12 * a - 3;
    return (day + int ((153 * m + 2) / 5) + y * 365 +
            int (y / 4) - int (y / 100) + int (y / 400) - 32045);
  }
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().

  {
    return MILLIS_PER_SEC * (SECONDS_PER_HOUR * hour +
                             SECONDS_PER_MIN * minute +
                             second) + millis;
  }

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

{
#if defined( HAVE_FTIME )
    timeb tb;
    ftime (&tb);
    return fromLocalTimeT( tb.time, tb.millitm );
#elif defined( _POSIX_SOURCE )
    struct timeval tv;
    gettimeofday (&tv, 0);
    return fromLocalTimeT( tv.tv_sec, tv.tv_usec / 1000 );
#else
    return fromLocalTimeT( ::time (0), 0 );
#endif
}

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

{
#if defined( HAVE_FTIME )
    timeb tb;
    ftime (&tb);
    return fromUtcTimeT (tb.time, tb.millitm);
#elif defined( _POSIX_SOURCE )
    struct timeval tv;
    gettimeofday (&tv, 0);
    return fromUtcTimeT( tv.tv_sec, tv.tv_usec / 1000 );
#else
    return fromUtcTimeT( ::time (0), 0 );
#endif
}
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.

  {
    int d = seconds / SECONDS_PER_DAY;
    int s = seconds % SECONDS_PER_DAY;

    m_date += d;
    m_time += s * MILLIS_PER_SEC;

    if( m_time > MILLIS_PER_DAY )
    {
      m_date++;
      m_time %= MILLIS_PER_DAY;
    }
    else if( m_time < 0 )
    {
      m_date--;
      m_time += MILLIS_PER_DAY;
    }
  }
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.

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

  {
    m_date = other.m_date;
    m_time = other.m_time;
  }
void FIX::DateTime::setHMS ( int  hour,
int  minute,
int  second,
int  millis 
) [inline]
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().

  {
    int old_hour, min, sec, millis;
    getHMS( old_hour, min, sec, millis );
    setHMS( hour, min, sec, millis );
  }
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().

  {
    int hour, min, sec, old_millis;
    getHMS( hour, min, sec, old_millis );
    setHMS( hour, min, sec, millis );
  }
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().

  {
    int hour, old_min, sec, millis;
    getHMS( hour, old_min, sec, millis );
    setHMS( hour, min, sec, millis );
  }
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().

  {
    int hour, min, old_sec, millis;
    getHMS( hour, min, old_sec, millis );
    setHMS( hour, min, sec, millis );
  }
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.

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

Member Data Documentation


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

Generated on Mon Jun 23 2014 23:49:39 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001