DateValue.cpp

00001 /*
00002     libvcard - vCard parsing library for vCard version 3.0
00003 
00004     Copyright (C) 1998 Rik Hemsley rik@kde.org
00005     
00006   Permission is hereby granted, free of charge, to any person obtaining a copy
00007   of this software and associated documentation files (the "Software"), to
00008   deal in the Software without restriction, including without limitation the
00009   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00010   sell copies of the Software, and to permit persons to whom the Software is
00011   furnished to do so, subject to the following conditions:
00012 
00013   The above copyright notice and this permission notice shall be included in
00014   all copies or substantial portions of the Software.
00015 
00016   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019   AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00020   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00021   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022 */
00023 
00024 #include <qregexp.h>
00025 
00026 #include <kdebug.h>
00027 
00028 #include <VCardDefines.h>
00029 #include <VCardDateValue.h>
00030 #include <VCardValue.h>
00031 
00032 using namespace VCARD;
00033 
00034 DateValue::DateValue()
00035     :   Value()
00036 {
00037     vDebug("DateValue::DateValue()");
00038 }
00039 
00040 DateValue::DateValue(
00041         unsigned int    year,
00042         unsigned int    month,
00043         unsigned int    day,
00044         unsigned int    hour,
00045         unsigned int    minute,
00046         unsigned int    second,
00047         double      secFrac,
00048         bool        zonePositive,
00049         unsigned int    zoneHour,
00050         unsigned int    zoneMinute)
00051     :   Value           (),
00052         year_           (year),
00053         month_          (month),
00054         day_            (day),
00055         hour_           (hour),
00056         minute_         (minute),
00057         second_         (second),
00058         zoneHour_       (zoneHour),
00059         zoneMinute_     (zoneMinute),
00060         secFrac_        (secFrac),
00061         zonePositive_   (zonePositive),
00062         hasTime_(true)
00063 {
00064     parsed_ = true;
00065     assembled_ = false;
00066 }
00067 
00068 DateValue::DateValue(const QDate & d)
00069     :   Value       (),
00070         year_       (d.year()),
00071         month_      (d.month()),
00072         day_        (d.day()),
00073         hasTime_(false)
00074 {
00075     parsed_ = true;
00076     assembled_ = false;
00077 }
00078 
00079 DateValue::DateValue(const QDateTime & d)
00080     :   Value       (),
00081         year_       (d.date().year()),
00082         month_      (d.date().month()),
00083         day_        (d.date().day()),
00084         hour_       (d.time().hour()),
00085         minute_     (d.time().minute()),
00086         second_     (d.time().second()),
00087         hasTime_(true)
00088 {
00089     parsed_ = true;
00090     assembled_ = false;
00091 }
00092 
00093 DateValue::DateValue(const DateValue & x)
00094     :   Value(x)
00095 {
00096     year_ = x.year_;
00097     month_ = x.month_;
00098     day_ = x.day_;
00099     hour_ = x.hour_;
00100     minute_ = x.minute_;
00101     second_ = x.second_;
00102     zoneHour_ = x.zoneHour_;
00103     zoneMinute_ = x.zoneMinute_;
00104     secFrac_ = x.secFrac_;
00105     hasTime_ = x.hasTime_;
00106 }
00107 
00108 DateValue::DateValue(const QCString & s)
00109     :   Value(s)
00110 {
00111 }
00112 
00113     DateValue &
00114 DateValue::operator = (DateValue & x)
00115 {
00116     if (*this == x) return *this;
00117 
00118     Value::operator = (x);
00119     return *this;
00120 }
00121 
00122     DateValue &
00123 DateValue::operator = (const QCString & s)
00124 {
00125     Value::operator = (s);
00126     return *this;
00127 }
00128 
00129     bool
00130 DateValue::operator == (DateValue & x)
00131 {
00132     x.parse();
00133     return false;
00134 }
00135 
00136 DateValue::~DateValue()
00137 {
00138 }
00139 
00140     DateValue *
00141 DateValue::clone()
00142 {
00143     return new DateValue( *this );
00144 }
00145 
00146     void
00147 DateValue::_parse()
00148 {
00149     vDebug("DateValue::_parse()");
00150 
00151     // date = date-full-year ["-"] date-month ["-"] date-mday
00152     // time = time-hour [":"] time-minute [":"] time-second [":"]
00153     // [time-secfrac] [time-zone]
00154     
00155     int timeSep = strRep_.find('T');
00156     
00157     QCString dateStr;
00158     QCString timeStr;
00159     
00160     if (timeSep == -1) {
00161         
00162         dateStr = strRep_;
00163         vDebug("Has date string \"" + dateStr + "\"");
00164         
00165     } else {
00166         
00167         dateStr = strRep_.left(timeSep);
00168         vDebug("Has date string \"" + dateStr + "\"");
00169         
00170         timeStr = strRep_.mid(timeSep + 1);
00171         vDebug("Has time string \"" + timeStr + "\"");
00172     }
00173     
00175     
00176     dateStr.replace(QRegExp("-"), "");
00177 
00178     kdDebug(5710) << "dateStr: " << dateStr << endl;
00179 
00180     year_   = dateStr.left(4).toInt();
00181     month_  = dateStr.mid(4, 2).toInt();
00182     day_    = dateStr.right(2).toInt();
00183     
00184     if (timeSep == -1) {
00185         hasTime_ = false;
00186         return; // No time, done.
00187     }
00188     else
00189         hasTime_ = true;
00190     
00192 
00194     
00195     int zoneSep = timeStr.find('Z');
00196     
00197     if (zoneSep != -1 && timeStr.length() - zoneSep > 3) {
00198         
00199         QCString zoneStr(timeStr.mid(zoneSep + 1));
00200         vDebug("zoneStr == " + zoneStr);
00201 
00202         zonePositive_   = (zoneStr[0] == '+');
00203         zoneHour_       = zoneStr.mid(1, 2).toInt();
00204         zoneMinute_     = zoneStr.right(2).toInt();
00205         
00206         timeStr.remove(zoneSep, timeStr.length() - zoneSep);
00207     }
00208 
00210     
00211     int secFracSep = timeStr.findRev(',');
00212     
00213     if (secFracSep != -1 && zoneSep != -1) { // zoneSep checked to avoid errors.
00214         QCString quirkafleeg = "0." + timeStr.mid(secFracSep + 1, zoneSep);
00215         secFrac_ = quirkafleeg.toDouble();
00216     }
00217     
00219 
00220     timeStr.replace(QRegExp(":"), "");
00221     
00222     hour_   = timeStr.left(2).toInt();
00223     minute_ = timeStr.mid(2, 2).toInt();
00224     second_ = timeStr.mid(4, 2).toInt();
00225 }
00226 
00227     void
00228 DateValue::_assemble()
00229 {
00230     vDebug("DateValue::_assemble");
00231 
00232     QCString year;
00233     QCString month;
00234     QCString day;
00235     
00236     year.setNum( year_ );
00237     month.setNum( month_ );
00238     day.setNum( day_ );
00239 
00240     if ( month.length() < 2 ) month.prepend( "0" );
00241     if ( day.length() < 2 ) day.prepend( "0" );
00242 
00243     strRep_ = year + '-' + month + '-' + day;
00244 
00245     if ( hasTime_ ) {
00246         QCString hour;
00247         QCString minute;
00248         QCString second;
00249 
00250         hour.setNum( hour_ );
00251         minute.setNum( minute_ );
00252         second.setNum( second_ );
00253 
00254         if ( hour.length() < 2 ) hour.prepend( "0" );
00255         if ( minute.length() < 2 ) minute.prepend( "0" );
00256         if ( second.length() < 2 ) second.prepend( "0" );
00257 
00258         strRep_ += 'T' + hour + ':' + minute + ':' + second + 'Z';
00259     }
00260 }
00261 
00262     unsigned int
00263 DateValue::year()
00264 {
00265     parse();
00266     return year_;
00267 }
00268 
00269     unsigned int
00270 DateValue::month()
00271 {
00272     parse();
00273     return month_;
00274 }
00275 
00276     unsigned int
00277 DateValue::day()
00278 {
00279     parse();
00280     return day_;
00281 }
00282     unsigned int
00283 DateValue::hour()
00284 {
00285     parse();
00286     return hour_;
00287 }
00288 
00289     unsigned int
00290 DateValue::minute()
00291 {
00292     parse();
00293     return minute_;
00294 }
00295 
00296     unsigned int
00297 DateValue::second()
00298 {
00299     parse();
00300     return second_;
00301 }
00302 
00303     double
00304 DateValue::secondFraction()
00305 {
00306     parse();
00307     return secFrac_;
00308 }
00309 
00310     bool
00311 DateValue::zonePositive()
00312 {
00313     parse();
00314     return zonePositive_;
00315 }
00316 
00317     unsigned int
00318 DateValue::zoneHour()
00319 {
00320     parse();
00321     return zoneHour_;
00322 }
00323 
00324     unsigned int
00325 DateValue::zoneMinute()
00326 {
00327     parse();
00328     return zoneMinute_;
00329 }
00330     
00331     void
00332 DateValue::setYear(unsigned int i)
00333 {
00334     year_ = i;
00335     assembled_ = false;
00336 }
00337 
00338     void
00339 DateValue::setMonth(unsigned int i)
00340 {
00341     month_ = i;
00342     assembled_ = false;
00343 }
00344 
00345     void
00346 DateValue::setDay(unsigned int i)
00347 {
00348     day_ = i;
00349     assembled_ = false;
00350 }
00351 
00352     void
00353 DateValue::setHour(unsigned int i)
00354 {
00355     hour_ = i;
00356     assembled_ = false;
00357 }
00358 
00359     void
00360 DateValue::setMinute(unsigned int i)
00361 {
00362     minute_ = i;
00363     assembled_ = false;
00364 }
00365 
00366     void
00367 DateValue::setSecond(unsigned int i)
00368 {
00369     second_ = i;
00370     assembled_ = false;
00371 }
00372 
00373     void
00374 DateValue::setSecondFraction(double d)
00375 {
00376     secFrac_ = d;
00377     assembled_ = false;
00378 }
00379 
00380     void
00381 DateValue::setZonePositive(bool b)
00382 {
00383     zonePositive_ = b;
00384     assembled_ = false;
00385 }   
00386 
00387     void
00388 DateValue::setZoneHour(unsigned int i)
00389 {
00390     zoneHour_ = i;
00391     assembled_ = false;
00392 }
00393 
00394     void
00395 DateValue::setZoneMinute(unsigned int i)
00396 {
00397     zoneMinute_ = i;
00398     assembled_ = false;
00399 }
00400 
00401     QDate
00402 DateValue::qdate()
00403 {
00404     parse();
00405     QDate d(year_, month_, day_);
00406     return d;
00407 }
00408 
00409     QTime
00410 DateValue::qtime()
00411 {
00412     parse();
00413     QTime t(hour_, minute_, second_);
00414 //  t.setMs(1 / secFrac_);
00415     return t;
00416 }
00417 
00418     QDateTime
00419 DateValue::qdt()
00420 {
00421     parse();
00422     QDateTime dt;
00423     dt.setDate(qdate());
00424     dt.setTime(qtime());
00425     return dt;
00426 }
00427 
00428     bool
00429 DateValue::hasTime()
00430 {
00431     parse();
00432     return hasTime_;
00433 }
00434 
KDE Home | KDE Accessibility Home | Description of Access Keys