Wt::Time Class Reference

A time class to escape SDL's 2^32 ms epoch limitations. More...

#include <datetime.h>

Collaboration diagram for Wt::Time:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Time ()
 Time (int h, int m, int s=0, int ms=0)
bool isNull () const
bool isValid () const
int hour () const
int minute () const
int second () const
int msec () const
 operator std::string () const
std::string toString () const
bool setHMS (int h, int m, int s, int ms=0)
Time addSecs (int nsecs) const
int secsTo (const Time &t) const
Time addMSecs (int ms) const
int msecsTo (const Time &t) const
bool operator== (const Time &t) const
bool operator!= (const Time &t) const
bool operator< (const Time &t) const
bool operator<= (const Time &t) const
bool operator> (const Time &t) const
bool operator>= (const Time &t) const
void start ()
int restart ()
int elapsed () const

Static Public Member Functions

static Time currentTime ()
static Time fromString (const std::string &s)
static bool isValid (int h, int m, int s, int ms=0)

Private Member Functions

int add_and_wrap (int &start, int duration, int range) const

Private Attributes

int hour_
int minute_
int second_
int msec_

Detailed Description

A time class to escape SDL's 2^32 ms epoch limitations.

Definition at line 29 of file datetime.h.


Constructor & Destructor Documentation

Wt::Time::Time (  ) 

Definition at line 31 of file datetime.cpp.

Referenced by fromString().

00031            :
00032         hour_(0),
00033         minute_(0),
00034         second_(0),
00035 msec_(0) {}

Wt::Time::Time ( int  h,
int  m,
int  s = 0,
int  ms = 0 
)

Definition at line 37 of file datetime.cpp.

00037                                       :
00038         hour_(h),
00039         minute_(m),
00040         second_(s),
00041 msec_(ms) {}


Member Function Documentation

int Wt::Time::add_and_wrap ( int start,
int  duration,
int  range 
) const [private]

Definition at line 75 of file datetime.cpp.

References start().

Referenced by addMSecs(), and addSecs().

00075                                                                 {
00076     start += duration;
00077     int p = start % range;
00078     int wrap = (start != p) ? 1 : 0;
00079     start = p;
00080     return wrap;
00081 }

Here is the call graph for this function:

Time Wt::Time::addMSecs ( int  ms  )  const

Definition at line 103 of file datetime.cpp.

References add_and_wrap(), hour_, minute_, msec_, and second_.

Referenced by currentTime().

00103                                 {
00104     int nh = ms / 3600000;
00105     int nm = (ms - nh * 3600000) / 60000;
00106     int ns = (ms - nh * 3600000 - nm * 60000) / 1000;
00107     int nms = (ms - nh * 3600000 - nm * 60000 - ns * 1000);
00108 
00109     Time t = *this;
00110     int wrap;
00111     wrap = add_and_wrap(t.msec_, nms, 1000);
00112     wrap = add_and_wrap(t.second_, ns + wrap, 60);
00113     wrap = add_and_wrap(t.minute_, nm + wrap, 60);
00114     wrap = add_and_wrap(t.hour_, nh + wrap, 24);
00115     return t;
00116 }

Here is the call graph for this function:

Time Wt::Time::addSecs ( int  nsecs  )  const

Definition at line 83 of file datetime.cpp.

References add_and_wrap(), hour_, minute_, and second_.

00083                                   {
00084     int nh = nsecs / 3600;
00085     int nm = (nsecs - nh * 3600) / 60;
00086     int ns = (nsecs - nh * 3600 - nm * 60);
00087 
00088     Time t = *this;
00089     int wrap;
00090     wrap = add_and_wrap(t.second_, ns, 60);
00091     wrap = add_and_wrap(t.minute_, nm + wrap, 60);
00092     wrap = add_and_wrap(t.hour_, nh + wrap, 24);
00093     return t;
00094 }

Here is the call graph for this function:

Time Wt::Time::currentTime (  )  [static]

Definition at line 204 of file datetime.cpp.

References addMSecs(), and trace.

Referenced by elapsed(), restart(), and start().

00204                        {
00205     Time t;
00206     Uint32 ticks = SDL_GetTicks();
00207     trace("time", "Ticks %u\n", ticks);
00208     return t.addMSecs(ticks);
00209 }

