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
bool operator>=(const Date &date) const
Compare julian date if later than or equal to another date.
bool is_valid(void) const
Check if date is valid.
bool operator<=(const Date &date) const
Compare julian date if earlier than or equal to another date.
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition: datetime.h:608
bool operator!() const
Check if time object has valid value for ! operator.
Definition: datetime.h:479
virtual const char * _print(void) const =0
Extract formatted string for object.
virtual int _input(int code)=0
Extract formatted string for object.
const char * c_str(void)
Extract char from string.
Definition: datetime.h:928
static const size_t sz_string
Size of datetime string field.
Definition: datetime.h:617
Generic smart pointer class.
Definition: generics.h:56
static const size_t sz_string
Size of time string field.
Definition: datetime.h:374
A copy-on-write string class that operates by reference count.
Definition: string.h:82
long operator-(const Date &date)
Operator to compute number of days between two dates.
Definition: datetime.h:267
struct tm tm_t
Convenience type for struct tm.
Definition: datetime.h:61
const char * put(char *buffer) const
Get a ISO string representation of the date (yyyy-mm-dd).
bool operator<(const Date &date) const
Compare julian date if earlier than another date.
DateTime datetime_t
Convenience type for using DateTime object.
Definition: datetime.h:1004
A number class that manipulates a string buffer that is also a date.
Definition: datetime.h:957
Date & operator=(const Date &date)
Assign date from another date object.
Date operator-(long days)
Subtract days from a julian date in an expression.
Used for processing input.
Definition: protocols.h:156
Support classes for manipulation of numbers as strings.
void release(SharedAccess &object)
Convenience function to unlock shared object through it&#39;s protocol.
Definition: access.h:260
Various miscellaneous platform specific headers and defines.
bool operator!() const
Check if julian date is not valid.
Definition: datetime.h:323
static const size_t sz_string
Size of date string field.
Definition: datetime.h:95
long operator*() const
Get object time in seconds.
Definition: datetime.h:514
bool operator!=(const Date &date) const
Compare julian dates if not same date.
A number manipulation class.
Definition: numbers.h:46
long operator*() const
Access julian value.
Definition: datetime.h:212
void set(void)
Set (update) the date with current date.
static const long c_day
Constant for number of seconds in a day.
Definition: datetime.h:359
The Date class uses a julian date representation of the current year, month, and day.
Definition: datetime.h:78
Date & operator--()
Decrement date by one day.
A DateTime string class.
Definition: datetime.h:858
static const long c_week
Constant for number of seconds in a week.
Definition: datetime.h:369
static const long c_hour
Constant for number of seconds in a hour.
Definition: datetime.h:364
The Time class uses a integer representation of the current time.
Definition: datetime.h:345
Date & operator++()
Increment date by one day.
long get(void) const
Get the date as a number for the object or 0 if invalid.
DateTimeString datetimestring_t
Convenience type for using DateTimeString object.
Definition: datetime.h:1009
Used for forming stream output.
Definition: protocols.h:137
Date & operator-=(long offset)
Decrement date by offset.
virtual void update(void)
A method to use to &quot;post&quot; any changed values when shadowing a mixed object class. ...
bool operator>(const Date &date) const
Compare julian date if later than another date.
void set(void)
Set (update) the date and time with current date and time.
A common string class and character string support functions.
void update(void)
A method to use to &quot;post&quot; any changed values when shadowing a mixed object class. ...
Date operator+(long days)
Add days to julian date in an expression.
Date & operator+=(long offset)
Increment date by offset.
Time tod_t
Convenience type for using Time object.
Definition: datetime.h:1019
bool operator==(const Date &date) const
Compare julian dates if same date.
Date date_t
Convenience type for using Date object.
Definition: datetime.h:1014