ucommon
datetime.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
29 #ifndef _UCOMMON_DATETIME_H_
30 #define _UCOMMON_DATETIME_H_
31 
32 #ifndef _UCOMMON_CONFIG_H_
33 #include <ucommon/platform.h>
34 #endif
35 
36 #ifndef _UCOMMON_NUMBERS_H_
37 #include <ucommon/numbers.h>
38 #endif
39 
40 #ifndef _UCOMMON_STRING_H_
41 #include <ucommon/string.h>
42 #endif
43 
44 #ifndef _MSWINDOWS_
45 #include <unistd.h>
46 #include <sys/time.h>
47 #endif
48 
49 #include <time.h>
50 
51 #define DATE_STRING_SIZE 10
52 #define DATE_BUFFER_SIZE 11
53 #define TIME_STRING_SIZE 8
54 #define TIME_BUFFER_SIZE 9
55 #define DATETIME_STRING_SIZE 19
56 #define DATETIME_BUFFER_SIZE 20
57 
61 typedef struct tm tm_t;
62 
63 NAMESPACE_UCOMMON
64 
65 #ifdef __BORLANDC__
66  using std::tm;
67  using std::time_t;
68 #endif
69 
78 class __EXPORT Date
79 {
80 protected:
81  long julian;
82 
83  void set(long year, long month, long day);
84 
89  virtual void update(void);
90 
91 public:
95  static const size_t sz_string;
96 
101  Date(time_t value);
102 
107  Date(struct tm *object);
108 
114  Date(const char *pointer, size_t size = 0);
115 
122  Date(int year, unsigned month, unsigned day);
123 
128  Date(const Date& object);
129 
133  Date();
134 
138  virtual ~Date();
139 
144  int year(void) const;
145 
150  unsigned month(void) const;
151 
156  unsigned day(void) const;
157 
162  unsigned dow(void) const;
163 
169  const char *put(char *buffer) const;
170 
175  time_t timeref(void) const;
176 
181  long get(void) const;
182 
186  void set(void);
187 
193  void set(const char *pointer, size_t size = 0);
194 
199  bool is_valid(void) const;
200 
205  inline operator long() const
206  {return get();};
207 
212  inline long operator*() const
213  {return get();};
214 
220  String operator()() const;
221 
226  Date& operator++();
227 
232  Date& operator--();
233 
239  Date& operator+=(long offset);
240 
246  Date& operator-=(long offset);
247 
253  Date operator+(long days);
254 
260  Date operator-(long days);
261 
267  inline long operator-(const Date &date)
268  {return (julian - date.julian);};
269 
275  Date& operator=(const Date& date);
276 
282  bool operator==(const Date& date) const;
283 
289  bool operator!=(const Date& date) const;
290 
296  bool operator<(const Date& date) const;
297 
303  bool operator<=(const Date& date) const;
304 
310  bool operator>(const Date& date) const;
311 
317  bool operator>=(const Date& date) const;
318 
323  inline bool operator!() const
324  {return !is_valid();};
325 
330  inline operator bool() const
331  {return is_valid();};
332 };
333 
345 class __EXPORT Time
346 {
347 protected:
348  long seconds;
349 
350 protected:
351  virtual void update(void);
352 
353 public:
354  void set(int hour, int minute = 0, int second = 0);
355 
359  static const long c_day;
360 
364  static const long c_hour;
365 
369  static const long c_week;
370 
374  static const size_t sz_string;
375 
380  Time(time_t value);
381 
386  Time(tm_t *object);
387 
393  Time(const char *pointer, size_t size = 0);
394 
401  Time(int hour, int minute, int second);
402 
407  Time(const Time& object);
408 
412  Time();
413 
417  virtual ~Time();
418 
423  long get(void) const;
424 
429  int hour(void) const;
430 
435  int minute(void) const;
436 
441  int second(void) const;
442 
448  const char *put(char *buffer) const;
449 
453  void set(void);
454 
460  void set(const char *pointer, size_t size = 0);
461 
466  bool is_valid(void) const;
467 
472  inline operator bool() const
473  {return is_valid();};
474 
479  inline bool operator!() const
480  {return !is_valid();};
481 
487  long operator-(const Time &reference);
488 
494  Time operator+(long seconds);
495 
501  Time operator-(long seconds);
502 
507  inline operator long()
508  {return get();};
509 
514  inline long operator*() const
515  {return get();};
516 
521  String operator()() const;
522 
527  Time& operator++();
528 
533  Time& operator--();
534 
540  Time& operator=(const Time& time);
541 
547  Time& operator+=(long seconds);
548 
554  Time& operator-=(long seconds);
555 
561  bool operator==(const Time &time) const;
562 
568  bool operator!=(const Time &time) const;
569 
575  bool operator<(const Time &time) const;
576 
582  bool operator<=(const Time &time) const;
583 
589  bool operator>(const Time &time) const;
590 
596  bool operator>=(const Time &time) const;
597 };
598 
608 class __EXPORT DateTime : public Date, public Time
609 {
610 protected:
611  void update(void);
612 
613 public:
617  static const size_t sz_string;
618 
623  DateTime(time_t time);
624 
629  DateTime(tm_t *tm);
630 
636  DateTime(const char *pointer, size_t size = 0);
637 
647  DateTime(int year, unsigned month, unsigned day,
648  int hour = 0, int minute = 0, int second = 0);
649 
654  DateTime(const DateTime& object);
655 
659  DateTime();
660 
664  virtual ~DateTime();
665 
671  const char *put(char *buffer) const;
672 
677  time_t get(void) const;
678 
683  bool is_valid(void) const;
684 
690  long operator-(const DateTime &datetime);
691 
697  DateTime& operator=(const DateTime& datetime);
698 
705  DateTime& operator+=(long seconds);
706 
713  DateTime& operator-=(long seconds);
714 
721  DateTime operator+(long seconds);
722 
729  DateTime operator-(long seconds);
730 
735  DateTime& operator++();
736 
741  DateTime& operator--();
742 
748  bool operator==(const DateTime& datetime) const;
749 
755  bool operator!=(const DateTime& datetime) const;
756 
762  bool operator<(const DateTime& datetime) const;
763 
770  bool operator<=(const DateTime& datetime) const;
771 
777  bool operator>(const DateTime& datetime) const;
778 
785  bool operator>=(const DateTime& datetime) const;
786 
791  bool operator!() const;
792 
797  operator bool() const;
798 
803  inline operator long() const
804  {return Date::get();};
805 
809  void set(void);
810 
815  operator double() const;
816 
822  String format(const char *strftime) const;
823 
832  static tm_t *local(time_t *time = NULL);
833 
842  static tm_t *gmt(time_t *time = NULL);
843 
848  static void release(tm_t *object);
849 };
850 
858 class __EXPORT DateTimeString : public DateTime
859 {
860 public:
865  typedef enum {
866  DATE, TIME, BOTH} mode_t;
867 
868 private:
869  char buffer[DATETIME_BUFFER_SIZE];
870  mode_t mode;
871 
872 protected:
873  void update(void);
874 
875 public:
880  DateTimeString(time_t time);
881 
886  DateTimeString(tm_t *tm);
887 
893  DateTimeString(const char *pointer, size_t size = 0);
894 
904  DateTimeString(int year, unsigned month, unsigned day,
905  int hour = 0, int minute = 0, int second = 0);
906 
911  DateTimeString(const DateTimeString& object);
912 
916  DateTimeString(mode_t string = DateTimeString::BOTH);
917 
921  virtual ~DateTimeString();
922 
928  inline const char *c_str(void)
929  {return buffer;};
930 
936  inline operator const char *(void)
937  {return buffer;};
938 
942  void set(void);
943 
948  void set(mode_t string);
949 };
950 
957 class __EXPORT DateNumber : public Number, public Date
958 {
959 protected:
960  void update(void);
961 
962 public:
967  DateNumber(char *pointer);
968 
972  virtual ~DateNumber();
973 
977  void set(void);
978 };
979 
980 class __EXPORT isotime : public PrintProtocol, public InputProtocol
981 {
982 private:
983  Date *d;
984  Time *t;
985 
986  enum {DATE, TIME, DATETIME} mode;
987  char buf[32];
988  unsigned pos;
989 
990 protected:
991  const char *_print(void) const;
992 
993  int _input(int code);
994 
995 public:
996  isotime(Date& date, Time& time);
997  isotime(Date& date);
998  isotime(Time& time);
999 };
1000 
1005 
1010 
1014 typedef Date date_t;
1015 
1019 typedef Time tod_t;
1020 
1021 extern "C" {
1022  __EXPORT long tzoffset(struct timezone *tz = NULL);
1023 }
1024 
1025 END_NAMESPACE
1026 
1027 #endif