UCommon

ucommon/datetime.h

Go to the documentation of this file.
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published 
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ 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
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00029 #ifndef _UCOMMON_DATETIME_H_
00030 #define _UCOMMON_DATETIME_H_
00031 
00032 #ifndef _UCOMMON_CONFIG_H_
00033 #include <ucommon/platform.h>
00034 #endif
00035 
00036 #ifndef _UCOMMON_NUMBERS_H_
00037 #include <ucommon/numbers.h>
00038 #endif
00039 
00040 #ifndef _UCOMMON_STRING_H_
00041 #include <ucommon/string.h>
00042 #endif
00043 
00044 #include <time.h>
00045 
00046 #define DATE_STRING_SIZE        10
00047 #define DATE_BUFFER_SIZE        11
00048 #define TIME_STRING_SIZE        8
00049 #define TIME_BUFFER_SIZE        9
00050 #define DATETIME_STRING_SIZE    19
00051 #define DATETIME_BUFFER_SIZE    20
00052 
00056 typedef struct tm   tm_t;
00057 
00058 NAMESPACE_UCOMMON
00059 
00060 #ifdef __BORLANDC__
00061     using std::tm;
00062     using std::time_t;
00063 #endif
00064 
00073 class __EXPORT Date
00074 {
00075 protected:
00076     long julian;
00077 
00078     void toJulian(long year, long month, long day);
00079     void fromJulian(char *buf) const;
00080 
00085     virtual void update(void);
00086 
00087 public:
00091     typedef enum {year = 10, month, day, dow} index_t;
00092 
00096     static const size_t sz_string;
00097 
00102     Date(time_t value);
00103 
00108     Date(struct tm *object);
00109 
00115     Date(const char *pointer, size_t size = 0);
00116 
00123     Date(int year, unsigned month = 1, unsigned day = 1);
00124 
00129     Date(const Date& object);
00130 
00134     Date();
00135 
00139     virtual ~Date();
00140 
00145     int getYear(void) const;
00146 
00151     unsigned getMonth(void) const;
00152 
00157     unsigned getDay(void) const;
00158 
00163     unsigned getDayOfWeek(void) const;
00164 
00169     inline long getJulian(void)
00170         {return julian;};
00171 
00177     char *get(char *buffer) const;
00178 
00183     time_t getTime(void) const;
00184     
00189     long get(void) const;
00190 
00194     void set(void);
00195 
00201     void set(const char *pointer, size_t size = 0);
00202 
00207     bool isValid(void) const;
00208 
00213     inline operator long() const
00214         {return get();};
00215 
00221     int operator[](index_t component) const;
00222 
00227     inline long operator*() const
00228         {return get();};
00229 
00235     String operator()() const;
00236 
00241     Date& operator++();
00242 
00247     Date& operator--();
00248 
00254     Date& operator+=(long offset);
00255 
00261     Date& operator-=(long offset);
00262     
00268     Date operator+(long days);
00269 
00275     Date operator-(long days);
00276 
00282     inline long operator-(const Date &date)
00283         {return (julian - date.julian);};
00284 
00290     Date& operator=(const Date& date);
00291 
00297     bool operator==(const Date& date);
00298 
00304     bool operator!=(const Date& date);
00305 
00311     bool operator<(const Date& date);
00312 
00318     bool operator<=(const Date& date);
00319 
00325     bool operator>(const Date& date);
00326 
00332     bool operator>=(const Date& date);
00333 
00338     inline bool operator!() const
00339         {return !isValid();};
00340 
00345     inline operator bool() const
00346         {return isValid();};
00347 };
00348 
00360 class __EXPORT Time
00361 {
00362 protected:
00363     long seconds;
00364 
00365 protected:
00366     void toSeconds(int hour, int minute = 0, int second = 0);
00367     void fromSeconds(char *buf) const;
00368     virtual void update(void);
00369 
00370 public:
00374     typedef enum {hour = 20, minute, second} index_t;
00375 
00379     static const size_t sz_string;
00380 
00385     Time(time_t value);
00386 
00391     Time(tm_t *object);
00392 
00398     Time(char *pointer, size_t size = 0);
00399 
00406     Time(int hour, int minute, int second);
00407 
00412     Time(const Time& object);
00413 
00417     Time();
00418 
00422     virtual ~Time();
00423 
00428     long get(void) const;
00429     
00434     int getHour(void) const;
00435 
00440     int getMinute(void) const;
00441 
00446     int getSecond(void) const;
00447 
00453     char *get(char *buffer) const;
00454 
00458     void set(void);
00459 
00465     void set(char *pointer, size_t size = 0);
00466 
00471     bool isValid(void) const;
00472 
00477     inline operator bool() const
00478         {return isValid();};
00479 
00484     inline bool operator!() const
00485         {return !isValid();};
00486 
00492     long operator-(const Time &reference);
00493 
00499     Time operator+(long seconds);
00500 
00506     Time operator-(long seconds);
00507 
00512     inline operator long()
00513         {return get();};
00514 
00519     inline long operator*() const
00520         {return get();};
00521 
00527     int operator[](index_t component) const;
00528     
00533     String operator()() const;
00534 
00539     Time& operator++();
00540 
00545     Time& operator--();
00546 
00552     Time& operator=(const Time& time);
00553 
00559     Time& operator+=(long seconds);
00560 
00566     Time& operator-=(long seconds);
00567 
00573     bool operator==(const Time &time);
00574 
00580     bool operator!=(const Time &time);
00581 
00587     bool operator<(const Time &time);
00588 
00594     bool operator<=(const Time &time);
00595 
00601     bool operator>(const Time &time);
00602 
00608     bool operator>=(const Time &time);
00609 };
00610 
00620 class __EXPORT DateTime : public Date, public Time
00621 {
00622 protected:
00623     void update(void);
00624 
00625 public:
00629     typedef enum {year = Date::year, month = Date::month, day = Date::day, 
00630         dow = Date::dow, 
00631         hour = Time::hour, minute = Time::minute, second = Time::second} index_t;
00632 
00636     static const long c_day;
00637 
00641     static const long c_hour;
00642 
00646     static const long c_week;
00647 
00651     static const size_t sz_string;
00652 
00657     DateTime(time_t time);
00658 
00663     DateTime(tm_t *tm);
00664 
00670     DateTime(const char *pointer, size_t size = 0);
00671 
00681     DateTime(int year, unsigned month, unsigned day,
00682          int hour = 0, int minute = 0, int second = 0);
00683 
00688     DateTime(const DateTime& object);
00689 
00693     DateTime();
00694 
00698     virtual ~DateTime();
00699 
00705     char *get(char *buffer) const;
00706 
00711     time_t get(void) const;
00712 
00717     bool isValid(void) const;
00718 
00724     long operator-(const DateTime &datetime);
00725 
00731     DateTime& operator=(const DateTime& datetime);
00732 
00739     DateTime& operator+=(long seconds);
00740 
00747     DateTime& operator-=(long seconds);
00748 
00755     DateTime operator+(long seconds);
00756 
00763     DateTime operator-(long seconds);
00764 
00769     DateTime& operator++();
00770     
00775     DateTime& operator--();
00776 
00782     bool operator==(const DateTime& datetime);
00783 
00789     bool operator!=(const DateTime& datetime);
00790 
00796     bool operator<(const DateTime& datetime);
00797 
00804     bool operator<=(const DateTime& datetime);
00805 
00811     bool operator>(const DateTime& datetime);
00812 
00819     bool operator>=(const DateTime& datetime);
00820 
00825     bool operator!() const;
00826 
00832     int operator[](index_t component) const;
00833 
00838     operator bool() const;
00839 
00844     inline operator long() const
00845         {return Date::get();};
00846 
00850     void set(void);
00851 
00856     operator double() const;
00857 
00863     String format(const char *strftime) const;
00864 
00873     static tm_t *glt(time_t *time = NULL);
00874 
00883     static tm_t *gmt(time_t *time = NULL);
00884 
00889     static void release(tm_t *object);
00890 };
00891 
00899 class __EXPORT DateTimeString : public DateTime
00900 {
00901 public:
00906     typedef enum {
00907         DATE, TIME, BOTH} mode_t;
00908     
00909 private:
00910     char buffer[DATETIME_BUFFER_SIZE];
00911     mode_t mode;
00912 
00913 protected:
00914     void update(void);
00915 
00916 public:
00921     DateTimeString(time_t time);
00922 
00927     DateTimeString(tm_t *tm);
00928 
00934     DateTimeString(const char *pointer, size_t size = 0);
00935 
00945     DateTimeString(int year, unsigned month, unsigned day,
00946          int hour = 0, int minute = 0, int second = 0);
00947 
00952     DateTimeString(const DateTimeString& object);
00953 
00957     DateTimeString(mode_t string = DateTimeString::BOTH);
00958 
00962     virtual ~DateTimeString();
00963 
00969     inline const char *c_str(void)
00970         {return buffer;};
00971 
00977     inline operator const char *(void)
00978         {return buffer;};
00979 
00983     void set(void);
00984 
00989     void set(mode_t string);
00990 };
00991 
00998 class __EXPORT DateNumber : public Number, public Date
00999 {
01000 protected:
01001     void update(void);
01002 
01003 public:
01008     DateNumber(char *pointer);
01009 
01013     virtual ~DateNumber();
01014 
01018     void set(void);
01019 };
01020 
01024 typedef DateTime    datetime_t;
01025 
01029 typedef DateTimeString  datetimestring_t;
01030 
01034 typedef Date        date_t;
01035 
01036 END_NAMESPACE
01037 
01038 #endif