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
70 namespace ost {
71 #ifdef __BORLANDC__
72  using std::tm;
73  using std::time_t;
74 #endif
75 #endif
76 
86 {
87 protected:
88  char *buffer;
89  unsigned size;
90 
91 public:
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 
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 
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 
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 
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 {
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 
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 
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);
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 
ost::Datetime
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition: numbers.h:279
ost::Time::update
virtual void update(void)
ost::ZNumber
Definition: numbers.h:133
ost::Number::operator<
int operator<(const Number &num)
ost::Date::~Date
virtual ~Date()
ost::Date::Date
Date()
ost::DateNumber
A number class that manipulates a string buffer that is also a date.
Definition: numbers.h:316
ost::Date::operator++
Date & operator++()
ost::Date::operator>=
int operator>=(const Date &date)
ost::Datetime::operator!
bool operator!() const
ost::Date::Date
Date(tm *dt)
ost::Time::operator!=
int operator!=(const Time &time)
ost::Date::Date
Date(int year, unsigned month, unsigned day)
ost::Datetime::operator==
int operator==(const Datetime &)
ost::Date::getMonth
unsigned getMonth(void) const
ost::Date::operator<
int operator<(const Date &date)
ost::Date::operator>
int operator>(const Date &date)
ost::Date::operator+=
Date & operator+=(const long val)
ost::Number::operator=
long operator=(const long value)
string.h
Common C++ generic string class.
ost::DateNumber::DateNumber
DateNumber(char *buffer)
ost::Date::getDate
char * getDate(char *buffer) const
ost::Date::julian
long julian
Definition: numbers.h:151
ost::Number::operator+=
long operator+=(const long value)
ost::Time::Time
Time(tm *dt)
ost::Time::Time
Time()
ost::Time::operator+
friend Time operator+(const Time &time, const int val)
ost::Date::getDate
time_t getDate(void) const
ost::Time::isValid
bool isValid(void) const
ost::Date::operator-
friend Date operator-(const long val, const Date &date)
ost::Number::operator>
int operator>(const Number &num)
ost::Time::operator==
int operator==(const Time &time)
ost::Datetime::getDatetime
time_t getDatetime(void) const
ost::Number::size
unsigned size
Definition: numbers.h:89
ost::Datetime::operator<
int operator<(const Datetime &)
ost::Time::getSecond
int getSecond(void) const
ost::Time::getValue
long getValue(void) const
ost::DateNumber::~DateNumber
virtual ~DateNumber()
ost::Datetime::strftime
String strftime(const char *format) const
ost::Date::isValid
bool isValid(void) const
ost::Datetime::operator>=
int operator>=(const Datetime &)
ost::Datetime::Datetime
Datetime(tm *dt)
ost::Time::getMinute
int getMinute(void) const
ost::Time
The Time class uses a integer representation of the current time.
Definition: numbers.h:216
ost::ZNumber::operator=
long operator=(long value)
ost::Date::getDay
unsigned getDay(void) const
ost::Number::operator!=
int operator!=(const Number &num)
ost::Datetime::~Datetime
virtual ~Datetime()
ost::Time::fromSeconds
void fromSeconds(char *buf) const
ost::Number::getValue
long getValue() const
ost::Time::operator-
friend Time operator-(const Time &time, const int val)
ost::Number::operator--
long operator--()
ost::Time::operator++
Time & operator++()
__EXPORT
#define __EXPORT
Definition: config.h:979
ost::Date::getDate
time_t getDate(tm *buf) const
ost::Number::setValue
void setValue(long value)
ost::Time::getTime
char * getTime(char *buffer) const
ost::Datetime::operator>
int operator>(const Datetime &)
ost::DateNumber::update
void update(void)
Definition: numbers.h:318
ost::Date::operator-
friend Date operator-(const Date &date, const long val)
ost::Datetime::operator-=
Datetime & operator-=(const Datetime &datetime)
ost::Time::operator+
friend Time operator+(const int val, const Time &time)
ost::Time::Time
Time(time_t tm)
ost::Date::operator==
int operator==(const Date &date)
ost::Time::operator!
bool operator!() const
Definition: numbers.h:264
ost::Date::operator<=
int operator<=(const Date &date)
ost::Datetime::operator+=
Datetime & operator+=(const Datetime &datetime)
ost::Time::~Time
virtual ~Time()
ost::Date::toJulian
void toJulian(long year, long month, long day)
ost::Number::operator<=
int operator<=(const Number &num)
ost::ZNumber::ZNumber
ZNumber(char *buf, unsigned size)
strchar.h
Common and portable character string related functions.
ost::Number::operator+
friend long operator+(const long val, const Number &num)
ost::Time::getHour
int getHour(void) const
ost::Date::operator--
Date & operator--()
ost::Number::buffer
char * buffer
Definition: numbers.h:88
ost::Date::getYear
int getYear(void) const
ost::Time::operator-
friend Time operator-(const Time &time1, const Time &time2)
ost::Time::operator-=
Time & operator-=(const int val)
ost::Date::getDayOfWeek
unsigned getDayOfWeek(void) const
ost::Datetime::Datetime
Datetime()
missing.h
substitute functions which may be missing in target platform libc.
ost::Time::Time
Time(int hour, int minute, int second)
ost::Date::fromJulian
void fromJulian(char *buf) const
ost::Datetime::getDatetime
char * getDatetime(char *buffer) const
ost
Definition: address.h:64
ost::Date::operator+
friend Date operator+(const long val, const Date &date)
ost::Datetime::operator-=
Datetime & operator-=(const Time &time)
ost::Time::operator-
friend Time operator-(const int val, const Time &time)
ost::Number::operator==
int operator==(const Number &num)
ost::Time::toSeconds
void toSeconds(int hour, int minute, int second)
ost::Date::operator-=
Date & operator-=(const long val)
ost::Time::operator--
Time & operator--()
ost::ZNumber::setValue
void setValue(long value)
ost::Date::setDate
void setDate(const char *str, size_t size=0)
ost::Number::getBuffer
const char * getBuffer() const
Definition: numbers.h:100
ost::Datetime::operator!=
int operator!=(const Datetime &)
ost::Datetime::Datetime
Datetime(time_t tm)
ost::Datetime::Datetime
Datetime(int year, unsigned month, unsigned day, int hour, int minute, int second)
ost::Datetime::operator+=
Datetime & operator+=(const Time &time)
ost::Number::operator()
long operator()()
Definition: numbers.h:105
ost::Time::operator<
int operator<(const Time &time)
ost::Date
The Date class uses a julian date representation of the current year, month, and day.
Definition: numbers.h:149
ost::Date::operator!
bool operator!() const
Definition: numbers.h:202
ost::Date::operator+
friend Date operator+(const Date &date, const long val)
ost::Number::operator++
long operator++()
ost::Time::getTime
time_t getTime(void) const
ost::Time::operator<=
int operator<=(const Time &time)
ost::Date::update
virtual void update(void)
A method to use to "post" any changed values when shadowing a mixed object class.
ost::Number::operator+
friend long operator+(const Number &num, const long val)
ost::Time::operator+=
Time & operator+=(const int val)
ost::Number::operator-
friend long operator-(const long val, const Number &num)
ost::Datetime::isValid
bool isValid(void) const
ost::Time::setTime
void setTime(char *str, size_t size=0)
ost::Datetime::operator<=
int operator<=(const Datetime &)
ost::Number::Number
Number(char *buffer, unsigned size)
Create an instance of a number.
ost::Number
A number manipulation class.
Definition: numbers.h:86
ost::Date::operator!=
int operator!=(const Date &date)
ost::Time::Time
Time(char *str, size_t size=0)
ost::Time::operator>=
int operator>=(const Time &time)
ost::Time::getTime
tm * getTime(tm *buf) const
ost::Number::operator-=
long operator-=(const long value)
ost::Number::operator-
friend long operator-(const Number &num, long val)
ost::Time::operator+
friend Time operator+(const Time &time1, const Time &time2)
thread.h
Synchronization and threading services.
ost::Time::seconds
long seconds
Definition: numbers.h:218
ost::String
This is a generic and portable string class.
Definition: string.h:81
ost::Time::operator>
int operator>(const Time &time)
ost::Date::Date
Date(char *str, size_t size=0)
ost::Date::getValue
long getValue(void) const
ost::Date::Date
Date(time_t tm)
ost::Datetime::operator=
Datetime & operator=(const Datetime datetime)
ost::Number::operator>=
int operator>=(const Number &num)
ost::Time::operator()
String operator()() const
ost::Datetime::Datetime
Datetime(const char *str, size_t size=0)
ost::Date::operator()
String operator()() const