Here is the call graph for this function:

int Wt::Time::elapsed (  )  const

Definition at line 198 of file datetime.cpp.

References currentTime(), and msecsTo().

Referenced by Wt::TimerBlock::operator()().

00198                         {
00199     Time t = currentTime();
00200     int ms = msecsTo(t);
00201     return ms;
00202 }

Here is the call graph for this function:

Time Wt::Time::fromString ( const std::string &  s  )  [static]

Definition at line 211 of file datetime.cpp.

References Time().

00211                                                       {
00212     std::istringstream iss(timedesctiption);
00213     std::string h, m, s, ms;
00214     std::getline(iss, h, ':');
00215     std::getline(iss, m, ':');
00216     std::getline(iss, s, '.');
00217     std::getline(iss, ms, ' ');
00218 
00219     return Time(atoi(h.c_str()), atoi(m.c_str()),
00220                 atoi(s.c_str()), atoi(ms.c_str()));
00221 }

Here is the call graph for this function:

int Wt::Time::hour (  )  const [inline]

Definition at line 35 of file datetime.h.

References hour_.

00035                      {
00036         return hour_;
00037     }

bool Wt::Time::isNull (  )  const

Definition at line 43 of file datetime.cpp.

References hour_, minute_, msec_, and second_.

00043                         {
00044     return (hour_ == 0 && minute_ == 0 && second_ == 0 && msec_ == 0);
00045 }

bool Wt::Time::isValid ( int  h,
int  m,
int  s,
int  ms = 0 
) [static]

Definition at line 223 of file datetime.cpp.

References isValid().

00223                                               {
00224     Time t(h, m, s, ms);
00225     return t.isValid();
00226 }

Here is the call graph for this function:

bool Wt::Time::isValid (  )  const

Definition at line 47 of file datetime.cpp.

References hour_, minute_, msec_, and second_.

Referenced by isValid(), and setHMS().

00047                          {
00048     return (0 <= hour_ && hour_ <= 23 &&
00049             0 <= minute_ && minute_ <= 59 &&
00050             0 <= second_ && second_ <= 59 &&
00051             0 <= msec_ && msec_ <= 999);
00052 }

int Wt::Time::minute (  )  const [inline]

Definition at line 38 of file datetime.h.

References minute_.

00038                        {
00039         return minute_;
00040     }

int Wt::Time::msec (  )  const [inline]

Definition at line 44 of file datetime.h.

References msec_.

00044                      {
00045         return msec_;
00046     }

int Wt::Time::msecsTo ( const Time t  )  const

Definition at line 118 of file datetime.cpp.

References msec_, and secsTo().

Referenced by elapsed(), Wt::SDLInput::EventInfo::operator==(), and restart().

00118                                      {
00119     return secsTo(t) * 1000 + t.msec_ - msec_;
00120 }

Here is the call graph for this function:

Wt::Time::operator std::string (  )  const

Definition at line 54 of file datetime.cpp.

References hour_, minute_, msec_, and second_.

00054                                {
00055     std::stringstream s;
00056     s << std::setw(2) << std::setfill('0') << hour_ << ":"
00057     << std::setw(2) << std::setfill('0') << minute_ <<  ":"
00058     << std::setw(2) << std::setfill('0') << second_ << "."
00059     << std::setw(3) << std::setfill('0') << msec_;
00060     return s.str();
00061 }

bool Wt::Time::operator!= ( const Time t  )  const

Definition at line 127 of file datetime.cpp.

00127                                          {
00128     return !(*this == t);
00129 }

bool Wt::Time::operator< ( const Time t  )  const

Definition at line 131 of file datetime.cpp.

References hour_, minute_, msec_, and second_.

00131                                         {
00132     if (hour_ < t.hour_)
00133         return true;
00134     if (hour_ > t.hour_)
00135         return false;
00136 
00137     if (minute_ < t.minute_)
00138         return true;
00139     if (minute_ > t.minute_)
00140         return false;
00141 
00142     if (second_ < t.second_)
00143         return true;
00144     if (second_ > t.second_)
00145         return false;
00146 
00147     if (msec_ < t.msec_)
00148         return true;
00149     if (msec_ > t.msec_)
00150         return false;
00151 
00152     return false;
00153 }

