GNU CommonC++
numbers.h
Go to the documentation of this file.
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef CCXX_NUMBERS_H_
44 #define CCXX_NUMBERS_H_
45 
46 #ifndef CCXX_THREAD_H_
47 #include <cc++/thread.h>
48 #endif
49 
50 #ifndef CCXX_MISSING_H_
51 #include <cc++/missing.h>
52 #endif
53 
54 #ifndef CCXX_STRCHAR_H_
55 #include <cc++/strchar.h>
56 #endif
57 
58 #ifndef CCXX_STRING_H_
59 #include <cc++/string.h>
60 #endif
61 
62 #ifndef CCXX_THREAD_H_
63 #include <cc++/thread.h>
64 #endif
65 
66 #include <ctime>
67 
68 #ifdef CCXX_NAMESPACES
69 namespace ost {
70 #ifdef __BORLANDC__
71  using std::tm;
72  using std::time_t;
73 #endif
74 #endif
75 
85 {
86 protected:
87  char *buffer;
88  unsigned size;
89 
90 public:
96  Number(char *buffer, unsigned size);
97 
98  void setValue(long value);
99  const char *getBuffer() const
100  {return buffer;};
101 
102  long getValue() const;
103 
104  long operator()()
105  {return getValue();};
106 
107  operator long()
108  {return getValue();};
109 
110  operator char*()
111  {return buffer;};
112 
113  long operator=(const long value);
114  long operator+=(const long value);
115  long operator-=(const long value);
116  long operator--();
117  long operator++();
118  int operator==(const Number &num);
119  int operator!=(const Number &num);
120  int operator<(const Number &num);
121  int operator<=(const Number &num);
122  int operator>(const Number &num);
123  int operator>=(const Number &num);
124 
125  friend long operator+(const Number &num, const long val);
126  friend long operator+(const long val, const Number &num);
127  friend long operator-(const Number &num, long val);
128  friend long operator-(const long val, const Number &num);
129 };
130 
131 class __EXPORT ZNumber : public Number
132 {
133 public:
134  ZNumber(char *buf, unsigned size);
135  void setValue(long value);
136  long operator=(long value);
137 };
138 
148 {
149 protected:
150  long julian;
151 
152 protected:
153  void toJulian(long year, long month, long day);
154  void fromJulian(char *buf) const;
155 
160  virtual void update(void);
161 
162 public:
163 
164  Date(time_t tm);
165  Date(tm *dt);
166  Date(char *str, size_t size = 0);
167  Date(int year, unsigned month, unsigned day);
168  Date();
169  virtual ~Date();
170 
171  int getYear(void) const;
172  unsigned getMonth(void) const;
173  unsigned getDay(void) const;
174  unsigned getDayOfWeek(void) const;
175  char *getDate(char *buffer) const;
176  time_t getDate(void) const;
177  time_t getDate(tm *buf) const;
178  long getValue(void) const;
179  void setDate(const char *str, size_t size = 0);
180  bool isValid(void) const;
181 
182  friend Date operator+(const Date &date, const long val);
183  friend Date operator-(const Date &date, const long val);
184  friend Date operator+(const long val, const Date &date);
185  friend Date operator-(const long val, const Date &date);
186 
187  operator long() const
188  {return getValue();};
189 
190  String operator()() const;
191  Date& operator++();
192  Date& operator--();
193  Date& operator+=(const long val);
194  Date& operator-=(const long val);
195  int operator==(const Date &date);
196  int operator!=(const Date &date);
197  int operator<(const Date &date);
198  int operator<=(const Date &date);
199  int operator>(const Date &date);
200  int operator>=(const Date &date);
201  bool operator!() const
202  {return !isValid();};
203 };
204 
215 {
216 protected:
217  long seconds;
218 
219 protected:
220  void toSeconds(int hour, int minute, int second);
221  void fromSeconds(char *buf) const;
222  virtual void update(void);
223 
224 public:
225  Time(time_t tm);
226  Time(tm *dt);
227  Time(char *str, size_t size = 0);
228  Time(int hour, int minute, int second);
229  Time();
230  virtual ~Time();
231 
232  long getValue(void) const;
233  int getHour(void) const;
234  int getMinute(void) const;
235  int getSecond(void) const;
236  char *getTime(char *buffer) const;
237  time_t getTime(void) const;
238  tm *getTime(tm *buf) const;
239  void setTime(char *str, size_t size = 0);
240  bool isValid(void) const;
241 
242  friend Time operator+(const Time &time1, const Time &time2);
243  friend Time operator-(const Time &time1, const Time &time2);
244  friend Time operator+(const Time &time, const int val);
245  friend Time operator-(const Time &time, const int val);
246  friend Time operator+(const int val, const Time &time);
247  friend Time operator-(const int val, const Time &time);
248 
249  operator long()
250  {return getValue();};
251 
252  String operator()() const;
253  Time& operator++();
254  Time& operator--();
255  Time& operator+=(const int val);
256  Time& operator-=(const int val);
257  int operator==(const Time &time);
258  int operator!=(const Time &time);
259  int operator<(const Time &time);
260  int operator<=(const Time &time);
261  int operator>(const Time &time);
262  int operator>=(const Time &time);
263  bool operator!() const
264  {return !isValid();};
265 };
266 
277 class __EXPORT Datetime : public Date, public Time
278 {
279  public:
280  Datetime(time_t tm);
281  Datetime(tm *dt);
282  Datetime(const char *str, size_t size = 0);
283  Datetime(int year, unsigned month, unsigned day,
284  int hour, int minute, int second);
285  Datetime();
286  virtual ~Datetime();
287 
288  char *getDatetime(char *buffer) const;
289  time_t getDatetime(void) const;
290  bool isValid(void) const;
291 
292  Datetime& operator=(const Datetime datetime);
293  Datetime& operator+=(const Datetime &datetime);
294  Datetime& operator-=(const Datetime &datetime);
295  Datetime& operator+=(const Time &time);
296  Datetime& operator-=(const Time &time);
297 
298  int operator==(const Datetime&);
299  int operator!=(const Datetime&);
300  int operator<(const Datetime&);
301  int operator<=(const Datetime&);
302  int operator>(const Datetime&);
303  int operator>=(const Datetime&);
304  bool operator!() const;
305 
306  String strftime(const char *format) const;
307 };
308 
315 class __EXPORT DateNumber : public Number, public Date
316 {
317 protected:
318  void update(void)
319  {fromJulian(buffer);};
320 
321 public:
322  DateNumber(char *buffer);
323  virtual ~DateNumber();
324 };
325 
326 #ifdef CCXX_NAMESPACES
327 }
328 #endif
329 
330 #endif
331