edelib  2.0.0
edelib/DateTime.h
00001 /*
00002  * $Id: DateTime.h 2967 2009-12-02 14:31:34Z karijes $
00003  *
00004  * Classes related to date/time and timezones.
00005  * Copyright (c) 2005-2007 edelib authors
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #ifndef __EDELIB_DATETIME_H__
00022 #define __EDELIB_DATETIME_H__
00023 
00024 #include "edelib-global.h"
00025 
00026 EDELIB_NS_BEGIN
00027 
00032 class EDELIB_API TimeZone {
00033 private:
00034         char* zoneval;
00035         char* zcode;
00036         unsigned long timeval;
00037 
00038         bool load(const char* zone);
00039         bool load_local(void);
00040         void clear(void);
00041 public:
00045         TimeZone();
00046 
00050         ~TimeZone();
00051 
00056         bool set(const char* n);
00057 
00061         const char* code(void)   { return (zcode ? zcode : "??"); }
00062 
00066         const char* zone(void)   { return (zoneval ? zoneval : "Unknown"); }
00067 
00072         unsigned long time(void) { return timeval; }
00073 };
00074 
00079 enum DateType {
00080         DATE_LOCAL = 0,   
00081         DATE_UTC          
00082 };
00083 
00140 class EDELIB_API Date {
00141 private:
00142         unsigned char dayval;
00143         unsigned char monthval;
00144         unsigned short yearval;
00145 
00146 public:
00151         enum Month { 
00152                 Jan = 1,   
00153                 Feb,       
00154                 Mar,       
00155                 Apr,       
00156                 May,       
00157                 Jun,       
00158                 Jul,       
00159                 Aug,       
00160                 Sep,       
00161                 Oct,       
00162                 Nov,       
00163                 Dec,       
00164                 MonthNow   
00165         };
00166 
00171         enum Day {
00172                 DayNow = 0  
00173         };
00174 
00179         enum Year { 
00180                 YearNow = 0 
00181         };
00182 
00187         Date();
00188 
00192         Date(const Date& d);
00193 
00198         Date& operator=(const Date& d);
00199 
00203         ~Date();
00204 
00220         bool set(unsigned short y, unsigned char m, unsigned char d, DateType t = DATE_LOCAL);
00221 
00233         bool system_set(void);
00234 
00236         bool leap_year(void) const { return leap_year(yearval); }
00237 
00239         unsigned char  day(void) const    { return dayval; }
00241         unsigned char  month(void) const  { return monthval; }
00243         unsigned short year(void) const   { return yearval; }
00244 
00250         const char*    day_name(void);
00251 
00257         const char*    month_name(void);
00258 
00259 
00261         unsigned char  days_in_month() const; 
00267         unsigned char  day_of_week() const;
00268 
00270         unsigned short day_of_year() const;
00271 
00277         Date& operator++();
00278 
00282         Date operator++(int);
00283 
00287         Date& operator--();
00288 
00292         Date operator--(int);
00293 
00300         static bool leap_year(unsigned short y);
00301 
00310         static unsigned char days_in_month(unsigned short y, unsigned char m); 
00311 
00320         static bool is_valid(unsigned short y, unsigned char m, unsigned char d);
00321 };
00322 
00323 #ifndef SKIP_DOCS
00324 inline bool operator==(const Date& d1, const Date& d2)
00325 { return (d1.day() == d2.day() && d1.month() == d2.month() && d1.year() == d2.year()); }
00326 
00327 inline bool operator>(const Date& d1, const Date& d2) {
00328         return (d1.year() > d2.year() || (d1.year() == d2.year() && d1.month() > d2.month()) || 
00329                 (d1.year() == d2.year() && d1.month() == d2.month() && d1.day() > d2.day()));
00330 }
00331                         
00332 inline bool operator!=(const Date& d1, const Date& d2) { return !(d1 == d2); }
00333 inline bool operator>=(const Date& d1, const Date& d2) { return (d1 > d2 || d1 == d2); }
00334 inline bool operator<(const Date& d1, const Date& d2)  { return (!(d1 > d2) && (d1 != d2)); }
00335 inline bool operator<=(const Date& d1, const Date& d2) { return (d1 == d2 || d1 < d2); }
00336 #endif
00337 
00349 class EDELIB_API Time {
00350 private:
00351         unsigned char hourval;
00352         unsigned char minval;
00353         unsigned char secval;
00354 
00355 public:
00360         Time();
00361 
00365         Time(const Time& t);
00366 
00370         Time& operator=(const Time& t);
00371 
00375         ~Time();
00376 
00384         void set(unsigned char h, unsigned char m, unsigned char s = 0);
00385 
00391         void set_now(void);
00392 
00400         bool system_set(void);
00401 
00405         unsigned char hour(void) const { return hourval; }
00406 
00410         unsigned char minute(void) const  { return minval; }
00411 
00415         unsigned char second(void) const  { return secval; }
00416 
00423         Time& operator++();
00424 
00428         Time operator++(int);
00429 
00433         Time& operator--();
00434 
00438         Time operator--(int);
00439 
00448         static bool is_valid(unsigned char h, unsigned char m, unsigned char s);
00449 };
00450 
00451 #ifndef SKIP_DOCS
00452 inline bool operator==(const Time& t1, const Time& t2) { 
00453         return (t1.hour() == t2.hour() && t1.minute() == t2.minute() && t1.second() == t2.second());
00454 }
00455 
00456 inline bool operator>(const Time& t1, const Time& t2) {
00457         return (t1.hour() > t2.hour() ||
00458                 (t1.hour() == t2.hour() && t1.second() > t2.second()) ||
00459                 t1.second() == t2.second());
00460 }
00461 
00462 inline bool operator<(const Time& t1, const Time& t2) {
00463         return (t1.hour() < t2.hour() ||
00464                 (t1.hour() == t2.hour() && t1.second() < t2.second()) ||
00465                 t1.second() == t2.second());
00466 }
00467 
00468 inline bool operator!=(const Time& t1, const Time& t2) { return !(t1 == t2); }
00469 inline bool operator>=(const Time& t1, const Time& t2) { return (t1 > t2 || t1 == t2); }
00470 inline bool operator<=(const Time& t1, const Time& t2) { return (t1 == t2 || t1 < t2); }
00471 #endif
00472 
00473 EDELIB_NS_END
00474 #endif