00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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 #define DATE_STRING_SIZE 10
00045 #define DATE_BUFFER_SIZE 11
00046 #define TIME_STRING_SIZE 8
00047 #define TIME_BUFFER_SIZE 9
00048 #define DATETIME_STRING_SIZE 19
00049 #define DATETIME_BUFFER_SIZE 20
00050
00051 NAMESPACE_UCOMMON
00052
00053 #ifdef __BORLANDC__
00054 using std::tm;
00055 using std::time_t;
00056 #endif
00057
00066 class __EXPORT Date
00067 {
00068 protected:
00069 long julian;
00070
00071 void toJulian(long year, long month, long day);
00072 void fromJulian(char *buf) const;
00073
00078 virtual void update(void);
00079
00080 public:
00084 typedef enum {year = 10, month, day, dow} index_t;
00085
00089 static const size_t sz_string;
00090
00095 Date(time_t value);
00096
00101 Date(struct tm *object);
00102
00108 Date(const char *pointer, size_t size = 0);
00109
00116 Date(int year, unsigned month = 1, unsigned day = 1);
00117
00122 Date(const Date& object);
00123
00127 Date();
00128
00132 virtual ~Date();
00133
00138 int getYear(void) const;
00139
00144 unsigned getMonth(void) const;
00145
00150 unsigned getDay(void) const;
00151
00156 unsigned getDayOfWeek(void) const;
00157
00162 inline long getJulian(void)
00163 {return julian;};
00164
00170 char *get(char *buffer) const;
00171
00176 time_t getTime(void) const;
00177
00182 long get(void) const;
00183
00187 void set(void);
00188
00194 void set(const char *pointer, size_t size = 0);
00195
00200 bool isValid(void) const;
00201
00206 inline operator long() const
00207 {return get();};
00208
00214 int operator[](index_t component) const;
00215
00220 inline long operator*() const
00221 {return get();};
00222
00228 String operator()() const;
00229
00234 Date& operator++();
00235
00240 Date& operator--();
00241
00247 Date& operator+=(long offset);
00248
00254 Date& operator-=(long offset);
00255
00261 Date operator+(long days);
00262
00268 Date operator-(long days);
00269
00275 inline long operator-(const Date &date)
00276 {return (julian - date.julian);};
00277
00283 Date& operator=(const Date& date);
00284
00290 bool 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
00331 inline bool operator!() const
00332 {return !isValid();};
00333
00338 inline operator bool() const
00339 {return isValid();};
00340 };
00341
00353 class __EXPORT Time
00354 {
00355 protected:
00356 long seconds;
00357
00358 protected:
00359 void toSeconds(int hour, int minute = 0, int second = 0);
00360 void fromSeconds(char *buf) const;
00361 virtual void update(void);
00362
00363 public:
00367 typedef enum {hour = 20, minute, second} index_t;
00368
00372 static const size_t sz_string;
00373
00378 Time(time_t value);
00379
00384 Time(struct tm *object);
00385
00391 Time(char *pointer, size_t size = 0);
00392
00399 Time(int hour, int minute, int second);
00400
00405 Time(const Time& object);
00406
00410 Time();
00411
00415 virtual ~Time();
00416
00421 long get(void) const;
00422
00427 int getHour(void) const;
00428
00433 int getMinute(void) const;
00434
00439 int getSecond(void) const;
00440
00446 char *get(char *buffer) const;
00447
00451 void set(void);
00452
00458 void set(char *pointer, size_t size = 0);
00459
00464 bool isValid(void) const;
00465
00470 inline operator bool() const
00471 {return isValid();};
00472
00477 inline bool operator!() const
00478 {return !isValid();};
00479
00485 long operator-(const Time &reference);
00486
00492 Time operator+(long seconds);
00493
00499 Time operator-(long seconds);
00500
00505 inline operator long()
00506 {return get();};
00507
00512 inline long operator*() const
00513 {return get();};
00514
00520 int operator[](index_t component) const;
00521
00526 String operator()() const;
00527
00532 Time& operator++();
00533
00538 Time& operator--();
00539
00545 Time& operator=(const Time& time);
00546
00552 Time& operator+=(long seconds);
00553
00559 Time& operator-=(long seconds);
00560
00566 bool operator==(const Time &time);
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 };
00603
00614 class __EXPORT DateTime : public Date, public Time
00615 {
00616 protected:
00617 void update(void);
00618
00619 public:
00623 typedef enum {year = Date::year, month = Date::month, day = Date::day,
00624 dow = Date::dow,
00625 hour = Time::hour, minute = Time::minute, second = Time::second} index_t;
00626
00630 static const long c_day;
00631
00635 static const long c_hour;
00636
00640 static const long c_week;
00641
00645 static const size_t sz_string;
00646
00651 DateTime(time_t time);
00652
00657 DateTime(struct tm *tm);
00658
00664 DateTime(const char *pointer, size_t size = 0);
00665
00675 DateTime(int year, unsigned month, unsigned day,
00676 int hour = 0, int minute = 0, int second = 0);
00677
00682 DateTime(const DateTime& object);
00683
00687 DateTime();
00688
00692 virtual ~DateTime();
00693
00699 char *get(char *buffer) const;
00700
00705 time_t get(void) const;
00706
00711 bool isValid(void) const;
00712
00718 long operator-(const DateTime &datetime);
00719
00725 DateTime& operator=(const DateTime& datetime);
00726
00733 DateTime& operator+=(long seconds);
00734
00741 DateTime& operator-=(long seconds);
00742
00749 DateTime operator+(long seconds);
00750
00757 DateTime operator-(long seconds);
00758
00763 DateTime& operator++();
00764
00769 DateTime& operator--();
00770
00776 bool operator==(const DateTime& datetime);
00777
00783 bool operator!=(const DateTime& datetime);
00784
00790 bool operator<(const DateTime& datetime);
00791
00798 bool operator<=(const DateTime& datetime);
00799
00805 bool operator>(const DateTime& datetime);
00806
00813 bool operator>=(const DateTime& datetime);
00814
00819 bool operator!() const;
00820
00826 int operator[](index_t component) const;
00827
00832 operator bool() const;
00833
00838 inline operator long() const
00839 {return Date::get();};
00840
00844 void set(void);
00845
00850 operator double() const;
00851
00857 String format(const char *strftime) const;
00858
00867 static struct tm *glt(time_t *time = NULL);
00868
00877 static struct tm *gmt(time_t *time = NULL);
00878
00883 static void release(struct tm *object);
00884 };
00885
00893 class __EXPORT DateTimeString : public DateTime
00894 {
00895 public:
00900 typedef enum {
00901 DATE, TIME, BOTH} mode_t;
00902
00903 private:
00904 char buffer[DATETIME_BUFFER_SIZE];
00905 mode_t mode;
00906
00907 protected:
00908 void update(void);
00909
00910 public:
00915 DateTimeString(time_t time);
00916
00921 DateTimeString(struct tm *tm);
00922
00928 DateTimeString(const char *pointer, size_t size = 0);
00929
00939 DateTimeString(int year, unsigned month, unsigned day,
00940 int hour = 0, int minute = 0, int second = 0);
00941
00946 DateTimeString(const DateTimeString& object);
00947
00951 DateTimeString(mode_t string = DateTimeString::BOTH);
00952
00956 virtual ~DateTimeString();
00957
00963 inline const char *c_str(void)
00964 {return buffer;};
00965
00971 inline operator const char *(void)
00972 {return buffer;};
00973
00977 void set(void);
00978
00983 void set(mode_t string);
00984 };
00985
00992 class __EXPORT DateNumber : public Number, public Date
00993 {
00994 protected:
00995 void update(void);
00996
00997 public:
01002 DateNumber(char *pointer);
01003
01007 virtual ~DateNumber();
01008
01012 void set(void);
01013 };
01014
01018 typedef DateTime datetime_t;
01019
01023 typedef DateTimeString datetimestring_t;
01024
01028 typedef Date date_t;
01029
01033 typedef struct tm tm_t;
01034
01035 END_NAMESPACE
01036
01037 #endif