ucommon
numbers.h
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 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 Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
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 
38 #ifndef COMMONCPP_NUMBERS_H_
39 #define COMMONCPP_NUMBERS_H_
40 
41 #ifndef COMMONCPP_CONFIG_H_
42 #include <commoncpp/config.h>
43 #endif
44 
45 #ifndef COMMONCPP_STRING_H_
46 #include <commoncpp/string.h>
47 #endif
48 
51 
52 namespace ost {
53 
54 class __EXPORT Date : public ucommon::Date
55 {
56 protected:
57  inline void toJulian(long year, long month, long day)
58  {ucommon::Date::set(year, month, day);}
59 
60  inline void fromJulian(char *buf) const
61  {put(buf);}
62 
63 public:
64  inline Date(time_t value) : ucommon::Date(value) {}
65 
66  inline Date(struct tm *object) : ucommon::Date(object) {}
67 
68  inline Date(const char *ptr, size_t size = 0) : ucommon::Date(ptr, size) {}
69 
70  inline Date(int y, unsigned m, unsigned d) : ucommon::Date(y, m, d) {}
71 
72  inline Date(const Date& object) : ucommon::Date(object) {}
73 
74  inline Date() : ucommon::Date() {}
75 
76  inline int getYear(void) const
77  {return year();}
78 
79  inline unsigned getMonth(void) const
80  {return month();}
81 
82  inline unsigned getDay(void) const
83  {return day();}
84 
85  inline unsigned getDayOfWeek(void) const
86  {return dow();}
87 
88  inline long getJulian(void) const
89  {return julian;}
90 
91  inline const char *get(char *buffer) const
92  {return put(buffer);}
93 
94  inline time_t getTime(void) const
95  {return timeref();}
96 
97  inline bool isValid(void) const
98  {return is_valid();}
99 };
100 
101 class __EXPORT Time : public ucommon::Time
102 {
103 protected:
104  inline void toSeconds(int h, int m = 0, int s = 0)
105  {set(h, m, s);}
106 
107  inline void fromSeconds(char *buf) const
108  {put(buf);}
109 
110 public:
111  inline Time(time_t value) : ucommon::Time(value) {}
112 
113  inline Time(tm_t *object) : ucommon::Time(object) {}
114 
115  inline Time(const char *ptr, size_t size) : ucommon::Time(ptr, size) {}
116 
117  inline Time(int h, int m, int s) : ucommon::Time(h, m, s) {}
118 
119  inline Time() : ucommon::Time() {}
120 
121  inline int getHour(void) const
122  {return hour();}
123 
124  inline int getMinute(void) const
125  {return minute();}
126 
127  inline int getSecond(void) const
128  {return second();}
129 
130  inline const char *get(char *buffer) const
131  {return put(buffer);}
132 
133  inline bool isValid(void) const
134  {return is_valid();}
135 
136 };
137 
138 class __EXPORT DateTime : public ucommon::DateTime
139 {
140 public:
141  inline DateTime(time_t time) : ucommon::DateTime(time) {}
142 
143  inline DateTime(struct tm *dt) : ucommon::DateTime(dt) {}
144 
145 
146  inline DateTime(int year, unsigned month, unsigned day,
147  int hour = 0, int minute = 0, int second = 0) :
148  ucommon::DateTime(year, month, day, hour, minute, second) {}
149 
150  inline DateTime(const char *ptr, size_t size) :
151  ucommon::DateTime(ptr, size) {}
152 
153  inline DateTime(const DateTime& obj) : ucommon::DateTime(obj) {}
154 
155  inline DateTime() : ucommon::DateTime() {}
156 
157  inline int getYear(void) const
158  {return year();}
159 
160  inline unsigned getMonth(void) const
161  {return month();}
162 
163  inline unsigned getDay(void) const
164  {return day();}
165 
166  inline unsigned getDayOfWeek(void) const
167  {return dow();}
168 
169  inline long getJulian(void) const
170  {return julian;}
171 
172  inline const char *get(char *buffer) const
173  {return ucommon::DateTime::put(buffer);}
174 
175  inline time_t getTime(void) const
176  {return ucommon::DateTime::timeref();}
177 
178  inline bool isValid(void) const
179  {return ucommon::DateTime::is_valid();}
180 
181  inline int getHour(void) const
182  {return hour();}
183 
184  inline int getMinute(void) const
185  {return minute();}
186 
187  inline int getSecond(void) const
188  {return second();}
189 
190  inline static tm_t *glt(time_t *time = NULL)
191  {return ucommon::DateTime::local(time);}
192 };
193 
194 } // namespace ost
195 
196 #endif
197 
time_t timeref(void) const
Get a time_t for the julian date if in time_t epoch.
A number class that manipulates a string buffer that is also a date.
Definition: datetime.h:957
struct tm tm_t
Convenience type for struct tm.
Definition: datetime.h:61
Common namespace for all ucommon objects.
Definition: access.h:46
Definition: address.h:58
void set(void)
Set (update) the date with current date.
The Date class uses a julian date representation of the current year, month, and day.
Definition: datetime.h:78
A DateTime string class.
Definition: datetime.h:858
bool is_valid(void) const
Test if object is valid.
Common C++ generic string class.
const char * put(char *buffer) const
Get a ISO formatted date and time string for current object.
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition: datetime.h:608
static tm_t * local(const time_t *time=((void *) 0))
Fetch an instance of time converted to local time.
The Time class uses a integer representation of the current time.
Definition: datetime.h:345