Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Core::Time Class Reference

#include <Time.h>

Inheritance diagram for Core::Time:

Inheritance graph
[legend]
Collaboration diagram for Core::Time:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Time (int hour=0, int minute=0, int second=0, int milli=0)
virtual StringclassName (void) const
 Answer receiver class name.
virtual int hour (void) const
virtual int minute (void) const
virtual int second (void) const
virtual int millisecond (void) const
virtual TimeaddHours (int hours)
 Adds the given number of hours to the current time.
virtual TimeaddMinutes (int minutes)
 Adds the given number of minutes to the current time.
virtual TimeaddSeconds (int seconds)
 Adds the given number of seconds to the current time.
virtual TimeaddMilliseconds (int millis)
 Adds the given number of milliseconds to the current time.
virtual TimesubtractHours (int hours)
virtual TimesubtractMinutes (int minutes)
virtual TimesubtractSeconds (int seconds)
virtual TimesubtractMilliseconds (int millis)
virtual TimedifferenceFrom (Time *time)
virtual long hash (void) const
 Answer object hash value.
virtual bool isEqual (const Object *object) const
 Compare receiver with given object.
virtual bool isEqual (const Object &object) const
 Compare receiver with given object.
virtual unsigned long asMilliseconds (void) const
virtual void printOn (Stream *stream) const
 Print object identification into the stream.

Static Public Member Functions

static Timenow ()
 Returns a Time with the system's current time.
static Timemidnight ()
 Returns a Time of midnight (0:00:00.000).
static Timezero ()

Protected Attributes

unsigned long _hours
unsigned char _minutes
unsigned char _seconds
unsigned short _milliseconds

Constructor & Destructor Documentation

Time::Time int  hour = 0,
int  minute = 0,
int  second = 0,
int  milli = 0
 

Definition at line 31 of file Time.cc.

References _hours, _milliseconds, _minutes, and _seconds.

Referenced by addHours(), addMilliseconds(), now(), subtractHours(), and subtractMilliseconds().

00032 {
00033     _hours = hour;
00034     _minutes = minute;
00035     _seconds = second;
00036     _milliseconds = milli;
00037 }


Member Function Documentation

Time * Time::addHours int  hours  )  [virtual]
 

Adds the given number of hours to the current time.

Definition at line 92 of file Time.cc.

References _hours, _milliseconds, _minutes, _seconds, and Time().

00093 {
00094     return new Time(_hours + hours, _minutes, _seconds, _milliseconds);
00095 }

Time * Time::addMilliseconds int  millis  )  [virtual]
 

Adds the given number of milliseconds to the current time.

The seconds roll over if they would exceed the 1000 millisecond limit (0 - 999).

Definition at line 112 of file Time.cc.

References _hours, _milliseconds, _minutes, _seconds, and Time().

Referenced by addSeconds().

00113 {
00114     int hours, minutes, seconds;
00115     millis += _milliseconds;
00116     seconds = _seconds + millis / 1000;
00117     minutes = _minutes + seconds / 60;
00118     hours = _hours + minutes / 60;
00119     minutes = minutes % 60;
00120     seconds = seconds % 60;
00121     millis = millis % 1000;
00122     return new Time(hours, minutes, seconds, millis);
00123 }

Time * Time::addMinutes int  minutes  )  [virtual]
 

Adds the given number of minutes to the current time.

The minutes roll over if they would exceed the 60 minute limit (0 - 59).

Definition at line 98 of file Time.cc.

References addSeconds().

00099 {
00100     return addSeconds(minutes * 60);
00101 }

Time * Time::addSeconds int  seconds  )  [virtual]
 

Adds the given number of seconds to the current time.

The seconds roll over if they would exceed the 60 second limit (0 - 59).

Definition at line 104 of file Time.cc.

References addMilliseconds().

Referenced by addMinutes().

00105 {
00106     return addMilliseconds(seconds * 1000);
00107 }

