1 // Generated by gmmproc 2.64.2 -- DO NOT MODIFY!
2 
3 
4 #include <glibmm.h>
5 
6 #include <glibmm/date.h>
7 #include <glibmm/private/date_p.h>
8 
9 
10 /* Copyright (C) 2002 The gtkmm Development Team
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 //#include <glib/gtestutils.h> //For g_assert() in glib >= 2.15.0
27 //#include <glib/gmessages.h> //For g_assert() in glib < 2.15.0
28 #include <glib.h> //For g_assert() in all versions of glib.
29 
30 #include <algorithm>
31 
32 #include <glibmm/convert.h>
33 #include <glibmm/utility.h>
34 
35 namespace Glib
36 {
37 
Date()38 Date::Date()
39 {
40   g_date_clear(&gobject_, 1);
41 }
42 
Date(Date::Day day,Date::Month month,Date::Year year)43 Date::Date(Date::Day day, Date::Month month, Date::Year year)
44 {
45   g_date_clear(&gobject_, 1);
46   g_date_set_dmy(&gobject_, day, (GDateMonth)month, year);
47 }
48 
Date(guint32 julian_day)49 Date::Date(guint32 julian_day)
50 {
51   g_date_clear(&gobject_, 1);
52   g_date_set_julian(&gobject_, julian_day);
53 }
54 
Date(const GDate & castitem)55 Date::Date(const GDate& castitem) : gobject_(castitem)
56 {
57 }
58 
Date(const Date & other)59 Date::Date(const Date& other)
60 {
61   g_date_clear(&gobject_, 1);
62   if (g_date_valid(&other.gobject_))
63     g_date_set_julian(&gobject_, other.get_julian());
64 }
65 
66 Date&
operator =(const Date & other)67 Date::operator=(const Date& other)
68 {
69   if (&other != this && g_date_valid(&other.gobject_))
70     g_date_set_julian(&gobject_, other.get_julian());
71 
72   return *this;
73 }
74 
75 void
clear()76 Date::clear()
77 {
78   g_date_clear(&gobject_, 1);
79 }
80 
81 void
set_parse(const Glib::ustring & str)82 Date::set_parse(const Glib::ustring& str)
83 {
84   g_date_set_parse(&gobject_, str.c_str());
85 }
86 
87 #ifndef GLIBMM_DISABLE_DEPRECATED
88 
89 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
90 // Avoid a build problem in the case that std::time_t is equivalent to gint32 (GTime is also gint32)
91 // That would make the set_time() method overload impossible.
92 #ifdef GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
93 void
set_time(GTime time)94 Date::set_time(GTime time)
95 {
96   // This method, and the C function g_date_set_time() that it wraps, are deprecated.
97   //(::time_t is used here instead of std::time_t, since the C function is declared
98   // with ::time_t. It's not important. The C++ standard requires that ::time_t
99   // and std::time_t shall be identical when both are defined.)
100   g_date_set_time_t(&gobject_, static_cast<time_t>(time));
101 }
102 #endif // GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
103 G_GNUC_END_IGNORE_DEPRECATIONS
104 #endif // GLIBMM_DISABLE_DEPRECATED
105 
106 
107 void
set_time(std::time_t timet)108 Date::set_time(std::time_t timet)
109 {
110   g_date_set_time_t(&gobject_, timet);
111 }
112 
113 void
set_time_current()114 Date::set_time_current()
115 {
116   // As suggested in the C documentation:
117   g_date_set_time_t(&gobject_, time(nullptr));
118 }
119 
120 #ifndef GLIBMM_DISABLE_DEPRECATED
121 
122 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
123 void
set_time(const GTimeVal & timeval)124 Date::set_time(const GTimeVal& timeval)
125 {
126   g_date_set_time_val(&gobject_, const_cast<GTimeVal*>(&timeval));
127 }
128 G_GNUC_END_IGNORE_DEPRECATIONS
129 #endif // GLIBMM_DISABLE_DEPRECATED
130 
131 
132 void
set_month(Date::Month month)133 Date::set_month(Date::Month month)
134 {
135   g_date_set_month(&gobject_, (GDateMonth)month);
136 }
137 
138 void
set_day(Date::Day day)139 Date::set_day(Date::Day day)
140 {
141   g_date_set_day(&gobject_, day);
142 }
143 
144 void
set_year(Date::Year year)145 Date::set_year(Date::Year year)
146 {
147   g_date_set_year(&gobject_, year);
148 }
149 
150 void
set_dmy(Date::Day day,Date::Month month,Date::Year year)151 Date::set_dmy(Date::Day day, Date::Month month, Date::Year year)
152 {
153   g_date_set_dmy(&gobject_, day, (GDateMonth)month, year);
154 }
155 
156 void
set_julian(guint32 julian_day)157 Date::set_julian(guint32 julian_day)
158 {
159   g_date_set_julian(&gobject_, julian_day);
160 }
161 
162 Date&
add_days(int n_days)163 Date::add_days(int n_days)
164 {
165   if (n_days >= 0)
166     g_date_add_days(&gobject_, n_days);
167   else
168     g_date_subtract_days(&gobject_, -n_days);
169   return *this;
170 }
171 
172 Date&
subtract_days(int n_days)173 Date::subtract_days(int n_days)
174 {
175   if (n_days >= 0)
176     g_date_subtract_days(&gobject_, n_days);
177   else
178     g_date_add_days(&gobject_, -n_days);
179   return *this;
180 }
181 
182 Date&
add_months(int n_months)183 Date::add_months(int n_months)
184 {
185   if (n_months >= 0)
186     g_date_add_months(&gobject_, n_months);
187   else
188     g_date_subtract_months(&gobject_, -n_months);
189   return *this;
190 }
191 
192 Date&
subtract_months(int n_months)193 Date::subtract_months(int n_months)
194 {
195   if (n_months >= 0)
196     g_date_subtract_months(&gobject_, n_months);
197   else
198     g_date_add_months(&gobject_, -n_months);
199   return *this;
200 }
201 
202 Date&
add_years(int n_years)203 Date::add_years(int n_years)
204 {
205   if (n_years >= 0)
206     g_date_add_years(&gobject_, n_years);
207   else
208     g_date_subtract_years(&gobject_, -n_years);
209   return *this;
210 }
211 
212 Date&
subtract_years(int n_years)213 Date::subtract_years(int n_years)
214 {
215   if (n_years >= 0)
216     g_date_subtract_years(&gobject_, n_years);
217   else
218     g_date_add_years(&gobject_, -n_years);
219   return *this;
220 }
221 
222 int
days_between(const Date & rhs) const223 Date::days_between(const Date& rhs) const
224 {
225   return g_date_days_between(&gobject_, &rhs.gobject_);
226 }
227 
228 int
compare(const Date & rhs) const229 Date::compare(const Date& rhs) const
230 {
231   return g_date_compare(&gobject_, &rhs.gobject_);
232 }
233 
234 Date&
clamp(const Date & min_date,const Date & max_date)235 Date::clamp(const Date& min_date, const Date& max_date)
236 {
237   g_date_clamp(&gobject_, &min_date.gobject_, &max_date.gobject_);
238   return *this;
239 }
240 
241 Date&
clamp_min(const Date & min_date)242 Date::clamp_min(const Date& min_date)
243 {
244   g_date_clamp(&gobject_, &min_date.gobject_, nullptr /* see the C docs */);
245   return *this;
246 }
247 
248 Date&
clamp_max(const Date & max_date)249 Date::clamp_max(const Date& max_date)
250 {
251   g_date_clamp(&gobject_, nullptr /* see the C docs */, &max_date.gobject_);
252   return *this;
253 }
254 
255 void
order(Date & other)256 Date::order(Date& other)
257 {
258   g_date_order(&gobject_, &other.gobject_);
259 }
260 
261 Date::Weekday
get_weekday() const262 Date::get_weekday() const
263 {
264   return (Date::Weekday)g_date_get_weekday(&gobject_);
265 }
266 
267 Date::Month
get_month() const268 Date::get_month() const
269 {
270   return (Date::Month)g_date_get_month(&gobject_);
271 }
272 
273 Date::Year
get_year() const274 Date::get_year() const
275 {
276   return g_date_get_year(&gobject_);
277 }
278 
279 Date::Day
get_day() const280 Date::get_day() const
281 {
282   return g_date_get_day(&gobject_);
283 }
284 
285 guint32
get_julian() const286 Date::get_julian() const
287 {
288   return g_date_get_julian(&gobject_);
289 }
290 
291 unsigned int
get_day_of_year() const292 Date::get_day_of_year() const
293 {
294   return g_date_get_day_of_year(&gobject_);
295 }
296 
297 unsigned int
get_monday_week_of_year() const298 Date::get_monday_week_of_year() const
299 {
300   return g_date_get_monday_week_of_year(&gobject_);
301 }
302 
303 unsigned int
get_sunday_week_of_year() const304 Date::get_sunday_week_of_year() const
305 {
306   return g_date_get_sunday_week_of_year(&gobject_);
307 }
308 
309 unsigned int
get_iso8601_week_of_year() const310 Date::get_iso8601_week_of_year() const
311 {
312   return g_date_get_iso8601_week_of_year(&gobject_);
313 }
314 
315 bool
is_first_of_month() const316 Date::is_first_of_month() const
317 {
318   return g_date_is_first_of_month(&gobject_);
319 }
320 
321 bool
is_last_of_month() const322 Date::is_last_of_month() const
323 {
324   return g_date_is_last_of_month(&gobject_);
325 }
326 
327 // static
328 guint8
get_days_in_month(Date::Month month,Date::Year year)329 Date::get_days_in_month(Date::Month month, Date::Year year)
330 {
331   return g_date_get_days_in_month((GDateMonth)month, year);
332 }
333 
334 // static
335 guint8
get_monday_weeks_in_year(Date::Year year)336 Date::get_monday_weeks_in_year(Date::Year year)
337 {
338   return g_date_get_monday_weeks_in_year(year);
339 }
340 
341 // static
342 guint8
get_sunday_weeks_in_year(Date::Year year)343 Date::get_sunday_weeks_in_year(Date::Year year)
344 {
345   return g_date_get_sunday_weeks_in_year(year);
346 }
347 
348 // static
349 bool
is_leap_year(Date::Year year)350 Date::is_leap_year(Date::Year year)
351 {
352   return g_date_is_leap_year(year);
353 }
354 
355 Glib::ustring
format_string(const Glib::ustring & format) const356 Date::format_string(const Glib::ustring& format) const
357 {
358   struct tm tm_data;
359   g_date_to_struct_tm(&gobject_, &tm_data);
360 
361   const auto locale_format = locale_from_utf8(format);
362 
363   gsize bufsize = std::max<gsize>(2 * locale_format.size(), 128);
364 
365   do
366   {
367     const auto buf = make_unique_ptr_gfree(static_cast<char*>(g_malloc(bufsize)));
368 
369     // Set the first byte to something other than '\0', to be able to
370     // recognize whether strftime actually failed or just returned "".
371     buf.get()[0] = '\1';
372     const auto len = strftime(buf.get(), bufsize, locale_format.c_str(), &tm_data);
373 
374     if (len != 0 || buf.get()[0] == '\0')
375     {
376       g_assert(len < bufsize);
377       return locale_to_utf8(std::string(buf.get(), len));
378     }
379   } while ((bufsize *= 2) <= 65536);
380 
381   // This error is quite unlikely (unless strftime is buggy).
382   g_warning("Glib::Date::format_string(): maximum size of strftime buffer exceeded, giving up");
383 
384   return Glib::ustring();
385 }
386 
387 void
to_struct_tm(struct tm & dest) const388 Date::to_struct_tm(struct tm& dest) const
389 {
390   g_date_to_struct_tm(&gobject_, &dest);
391 }
392 
393 bool
valid() const394 Date::valid() const
395 {
396   return g_date_valid(&gobject_);
397 }
398 
399 // static
400 bool
valid_day(Date::Day day)401 Date::valid_day(Date::Day day)
402 {
403   return g_date_valid_day(day);
404 }
405 
406 // static
407 bool
valid_month(Date::Month month)408 Date::valid_month(Date::Month month)
409 {
410   return g_date_valid_month((GDateMonth)month);
411 }
412 
413 // static
414 bool
valid_year(Date::Year year)415 Date::valid_year(Date::Year year)
416 {
417   return g_date_valid_year(year);
418 }
419 
420 // static
421 bool
valid_weekday(Date::Weekday weekday)422 Date::valid_weekday(Date::Weekday weekday)
423 {
424   return g_date_valid_weekday((GDateWeekday)weekday);
425 }
426 
427 // static
428 bool
valid_julian(guint32 julian_day)429 Date::valid_julian(guint32 julian_day)
430 {
431   return g_date_valid_julian(julian_day);
432 }
433 
434 // static
435 bool
valid_dmy(Date::Day day,Date::Month month,Date::Year year)436 Date::valid_dmy(Date::Day day, Date::Month month, Date::Year year)
437 {
438   return g_date_valid_dmy(day, (GDateMonth)month, year);
439 }
440 
441 } // namespace Glib
442 
443 namespace
444 {
445 } // anonymous namespace
446 
447 
448