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

Timestamp.cc

Go to the documentation of this file.
00001 /*
00002  * Timestamp.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Timestamp.
00006  *
00007  * Copyright (c) 2005 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include <time.h>
00025 #include <sys/time.h>
00026 #include <stlib/Date.h>
00027 #include <stlib/String.h>
00028 #include <stlib/Stream.h>
00029 #include <stlib/Time.h>
00030 #include <stlib/Timestamp.h>
00031 
00032 Timestamp::Timestamp(int month, int day, int year, int hour,
00033                      int minute, int second, int millisecond)
00034 {
00035     _timestamp.day = day;
00036     _timestamp.month = month;
00037     _timestamp.year = year;
00038     _timestamp.hours = hour;
00039     _timestamp.minutes = minute;
00040     _timestamp.seconds = second;
00041     _timestamp.milliseconds = millisecond;
00042 }
00043 
00044 /* Class-accessing protocol */
00045 String *Timestamp::className(void) const
00046 {
00047     return new String("Timestamp");
00048 }
00049 
00050 /* Instance creating protocol */
00052 Timestamp *Timestamp::now(void)
00053 {
00054     struct timeval timestamp;
00055     struct timezone tz;
00056     Timestamp *inst;
00057 
00058     gettimeofday(&timestamp, &tz);
00059     const struct tm* time = localtime(&timestamp.tv_sec);
00060     return new Timestamp(time->tm_mon+1, time->tm_mday, time->tm_year+1900,
00061                          time->tm_hour, time->tm_min, time->tm_sec,
00062                          timestamp.tv_usec / 1000);
00063 }
00064 
00065 Timestamp *Timestamp::fromUnixTimestamp(time_t timestamp)
00066 {
00067     const struct tm* time = localtime(&timestamp);
00068     return new Timestamp(time->tm_mon+1, time->tm_mday, time->tm_year+1900,
00069                          time->tm_hour, time->tm_min, time->tm_sec);
00070 }
00071 
00072 Timestamp *Timestamp::fromDateAndTime(Date *date, Time *time)
00073 {
00074     return new Timestamp(date->month(), date->day(), date->year(),
00075                          time->hour(), time->minute(), time->second(),
00076                          time->millisecond());
00077 }
00078 
00079 /* Accessing protocol */
00081 int Timestamp::month(void) const
00082 {
00083     return _timestamp.month;
00084 }
00085 
00087 int Timestamp::day(void) const
00088 {
00089     return _timestamp.day;
00090 }
00091 
00093 int Timestamp::year(void) const
00094 {
00095     return _timestamp.year;
00096 }
00097 
00099 int Timestamp::weekday(void) const
00100 {
00101     return asDate()->weekday();
00102 }
00103 
00105 int Timestamp::century(void) const
00106 {
00107     return year() / 100;
00108 }
00109 
00110 int Timestamp::hour(void) const
00111 {
00112     return _timestamp.hours;
00113 }
00114 
00115 int Timestamp::minute(void) const
00116 {
00117     return _timestamp.minutes;
00118 }
00119 
00120 int Timestamp::second(void) const
00121 {
00122     return _timestamp.seconds;
00123 }
00124 
00125 int Timestamp::millisecond(void) const
00126 {
00127     return _timestamp.milliseconds;
00128 }
00129 
00130 /* Arithmetics protocol */
00131 Timestamp *Timestamp::addDays(int dayCount)
00132 {
00133     return fromDateAndTime(asDate()->addDays(dayCount), asTime());
00134 }
00135 
00136 Timestamp *Timestamp::addMonths(int monthCount)
00137 {
00138     return fromDateAndTime(asDate()->addMonths(monthCount), asTime());
00139 }
00140 
00141 Timestamp *Timestamp::addYears(int yearCount)
00142 {
00143     return fromDateAndTime(asDate()->addYears(yearCount), asTime());
00144 }
00145 
00146 Timestamp *Timestamp::addHours(int hours)
00147 {
00148     hours += _timestamp.hours;
00149     int days = hours / 24;
00150     hours %= 24;
00151     Time *time = new Time(hours, _timestamp.minutes, _timestamp.seconds,
00152                           _timestamp.milliseconds);
00153     return fromDateAndTime(asDate()->addDays(days), time);
00154 }
00155 
00156 Timestamp *Timestamp::addMinutes(int minutes)
00157 {
00158     return addSeconds(minutes * 60);
00159 }
00160 
00161 Timestamp *Timestamp::addSeconds(int seconds)
00162 {
00163     return addMilliseconds(seconds * 1000);
00164 }
00165 
00166 Timestamp *Timestamp::addMilliseconds(int millis)
00167 {
00168     int days, hours, minutes, seconds;
00169     millis += _timestamp.milliseconds;
00170     seconds = _timestamp.seconds + millis / 1000;
00171     minutes = _timestamp.minutes + seconds / 60;
00172     hours = _timestamp.hours + minutes / 60;
00173     days = hours / 24;
00174     hours = hours % 24;
00175     minutes = minutes % 60;
00176     seconds = seconds % 60;
00177     millis = millis % 1000;
00178     return fromDateAndTime(asDate()->addDays(days),
00179                            new Time(hours, minutes, seconds, millis));
00180 }
00181 
00182 Timestamp *Timestamp::subtractDays(int dayCount)
00183 {
00184     return fromDateAndTime(asDate()->subtractDays(dayCount), asTime());
00185 }
00186 
00187 Timestamp *Timestamp::subtractMonths(int monthCount)
00188 {
00189     return fromDateAndTime(asDate()->subtractMonths(monthCount), asTime());
00190 }
00191 
00192 Timestamp *Timestamp::subtractYears(int yearCount)
00193 {
00194     return fromDateAndTime(asDate()->subtractYears(yearCount), asTime());
00195 }
00196 
00197 Timestamp *Timestamp::subtractHours(int hours)
00198 {
00199     hours = _timestamp.hours - hours;
00200     int days = 0;
00201     while (hours < 0) {
00202         days++; hours += 24;
00203     }
00204     Time *time = new Time(hours, _timestamp.minutes, _timestamp.seconds,
00205                           _timestamp.milliseconds);
00206     return fromDateAndTime(asDate()->subtractDays(days), time);
00207 }
00208 
00209 Timestamp *Timestamp::subtractMinutes(int minutes)
00210 {
00211     return subtractSeconds(minutes * 60);
00212 }
00213 
00214 Timestamp *Timestamp::subtractSeconds(int seconds)
00215 {
00216     return subtractMilliseconds(seconds * 1000);
00217 }
00218 
00219 Timestamp *Timestamp::subtractMilliseconds(int millis)
00220 {
00221     int seconds = millis / 1000;
00222     millis = _timestamp.milliseconds - millis % 1000;
00223     while (millis < 0) {
00224         seconds++; millis += 1000;
00225     }
00226     int minutes = seconds / 60;
00227     seconds = _timestamp.seconds - seconds % 60;
00228     while (seconds < 0) {
00229         minutes++; seconds += 60;
00230     }
00231     int hours = minutes / 60;
00232     minutes = _timestamp.minutes - minutes % 60;
00233     while (minutes < 0) {
00234         hours++; minutes += 60;
00235     }
00236     int days = hours / 24;
00237     hours = _timestamp.hours - hours % 24;
00238     while (hours < 0) {
00239         days++; hours += 24;
00240     }
00241     return fromDateAndTime(asDate()->subtractDays(days),
00242                            new Time(hours, minutes, seconds, millis));
00243 }
00244 
00245 Time *Timestamp::differenceFrom(Timestamp *stamp)
00246 {
00247     int days = asDate()->dayDifferenceFrom(stamp->asDate());
00248     return asTime()->differenceFrom(stamp->asTime())->addHours(days * 24);
00249 }
00250 
00251 /* Comparing protocol */
00252 long Timestamp::hash(void) const
00253 {
00254     unsigned long hashVal, mon_year;
00255     mon_year = _timestamp.month;
00256     mon_year = _timestamp.year << 4;
00257     hashVal = _timestamp.milliseconds;
00258     hashVal += _timestamp.seconds << 10;
00259     hashVal += _timestamp.minutes << 16;
00260     hashVal += _timestamp.hours << 22;
00261     hashVal += _timestamp.day << 27;
00262     return (long) (hashVal ^ mon_year);
00263 }
00264 
00265 bool Timestamp::isEqual(const Object *object) const
00266 {
00267     if (!object->className()->isEqual(className())) return false;
00268 
00269     const Timestamp *t = dynamic_cast<const Timestamp *>(object);
00270     return t->_timestamp.year == _timestamp.year &&
00271            t->_timestamp.month == _timestamp.month &&
00272            t->_timestamp.day == _timestamp.day &&
00273            t->_timestamp.hours == _timestamp.hours &&
00274            t->_timestamp.minutes == _timestamp.minutes &&
00275            t->_timestamp.seconds == _timestamp.seconds &&
00276            t->_timestamp.milliseconds == _timestamp.milliseconds;
00277 }
00278 
00279 /* Converting protocol */
00281 Date *Timestamp::asDate(void) const
00282 {
00283     return new Date(_timestamp.month, _timestamp.day, _timestamp.year);
00284 }
00285 
00287 Time *Timestamp::asTime(void) const
00288 {
00289     return new Time(_timestamp.hours, _timestamp.minutes, _timestamp.seconds,
00290                     _timestamp.milliseconds);
00291 }
00292 
00293 time_t Timestamp::asUnixTimestamp(void) const
00294 {
00295     struct tm time;
00296 
00297     time.tm_sec = _timestamp.seconds;
00298     time.tm_min = _timestamp.minutes;
00299     time.tm_hour = _timestamp.hours;
00300     time.tm_mday = _timestamp.day;
00301     time.tm_mon = _timestamp.month - 1;
00302     time.tm_year = _timestamp.year - 1900;
00303     return mktime(&time);
00304 }
00305 
00306 /* Printing protocol */
00307 void Timestamp::printOn(Stream *stream) const
00308 {
00309     asDate()->printOn(stream);
00310     stream->space();
00311     asTime()->printOn(stream);
00312 }
00313 
00314 /* Testing protocol */
00316 bool Timestamp::isLeapYear(void) const
00317 {
00318     return Date::isLeapYear(_timestamp.year);
00319 }

Generated on Mon Nov 27 09:47:56 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2