rllib  1
Public Member Functions | Public Attributes | Private Attributes
rlTime Class Reference

#include <rltime.h>

List of all members.

Public Member Functions

 rlTime (int Year=0, int Month=0, int Day=0, int Hour=0, int Minute=0, int Second=0, int Millisecond=0)
virtual ~rlTime ()
const char * getTimeString ()
const char * getIsoTimeString ()
void getLocalTime ()
int getFileModificationTime (const char *filename)
void setTimeFromString (const char *time_string)
void setTimeFromIsoString (const char *iso_time_string)
void setLocalTime ()
double secondsSinceEpoche ()
rlTimeoperator+= (rlTime &time)
rlTimeoperator-= (rlTime &time)
rlTime operator+ (rlTime &time)
rlTime operator- (rlTime &time)
int operator== (rlTime &time)
int operator< (rlTime &time)
int operator<= (rlTime &time)
int operator> (rlTime &time)
int operator>= (rlTime &time)

Public Attributes

int year
int month
int day
int hour
int minute
int second
int millisecond

Private Attributes

char time_string [32]

Detailed Description

class for handling time.

Definition at line 24 of file rltime.h.


Constructor & Destructor Documentation

rlTime::rlTime ( int  Year = 0,
int  Month = 0,
int  Day = 0,
int  Hour = 0,
int  Minute = 0,
int  Second = 0,
int  Millisecond = 0 
)

Definition at line 62 of file rltime.cpp.

{
  year        = Year;
  month       = Month;
  day         = Day;
  hour        = Hour;
  minute      = Minute;
  second      = Second;
  millisecond = Millisecond;
}
rlTime::~rlTime ( ) [virtual]

Definition at line 73 of file rltime.cpp.

{
}

Member Function Documentation

int rlTime::getFileModificationTime ( const char *  filename)

Definition at line 163 of file rltime.cpp.

{
  struct stat statbuf;
  struct tm *tms;

#ifdef RLUNIX
  if(lstat(filename,&statbuf)) return -1;
#else
  if(stat(filename,&statbuf)) return -1;
#endif
  tms = localtime(&statbuf.st_mtime);

  /* adjust year and month */
  tms->tm_year += 1900;
  tms->tm_mon  += 1;

  millisecond = 0;
  second      = (int)tms->tm_sec;
  minute      = (int)tms->tm_min;
  hour        = (int)tms->tm_hour;
  day         = (int)tms->tm_mday;
  month       = (int)tms->tm_mon;
  year        = (int)tms->tm_year;

  return 0;
}
const char * rlTime::getIsoTimeString ( )

Definition at line 107 of file rltime.cpp.

{
  sprintf(time_string,"%04d-%02d-%02dT%02d:%02d:%02d.%d",year,month,day, hour,minute,second, millisecond);
  return time_string;
}
void rlTime::getLocalTime ( )

Definition at line 113 of file rltime.cpp.

{
#ifdef RLUNIX
  time_t t;
  struct tm       *tms;
  struct timeval  tv;
  struct timezone tz;

  time(&t);
  tms = localtime(&t);
  gettimeofday(&tv, &tz);

  /* adjust year and month */
  tms->tm_year += 1900;
  tms->tm_mon  += 1;

  millisecond = (int)tv.tv_usec / 1000;
  second      = (int)tms->tm_sec;
  minute      = (int)tms->tm_min;
  hour        = (int)tms->tm_hour;
  day         = (int)tms->tm_mday;
  month       = (int)tms->tm_mon;
  year        = (int)tms->tm_year;
#endif

#ifdef __VMS
  TDS    tds;
  sys$numtim(&tds, 0);
  millisecond  = (int)tds.hth * 10;
  second       = (int)tds.sec;
  minute       = (int)tds.min;
  hour         = (int)tds.hour;
  day          = (int)tds.day;
  month        = (int)tds.month;
  year         = (int)tds.year;
#endif

#ifdef RLWIN32
  SYSTEMTIME st;
  GetLocalTime(&st);
  millisecond  = st.wMilliseconds;
  second       = st.wSecond;
  minute       = st.wMinute;
  hour         = st.wHour;
  day          = st.wDay;
  month        = st.wMonth;
  year         = st.wYear;
#endif
}
const char * rlTime::getTimeString ( )

