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 #include <time.h>
45 
46 #define DATE_STRING_SIZE 10
47 #define DATE_BUFFER_SIZE 11
48 #define TIME_STRING_SIZE 8
49 #define TIME_BUFFER_SIZE 9
50 #define DATETIME_STRING_SIZE 19
51 #define DATETIME_BUFFER_SIZE 20
52 
56 typedef struct tm tm_t;
57 
58 NAMESPACE_UCOMMON
59 
60 #ifdef __BORLANDC__
61  using std::tm;
62  using std::time_t;
63 #endif
64 
73 class __EXPORT Date
74 {
75 protected:
76  long julian;
77 
78  void toJulian(long year, long month, long day);
79  void fromJulian(char *buf) const;
80 
85  virtual void update(void);
86 
87 public:
91  typedef enum {year = 10, month, day, dow} index_t;
92 
96  static const size_t sz_string;
97 
102  Date(time_t value);
103 
108  Date(struct tm *object);
109 
115  Date(const char *pointer, size_t size = 0);
116 
123  Date(int year, unsigned month = 1, unsigned day = 1);
124 
129  Date(const Date& object);
130 
134  Date();
135 
139  virtual ~Date();
140 
145  int getYear(void) const;
146 
151  unsigned getMonth(void) const;
152 
157  unsigned getDay(void) const;
158 
163  unsigned getDayOfWeek(void) const;
164 
169  inline long getJulian(void)
170  {return julian;};
171 
177  char *get(char *buffer) const;
178 
183  time_t getTime(void) const;
184 
189  long get(void) const;
190 
194  void set(void);
195 
201  void set(const char *pointer, size_t size = 0);
202 
207  bool isValid(void) const;
208 
213  inline operator long() const
214  {return get();};
215 
221  int operator[](index_t component) const;
222 
227  inline long operator*() const
228  {return get();};
229 
235  String operator()() const;
236 
241  Date& operator++();
242 
247  Date& operator--();
248 
254  Date& operator+=(long offset);
255 
261  Date& operator-=(long offset);
262 
268  Date operator+(long days);
269 
275  Date operator-(long days);
276 
282  inline long operator-(const Date &date)
283  {return (julian - date.julian);};
284 
290  Date& operator=(const Date& date);
291 
297  bool operator==(const Date& date);
298 
304  bool operator!=(const Date& date);
305 
311  bool operator<(const Date& date);
312 
318  bool operator<=(const Date& date);
319 
325  bool operator>(const Date& date);
326 
332  bool operator>=(const Date& date);
333 
338  inline bool operator!() const
339  {return !isValid();};
340 
345  inline operator bool() const
346  {return isValid();};
347 };
348 
360 class __EXPORT Time
361 {
362 protected:
363  long seconds;
364 
365 protected:
366  void toSeconds(int hour, int minute = 0, int second = 0);
367  void fromSeconds(char *buf) const;
368  virtual void update(void);
369 
370 public:
374  typedef enum {hour = 20, minute, second} index_t;
375 
379  static const size_t sz_string;
380 
385  Time(time_t value);
386 
391  Time(tm_t *object);
392 
398  Time(char *pointer, size_t size = 0);
399 
406  Time(int hour, int minute, int second);
407 
412  Time(const Time& object);
413 
417  Time();
418 
422  virtual ~Time();
423 
428  long get(void) const;
429 
434  int getHour(void) const;
435 
440  int getMinute(void) const;
441 
446  int getSecond(void) const;
447 
453  char *get(char *buffer) const;
454 
458  void set(void);
459 
465  void set(char *pointer, size_t size = 0);
466 
471  bool isValid(void) const;
472 
477  inline operator bool() const
478  {return isValid();};
479 
484  inline bool operator!() const
485  {return !isValid();};
486 
492  long operator-(const Time &reference);
493 
499  Time operator+(long seconds);
500 
506  Time operator-(long seconds);
507 
512  inline operator long()
513  {return get();};
514 
519  inline long operator*() const
520  {return get();};
521 
527  int operator[](index_t component) const;
528 
533  String operator()() const;
534 
539  Time& operator++();
540 
545  Time& operator--();
546 
552  Time& operator=(const Time& time);
553 
559  Time& operator+=(long seconds);
560 
566  Time& operator-=(long seconds);
567 
573  bool operator==(const Time &time);
574 
580  bool operator!=(const Time &time);
581 
587  bool operator<(const Time &time);
588 
594  bool operator<=(const Time &time);
595 
601  bool operator>(const Time &time);
602 
608  bool operator>=(const Time &time);
609 };
610 
620 class __EXPORT DateTime : public Date, public Time
621 {
622 protected:
623  void update(void);
624 
625 public:
629  typedef enum {year = Date::year, month = Date::month, day = Date::day,
630  dow = Date::dow,
631  hour = Time::hour, minute = Time::minute, second = Time::second} index_t;
632 
636  static const long c_day;
637 
641  static const long c_hour;
642 
646  static const long c_week;
647 
651  static const size_t sz_string;
652 
657  DateTime(time_t time);
658 
663  DateTime(tm_t *tm);
664 
670  DateTime(const char *pointer, size_t size = 0);
671 
681  DateTime(int year, unsigned month, unsigned day,
682  int hour = 0, int minute = 0, int second = 0);
683 
688  DateTime(const DateTime& object);
689 
693  DateTime();
694 
698  virtual ~DateTime();
699 
705  char *get(char *buffer) const;
706 
711  time_t get(void) const;
712 
717  bool isValid(void) const;
718 
724  long operator-(const DateTime &datetime);
725 
731  DateTime& operator=(const DateTime& datetime);
732 
739  DateTime& operator+=(long seconds);
740 
747  DateTime& operator-=(long seconds);
748 
755  DateTime operator+(long seconds);
756 
763  DateTime operator-(long seconds);
764 
769  DateTime& operator++();
770 
775  DateTime& operator--();
776 
782  bool operator==(const DateTime& datetime);
783 
789  bool operator!=(const DateTime& datetime);
790 
796  bool operator<(const DateTime& datetime);
797 
804  bool operator<=(const DateTime& datetime);
805 
811  bool operator>(const DateTime& datetime);
812 
819  bool operator>=(const DateTime& datetime);
820 
825  bool operator!() const;
826 
832  int operator[](index_t component) const;
833 
838  operator bool() const;
839 
844  inline operator long() const
845  {return Date::get();};
846 
850  void set(void);
851 
856  operator double() const;
857 
863  String format(const char *strftime) const;
864 
873  static tm_t *glt(time_t *time = NULL);
874 
883  static tm_t *gmt(time_t *time = NULL);
884 
889  static void release(tm_t *object);
890 };
891 
899 class __EXPORT DateTimeString : public DateTime
900 {
901 public:
906  typedef enum {
907  DATE, TIME, BOTH} mode_t;
908 
909 private:
910  char buffer[DATETIME_BUFFER_SIZE];
911  mode_t mode;
912 
913 protected:
914  void update(void);
915 
916 public:
921  DateTimeString(time_t time);
922 
927  DateTimeString(tm_t *tm);
928 
934  DateTimeString(const char *pointer, size_t size = 0);
935 
945  DateTimeString(int year, unsigned month, unsigned day,
946  int hour = 0, int minute = 0, int second = 0);
947 
952  DateTimeString(const DateTimeString& object);
953 
957  DateTimeString(mode_t string = DateTimeString::BOTH);
958 
962  virtual ~DateTimeString();
963 
969  inline const char *c_str(void)
970  {return buffer;};
971 
977  inline operator const char *(void)
978  {return buffer;};
979 
983  void set(void);
984 
989  void set(mode_t string);
990 };
991 
998 class __EXPORT DateNumber : public Number, public Date
999 {
1000 protected:
1001  void update(void);
1002 
1003 public:
1008  DateNumber(char *pointer);
1009 
1013  virtual ~DateNumber();
1014 
1018  void set(void);
1019 };
1020 
1025 
1030 
1034 typedef Date date_t;
1035 
1036 END_NAMESPACE
1037 
1038 #endif