UCommon
numbers.h
1 // Copyright (C) 1999-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 // Copyright (C) 2015 Cherokees of Idaho.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
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 #ifndef COMMONCPP_NUMBERS_H_
40 #define COMMONCPP_NUMBERS_H_
41 
42 #ifndef COMMONCPP_CONFIG_H_
43 #include <commoncpp/config.h>
44 #endif
45 
46 #ifndef COMMONCPP_STRING_H_
47 #include <commoncpp/string.h>
48 #endif
49 
52 
53 namespace ost {
54 
55 class __EXPORT Date : public ucommon::Date
56 {
57 protected:
58  inline void toJulian(long year, long month, long day)
59  {ucommon::Date::set(year, month, day);}
60 
61  inline void fromJulian(char *buf) const
62  {put(buf);}
63 
64 public:
65  inline Date(time_t value) : ucommon::Date(value) {}
66 
67  inline Date(struct tm *object) : ucommon::Date(object) {}
68 
69  inline Date(const char *ptr, size_t size = 0) : ucommon::Date(ptr, size) {}
70 
71  inline Date(int y, unsigned m, unsigned d) : ucommon::Date(y, m, d) {}
72 
73  inline Date(const Date& object) : ucommon::Date(object) {}
74 
75  inline Date() : ucommon::Date() {}
76 
77  inline int getYear(void) const
78  {return year();}
79 
80  inline unsigned getMonth(void) const
81  {return month();}
82 
83  inline unsigned getDay(void) const
84  {return day();}
85 
86  inline unsigned getDayOfWeek(void) const
87  {return dow();}
88 
89  inline long getJulian(void) const
90  {return julian;}
91 
92  inline const char *get(char *buffer) const
93  {return put(buffer);}
94 
95  inline time_t getTime(void) const
96  {return timeref();}
97 
98  inline bool isValid(void) const
99  {return is_valid();}
100 };
101 
102 class __EXPORT Time : public ucommon::Time
103 {
104 protected:
105  inline void toSeconds(int h, int m = 0, int s = 0)
106  {set(h, m, s);}
107 
108  inline void fromSeconds(char *buf) const
109  {put(buf);}
110 
111 public:
112  inline Time(time_t value) : ucommon::Time(value) {}
113 
114  inline Time(tm_t *object) : ucommon::Time(object) {}
115 
116  inline Time(const char *ptr, size_t size) : ucommon::Time(ptr, size) {}
117 
118  inline Time(int h, int m, int s) : ucommon::Time(h, m, s) {}
119 
120  inline Time() : ucommon::Time() {}
121 
122  inline int getHour(void) const
123  {return hour();}
124 
125  inline int getMinute(void) const
126  {return minute();}
127 
128  inline int getSecond(void) const
129  {return second();}
130 
131  inline const char *get(char *buffer) const
132  {return put(buffer);}
133 
134  inline bool isValid(void) const
135  {return is_valid();}
136 
137 };
138 
139 class __EXPORT DateTime : public ucommon::DateTime
140 {
141 public:
142  inline DateTime(time_t time) : ucommon::DateTime(time) {}
143 
144  inline DateTime(struct tm *dt) : ucommon::DateTime(dt) {}
145 
146 
147  inline DateTime(int year, unsigned month, unsigned day,
148  int hour = 0, int minute = 0, int second = 0) :
149  ucommon::DateTime(year, month, day, hour, minute, second) {}
150 
151  inline DateTime(const char *ptr, size_t size) :
152  ucommon::DateTime(ptr, size) {}
153 
154  inline DateTime(const DateTime& obj) : ucommon::DateTime(obj) {}
155 
156  inline DateTime() : ucommon::DateTime() {}
157 
158  inline int getYear(void) const
159  {return year();}
160 
161  inline unsigned getMonth(void) const
162  {return month();}
163 
164  inline unsigned getDay(void) const
165  {return day();}
166 
167  inline unsigned getDayOfWeek(void) const
168  {return dow();}
169 
170  inline long getJulian(void) const
171  {return julian;}
172 
173  inline const char *get(char *buffer) const
174  {return ucommon::DateTime::put(buffer);}
175 
176  inline time_t getTime(void) const
177  {return ucommon::DateTime::timeref();}
178 
179  inline bool isValid(void) const
180  {return ucommon::DateTime::is_valid();}
181 
182  inline int getHour(void) const
183  {return hour();}
184 
185  inline int getMinute(void) const
186  {return minute();}
187 
188  inline int getSecond(void) const
189  {return second();}
190 
191  inline static tm_t *glt(time_t *time = NULL)
192  {return ucommon::DateTime::local(time);}
193 };
194 
195 } // namespace ost
196 
197 #endif
198 
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:971
Definition: address.h:59
The Time class uses a integer representation of the current time.
Definition: datetime.h:351
void set(void)
Set (update) the date with current date.
struct tm tm_t
Convenience type for struct tm.
Definition: datetime.h:62
The Date class uses a julian date representation of the current year, month, and day.
Definition: datetime.h:79
A DateTime string class.
Definition: datetime.h:869
bool is_valid(void) const
Test if object is valid.
Common namespace for all ucommon objects.
Definition: access.h:47
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:618
static tm_t * local(const time_t *time=((void *) 0))
Fetch an instance of time converted to local time.