bool Wt::Time::operator<= ( const Time t  )  const

Definition at line 183 of file datetime.cpp.

00183                                          {
00184     return !(*this > t);
00185 }

bool Wt::Time::operator== ( const Time t  )  const

Definition at line 122 of file datetime.cpp.

References hour_, minute_, msec_, and second_.

00122                                          {
00123     return (hour_ == t.hour_ && minute_ == t.minute_ &&
00124             second_ == t.second_ && msec_ == t.msec_);
00125 }

bool Wt::Time::operator> ( const Time t  )  const

Definition at line 159 of file datetime.cpp.

References hour_, minute_, msec_, and second_.

00159                                         {
00160     if (hour_ > t.hour_)
00161         return true;
00162     if (hour_ < t.hour_)
00163         return false;
00164 
00165     if (minute_ > t.minute_)
00166         return true;
00167     if (minute_ < t.minute_)
00168         return false;
00169 
00170     if (second_ > t.second_)
00171         return true;
00172     if (second_ < t.second_)
00173         return false;
00174 
00175     if (msec_ > t.msec_)
00176         return true;
00177     if (msec_ < t.msec_)
00178         return false;
00179 
00180     return false;
00181 }

bool Wt::Time::operator>= ( const Time t  )  const

Definition at line 155 of file datetime.cpp.

00155                                          {
00156     return !(*this < t);
00157 }

int Wt::Time::restart (  ) 

Definition at line 191 of file datetime.cpp.

References currentTime(), and msecsTo().

00191                   {
00192     const Time& t = currentTime();
00193     int ms = msecsTo(t);
00194     *this = t;
00195     return ms;
00196 }

Here is the call graph for this function:

int Wt::Time::second (  )  const [inline]

Definition at line 41 of file datetime.h.

References second_.

00041                        {
00042         return second_;
00043     }

int Wt::Time::secsTo ( const Time t  )  const

Definition at line 96 of file datetime.cpp.

References hour_, minute_, and second_.

Referenced by msecsTo().

00096                                     {
00097     int dh = t.hour_ - hour_;
00098     int dm = dh * 60 + t.minute_ - minute_;
00099     int ds = dm * 60 + t.second_ - second_;
00100     return ds;
00101 }

bool Wt::Time::setHMS ( int  h,
int  m,
int  s,
int  ms = 0 
)

Definition at line 67 of file datetime.cpp.

References hour_, isValid(), minute_, msec_, and second_.

00067                                              {
00068     hour_ = h;
00069     minute_ = m;
00070     second_ = s;
00071     msec_ = ms;
00072     return isValid();
00073 }

Here is the call graph for this function:

void Wt::Time::start (  ) 

Definition at line 187 of file datetime.cpp.

References currentTime().

Referenced by add_and_wrap(), Wt::SDLInput::EventInfo::EventInfo(), and Wt::TimerBlock::TimerBlock().

00187                  {
00188     *this = currentTime();
00189 }

Here is the call graph for this function:

std::string Wt::Time::toString (  )  const

Definition at line 63 of file datetime.cpp.

00063                                {
00064     return *this;
00065 }


Member Data Documentation

int Wt::Time::hour_ [private]

Definition at line 70 of file datetime.h.

Referenced by addMSecs(), addSecs(), hour(), isNull(), isValid(), operator std::string(), operator<(), operator==(), operator>(), secsTo(), and setHMS().

int Wt::Time::minute_ [private]

Definition at line 71 of file datetime.h.

Referenced by addMSecs(), addSecs(), isNull(), isValid(), minute(), operator std::string(), operator<(), operator==(), operator>(), secsTo(), and setHMS().

int Wt::Time::msec_ [private]

Definition at line 73 of file datetime.h.

Referenced by addMSecs(), isNull(), isValid(), msec(), msecsTo(), operator std::string(), operator<(), operator==(), operator>(), and setHMS().

int Wt::Time::second_ [private]

Definition at line 72 of file datetime.h.

Referenced by addMSecs(), addSecs(), isNull(), isValid(), operator std::string(), operator<(), operator==(), operator>(), second(), secsTo(), and setHMS().


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

Generated Fri Jul 28 19:27:50 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.