More gracefully handle type mismatch errors when doing playlist things (just ignore...
[ardour.git] / libs / glibmm2 / glib / src / date.ccg
1 // -*- c++ -*-
2 /* $Id: date.ccg,v 1.7 2006/07/16 13:54:02 jjongsma Exp $ */
3
4 /* Copyright (C) 2002 The gtkmm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <glib/gmem.h>
22 #include <glib.h>
23
24 #include <ctime>
25 #include <algorithm>
26
27 #include <glibmm/convert.h>
28 #include <glibmm/utility.h>
29
30 #include <glibmmconfig.h>
31 GLIBMM_USING_STD(max)
32
33 namespace Glib
34 {
35
36 Date::Date()
37 {
38   g_date_clear(&gobject_, 1);
39 }
40
41 Date::Date(Date::Day day, Date::Month month, Date::Year year)
42 {
43   g_date_clear(&gobject_, 1);
44   g_date_set_dmy(&gobject_, day, (GDateMonth) month, year);
45 }
46
47 Date::Date(guint32 julian_day)
48 {
49   g_date_clear(&gobject_, 1);
50   g_date_set_julian(&gobject_, julian_day);
51 }
52
53 Date::Date(const GDate& castitem)
54 :
55   gobject_ (castitem)
56 {}
57
58 void Date::clear()
59 {
60   g_date_clear(&gobject_, 1);
61 }
62
63 void Date::set_parse(const Glib::ustring& str)
64 {
65   g_date_set_parse(&gobject_, str.c_str());
66 }
67
68
69 _DEPRECATE_IFDEF_START
70
71 //Avoid a build problem in the case that time_t is equivalent to guint32 (GTime is also guint32)
72 //That would make the set_time() method overload impossible.
73 #ifdef GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
74 void Date::set_time(GTime time)
75 {
76   //This method, and the C function that it wraps, are deprecated.
77   g_date_set_time(&gobject_, time);
78 }
79 #endif //GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
80
81 _DEPRECATE_IFDEF_END
82
83
84 void Date::set_time(time_t timet)
85 {
86   g_date_set_time_t(&gobject_, timet);
87 }
88
89 void Date::set_time_current()
90 {
91   //As suggested in the C documentation:
92   g_date_set_time_t(&gobject_, time(NULL));
93 }
94
95 void Date::set_time(const GTimeVal& timeval)
96 {
97   g_date_set_time_val(&gobject_, const_cast<GTimeVal*>(&timeval));
98 }
99
100 void Date::set_month(Date::Month month)
101 {
102   g_date_set_month(&gobject_, (GDateMonth) month);
103 }
104
105 void Date::set_day(Date::Day day)
106 {
107   g_date_set_day(&gobject_, day);
108 }
109
110 void Date::set_year(Date::Year year)
111 {
112   g_date_set_year(&gobject_, year);
113 }
114
115 void Date::set_dmy(Date::Day day, Date::Month month, Date::Year year)
116 {
117   g_date_set_dmy(&gobject_, day, (GDateMonth) month, year);
118 }
119
120 void Date::set_julian(guint32 julian_day)
121 {
122   g_date_set_julian(&gobject_, julian_day);
123 }
124
125 Date& Date::add_days(int n_days)
126 {
127   if(n_days >= 0)
128     g_date_add_days(&gobject_, n_days);
129   else
130     g_date_subtract_days(&gobject_, -n_days);
131   return *this;
132 }
133
134 Date& Date::subtract_days(int n_days)
135 {
136   if(n_days >= 0)
137     g_date_subtract_days(&gobject_, n_days);
138   else
139     g_date_add_days(&gobject_, -n_days);
140   return *this;
141 }
142
143 Date& Date::add_months(int n_months)
144 {
145   if(n_months >= 0)
146     g_date_add_months(&gobject_, n_months);
147   else
148     g_date_subtract_months(&gobject_, -n_months);
149   return *this;
150 }
151
152 Date& Date::subtract_months(int n_months)
153 {
154   if(n_months >= 0)
155     g_date_subtract_months(&gobject_, n_months);
156   else
157     g_date_add_months(&gobject_, -n_months);
158   return *this;
159 }
160
161 Date& Date::add_years(int n_years)
162 {
163   if(n_years >= 0)
164     g_date_add_years(&gobject_, n_years);
165   else
166     g_date_subtract_years(&gobject_, -n_years);
167   return *this;
168 }
169
170 Date& Date::subtract_years(int n_years)
171 {
172   if(n_years >= 0)
173     g_date_subtract_years(&gobject_, n_years);
174   else
175     g_date_add_years(&gobject_, -n_years);
176   return *this;
177 }
178
179 int Date::days_between(const Date& rhs) const
180 {
181   return g_date_days_between(&gobject_, &rhs.gobject_);
182 }
183
184 int Date::compare(const Date& rhs) const
185 {
186   return g_date_compare(&gobject_, &rhs.gobject_);
187 }
188
189 Date& Date::clamp(const Date& min_date, const Date& max_date)
190 {
191   g_date_clamp(&gobject_, &min_date.gobject_, &max_date.gobject_);
192   return *this;
193 }
194
195 Date& Date::clamp_min(const Date& min_date)
196 {
197   g_date_clamp(&gobject_, &min_date.gobject_, 0 /* see the C docs */);
198   return *this;
199 }
200
201 Date& Date::clamp_max(const Date& max_date)
202 {
203   g_date_clamp(&gobject_, 0 /* see the C docs */, &max_date.gobject_);
204   return *this;
205 }
206
207 void Date::order(Date& other)
208 {
209   g_date_order(&gobject_, &other.gobject_);
210 }
211
212 Date::Weekday Date::get_weekday() const
213 {
214   return (Date::Weekday) g_date_get_weekday(&gobject_);
215 }
216
217 Date::Month Date::get_month() const
218 {
219   return (Date::Month) g_date_get_month(&gobject_);
220 }
221
222 Date::Year Date::get_year() const
223 {
224   return g_date_get_year(&gobject_);
225 }
226
227 Date::Day Date::get_day() const
228 {
229   return g_date_get_day(&gobject_);
230 }
231
232 guint32 Date::get_julian() const
233 {
234   return g_date_get_julian(&gobject_);
235 }
236
237 unsigned int Date::get_day_of_year() const
238 {
239   return g_date_get_day_of_year(&gobject_);
240 }
241
242 unsigned int Date::get_monday_week_of_year() const
243 {
244   return g_date_get_monday_week_of_year(&gobject_);
245 }
246
247 unsigned int Date::get_sunday_week_of_year() const
248 {
249   return g_date_get_sunday_week_of_year(&gobject_);
250 }
251
252 bool Date::is_first_of_month() const
253 {
254   return g_date_is_first_of_month(&gobject_);
255 }
256
257 bool Date::is_last_of_month() const
258 {
259   return g_date_is_last_of_month(&gobject_);
260 }
261
262 //static
263 guint8 Date::get_days_in_month(Date::Month month, Date::Year year)
264 {
265   return g_date_get_days_in_month((GDateMonth) month, year);
266 }
267
268 //static
269 guint8 Date::get_monday_weeks_in_year(Date::Year year)
270 {
271   return g_date_get_monday_weeks_in_year(year);
272 }
273
274 //static
275 guint8 Date::get_sunday_weeks_in_year(Date::Year year)
276 {
277   return g_date_get_sunday_weeks_in_year(year);
278 }
279
280 //static
281 bool Date::is_leap_year(Date::Year year)
282 {
283   return g_date_is_leap_year(year);
284 }
285
286 Glib::ustring Date::format_string(const Glib::ustring& format) const
287 {
288   struct tm tm_data;
289   g_date_to_struct_tm(&gobject_, &tm_data);
290
291   #ifdef GLIBMM_EXCEPTIONS_ENABLED
292   const std::string locale_format = locale_from_utf8(format);
293   #else
294   std::auto_ptr<Glib::Error> error; //TODO: Check it?
295   const std::string locale_format = locale_from_utf8(format, error);
296   #endif //GLIBMM_EXCEPTIONS_ENABLED
297
298   gsize bufsize = std::max<gsize>(2 * locale_format.size(), 128);
299
300   do
301   {
302     const ScopedPtr<char> buf (static_cast<char*>(g_malloc(bufsize)));
303
304     // Set the first byte to something other than '\0', to be able to
305     // recognize whether strftime actually failed or just returned "".
306     buf.get()[0] = '\1';
307     const gsize len = strftime(buf.get(), bufsize, locale_format.c_str(), &tm_data);
308
309     if(len != 0 || buf.get()[0] == '\0')
310     {
311       g_assert(len < bufsize);
312       #ifdef GLIBMM_EXCEPTIONS_ENABLED
313       return locale_to_utf8(std::string(buf.get(), len));
314       #else
315       std::auto_ptr<Glib::Error> error; //TODO: Check it?
316       return locale_to_utf8(std::string(buf.get(), len), error);
317       #endif //GLIBMM_EXCEPTIONS_ENABLED
318     }
319   }
320   while((bufsize *= 2) <= 65536);
321
322   // This error is quite unlikely (unless strftime is buggy).
323   g_warning("Glib::Date::format_string(): maximum size of strftime buffer exceeded, giving up");
324
325   return Glib::ustring();
326 }
327
328 void Date::to_struct_tm(struct tm& dest) const
329 {
330   g_date_to_struct_tm(&gobject_, &dest);
331 }
332
333 bool Date::valid() const
334 {
335   return g_date_valid(&gobject_);
336 }
337
338 //static
339 bool Date::valid_day(Date::Day day)
340 {
341   return g_date_valid_day(day);
342 }
343
344 //static
345 bool Date::valid_month(Date::Month month)
346 {
347   return g_date_valid_month((GDateMonth) month);
348 }
349
350 //static
351 bool Date::valid_year(Date::Year year)
352 {
353   return g_date_valid_year(year);
354 }
355
356 //static
357 bool Date::valid_weekday(Date::Weekday weekday)
358 {
359   return g_date_valid_weekday((GDateWeekday) weekday);
360 }
361
362 //static
363 bool Date::valid_julian(guint32 julian_day)
364 {
365   return g_date_valid_julian(julian_day);
366 }
367
368 //static
369 bool Date::valid_dmy(Date::Day day, Date::Month month, Date::Year year)
370 {
371   return g_date_valid_dmy(day, (GDateMonth) month, year);
372 }
373
374 } // namespace Glib
375