GNU CommonC++
numbers.h
Go to the documentation of this file.
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
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
70namespace ost {
71#ifdef __BORLANDC__
72 using std::tm;
73 using std::time_t;
74#endif
75#endif
76
86{
87protected:
88 char *buffer;
89 unsigned size;
90
91public:
97 Number(char *buffer, unsigned size);
98
99 void setValue(long value);
100 const char *getBuffer() const
101 {return buffer;};
102
103 long getValue() const;
104
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);
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
132class __EXPORT ZNumber : public Number
133{
134public:
135 ZNumber(char *buf, unsigned size);
136 void setValue(long value);
137 long operator=(long value);
138};
139
149{
150protected:
151 long julian;
152
153protected:
154 void toJulian(long year, long month, long day);
155 void fromJulian(char *buf) const;
156
161 virtual void update(void);
162
163public:
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);
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
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
216{
217protected:
219
220protected:
221 void toSeconds(int hour, int minute, int second);
222 void fromSeconds(char *buf) const;
223 virtual void update(void);
224
225public:
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);
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
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
278class __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);
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
315class __EXPORT DateNumber : public Number, public Date
316{
317protected:
318 void update(void)
319 {fromJulian(buffer);};
320
321public:
322 DateNumber(char *buffer);
323 virtual ~DateNumber();
324};
325
326#ifdef CCXX_NAMESPACES
327}
328#endif
329
330#endif
331
The Date class uses a julian date representation of the current year, month, and day.
Definition numbers.h:149
void setDate(const char *str, size_t size=0)
int operator>=(const Date &date)
friend Date operator-(const long val, const Date &date)
Date & operator++()
int operator!=(const Date &date)
Date & operator-=(const long val)
long julian
Definition numbers.h:151
int operator<(const Date &date)
friend Date operator-(const Date &date, const long val)
int operator<=(const Date &date)
friend Date operator+(const Date &date, const long val)
unsigned getDayOfWeek(void) const
virtual ~Date()
int getYear(void) const
Date(tm *dt)
unsigned getMonth(void) const
unsigned getDay(void) const
Date(int year, unsigned month, unsigned day)
Date & operator--()
void toJulian(long year, long month, long day)
bool operator!() const
Definition numbers.h:202
long getValue(void) const
String operator()() const
time_t getDate(tm *buf) const
Date(time_t tm)
int operator>(const Date &date)
friend Date operator+(const long val, const Date &date)
Date & operator+=(const long val)
bool isValid(void) const
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
int operator==(const Date &date)
time_t getDate(void) const
char * getDate(char *buffer) const
Date(char *str, size_t size=0)
void fromJulian(char *buf) const
A number class that manipulates a string buffer that is also a date.
Definition numbers.h:316
DateNumber(char *buffer)
void update(void)
Definition numbers.h:318
virtual ~DateNumber()
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition numbers.h:279
Datetime & operator+=(const Time &time)
Datetime & operator=(const Datetime datetime)
time_t getDatetime(void) const
bool isValid(void) const
Datetime(time_t tm)
Datetime & operator-=(const Time &time)
Datetime & operator-=(const Datetime &datetime)
bool operator!() const
Datetime(int year, unsigned month, unsigned day, int hour, int minute, int second)
String strftime(const char *format) const
char * getDatetime(char *buffer) const
int operator==(const Datetime &)
int operator<=(const Datetime &)
int operator<(const Datetime &)
Datetime(const char *str, size_t size=0)
Datetime & operator+=(const Datetime &datetime)
int operator>(const Datetime &)
int operator>=(const Datetime &)
Datetime(tm *dt)
int operator!=(const Datetime &)
virtual ~Datetime()
A number manipulation class.
Definition numbers.h:86
char * buffer
Definition numbers.h:88
friend long operator-(const Number &num, long val)
long operator()()
Definition numbers.h:105
friend long operator+(const long val, const Number &num)
long operator--()
long operator-=(const long value)
long operator++()
int operator>(const Number &num)
friend long operator+(const Number &num, const long val)
int operator<(const Number &num)
long getValue() const
unsigned size
Definition numbers.h:89
int operator==(const Number &num)
void setValue(long value)
int operator<=(const Number &num)
const char * getBuffer() const
Definition numbers.h:100
int operator>=(const Number &num)
Number(char *buffer, unsigned size)
Create an instance of a number.
friend long operator-(const long val, const Number &num)
long operator+=(const long value)
long operator=(const long value)
int operator!=(const Number &num)
This is a generic and portable string class.
Definition string.h:81
The Time class uses a integer representation of the current time.
Definition numbers.h:216
Time & operator-=(const int val)
long getValue(void) const
int operator>=(const Time &time)
String operator()() const
bool operator!() const
Definition numbers.h:264
virtual void update(void)
void fromSeconds(char *buf) const
friend Time operator+(const Time &time, const int val)
int getMinute(void) const
int operator<(const Time &time)
int operator!=(const Time &time)
virtual ~Time()
int operator>(const Time &time)
tm * getTime(tm *buf) const
Time & operator--()
Time & operator+=(const int val)
int getHour(void) const
int getSecond(void) const
int operator==(const Time &time)
char * getTime(char *buffer) const
friend Time operator-(const int val, const Time &time)
friend Time operator+(const Time &time1, const Time &time2)
Time(char *str, size_t size=0)
long seconds
Definition numbers.h:218
time_t getTime(void) const
Time & operator++()
friend Time operator-(const Time &time1, const Time &time2)
bool isValid(void) const
friend Time operator-(const Time &time, const int val)
void toSeconds(int hour, int minute, int second)
int operator<=(const Time &time)
Time(tm *dt)
void setTime(char *str, size_t size=0)
Time(time_t tm)
friend Time operator+(const int val, const Time &time)
Time(int hour, int minute, int second)
Definition numbers.h:133
ZNumber(char *buf, unsigned size)
void setValue(long value)
long operator=(long value)
#define __EXPORT
Definition config.h:1045
substitute functions which may be missing in target platform libc.
Definition address.h:64
Common and portable character string related functions.
Common C++ generic string class.
Synchronization and threading services.