unsigned long Time::asMilliseconds void   )  const [virtual]
 

Definition at line 188 of file Time.cc.

References _hours, _milliseconds, _minutes, and _seconds.

Referenced by differenceFrom().

00189 {
00190     return ((_hours * 60 + _minutes) * 60 + _seconds) * 1000 + _milliseconds;
00191 }

String * Time::className void   )  const [virtual]
 

Answer receiver class name.

Because there isn't any standard way to obtain class name this method comes to place.

Every class should rewrite this method but many didn't (yet).

Reimplemented from Core::Object.

Definition at line 40 of file Time.cc.

Referenced by isEqual().

00041 {
00042     return new String("Time");
00043 }

Time * Time::differenceFrom Time time  )  [virtual]
 

Definition at line 161 of file Time.cc.

References asMilliseconds(), and zero().

00162 {
00163     unsigned long millis = abs(asMilliseconds() - time->asMilliseconds());
00164     return Time::zero()->addMilliseconds(millis);
00165 }

long Time::hash void   )  const [virtual]
 

Answer object hash value.

The value should be same for objects found equal with isEqual() method. So if you rewrite this method you should rewrite isEqual() method too.

Reimplemented from Core::Object.

Definition at line 168 of file Time.cc.

References _hours, _milliseconds, _minutes, and _seconds.

00169 {
00170     long hashVal = _hours;
00171     hashVal += _minutes << 10;
00172     hashVal += _seconds << 16;
00173     hashVal += _milliseconds << 22;
00174 }

int Time::hour void   )  const [virtual]
 

Definition at line 70 of file Time.cc.

References _hours.

Referenced by Core::Timestamp::fromDateAndTime(), and printOn().

00071 {
00072     return _hours;
00073 }

virtual bool Core::Time::isEqual const Object object  )  const [inline, virtual]
 

Compare receiver with given object.

Do not rewrite this method. It just a sugar for Object::isEqual(Object *).

  • object - object to compare receiver with

Reimplemented from Core::Object.

Definition at line 90 of file Time.h.

References Core::Object::isEqual().

00091       { return Object::isEqual(object); }

bool Time::isEqual const Object object  )  const [virtual]
 

Compare receiver with given object.

This method could compare objects with more sophisticated algorithm (eg. based on instance variables comparing or so).

If you rewrite this method you should rewrite hash() too.

  • object - object to compare receiver with

Reimplemented from Core::Object.

Definition at line 176 of file Time.cc.

References _hours, _milliseconds, _minutes, _seconds, className(), and Core::Object::className().

00177 {
00178     if (!object->className()->isEqual(className())) return false;
00179 
00180     const Time *t = dynamic_cast<const Time *>(object);
00181     return t->_hours == this->_hours &&
00182            t->_minutes == this->_minutes &&
00183            t->_seconds == this->_seconds &&
00184            t->_milliseconds == this->_milliseconds;
00185 }

Time * Time::midnight  )  [static]
 

Returns a Time of midnight (0:00:00.000).

Definition at line 59 of file Time.cc.

References zero().

00060 {
00061     return Time::zero();
00062 }

int Time::millisecond void   )  const [virtual]
 

Definition at line 85 of file Time.cc.

References _milliseconds.

Referenced by Core::Timestamp::fromDateAndTime(), and printOn().

00086 {
00087     return _milliseconds;
00088 }

int Time::minute void   )  const [virtual]
 

Definition at line 75 of file Time.cc.

References _minutes.

Referenced by Core::Timestamp::fromDateAndTime(), and printOn().

00076 {
00077     return _minutes;
00078 }

Time * Time::now  )  [static]
 

Returns a Time with the system's current time.

Definition at line 47 of file Time.cc.

References Time().

00048 {
00049     struct timeval timestamp;
00050     struct timezone tz;
00051 
00052     gettimeofday(&timestamp, &tz);
00053     const struct tm* time = localtime(&timestamp.tv_sec);
00054     return new Time(time->tm_hour, time->tm_min, time->tm_sec,
00055                     timestamp.tv_usec/1000);
00056 }