Definition at line 101 of file rltime.cpp.

{
  sprintf(time_string,"%04d-%02d-%02d %02d:%02d:%02d %04d",year,month,day, hour,minute,second, millisecond);
  return time_string;
}
rlTime rlTime::operator+ ( rlTime time)

Definition at line 276 of file rltime.cpp.

{
  int maxmonth,y,m;
  rlTime t;

  t.year        = year        + time.year;
  t.month       = month       + time.month;
  t.day         = day         + time.day;
  t.hour        = hour        + time.hour;
  t.minute      = minute      + time.minute;
  t.second      = second      + time.second;
  t.millisecond = millisecond + time.millisecond;

  y = t.year;
  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) y++;
  m = t.month;
  if(t.month > 12 || (t.month==12 && t.day==31 && t.hour>=24)) m = 1;

  switch(m % 12)
  {
    case 1: // january
      maxmonth = 31;
      break;
    case 2: // february
      maxmonth = 28;
      // Annus bisextilis (calendario Gregoriano)
      if(y%4==0) 
      {
        maxmonth = 29;
        int hth = y % 100;
        int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
        if(hth == 0 && special != 0) maxmonth = 28;
      }  
      break;
    case 3: // march
      maxmonth = 31;
      break;
    case 4: // april
      maxmonth = 30;
      break;
    case 5: // may
      maxmonth = 31;
      break;
    case 6: // june
      maxmonth = 30;
      break;
    case 7: // july
      maxmonth = 31;
      break;
    case 8: // august
      maxmonth = 31;
      break;
    case 9: // september
      maxmonth = 30;
      break;
    case 10: // october
      maxmonth = 31;
      break;
    case 11: // november
      maxmonth = 30;
      break;
    case 12: // december
      maxmonth = 31;
      break;
    default:
      maxmonth = 31;
      break;
  }

  if(t.millisecond >= 1000) { t.second++; t.millisecond -= 1000; }
  if(t.second >= 60)        { t.minute++; t.second      -= 60; }
  if(t.minute >= 60)        { t.hour++,   t.minute      -= 60; }
  if(t.hour >= 24)          { t.day++;    t.hour        -= 24; }
  if(t.day > maxmonth)      { t.month++;  t.day         -= maxmonth; }
  if(t.month > 12)          { t.year++;   t.month       -= 12; }
  return t;
}
rlTime & rlTime::operator+= ( rlTime time)

Definition at line 260 of file rltime.cpp.

{
  rlTime t;
  t = *this + time;
  *this = t;
  return *this;
}
rlTime rlTime::operator- ( rlTime time)

Definition at line 354 of file rltime.cpp.

{
  int maxmonth,y,m;
  rlTime t;

  t.year        = year        - time.year;
  t.month       = month       - time.month;
  t.day         = day         - time.day;
  t.hour        = hour        - time.hour;
  t.minute      = minute      - time.minute;
  t.second      = second      - time.second;
  t.millisecond = millisecond - time.millisecond;

  if(t.millisecond < 0) { t.second--; t.millisecond += 1000; }
  if(t.second < 0)      { t.minute--; t.second      += 60; }
  if(t.minute < 0)      { t.hour--,   t.minute      += 60; }
  if(t.hour < 0)        { t.day--;    t.hour        += 24; }

  if(t.day < 0)
  {
    t.month--;
    y = t.year;
    m = t.month;
    if(m <= 0) { m += 12; y--; }
    switch(m % 12)
    {
      case 1: // january
        maxmonth = 31;
        break;
      case 2: // february
        maxmonth = 28;
        // Annus bisextilis (calendario Gregoriano)
        if(y%4==0) 
        {
          maxmonth = 29;
          int hth = y % 100;
          int special = y % 400; // 1900-+-2100-2200-2300-+-2500-2600-2700
          if(hth == 0 && special != 0) maxmonth = 28;
        }  
        break;
      case 3: // march
        maxmonth = 31;
        break;
      case 4: // april
        maxmonth = 30;
        break;
      case 5: // may
        maxmonth = 31;
        break;
      case 6: // june
        maxmonth = 30;
        break;
      case 7: // july
        maxmonth = 31;
        break;
      case 8: // august
        maxmonth = 31;
        break;
      case 9: // september
        maxmonth = 30;
        break;
      case 10: // october
        maxmonth = 31;
        break;
      case 11: // november
        maxmonth = 30;
        break;
      case 12: // december
        maxmonth = 31;
        break;
      default:
        maxmonth = 31;
        break;
    }
    t.day += maxmonth; 
  }
  if(t.month <= 0) { t.year--; t.month += 12; }

  return t;
}
rlTime & rlTime::operator-= ( rlTime time)

