ucommon
ucommon/datetime.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2014 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 #ifndef _MSWINDOWS_
00045 #include <unistd.h>
00046 #include <sys/time.h>
00047 #endif
00048 
00049 #include <time.h>
00050 
00051 #define DATE_STRING_SIZE        10
00052 #define DATE_BUFFER_SIZE        11
00053 #define TIME_STRING_SIZE        8
00054 #define TIME_BUFFER_SIZE        9
00055 #define DATETIME_STRING_SIZE    19
00056 #define DATETIME_BUFFER_SIZE    20
00057 
00061 typedef struct tm   tm_t;
00062 
00063 namespace ucommon {
00064 
00065 #ifdef __BORLANDC__
00066     using std::tm;
00067     using std::time_t;
00068 #endif
00069 
00078 class __EXPORT Date
00079 {
00080 protected:
00081     long julian;
00082 
00083     void set(long year, long month, long day);
00084 
00089     virtual void update(void);
00090 
00091 public:
00095     static const size_t sz_string;
00096 
00101     Date(time_t value);
00102 
00107     Date(struct tm *object);
00108 
00114     Date(const char *pointer, size_t size = 0);
00115 
00122     Date(int year, unsigned month, unsigned day);
00123 
00128     Date(const Date& object);
00129 
00133     Date();
00134 
00138     virtual ~Date();
00139 
00144     int year(void) const;
00145 
00150     unsigned month(void) const;
00151 
00156     unsigned day(void) const;
00157 
00162     unsigned dow(void) const;
00163 
00169     const char *put(char *buffer) const;
00170 
00175     time_t timeref(void) const;
00176 
00181     long get(void) const;
00182 
00186     void set(void);
00187 
00193     void set(const char *pointer, size_t size = 0);
00194 
00199     bool is_valid(void) const;
00200 
00205     inline operator long() const
00206         {return get();}
00207 
00212     inline long operator*() const
00213         {return get();}
00214 
00220     String operator()() const;
00221 
00226     Date& operator++();
00227 
00232     Date& operator--();
00233 
00239     Date& operator+=(long offset);
00240 
00246     Date& operator-=(long offset);
00247 
00253     Date operator+(long days);
00254 
00260     Date operator-(long days);
00261 
00267     inline long operator-(const Date &date)
00268         {return (julian - date.julian);}
00269 
00275     Date& operator=(const Date& date);
00276 
00282     bool operator==(const Date& date) const;
00283 
00289     bool operator!=(const Date& date) const;
00290 
00296     bool operator<(const Date& date) const;
00297 
00303     bool operator<=(const Date& date) const;
00304 
00310     bool operator>(const Date& date) const;
00311 
00317     bool operator>=(const Date& date) const;
00318 
00323     inline bool operator!() const
00324         {return !is_valid();}
00325 
00330     inline operator bool() const
00331         {return is_valid();}
00332 };
00333 
00345 class __EXPORT Time
00346 {
00347 protected:
00348     long seconds;
00349 
00350 protected:
00351     virtual void update(void);
00352 
00353 public:
00354     void set(int hour, int minute = 0, int second = 0);
00355 
00359     static const long c_day;
00360 
00364     static const long c_hour;
00365 
00369     static const long c_week;
00370 
00374     static const size_t sz_string;
00375 
00380     Time(time_t value);
00381 
00386     Time(tm_t *object);
00387 
00393     Time(const char *pointer, size_t size = 0);
00394 
00401     Time(int hour, int minute, int second);
00402 
00407     Time(const Time& object);
00408 
00412     Time();
00413 
00417     virtual ~Time();
00418 
00423     long get(void) const;
00424 
00429     int hour(void) const;
00430 
00435     int minute(void) const;
00436 
00441     int second(void) const;
00442 
00448     const char *put(char *buffer) const;
00449 
00453     void set(void);
00454 
00460     void set(const char *pointer, size_t size = 0);
00461 
00466     bool is_valid(void) const;
00467 
00472     inline operator bool() const
00473         {return is_valid();}
00474 
00479     inline bool operator!() const
00480         {return !is_valid();}
00481 
00487     long operator-(const Time &reference);
00488 
00494     Time operator+(long seconds);
00495 
00501     Time operator-(long seconds);
00502 
00507     inline operator long()
00508         {return get();}
00509 
00514     inline long operator*() const
00515         {return get();}
00516 
00521     String operator()() const;
00522 
00527     Time& operator++();
00528 
00533     Time& operator--();
00534 
00540     Time& operator=(const Time& time);
00541 
00547     Time& operator+=(long seconds);
00548 
00554     Time& operator-=(long seconds);
00555 
00561     bool operator==(const Time &time) const;
00562 
00568     bool operator!=(const Time &time) const;
00569 
00575     bool operator<(const Time &time) const;
00576 
00582     bool operator<=(const Time &time) const;
00583 
00589     bool operator>(const Time &time) const;
00590 
00596     bool operator>=(const Time &time) const;
00597 };
00598 
00608 class __EXPORT DateTime : public Date, public Time
00609 {
00610 protected:
00611     void update(void);
00612 
00613 public:
00617     static const size_t sz_string;
00618 
00623     DateTime(time_t time);
00624 
00629     DateTime(tm_t *tm);
00630 
00636     DateTime(const char *pointer, size_t size = 0);
00637 
00647     DateTime(int year, unsigned month, unsigned day,
00648          int hour = 0, int minute = 0, int second = 0);
00649 
00654     DateTime(const DateTime& object);
00655 
00659     DateTime();
00660 
00664     virtual ~DateTime();
00665 
00671     const char *put(char *buffer) const;
00672 
00677     time_t get(void) const;
00678 
00683     bool is_valid(void) const;
00684 
00690     long operator-(const DateTime &datetime);
00691 
00697     DateTime& operator=(const DateTime& datetime);
00698 
00705     DateTime& operator+=(long seconds);
00706 
00713     DateTime& operator-=(long seconds);
00714 
00721     DateTime operator+(long seconds);
00722 
00729     DateTime operator-(long seconds);
00730 
00735     DateTime& operator++();
00736 
00741     DateTime& operator--();
00742 
00748     bool operator==(const DateTime& datetime) const;
00749 
00755     bool operator!=(const DateTime& datetime) const;
00756 
00762     bool operator<(const DateTime& datetime) const;
00763 
00770     bool operator<=(const DateTime& datetime) const;
00771 
00777     bool operator>(const DateTime& datetime) const;
00778 
00785     bool operator>=(const DateTime& datetime) const;
00786 
00791     bool operator!() const;
00792 
00797     operator bool() const;
00798 
00803     inline operator long() const
00804         {return Date::get();}
00805 
00809     void set(void);
00810 
00815     operator double() const;
00816 
00822     String format(const char *strftime) const;
00823 
00832     static tm_t *local(time_t *time = NULL);
00833 
00842     static tm_t *gmt(time_t *time = NULL);
00843 
00848     static void release(tm_t *object);
00849 };
00850 
00858 class __EXPORT DateTimeString : public DateTime
00859 {
00860 public:
00865     typedef enum {
00866         DATE, TIME, BOTH} mode_t;
00867 
00868 private:
00869     char buffer[DATETIME_BUFFER_SIZE];
00870     mode_t mode;
00871 
00872 protected:
00873     void update(void);
00874 
00875 public:
00880     DateTimeString(time_t time);
00881 
00886     DateTimeString(tm_t *tm);
00887 
00893     DateTimeString(const char *pointer, size_t size = 0);
00894 
00904     DateTimeString(int year, unsigned month, unsigned day,
00905          int hour = 0, int minute = 0, int second = 0);
00906 
00911     DateTimeString(const DateTimeString& object);
00912 
00916     DateTimeString(mode_t string = DateTimeString::BOTH);
00917 
00921     virtual ~DateTimeString();
00922 
00928     inline const char *c_str(void)
00929         {return buffer;}
00930 
00936     inline operator const char *(void)
00937         {return buffer;}
00938 
00942     void set(void);
00943 
00948     void set(mode_t string);
00949 };
00950 
00957 class __EXPORT DateNumber : public Number, public Date
00958 {
00959 protected:
00960     void update(void);
00961 
00962 public:
00967     DateNumber(char *pointer);
00968 
00972     virtual ~DateNumber();
00973 
00977     void set(void);
00978 };
00979 
00980 class __EXPORT isotime : public PrintProtocol, public InputProtocol
00981 {
00982 private:
00983     Date *d;
00984     Time *t;
00985 
00986     enum {DATE, TIME, DATETIME} mode;
00987     char buf[32];
00988     unsigned pos;
00989 
00990 protected:
00991     const char *_print(void) const;
00992 
00993     int _input(int code);
00994 
00995 public:
00996     isotime(Date& date, Time& time);
00997     isotime(Date& date);
00998     isotime(Time& time);
00999 };
01000 
01004 typedef DateTime    datetime_t;
01005 
01009 typedef DateTimeString  datetimestring_t;
01010 
01014 typedef Date        date_t;
01015 
01019 typedef Time        tod_t;
01020 
01021 } // namespace ucommon
01022 
01023 extern "C" {
01024     __EXPORT long tzoffset(struct timezone *tz = NULL);
01025 }
01026 
01027 #endif