void Time::printOn Stream stream  )  const [virtual]
 

Print object identification into the stream.

Object identification is formed from its className() by default. But complicated classes (eg. collections) may print some other information.

  • stream - stream to print to.

Reimplemented from Core::Object.

Definition at line 194 of file Time.cc.

References Core::String::format(), hour(), millisecond(), minute(), and second().

00195 {
00196     String *timeStr;
00197     timeStr = String::format("%u:%02u:%02u.%03u",
00198                              hour(), minute(), second(), millisecond());
00199     stream->nextPutAll(timeStr);
00200 }

int Time::second void   )  const [virtual]
 

Definition at line 80 of file Time.cc.

References _seconds.

Referenced by Core::Timestamp::fromDateAndTime(), and printOn().

00081 {
00082     return _seconds;
00083 }

Time * Time::subtractHours int  hours  )  [virtual]
 

Definition at line 125 of file Time.cc.

References _hours, _milliseconds, _minutes, _seconds, and Time().

00126 {
00127     return new Time(_hours - hours, _minutes, _seconds, _milliseconds);
00128 }

Time * Time::subtractMilliseconds int  millis  )  [virtual]
 

Definition at line 140 of file Time.cc.

References _hours, _milliseconds, _minutes, _seconds, and Time().

Referenced by subtractSeconds().

00141 {
00142     int seconds = millis / 1000;
00143     millis = _milliseconds - millis % 1000;
00144     while (millis < 1000) {
00145         seconds++; millis -= 1000;
00146     }
00147     int minutes = seconds / 60;
00148     seconds = _seconds - seconds % 60;
00149     while (seconds < 60) {
00150         minutes++; seconds -= 60;
00151     }
00152     int hours = minutes / 60;
00153     minutes = _minutes - minutes % 60;
00154     while (minutes < 60) {
00155         hours++; minutes -= 60;
00156     }
00157     hours = _hours - hours;
00158     return new Time(hours, minutes, seconds, millis);
00159 }

Time * Time::subtractMinutes int  minutes  )  [virtual]
 

Definition at line 130 of file Time.cc.

References subtractSeconds().

00131 {
00132     return subtractSeconds(minutes * 60);
00133 }

Time * Time::subtractSeconds int  seconds  )  [virtual]
 

Definition at line 135 of file Time.cc.

References subtractMilliseconds().

Referenced by subtractMinutes().

00136 {
00137     return subtractMilliseconds(seconds * 1000);
00138 }

Time * Time::zero  )  [static]
 

Definition at line 64 of file Time.cc.

Referenced by differenceFrom(), and midnight().

00065 {
00066     return new Time;
00067 }


Member Data Documentation

unsigned long Core::Time::_hours [protected]
 

Definition at line 37 of file Time.h.

Referenced by addHours(), addMilliseconds(), asMilliseconds(), hash(), hour(), isEqual(), subtractHours(), subtractMilliseconds(), and Time().

unsigned short Core::Time::_milliseconds [protected]
 

Definition at line 40 of file Time.h.

Referenced by addHours(), addMilliseconds(), asMilliseconds(), hash(), isEqual(), millisecond(), subtractHours(), subtractMilliseconds(), and Time().

unsigned char Core::Time::_minutes [protected]
 

Definition at line 38 of file Time.h.

Referenced by addHours(), addMilliseconds(), asMilliseconds(), hash(), isEqual(), minute(), subtractHours(), subtractMilliseconds(), and Time().

unsigned char Core::Time::_seconds [protected]
 

Definition at line 39 of file Time.h.

Referenced by addHours(), addMilliseconds(), asMilliseconds(), hash(), isEqual(), second(), subtractHours(), subtractMilliseconds(), and Time().


The documentation for this class was generated from the following files:
Generated on Mon Nov 27 09:52:02 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2