Definition at line 268 of file rltime.cpp.

{
  rlTime t;
  t = *this - time;
  *this = t;
  return *this;
}
int rlTime::operator< ( rlTime time)

Definition at line 448 of file rltime.cpp.

{
  rlTime diff,t1;

  t1.year        = year;
  t1.month       = month;
  t1.day         = day;
  t1.hour        = hour;
  t1.minute      = minute;
  t1.second      = second;
  t1.millisecond = millisecond;
  //printf("<t1=%s\n",t1.getTimeString());
  //printf("<time=%s\n",time.getTimeString());
  diff = t1 - time;
  //printf("<diff=%s\n",diff.getTimeString());
  if(diff.year        < 0) return 1;
  if(diff.month       < 0) return 1;
  if(diff.day         < 0) return 1;
  if(diff.hour        < 0) return 1;
  if(diff.minute      < 0) return 1;
  if(diff.second      < 0) return 1;
  if(diff.millisecond < 0) return 1;
  return 0;
}
int rlTime::operator<= ( rlTime time)

Definition at line 473 of file rltime.cpp.

{
  if((*this) == time) return 1;
  if((*this) <  time) return 1;
  return 0;
}
int rlTime::operator== ( rlTime time)

Definition at line 435 of file rltime.cpp.

{
  if(year        != time.year)        return 0;
  if(month       != time.month)       return 0;
  if(day         != time.day)         return 0;
  if(hour        != time.hour)        return 0;
  if(minute      != time.minute)      return 0;
  if(second      != time.second)      return 0;
  if(millisecond != time.millisecond) return 0;

  return 1;
}
int rlTime::operator> ( rlTime time)

Definition at line 480 of file rltime.cpp.

{
  rlTime diff,t1;

  t1.year        = year;
  t1.month       = month;
  t1.day         = day;
  t1.hour        = hour;
  t1.minute      = minute;
  t1.second      = second;
  t1.millisecond = millisecond;
  //printf(">t1=%s\n",t1.getTimeString());
  //printf(">time=%s\n",time.getTimeString());
  diff = time - t1;
  //printf(">diff=%s\n",diff.getTimeString());
  if(diff.year        < 0)        return 1;
  if(diff.month       < 0)        return 1;
  if(diff.day         < 0)        return 1;
  if(diff.hour        < 0)        return 1;
  if(diff.minute      < 0)        return 1;
  if(diff.second      < 0)        return 1;
  if(diff.millisecond < 0)        return 1;
  return 0;
}
int rlTime::operator>= ( rlTime time)

Definition at line 505 of file rltime.cpp.

{
  if((*this) == time) return 1;
  if((*this) >  time) return 1;
  return 0;
}
double rlTime::secondsSinceEpoche ( )

Definition at line 512 of file rltime.cpp.

{
  struct tm begin;
  struct tm test;

  memset(&begin,0,sizeof(tm));
  memset(&test,0,sizeof(tm));

  begin.tm_year = 70;
  begin.tm_mon  = 0;
  begin.tm_mday = 1;
  begin.tm_hour = 0;
  begin.tm_min  = 0;
  begin.tm_sec  = 0;

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

  time_t t0 = mktime(&begin);
  time_t t1 = mktime(&test);

  return difftime(t1,t0) + (((double) millisecond) / 1000);
}
void rlTime::setLocalTime ( )

