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 class __EXPORT Date : public ucommon::Date
53 {
54 protected:
55  inline void toJulian(long year, long month, long day)
56  {ucommon::Date::set(year, month, day);}
57 
58  inline void fromJulian(char *buf) const
59  {put(buf);}
60 
61 public:
62  inline Date(time_t value) : ucommon::Date(value) {};
63 
64  inline Date(struct tm *object) : ucommon::Date(object) {};
65 
66  inline Date(const char *ptr, size_t size = 0) : ucommon::Date(ptr, size) {};
67 
68  inline Date(int y, unsigned m, unsigned d) : ucommon::Date(y, m, d) {};
69 
70  inline Date(const Date& object) : ucommon::Date(object) {};
71 
72  inline Date() : ucommon::Date() {};
73 
74  inline int getYear(void) const
75  {return year();}
76 
77  inline unsigned getMonth(void) const
78  {return month();}
79 
80  inline unsigned getDay(void) const
81  {return day();}
82 
83  inline unsigned getDayOfWeek(void) const
84  {return dow();}
85 
86  inline long getJulian(void) const
87  {return julian;}
88 
89  inline const char *get(char *buffer) const
90  {return put(buffer);}
91 
92  inline time_t getTime(void) const
93  {return timeref();}
94 
95  inline bool isValid(void) const
96  {return is_valid();}
97 };
98 
99 class __EXPORT Time : public ucommon::Time
100 {
101 protected:
102  inline void toSeconds(int h, int m = 0, int s = 0)
103  {set(h, m, s);}
104 
105  inline void fromSeconds(char *buf) const
106  {put(buf);}
107 
108 public:
109  inline Time(time_t value) : ucommon::Time(value) {};
110 
111  inline Time(tm_t *object) : ucommon::Time(object) {};
112 
113  inline Time(const char *ptr, size_t size) : ucommon::Time(ptr, size) {};
114 
115  inline Time(int h, int m, int s) : ucommon::Time(h, m, s) {};
116 
117  inline Time() : ucommon::Time() {};
118 
119  inline int getHour(void) const
120  {return hour();}
121 
122  inline int getMinute(void) const
123  {return minute();}
124 
125  inline int getSecond(void) const
126  {return second();}
127 
128  inline const char *get(char *buffer) const
129  {return put(buffer);}
130 
131  inline bool isValid(void) const
132  {return is_valid();}
133 
134 };
135 
136 class __EXPORT DateTime : public ucommon::DateTime
137 {
138 public:
139  inline DateTime(time_t time) : ucommon::DateTime(time) {};
140 
141  inline DateTime(struct tm *dt) : ucommon::DateTime(dt) {};
142 
143 
144  inline DateTime(int year, unsigned month, unsigned day,
145  int hour = 0, int minute = 0, int second = 0) :
146  ucommon::DateTime(year, month, day, hour, minute, second) {};
147 
148  inline DateTime(const char *ptr, size_t size) :
149  ucommon::DateTime(ptr, size) {};
150 
151  inline DateTime(const DateTime& obj) : ucommon::DateTime(obj) {};
152 
153  inline DateTime() : ucommon::DateTime() {};
154 
155  inline int getYear(void) const
156  {return year();}
157 
158  inline unsigned getMonth(void) const
159  {return month();}
160 
161  inline unsigned getDay(void) const
162  {return day();}
163 
164  inline unsigned getDayOfWeek(void) const
165  {return dow();}
166 
167  inline long getJulian(void) const
168  {return julian;}
169 
170  inline const char *get(char *buffer) const
171  {return ucommon::DateTime::put(buffer);}
172 
173  inline time_t getTime(void) const
174  {return ucommon::DateTime::timeref();}
175 
176  inline bool isValid(void) const
177  {return ucommon::DateTime::is_valid();}
178 
179  inline int getHour(void) const
180  {return hour();}
181 
182  inline int getMinute(void) const
183  {return minute();}
184 
185  inline int getSecond(void) const
186  {return second();}
187 
188  inline static tm_t *glt(time_t *time = NULL)
189  {return ucommon::DateTime::local(time);}
190 };
191 
192 #endif
193 
Common namespace for all ucommon objects.
Definition: access.h:46
int hour(void) const
Get hours from midnight.
time_t timeref(void) const
Get a time_t for the julian date if in time_t epoch.
int second(void) const
Get seconds from current minute.
A number class that manipulates a string buffer that is also a date.
Definition: datetime.h:957
The Time class uses a integer representation of the current time.
Definition: datetime.h:345
const char * put(char *buffer) const
Get a ISO string representation of the date (yyyy-mm-dd).
Time()
Create a time from current time.
static tm_t * local(time_t *time=((void *) 0))
Fetch an instance of time converted to local time.
void set(void)
Set (update) the date with current date.
int minute(void) const
Get minutes from current hour.
bool is_valid(void) const
Check if time object had valid value.
unsigned month(void) const
Get the month of the date (1-12).
Common C++ generic string class.
The Date class uses a julian date representation of the current year, month, and day.
Definition: datetime.h:78
unsigned day(void) const
Get the day of the month of the date.
A DateTime string class.
Definition: datetime.h:858
struct tm tm_t
Convenience type for struct tm.
Definition: datetime.h:61
bool is_valid(void) const
Test if object is valid.
DateTime()
Construct a new date and time object with current date and time.
unsigned dow(void) const
Get the day of the week (0-7).
const char * put(char *buffer) const
Get a ISO formatted date and time string for current object.
void set(void)
Set (update) the time with current time.
const char * put(char *buffer) const
Get a hh:mm:ss formatted string for current time.
Date()
Construct a new julian date with today's date.
int year(void) const
Get the year of the date.
The Datetime class uses a julian date representation of the current year, month, and day and a intege...
Definition: datetime.h:608
bool is_valid(void) const
Check if date is valid.