1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception, you may use this file as part of a free software
19 // library without restriction.  Specifically, if other files instantiate
20 // templates or use macros or inline functions from this file, or you compile
21 // this file and link it with other files to produce an executable, this
22 // file does not by itself cause the resulting executable to be covered by
23 // the GNU General Public License.  This exception does not however
24 // invalidate any other reasons why the executable file might be covered by
25 // the GNU General Public License.
26 //
27 // This exception applies only to the code released under the name GNU
28 // Common C++.  If you copy code from other releases into a copy of GNU
29 // Common C++, as the General Public License permits, the exception does
30 // not apply to the code that you add in this way.  To avoid misleading
31 // anyone as to the status of such modified files, you must delete
32 // this exception notice from them.
33 //
34 // If you write modifications of your own for GNU Common C++, it is your choice
35 // whether to permit this exception to apply to your modifications.
36 // If you do not wish that, delete this exception notice.
37 //
38 
39 /**
40  * @file numbers.h
41  * @short Numbers and dates manipulation.
42  **/
43 
44 #ifndef CCXX_NUMBERS_H_
45 #define CCXX_NUMBERS_H_
46 
47 #ifndef CCXX_THREAD_H_
48 #include <cc++/thread.h>
49 #endif
50 
51 #ifndef CCXX_MISSING_H_
52 #include <cc++/missing.h>
53 #endif
54 
55 #ifndef CCXX_STRCHAR_H_
56 #include <cc++/strchar.h>
57 #endif
58 
59 #ifndef CCXX_STRING_H_
60 #include <cc++/string.h>
61 #endif
62 
63 #ifndef CCXX_THREAD_H_
64 #include <cc++/thread.h>
65 #endif
66 
67 #include <ctime>
68 
69 #ifdef  CCXX_NAMESPACES
70 namespace ost {
71 #ifdef __BORLANDC__
72     using std::tm;
73     using std::time_t;
74 #endif
75 #endif
76 
77 /**
78  * A number manipulation class.  This is used to extract, convert,
79  * and manage simple numbers that are represented in C ascii strings
80  * in a very quick and optimal way.
81  *
82  * @author David Sugar <dyfet@ostel.com>
83  * @short number manipulation.
84  */
85 class __EXPORT Number
86 {
87 protected:
88     char *buffer;
89     unsigned size;
90 
91 public:
92     /**
93      * Create an instance of a number.
94      * @param buffer or NULL if created internally.
95      * @param size use - values for zero filled.
96      */
97     Number(char *buffer, unsigned size);
98 
99     void setValue(long value);
getBuffer()100     const char *getBuffer() const
101         {return buffer;};
102 
103     long getValue() const;
104 
operator()105     long operator()()
106         {return getValue();};
107 
108     operator long()
109         {return getValue();};
110 
111     operator char*()
112         {return buffer;};
113 
114     long operator=(const long value);
115     long operator+=(const long value);
116     long operator-=(const long value);
117     long operator--();
118     long operator++();
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     int operator>=(const Number &num);
125 
126     friend long operator+(const Number &num, const long val);
127     friend long operator+(const long val, const Number &num);
128     friend long operator-(const Number &num, long val);
129     friend long operator-(const long val, const Number &num);
130 };
131 
132 class __EXPORT ZNumber : public Number
133 {
134 public:
135     ZNumber(char *buf, unsigned size);
136     void setValue(long value);
137     long operator=(long value);
138 };
139 
140 /**
141  * The Date class uses a julian date representation of the current
142  * year, month, and day.  This is then manipulated in several forms
143  * and may be exported as needed.
144  *
145  * @author David Sugar <dyfet@ostel.com>
146  * @short julian number based date class.
147  */
148 class __EXPORT Date
149 {
150 protected:
151     long julian;
152 
153 protected:
154     void toJulian(long year, long month, long day);
155     void fromJulian(char *buf) const;
156 
157     /**
158      * A method to use to "post" any changed values when shadowing
159      * a mixed object class.  This is used by DateNumber.
160      */
161     virtual void update(void);
162 
163 public:
164 
165     Date(time_t tm);
166     Date(tm *dt);
167     Date(char *str, size_t size = 0);
168     Date(int year, unsigned month, unsigned day);
169     Date();
170     virtual ~Date();
171 
172     int getYear(void) const;
173     unsigned getMonth(void) const;
174     unsigned getDay(void) const;
175     unsigned getDayOfWeek(void) const;
176     char *getDate(char *buffer) const;
177     time_t getDate(void) const;
178     time_t getDate(tm *buf) const;
179     long getValue(void) const;
180     void setDate(const char *str, size_t size = 0);
181     bool isValid(void) const;
182 
183     friend Date operator+(const Date &date, const long val);
184     friend Date operator-(const Date &date, const long val);
185     friend Date operator+(const long val, const Date &date);
186     friend Date operator-(const long val, const Date &date);
187 
188     operator long() const
189         {return getValue();};
190 
191     String operator()() const;
192     Date& operator++();
193     Date& operator--();
194     Date& operator+=(const long val);
195     Date& operator-=(const long val);
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     int operator>=(const Date &date);
202     bool operator!() const
203         {return !isValid();};
204 };
205 
206 /**
207  * The Time class uses a integer representation of the current
208  * time.  This is then manipulated in several forms
209  * and may be exported as needed.
210  *
211  * @author Marcelo Dalmas <mad@brasmap.com.br>
212  * @short Integer based time class.
213  */
214 
215 class __EXPORT Time
216 {
217 protected:
218     long seconds;
219 
220 protected:
221     void toSeconds(int hour, int minute, int second);
222     void fromSeconds(char *buf) const;
223     virtual void update(void);
224 
225 public:
226     Time(time_t tm);
227     Time(tm *dt);
228     Time(char *str, size_t size = 0);
229     Time(int hour, int minute, int second);
230     Time();
231     virtual ~Time();
232 
233     long getValue(void) const;
234     int getHour(void) const;
235     int getMinute(void) const;
236     int getSecond(void) const;
237     char *getTime(char *buffer) const;
238     time_t getTime(void) const;
239     tm *getTime(tm *buf) const;
240     void setTime(char *str, size_t size = 0);
241     bool isValid(void) const;
242 
243     friend Time operator+(const Time &time1, const Time &time2);
244     friend Time operator-(const Time &time1, const Time &time2);
245     friend Time operator+(const Time &time, const int val);
246     friend Time operator-(const Time &time, const int val);
247     friend Time operator+(const int val, const Time &time);
248     friend Time operator-(const int val, const Time &time);
249 
250     operator long()
251         {return getValue();};
252 
253     String operator()() const;
254     Time& operator++();
255     Time& operator--();
256     Time& operator+=(const int val);
257     Time& operator-=(const int val);
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     int operator>=(const Time &time);
264     bool operator!() const
265         {return !isValid();};
266 };
267 
268 /**
269  * The Datetime class uses a julian date representation of the current
270  * year, month, and day and a integer representation of the current
271  * time.  This is then manipulated in several forms
272  * and may be exported as needed.
273  *
274  * @author Marcelo Dalmas <mad@brasmap.com.br>
275  * @short Integer based time class.
276  */
277 
278 class __EXPORT Datetime : public Date, public Time
279 {
280   public:
281     Datetime(time_t tm);
282     Datetime(tm *dt);
283     Datetime(const char *str, size_t size = 0);
284     Datetime(int year, unsigned month, unsigned day, 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 
309 /**
310  * A number class that manipulates a string buffer that is also a date.
311  *
312  * @author David Sugar <dyfet@ostel.com>
313  * @short a number that is also a date string.
314  */
315 class __EXPORT DateNumber : public Number, public Date
316 {
317 protected:
update(void)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 
332 /** EMACS **
333  * Local variables:
334  * mode: c++
335  * c-basic-offset: 4
336  * End:
337  */
338 
339