Definition at line 190 of file rltime.cpp.

{
#ifdef RLUNIX
  struct timeval tv;
  struct tm t;

  t.tm_mday  = day;
  t.tm_mon   = month - 1;
  t.tm_year  = year - 1900;
  t.tm_hour  = hour;
  t.tm_min   = minute;
  t.tm_sec   = second;
  tv.tv_sec  = mktime(&t);
  tv.tv_usec = 1000 * millisecond;
  settimeofday(&tv,NULL);
#endif

#ifdef __VMS
  VAX_BIN_TIME vbt;
  struct dsc$descriptor_s  d_time;
  char smonth[12][4],buf[64];

  // Initialize month array
  memset (smonth   , 0, sizeof(smonth));
  memcpy (smonth  [0], "JAN", 3);
  memcpy (smonth  [1], "FEB", 3);
  memcpy (smonth  [2], "MAR", 3);
  memcpy (smonth  [3], "APR", 3);
  memcpy (smonth  [4], "MAY", 3);
  memcpy (smonth  [5], "JUN", 3);
  memcpy (smonth  [6], "JUL", 3);
  memcpy (smonth  [7], "AUG", 3);
  memcpy (smonth  [8], "SEP", 3);
  memcpy (smonth  [9], "OCT", 3);
  memcpy (smonth [10], "NOV", 3);
  memcpy (smonth [11], "DEC", 3);
  // Create time buffer
  sprintf(buf, "%02d-%3.3s-%04d %02d:%02d:%02d.%02d",
                day,
                smonth[month-1],
                year,
                hour,
                minute,
                second,
                millisecond / 10);

  // Fill string descriptor
  d_time.dsc$w_length  = strlen(buf);
  d_time.dsc$b_dtype   = DSC$K_DTYPE_T;
  d_time.dsc$b_class   = DSC$K_CLASS_S;
  d_time.dsc$a_pointer = buf;
  // Convert time buf to VAX bin time
  sys$bintim(&d_time, &vbt);
  // Set the system time
  sys$setime(&vbt);
#endif

#ifdef RLWIN32
  SYSTEMTIME st;
  st.wDay          = day;
  st.wMonth        = month;
  st.wYear         = year;
  st.wHour         = hour;
  st.wMinute       = minute;
  st.wSecond       = second;
  st.wMilliseconds = millisecond;
  SetSystemTime(&st);
#endif
}
void rlTime::setTimeFromIsoString ( const char *  iso_time_string)

Definition at line 89 of file rltime.cpp.

{
  year        = 0;
  month       = 0;
  day         = 0;
  hour        = 0;
  minute      = 0;
  second      = 0;
  millisecond = 0;
  sscanf(iso_time_string,"%d-%d-%dT%d:%d:%d.%d",&year,&month,&day, &hour,&minute,&second, &millisecond);
}
void rlTime::setTimeFromString ( const char *  time_string)
  format: sscanf(time_string,"%d-%d-%d %d:%d:%d %d",&year,&month,&day, &hour,&minute,&second, &millisecond);
  

Definition at line 77 of file rltime.cpp.

{
  year        = 0;
  month       = 0;
  day         = 0;
  hour        = 0;
  minute      = 0;
  second      = 0;
  millisecond = 0;
  sscanf(time_string,"%d-%d-%d %d:%d:%d %d",&year,&month,&day, &hour,&minute,&second, &millisecond);
}

Member Data Documentation

Definition at line 52 of file rltime.h.

Definition at line 53 of file rltime.h.

Definition at line 56 of file rltime.h.

Definition at line 54 of file rltime.h.

Definition at line 51 of file rltime.h.

Definition at line 55 of file rltime.h.

char rlTime::time_string[32] [private]

Definition at line 58 of file rltime.h.

Definition at line 50 of file rltime.h.


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines