00001 /* 00002 libwt - Vassilis Virvilis Toolkit - a widget library 00003 Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #include <sstream> 00022 #include <iomanip> 00023 00024 #include <SDL/SDL_timer.h> 00025 00026 #include <wt/datetime.h> 00027 #include <wt/trace.h> 00028 00029 namespace Wt { 00030 00031 Time::Time() : 00032 hour_(0), 00033 minute_(0), 00034 second_(0), 00035 msec_(0) {} 00036 00037 Time::Time(int h, int m, int s, int ms) : 00038 hour_(h), 00039 minute_(m), 00040 second_(s), 00041 msec_(ms) {} 00042 00043 bool Time::isNull() const { 00044 return (hour_ == 0 && minute_ == 0 && second_ == 0 && msec_ == 0); 00045 } 00046 00047 bool Time::isValid() const { 00048 return (0 <= hour_ && hour_ <= 23 && 00049 0 <= minute_ && minute_ <= 59 && 00050 0 <= second_ && second_ <= 59 && 00051 0 <= msec_ && msec_ <= 999); 00052 } 00053 00054 Time::operator std::string() const { 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 } 00062 00063 std::string Time::toString() const { 00064 return *this; 00065 } 00066 00067 bool Time::setHMS(int h, int m, int s, int ms) { 00068 hour_ = h; 00069 minute_ = m; 00070 second_ = s; 00071 msec_ = ms; 00072 return isValid(); 00073 } 00074 00075 int Time::add_and_wrap(int& start, int duration, int range) const { 00076 start += duration; 00077 int p = start % range; 00078 int wrap = (start != p) ? 1 : 0; 00079 start = p; 00080 return wrap; 00081 } 00082 00083 Time Time::addSecs(int nsecs) const { 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 } 00095 00096 int Time::secsTo(const Time& t) const { 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 } 00102 00103 Time Time::addMSecs(int ms) const { 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 } 00117 00118 int Time::msecsTo(const Time& t) const { 00119 return secsTo(t) * 1000 + t.msec_ - msec_; 00120 } 00121 00122 bool Time::operator==(const Time& t) const { 00123 return (hour_ == t.hour_ && minute_ == t.minute_ && 00124 second_ == t.second_ && msec_ == t.msec_); 00125 } 00126 00127 bool Time::operator!=(const Time& t) const { 00128 return !(*this == t); 00129 } 00130 00131 bool Time::operator<(const Time& t) const { 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 } 00154 00155 bool Time::operator>=(const Time& t) const { 00156 return !(*this < t); 00157 } 00158 00159 bool Time::operator>(const Time& t) const { 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 } 00182 00183 bool Time::operator<=(const Time& t) const { 00184 return !(*this > t); 00185 } 00186 00187 void Time::start() { 00188 *this = currentTime(); 00189 } 00190 00191 int Time::restart() { 00192 const Time& t = currentTime(); 00193 int ms = msecsTo(t); 00194 *this = t; 00195 return ms; 00196 } 00197 00198 int Time::elapsed() const { 00199 Time t = currentTime(); 00200 int ms = msecsTo(t); 00201 return ms; 00202 } 00203 00204 Time Time::currentTime() { 00205 Time t; 00206 Uint32 ticks = SDL_GetTicks(); 00207 trace("time", "Ticks %u\n", ticks); 00208 return t.addMSecs(ticks); 00209 } 00210 00211 Time Time::fromString(const std::string& timedesctiption) { 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 } 00222 00223 bool Time::isValid(int h, int m, int s, int ms) { 00224 Time t(h, m, s, ms); 00225 return t.isValid(); 00226 } 00227 00228 } // namespace
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.