2 /* $Id: date.ccg,v 1.7 2006/07/16 13:54:02 jjongsma Exp $ */
4 /* Copyright (C) 2002 The gtkmm Development Team
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.
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.
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.
21 #include <glib/gmem.h>
27 #include <glibmm/convert.h>
28 #include <glibmm/utility.h>
30 #include <glibmmconfig.h>
38 g_date_clear(&gobject_, 1);
41 Date::Date(Date::Day day, Date::Month month, Date::Year year)
43 g_date_clear(&gobject_, 1);
44 g_date_set_dmy(&gobject_, day, (GDateMonth) month, year);
47 Date::Date(guint32 julian_day)
49 g_date_clear(&gobject_, 1);
50 g_date_set_julian(&gobject_, julian_day);
53 Date::Date(const GDate& castitem)
60 g_date_clear(&gobject_, 1);
63 void Date::set_parse(const Glib::ustring& str)
65 g_date_set_parse(&gobject_, str.c_str());
69 _DEPRECATE_IFDEF_START
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)
76 //This method, and the C function that it wraps, are deprecated.
77 g_date_set_time(&gobject_, time);
79 #endif //GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
84 void Date::set_time(time_t timet)
86 g_date_set_time_t(&gobject_, timet);
89 void Date::set_time_current()
91 //As suggested in the C documentation:
92 g_date_set_time_t(&gobject_, time(NULL));
95 void Date::set_time(const GTimeVal& timeval)
97 g_date_set_time_val(&gobject_, const_cast<GTimeVal*>(&timeval));
100 void Date::set_month(Date::Month month)
102 g_date_set_month(&gobject_, (GDateMonth) month);
105 void Date::set_day(Date::Day day)
107 g_date_set_day(&gobject_, day);
110 void Date::set_year(Date::Year year)
112 g_date_set_year(&gobject_, year);
115 void Date::set_dmy(Date::Day day, Date::Month month, Date::Year year)
117 g_date_set_dmy(&gobject_, day, (GDateMonth) month, year);
120 void Date::set_julian(guint32 julian_day)
122 g_date_set_julian(&gobject_, julian_day);
125 Date& Date::add_days(int n_days)
128 g_date_add_days(&gobject_, n_days);
130 g_date_subtract_days(&gobject_, -n_days);
134 Date& Date::subtract_days(int n_days)
137 g_date_subtract_days(&gobject_, n_days);
139 g_date_add_days(&gobject_, -n_days);
143 Date& Date::add_months(int n_months)
146 g_date_add_months(&gobject_, n_months);
148 g_date_subtract_months(&gobject_, -n_months);
152 Date& Date::subtract_months(int n_months)
155 g_date_subtract_months(&gobject_, n_months);
157 g_date_add_months(&gobject_, -n_months);
161 Date& Date::add_years(int n_years)
164 g_date_add_years(&gobject_, n_years);
166 g_date_subtract_years(&gobject_, -n_years);
170 Date& Date::subtract_years(int n_years)
173 g_date_subtract_years(&gobject_, n_years);
175 g_date_add_years(&gobject_, -n_years);
179 int Date::days_between(const Date& rhs) const
181 return g_date_days_between(&gobject_, &rhs.gobject_);
184 int Date::compare(const Date& rhs) const
186 return g_date_compare(&gobject_, &rhs.gobject_);
189 Date& Date::clamp(const Date& min_date, const Date& max_date)
191 g_date_clamp(&gobject_, &min_date.gobject_, &max_date.gobject_);
195 Date& Date::clamp_min(const Date& min_date)
197 g_date_clamp(&gobject_, &min_date.gobject_, 0 /* see the C docs */);
201 Date& Date::clamp_max(const Date& max_date)
203 g_date_clamp(&gobject_, 0 /* see the C docs */, &max_date.gobject_);
207 void Date::order(Date& other)
209 g_date_order(&gobject_, &other.gobject_);
212 Date::Weekday Date::get_weekday() const
214 return (Date::Weekday) g_date_get_weekday(&gobject_);
217 Date::Month Date::get_month() const
219 return (Date::Month) g_date_get_month(&gobject_);
222 Date::Year Date::get_year() const
224 return g_date_get_year(&gobject_);
227 Date::Day Date::get_day() const
229 return g_date_get_day(&gobject_);
232 guint32 Date::get_julian() const
234 return g_date_get_julian(&gobject_);
237 unsigned int Date::get_day_of_year() const
239 return g_date_get_day_of_year(&gobject_);
242 unsigned int Date::get_monday_week_of_year() const
244 return g_date_get_monday_week_of_year(&gobject_);
247 unsigned int Date::get_sunday_week_of_year() const
249 return g_date_get_sunday_week_of_year(&gobject_);
252 bool Date::is_first_of_month() const
254 return g_date_is_first_of_month(&gobject_);
257 bool Date::is_last_of_month() const
259 return g_date_is_last_of_month(&gobject_);
263 guint8 Date::get_days_in_month(Date::Month month, Date::Year year)
265 return g_date_get_days_in_month((GDateMonth) month, year);
269 guint8 Date::get_monday_weeks_in_year(Date::Year year)
271 return g_date_get_monday_weeks_in_year(year);
275 guint8 Date::get_sunday_weeks_in_year(Date::Year year)
277 return g_date_get_sunday_weeks_in_year(year);
281 bool Date::is_leap_year(Date::Year year)
283 return g_date_is_leap_year(year);
286 Glib::ustring Date::format_string(const Glib::ustring& format) const
289 g_date_to_struct_tm(&gobject_, &tm_data);
291 #ifdef GLIBMM_EXCEPTIONS_ENABLED
292 const std::string locale_format = locale_from_utf8(format);
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
298 gsize bufsize = std::max<gsize>(2 * locale_format.size(), 128);
302 const ScopedPtr<char> buf (static_cast<char*>(g_malloc(bufsize)));
304 // Set the first byte to something other than '\0', to be able to
305 // recognize whether strftime actually failed or just returned "".
307 const gsize len = strftime(buf.get(), bufsize, locale_format.c_str(), &tm_data);
309 if(len != 0 || buf.get()[0] == '\0')
311 g_assert(len < bufsize);
312 #ifdef GLIBMM_EXCEPTIONS_ENABLED
313 return locale_to_utf8(std::string(buf.get(), len));
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
320 while((bufsize *= 2) <= 65536);
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");
325 return Glib::ustring();
328 void Date::to_struct_tm(struct tm& dest) const
330 g_date_to_struct_tm(&gobject_, &dest);
333 bool Date::valid() const
335 return g_date_valid(&gobject_);
339 bool Date::valid_day(Date::Day day)
341 return g_date_valid_day(day);
345 bool Date::valid_month(Date::Month month)
347 return g_date_valid_month((GDateMonth) month);
351 bool Date::valid_year(Date::Year year)
353 return g_date_valid_year(year);
357 bool Date::valid_weekday(Date::Weekday weekday)
359 return g_date_valid_weekday((GDateWeekday) weekday);
363 bool Date::valid_julian(guint32 julian_day)
365 return g_date_valid_julian(julian_day);
369 bool Date::valid_dmy(Date::Day day, Date::Month month, Date::Year year)
371 return g_date_valid_dmy(day, (GDateMonth) month, year);