UCommon
/usr/src/RPM/BUILD/ucommon-6.3.3/inc/ucommon/datetime.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
00002 // Copyright (C) 2015 Cherokees of Idaho.
00003 //
00004 // This file is part of GNU uCommon C++.
00005 //
00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License as published
00008 // by the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // GNU uCommon C++ is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00018 
00030 #ifndef _UCOMMON_DATETIME_H_
00031 #define _UCOMMON_DATETIME_H_
00032 
00033 #ifndef _UCOMMON_CONFIG_H_
00034 #include <ucommon/platform.h>
00035 #endif
00036 
00037 #ifndef _UCOMMON_NUMBERS_H_
00038 #include <ucommon/numbers.h>
00039 #endif
00040 
00041 #ifndef _UCOMMON_STRING_H_
00042 #include <ucommon/string.h>
00043 #endif
00044 
00045 #ifndef _MSWINDOWS_
00046 #include <unistd.h>
00047 #include <sys/time.h>
00048 #endif
00049 
00050 #include <time.h>
00051 
00052 #define DATE_STRING_SIZE        10
00053 #define DATE_BUFFER_SIZE        11
00054 #define TIME_STRING_SIZE        8
00055 #define TIME_BUFFER_SIZE        9
00056 #define DATETIME_STRING_SIZE    19
00057 #define DATETIME_BUFFER_SIZE    20
00058 
00062 typedef struct tm   tm_t;
00063 
00064 namespace ucommon {
00065 
00066 #ifdef __BORLANDC__
00067     using std::tm;
00068     using std::time_t;
00069 #endif
00070 
00079 class __EXPORT Date
00080 {
00081 protected:
00082     long julian;
00083 
00084     void set(long year, long month, long day);
00085 
00090     virtual void update(void);
00091 
00092 public:
00096     static const size_t sz_string;
00097 
00102     Date(time_t value);
00103 
00108     Date(const struct tm *object);
00109 
00115     Date(const char *pointer, size_t size = 0);
00116 
00123     Date(int year, unsigned month, unsigned day);
00124 
00129     Date(const Date& object);
00130 
00134     Date();
00135 
00139     virtual ~Date();
00140 
00145     int year(void) const;
00146 
00151     unsigned month(void) const;
00152 
00157     unsigned day(void) const;
00158 
00163     unsigned dow(void) const;
00164 
00170     const char *put(char *buffer) const;
00171 
00176     time_t timeref(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 is_valid(void) const;
00201 
00206     inline operator long() const
00207         {return get();}
00208 
00213     inline long operator*() const
00214         {return get();}
00215 
00221     String operator()() const;
00222 
00227     Date& operator++();
00228 
00233     Date& operator--();
00234 
00240     Date& operator+=(long offset);
00241 
00247     Date& operator-=(long offset);
00248 
00254     Date operator+(long days);
00255 
00261     Date operator-(long days);
00262 
00268     inline long operator-(const Date &date)
00269         {return (julian - date.julian);}
00270 
00276     Date& operator=(const Date& date);
00277 
00283     bool operator==(const Date& date) const;
00284 
00290     bool operator!=(const Date& date) const;
00291 
00297     bool operator<(const Date& date) const;
00298 
00304     bool operator<=(const Date& date) const;
00305 
00311     bool operator>(const Date& date) const;
00312 
00318     bool operator>=(const Date& date) const;
00319 
00324     inline bool operator!() const
00325         {return !is_valid();}
00326 
00331     inline operator bool() const
00332         {return is_valid();}
00333 };
00334 
00346 class __EXPORT Time
00347 {
00348 protected:
00349     long seconds;
00350 
00351 protected:
00352     virtual void update(void);
00353 
00354 public:
00355     void set(int hour, int minute = 0, int second = 0);
00356 
00360     static const long c_day;
00361 
00365     static const long c_hour;
00366 
00370     static const long c_week;
00371 
00375     static const size_t sz_string;
00376 
00381     Time(const time_t value);
00382 
00387     Time(const tm_t *object);
00388 
00394     Time(const char *pointer, size_t size = 0);
00395 
00402     Time(int hour, int minute, int second);
00403 
00408     Time(const Time& object);
00409 
00413     Time();
00414 
00418     virtual ~Time();
00419 
00424     long get(void) const;
00425 
00430     int hour(void) const;
00431 
00436     int minute(void) const;
00437 
00442     int second(void) const;
00443 
00449     const char *put(char *buffer) const;
00450 
00454     void set(void);
00455 
00461     void set(const char *pointer, size_t size = 0);
00462 
00467     bool is_valid(void) const;
00468 
00473     inline operator bool() const
00474         {return is_valid();}
00475 
00480     inline bool operator!() const
00481         {return !is_valid();}
00482 
00488     long operator-(const Time &reference);
00489 
00495     Time operator+(long seconds);
00496 
00502     Time operator-(long seconds);
00503 
00508     inline operator long() const
00509         {return get();}
00510 
00515     inline long operator*() const
00516         {return get();}
00517 
00522     String operator()() const;
00523 
00528     Time& operator++();
00529 
00534     Time& operator--();
00535 
00541     Time& operator=(const Time& time);
00542 
00548     Time& operator+=(long seconds);
00549 
00555     Time& operator-=(long seconds);
00556 
00562     bool operator==(const Time &time) const;
00563 
00569     bool operator!=(const Time &time) const;
00570 
00576     bool operator<(const Time &time) const;
00577 
00583     bool operator<=(const Time &time) const;
00584 
00590     bool operator>(const Time &time) const;
00591 
00597     bool operator>=(const Time &time) const;
00598 };
00599 
00609 class __EXPORT DateTime : public Date, public Time
00610 {
00611 protected:
00612     void update(void);
00613 
00614 public:
00618     static const size_t sz_string;
00619 
00624     DateTime(const time_t time);
00625 
00630     DateTime(const tm_t *tm);
00631 
00637     DateTime(const char *pointer, size_t size = 0);
00638 
00648     DateTime(int year, unsigned month, unsigned day,
00649          int hour = 0, int minute = 0, int second = 0);
00650 
00655     DateTime(const DateTime& object);
00656 
00660     DateTime();
00661 
00665     virtual ~DateTime();
00666 
00672     const char *put(char *buffer) const;
00673 
00678     time_t get(void) const;
00679 
00684     bool is_valid(void) const;
00685 
00691     long operator-(const DateTime &datetime);
00692 
00698     DateTime& operator=(const DateTime& datetime);
00699 
00706     DateTime& operator+=(long seconds);
00707 
00714     DateTime& operator-=(long seconds);
00715 
00722     DateTime operator+(long seconds);
00723 
00730     DateTime operator-(long seconds);
00731 
00736     DateTime& operator++();
00737 
00742     DateTime& operator--();
00743 
00749     bool operator==(const DateTime& datetime) const;
00750 
00756     bool operator!=(const DateTime& datetime) const;
00757 
00763     bool operator<(const DateTime& datetime) const;
00764 
00771     bool operator<=(const DateTime& datetime) const;
00772 
00778     bool operator>(const DateTime& datetime) const;
00779 
00786     bool operator>=(const DateTime& datetime) const;
00787 
00792     bool operator!() const;
00793 
00798     operator bool() const;
00799 
00804     inline operator long() const
00805         {return Date::get();}
00806 
00810     void set(void);
00811 
00816     operator double() const;
00817 
00823     String format(const char *strftime) const;
00824 
00833     static tm_t *local(const time_t *time = NULL);
00834 
00843     static tm_t *gmt(const time_t *time = NULL);
00844 
00849     static void release(tm_t *object);
00850 };
00851 
00859 class __EXPORT DateTimeString : public DateTime
00860 {
00861 public:
00866     typedef enum {
00867         DATE, TIME, BOTH} mode_t;
00868 
00869 private:
00870     char buffer[DATETIME_BUFFER_SIZE];
00871     mode_t mode;
00872 
00873 protected:
00874     void update(void);
00875 
00876 public:
00881     DateTimeString(const time_t time);
00882 
00887     DateTimeString(const tm_t *tm);
00888 
00894     DateTimeString(const char *pointer, size_t size = 0);
00895 
00905     DateTimeString(int year, unsigned month, unsigned day,
00906          int hour = 0, int minute = 0, int second = 0);
00907 
00912     DateTimeString(const DateTimeString& object);
00913 
00917     DateTimeString(mode_t string = DateTimeString::BOTH);
00918 
00922     virtual ~DateTimeString();
00923 
00929     inline const char *c_str(void) const
00930         {return buffer;}
00931 
00937     inline operator const char *(void) const
00938         {return buffer;}
00939 
00943     void set(void);
00944 
00949     void set(mode_t string);
00950 };
00951 
00958 class __EXPORT DateNumber : public Number, public Date
00959 {
00960 protected:
00961     void update(void);
00962 
00963 public:
00968     DateNumber(char *pointer);
00969 
00973     virtual ~DateNumber();
00974 
00978     void set(void);
00979 };
00980 
00981 class __EXPORT isotime : public PrintProtocol, public InputProtocol
00982 {
00983 private:
00984     Date *d;
00985     Time *t;
00986 
00987     enum {DATE, TIME, DATETIME} mode;
00988     char buf[32];
00989     unsigned pos;
00990 
00991 protected:
00992     const char *_print(void) const;
00993 
00994     int _input(int code);
00995 
00996 public:
00997     isotime(Date& date, Time& time);
00998     isotime(Date& date);
00999     isotime(Time& time);
01000 };
01001 
01005 typedef DateTime    datetime_t;
01006 
01010 typedef DateTimeString  datetimestring_t;
01011 
01015 typedef Date        date_t;
01016 
01020 typedef Time        tod_t;
01021 
01022 } // namespace ucommon
01023 
01024 extern "C" {
01025     __EXPORT long tzoffset(struct timezone *tz = NULL);
01026 }
01027 
01028 #endif