ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

datetime.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
30 #ifndef _UCOMMON_DATETIME_H_
31 #define _UCOMMON_DATETIME_H_
32 
33 #ifndef _UCOMMON_CONFIG_H_
34 #include <ucommon/platform.h>
35 #endif
36 
37 #ifndef _UCOMMON_NUMBERS_H_
38 #include <ucommon/numbers.h>
39 #endif
40 
41 #ifndef _UCOMMON_TYPEREF_H_
42 #include <ucommon/typeref.h>
43 #endif
44 
45 #ifndef _MSWINDOWS_
46 #include <unistd.h>
47 #include <sys/time.h>
48 #endif
49 
50 #include <time.h>
51 
52 #define DATE_STRING_SIZE 10
53 #define DATE_BUFFER_SIZE 11
54 #define TIME_STRING_SIZE 8
55 #define TIME_BUFFER_SIZE 9
56 #define DATETIME_STRING_SIZE 19
57 #define DATETIME_BUFFER_SIZE 20
58 
62 typedef struct tm tm_t;
63 
64 namespace ucommon {
65 
66 #ifdef __BORLANDC__
67  using std::tm;
68  using std::time_t;
69 #endif
70 
80 {
81 protected:
82  long julian;
83 
84  void set(long year, long month, long day);
85 
90  virtual void update(void);
91 
92 public:
96  static const size_t sz_string;
97 
102  Date(time_t value);
103 
108  Date(const struct tm *object);
109 
115  Date(const char *pointer, size_t size = 0);
116 
123  Date(int year, unsigned month, unsigned day);
124 
129  Date(const Date& object);
130 
134  Date();
135 
139  virtual ~Date();
140 
145  int year(void) const;
146 
151  unsigned month(void) const;
152 
157  unsigned day(void) const;
158 
163  unsigned dow(void) const;
164 
170  const char *put(char *buffer) const;
171 
176  time_t timeref(void) const;
177 
182  long get(void) const;
183 
187  void set(void);
188 
194  void set(const char *pointer, size_t size = 0);
195 
200  bool is_valid(void) const;
201 
206  inline operator long() const {
207  return get();
208  }
209 
214  inline long operator*() const {
215  return get();
216  }
217 
223  stringref_t operator()() const;
224 
229  Date& operator++();
230 
235  Date& operator--();
236 
242  Date& operator+=(long offset);
243 
249  Date& operator-=(long offset);
250 
256  const Date operator+(long days) const;
257 
263  const Date operator-(long days) const;
264 
270  inline long operator-(const Date &date) {
271  return (julian - date.julian);
272  }
273 
279  Date& operator=(const Date& date);
280 
286  bool operator==(const Date& date) const;
287 
293  bool operator!=(const Date& date) const;
294 
300  bool operator<(const Date& date) const;
301 
307  bool operator<=(const Date& date) const;
308 
314  bool operator>(const Date& date) const;
315 
321  bool operator>=(const Date& date) const;
322 
327  inline bool operator!() const {
328  return !is_valid();
329  }
330 
335  inline operator bool() const {
336  return is_valid();
337  }
338 };
339 
352 {
353 protected:
354  long seconds;
355 
356 protected:
357  virtual void update(void);
358 
359 public:
360  void set(int hour, int minute = 0, int second = 0);
361 
365  static const long c_day;
366 
370  static const long c_hour;
371 
375  static const long c_week;
376 
380  static const size_t sz_string;
381 
386  Time(const time_t value);
387 
392  Time(const tm_t *object);
393 
399  Time(const char *pointer, size_t size = 0);
400 
407  Time(int hour, int minute, int second);
408 
413  Time(const Time& object);
414 
418  Time();
419 
423  virtual ~Time();
424 
429  long get(void) const;
430 
435  int hour(void) const;
436 
441  int minute(void) const;
442 
447  int second(void) const;
448 
454  const char *put(char *buffer) const;
455 
459  void set(void);
460 
466  void set(const char *pointer, size_t size = 0);
467 
472  bool is_valid(void) const;
473 
478  inline operator bool() const {
479  return is_valid();
480  }
481 
486  inline bool operator!() const {
487  return !is_valid();
488  }
489 
495  long operator-(const Time &reference);
496 
502  const Time operator+(long seconds) const;
503 
509  const Time operator-(long seconds) const;
510 
515  inline operator long() const {
516  return get();
517  }
518 
523  inline long operator*() const {
524  return get();
525  }
526 
531  stringref_t operator()() const;
532 
537  Time& operator++();
538 
543  Time& operator--();
544 
550  Time& operator=(const Time& time);
551 
557  Time& operator+=(long seconds);
558 
564  Time& operator-=(long seconds);
565 
571  bool operator==(const Time &time) const;
572 
578  bool operator!=(const Time &time) const;
579 
585  bool operator<(const Time &time) const;
586 
592  bool operator<=(const Time &time) const;
593 
599  bool operator>(const Time &time) const;
600 
606  bool operator>=(const Time &time) const;
607 };
608 
618 class __EXPORT DateTime : public Date, public Time
619 {
620 protected:
621  virtual void update(void) __OVERRIDE;
622 
623 public:
627  static const size_t sz_string;
628 
633  DateTime(const time_t time);
634 
639  DateTime(const tm_t *tm);
640 
646  DateTime(const char *pointer, size_t size = 0);
647 
657  DateTime(int year, unsigned month, unsigned day,
658  int hour = 0, int minute = 0, int second = 0);
659 
664  DateTime(const DateTime& object);
665 
669  DateTime();
670 
674  virtual ~DateTime();
675 
681  const char *put(char *buffer) const;
682 
687  time_t get(void) const;
688 
693  bool is_valid(void) const;
694 
700  long operator-(const DateTime &datetime);
701 
707  DateTime& operator=(const DateTime& datetime);
708 
715  DateTime& operator+=(long seconds);
716 
723  DateTime& operator-=(long seconds);
724 
731  const DateTime operator+(long seconds) const;
732 
739  const DateTime operator-(long seconds) const;
740 
745  DateTime& operator++();
746 
751  DateTime& operator--();
752 
758  bool operator==(const DateTime& datetime) const;
759 
765  bool operator!=(const DateTime& datetime) const;
766 
772  bool operator<(const DateTime& datetime) const;
773 
780  bool operator<=(const DateTime& datetime) const;
781 
787  bool operator>(const DateTime& datetime) const;
788 
795  bool operator>=(const DateTime& datetime) const;
796 
801  bool operator!() const;
802 
807  operator bool() const;
808 
813  inline operator long() const {
814  return Date::get();
815  }
816 
820  void set(void);
821 
826  operator double() const;
827 
833  stringref_t format(const char *strftime) const;
834 
843  static tm_t *local(const time_t *time = NULL);
844 
853  static tm_t *gmt(const time_t *time = NULL);
854 
859  static void release(tm_t *object);
860 };
861 
870 {
871 public:
876  typedef enum {
877  DATE, TIME, BOTH
878  } mode_t;
879 
880 private:
883 
884 protected:
885  virtual void update(void) __OVERRIDE;
886 
887 public:
892  DateTimeString(const time_t time);
893 
898  DateTimeString(const tm_t *tm);
899 
905  DateTimeString(const char *pointer, size_t size = 0);
906 
916  DateTimeString(int year, unsigned month, unsigned day,
917  int hour = 0, int minute = 0, int second = 0);
918 
923  DateTimeString(const DateTimeString& object);
924 
929 
933  virtual ~DateTimeString();
934 
940  inline const char *c_str(void) const {
941  return buffer;
942  }
943 
949  inline operator const char *(void) const {
950  return buffer;
951  }
952 
956  void set(void);
957 
962  void set(mode_t string);
963 };
964 
971 class __EXPORT DateNumber : public Number, public Date
972 {
973 protected:
974  virtual void update(void) __OVERRIDE;
975 
976 public:
981  DateNumber(char *pointer);
982 
986  virtual ~DateNumber();
987 
991  void set(void);
992 };
993 
995 {
996 private:
997  Date *d;
998  Time *t;
999 
1000  enum {
1001  DATE, TIME, DATETIME
1002  } mode;
1003 
1004  char buf[32];
1005  unsigned pos;
1006 
1007 protected:
1008  const char *_print(void) const;
1009 
1010  int _input(int code) __OVERRIDE;
1011 
1012 public:
1013  isotime(Date& date, Time& time);
1014  isotime(Date& date);
1015  isotime(Time& time);
1016 };
1017 
1022 
1027 
1031 typedef Date date_t;
1032 
1036 typedef Time tod_t;
1037 
1038 } // namespace ucommon
1039 
1040 #endif
ucommon::Date::operator*
long operator*() const
Definition: datetime.h:214
ucommon::DateTimeString::BOTH
Definition: datetime.h:877
ucommon::PrintProtocol
Definition: protocols.h:134
ucommon::operator+
std::string & operator+(std::string &target, String &source)
Definition: stream.h:577
ucommon::InputProtocol
Definition: protocols.h:153
numbers.h
ucommon
Definition: access.cpp:23
ucommon::Time::operator!
bool operator!() const
Definition: datetime.h:486
ucommon::DateTimeString::c_str
const char * c_str(void) const
Definition: datetime.h:940
ucommon::isotime::TIME
Definition: datetime.h:1001
ucommon::DateTime::sz_string
static const size_t sz_string
Definition: datetime.h:627
__OVERRIDE
#define __OVERRIDE
Definition: platform.h:158
ucommon::DateTimeString
a datetime class that returns strings.
Definition: datetime.h:869
ucommon::tod_t
Time tod_t
Definition: datetime.h:1036
ucommon::isotime::t
Time * t
Definition: datetime.h:998
ucommon::date_t
Date date_t
Definition: datetime.h:1031
ucommon::Date::get
long get(void) const
Definition: datetime.cpp:307
__EXPORT
#define __EXPORT
Definition: config.h:49
ucommon::Date::operator-
long operator-(const Date &date)
Definition: datetime.h:270
ucommon::Time::sz_string
static const size_t sz_string
Definition: datetime.h:380
ucommon::isotime
Definition: datetime.h:994
ucommon::Time::operator*
long operator*() const
Definition: datetime.h:523
tm_t
struct tm tm_t
Definition: datetime.h:62
ucommon::Time::c_hour
static const long c_hour
Definition: datetime.h:370
ucommon::Date::sz_string
static const size_t sz_string
Definition: datetime.h:96
ucommon::Date::julian
long julian
Definition: datetime.h:82
ucommon::datetime_t
DateTime datetime_t
Definition: datetime.h:1021
ucommon::pointer
Definition: generics.h:54
ucommon::Number
number manipulation.
Definition: numbers.h:47
platform.h
ucommon::Date
julian number based date class.
Definition: datetime.h:79
ucommon::isotime::pos
unsigned pos
Definition: datetime.h:1005
buffer
static uint8_t buffer[65536]
Definition: zerofill.cpp:27
DATETIME_BUFFER_SIZE
#define DATETIME_BUFFER_SIZE
Definition: datetime.h:57
ucommon::typeref
Definition: typeref.h:326
ucommon::DateTimeString::mode_t
mode_t
Definition: datetime.h:876
ucommon::Time::c_day
static const long c_day
Definition: datetime.h:365
ucommon::DateTimeString::TIME
Definition: datetime.h:877
ucommon::DateTimeString::mode
mode_t mode
Definition: datetime.h:882
ucommon::DateNumber
a number that is also a date string.
Definition: datetime.h:971
ucommon::Time::seconds
long seconds
Definition: datetime.h:354
__PROTOCOL
#define __PROTOCOL
Definition: platform.h:112
DateTimeString
ucommon::DateTimeString DateTimeString
Definition: numbers.h:50
ucommon::Date::operator!
bool operator!() const
Definition: datetime.h:327
ucommon::DateTime
Integer based time class.
Definition: datetime.h:618
ucommon::operator+=
std::string & operator+=(std::string &target, String &source)
Definition: stream.h:581
ucommon::Time::c_week
static const long c_week
Definition: datetime.h:375
ucommon::datetimestring_t
DateTimeString datetimestring_t
Definition: datetime.h:1026
ucommon::Time
Integer based time class.
Definition: datetime.h:351
typeref.h
DateNumber
ucommon::DateNumber DateNumber
Definition: numbers.h:51
ucommon::isotime::d
Date * d
Definition: datetime.h:997