add new sigc++2 directory
[ardour.git] / libs / glibmm2 / glib / src / date.hg
1 /* $Id: date.hg,v 1.6 2005/11/29 15:53:27 murrayc Exp $ */
2
3 /* Copyright (C) 2002 The gtkmm Development Team
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 _DEFS(glibmm,glib)
21
22 #m4 _PUSH(SECTION_HEADER1)
23 #m4 #undef G_DISABLE_DEPRECATED //So we can use deprecated functions in our deprecated methods.
24 #m4 _POP()
25
26 #include <glibmm/ustring.h>
27
28 #include <glib/gdate.h>
29 #include <glib/gtypes.h>
30
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 extern "C" { struct tm; }
33 #endif
34
35 namespace Glib
36 {
37
38 /** Julian calendar date.
39  */
40 class Date
41 {
42 public:
43   typedef guint8  Day;
44   typedef guint16 Year;
45
46   _WRAP_ENUM(Month, GDateMonth, s#^DATE_##, NO_GTYPE, get_type_func=)
47   _WRAP_ENUM(Weekday, GDateWeekday, s#^DATE_##, NO_GTYPE)
48   _WRAP_ENUM(DMY, GDateDMY, s#^DATE_##, NO_GTYPE)
49
50   static const Day     BAD_DAY    = 0;
51   static const Year    BAD_YEAR   = 0;
52   static const guint32 BAD_JULIAN = 0;
53
54   Date();
55   Date(Day day, Month month, Year year);
56   explicit Date(guint32 julian_day);
57
58 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59   explicit Date(const GDate& castitem);
60 #endif
61
62   void clear();
63   /** Clear the date. The cleared dates will not represent an existing date, but will not contain garbage.
64    * @param month Month to set.
65    */
66
67   /** Parses a user-inputted string str, and try to figure out what date it represents, taking the current locale into account. If the string is successfully parsed, the date will be valid after the call. Otherwise, it will be invalid.   
68    * This function is not appropriate for file formats and the like; it isn't very precise, and its exact behavior varies with the locale. It's intended to be a heuristic routine that guesses what the user means by a given string (and it does work pretty well in that capacity).   
69    * @param str String to parse.
70    */
71   void set_parse (const Glib::ustring& str);
72
73
74   _DEPRECATE_IFDEF_START
75   
76   //Avoid a build problem in the case that time_t is equivalent to guint32 (GTime is also guint32)
77   //That would make the set_time() method overload impossible.
78   #ifdef GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
79   /** Sets the value of a date from a GTime (time_t) value. 
80    *
81    * @param time GTime value to set.
82    *
83    * @deprecated Please use set_time(time_t) or set_time(const GTimeVal&).
84    */
85   void set_time(GTime time);
86   #endif //GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
87
88   _DEPRECATE_IFDEF_END
89
90
91   /** Sets the value of a date from a <type>time_t</type> value. 
92    *
93    * @param timet time_t value to set
94    *
95    * @see set_time_current()
96    *
97    * Since: 2.10
98    */
99   void set_time(time_t timet);
100
101   /** Sets the value of a date from a GTimeVal value.  Note that the
102    * tv_usec member is ignored, because Glib::Date can't make use of the
103    * additional precision.
104    *
105    * @see set_time_current()
106    * 
107    * @param timeval GTimeVal value to set
108    *
109    * Since: 2.10
110    */
111   void set_time(const GTimeVal& timeval);
112
113   void set_time_current();
114
115   /** Sets the month of the year. If the resulting day-month-year triplet is invalid, the date will be invalid.
116    * @param month Month to set.
117    */
118   void set_month(Month month);
119
120   /** Sets the day of the month. If the resulting day-month-year triplet is invalid, the date will be invalid.
121    * @param day Day to set.
122    */
123   void set_day(Day day);
124
125   /** Sets the year. If the resulting day-month-year triplet is invalid, the date will be invalid.
126    * @param year Year to set.
127    */
128   void set_year(Year year);
129
130   /** Sets the value of a day, month, and year.. If the resulting day-month-year triplet is invalid, the date will be invalid.
131    * @param day Day to set.
132    * @param month Month to set.
133    * @param year Year to set.
134    */
135   void set_dmy(Day day, Month month, Year year);
136
137   /** Sets the value of a GDate from a Julian day number.
138    * @param julian_day Julian day to set.
139    */
140    void set_julian(guint32 julian_day);
141
142   //TODO: Why return Date& (which is always *this) from these methods?
143   //Isn't it enough to also change the current instance?
144   //Maybe we need a copy constructor too.
145   //murrayc
146   
147   /** Add a number of days to a Date.
148    * @param n_days Days to add.
149    * @return Resulting Date
150    */
151   Date& add_days(int n_days);
152
153   /** Subtract n_days to a Date.
154    * @param n_days Days to subtract.
155    * @return Resulting Date
156    */
157   Date& subtract_days(int n_days);
158
159   /** Add n_months to a Date.
160    * @param n_months Months to add.
161    * @return Resulting Date
162    */
163   Date& add_months(int n_months);
164
165   /** Subtract n_months to a Date.
166    * @param n_months Months to subtract.
167    * @return Resulting Date
168    */
169   Date& subtract_months(int n_months);
170
171   /** Add n_days to a Date.
172    * @param n_years Years to add.
173    * @return Resulting Date
174    */
175   Date& add_years(int n_years);
176
177   /** Subtract n_years to a Date.
178    * @param n_years Years to subtract.
179    * @return Resulting Date
180    */
181   Date& subtract_years(int n_years);
182
183   /** Calculate days between two dates.
184    * @param rhs Date.
185    * @return Numbers of days.
186    */
187   int days_between(const Date& rhs) const;
188
189   /** Compare two dates.
190    * @param rhs Date to compare.
191    * @return Result of comparition.
192    */
193   int compare(const Date& rhs) const;
194
195   /** If date is prior to min_date, sets date equal to min_date. 
196    * If date falls after max_date, sets date equal to max_date. All dates must be valid.
197    * See also clamp_min() and clamp_max(). 
198    * @param min_date Date minimum value.
199    * @param max_date Date maximum value.
200    * @return Date in interval.
201    */
202   Date& clamp(const Date& min_date, const Date& max_date);
203
204   /** If date is prior to min_date, sets date equal to min_date.
205    * See also clamp(), and clamp_max().
206    * @param min_date Date minimum value.
207    * @return Date in interval.
208    */
209   Date& clamp_min(const Date& min_date);
210
211   /** If date falls after max_date, sets date equal to max_date.
212    * See also clamp(), and clamp_min().
213    * @param max_date Date maximum value.
214    * @return Date in interval.
215    */
216   Date& clamp_max(const Date& max_date);
217   
218   /** Checks if date is less than or equal to other date, and swap the values if this is not the case.
219    * @param other Date ro compare.
220    * @return Date.
221    */
222   void order(Date& other);
223
224   /** Returns the day of the week for a Date. The date must be valid.
225    * @return Day of the week as a Date::Weekday.
226    */
227   Weekday get_weekday() const;
228
229   /** Returns the month of the year. The date must be valid.
230    * @return Month of the year as a Date::Month. 
231    */
232   Month        get_month()               const;
233
234   /** Returns the year of a Date. The date must be valid.
235    * @return Year in which the date falls.
236    */
237   Year         get_year()                const;
238
239   /** Returns the day of the month. The date must be valid.
240    * @return Day of the month..
241    */
242   Day          get_day()                 const;
243
244   /** Returns the Julian day or "serial number" of the Date. 
245    * The Julian day is simply the number of days since January 1, Year 1; 
246    * i.e., January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2, etc. 
247    * The date must be valid.
248    * @return Julian day.
249    */
250   guint32      get_julian()              const;
251
252   /** Returns the day of the year, where Jan 1 is the first day of the year.
253    * The date must be valid.
254    * @return Julian day.
255    */
256   unsigned int get_day_of_year()         const;
257
258   /** Returns the week of the year, where weeks are understood to start on Monday. 
259    * If the date is before the first Monday of the year, return 0. 
260    * The date must be valid.
261    * @return Week of the year.
262    */
263   unsigned int get_monday_week_of_year() const;
264
265   /** Returns the week of the year during which this date falls, if weeks are understood to being on Sunday. 
266    * Can return 0 if the day is before the first Sunday of the year.
267    * The date must be valid.
268    * @return Week of the year.
269    */
270   unsigned int get_sunday_week_of_year() const;
271
272   /** Returns true if the date is on the first of a month. 
273    * The date must be valid.
274    * @return true if the date is the first of the month. 
275    */
276   bool         is_first_of_month()       const;
277
278   /** Returns true if the date is the last day of the month.
279    * The date must be valid.
280    * @return true if the date is the last day of the month.
281    */
282   bool         is_last_of_month()        const;
283
284   /** Returns the number of days in a month, taking leap years into account.
285    * @param month Month.
286    * @param year Year.
287    * @return Number of days in month during the year.
288    */
289   static guint8 get_days_in_month(Month month, Year year);
290
291   /** Returns the number of weeks in the year, where weeks are taken to start on Monday. Will be 52 or 53. 
292    * (Years always have 52 7-day periods, plus 1 or 2 extra days depending on whether it's a leap year. This function is basically telling you how many Mondays are in the year, i.e. there are 53 Mondays if one of the extra days happens to be a Monday.)
293    * @param year Year to count weeks in.
294    * @return Number of weeks.
295    */
296   static guint8 get_monday_weeks_in_year(Year year);
297
298   /** Returns the number of weeks in the year, where weeks are taken to start on Sunday. Will be 52 or 53. 
299    * (Years always have 52 7-day periods, plus 1 or 2 extra days depending on whether it's a leap year. This function is basically telling you how many Sundays are in the year, i.e. there are 53 Sundays if one of the extra days happens to be a Sunday.)
300    * @param year Year to count weeks in.
301    * @return Number of weeks.
302    */
303   static guint8 get_sunday_weeks_in_year(Year year);
304
305   /** Returns true if the year is a leap year.
306    * @param year Year to check.
307    * @return true if the year is a leap year.
308    */
309   static bool   is_leap_year(Year year);
310
311   /** Convert date to string.
312    * @param format A format string as used by @c strftime(), in UTF-8
313    * encoding.  Only date formats are allowed, the result of time formats
314    * is undefined.
315    * @return The formatted date string.
316    * @throw Glib::ConvertError
317    */
318   Glib::ustring format_string(const Glib::ustring& format) const;
319
320   /** Fills in the date-related bits of a struct tm using the date value. Initializes the non-date parts with something sane but meaningless.
321    * @param dest Struct tm to fill.
322    */
323   void to_struct_tm(struct tm& dest) const;
324
325   /** Returns true if the Date represents an existing day. 
326    * @return true if the date is valid.
327    */
328   bool valid() const;
329
330   /** Returns true if the day of the month is valid (a day is valid if it's between 1 and 31 inclusive).
331    * @param day Day to check.
332    * @return true if the day is valid.
333    */
334   static bool valid_day(Day day);
335
336   /** Returns true if the month value is valid. The 12 Date::Month enumeration values are the only valid months.
337    * @param month Month to check.
338    * @return true if the month is valid.
339    */
340   static bool valid_month(Month month);
341
342
343   /** Returns true if the year is valid. 
344    * Any year greater than 0 is valid, though there is a 16-bit limit to what Date will understand.
345    * @param year Year to check.
346    * @return true if the year is valid.
347    */
348   static bool valid_year(Year year);
349
350   /** Returns true if the weekday is valid. 
351    * The 7 Date::Weekday enumeration values are the only valid.
352    * @param weekday Weekday to check.
353    * @return true if the weekday is valid.
354    */
355   static bool valid_weekday(Weekday weekday);
356
357   /** Returns true if the Julian day is valid. 
358    * Anything greater than zero is basically a valid Julian, though there is a 32-bit limit.
359    * @param julian_day Julian day to check.
360    * @return true if the Julian day is valid.
361    */
362   static bool valid_julian(guint32 julian_day);
363
364
365   /** Returns true if the day-month-year triplet forms a valid, existing day in the range of days Date understands (Year 1 or later, no more than a few thousand years in the future).
366    * @param day Day to check.
367    * @param month Month to check.
368    * @param year Year to check.
369    * @return true if the date is a valid one. 
370    */
371   static bool valid_dmy(Day day, Month month, Year year);
372
373 #ifndef DOXYGEN_SHOULD_SKIP_THIS
374   GDate*       gobj()       { return &gobject_; }
375   const GDate* gobj() const { return &gobject_; }
376 #endif
377
378 private:
379   GDate gobject_;
380 };
381
382
383 /** @relates Glib::Date */
384 inline bool operator==(const Date& lhs, const Date& rhs)
385   { return (lhs.compare(rhs) == 0); }
386
387 /** @relates Glib::Date */
388 inline bool operator!=(const Date& lhs, const Date& rhs)
389   { return (lhs.compare(rhs) != 0); }
390
391 /** @relates Glib::Date */
392 inline bool operator<(const Date& lhs, const Date& rhs)
393   { return (lhs.compare(rhs) < 0); }
394
395 /** @relates Glib::Date */
396 inline bool operator>(const Date& lhs, const Date& rhs)
397   { return (lhs.compare(rhs) > 0); }
398
399 /** @relates Glib::Date */
400 inline bool operator<=(const Date& lhs, const Date& rhs)
401   { return (lhs.compare(rhs) <= 0); }
402
403 /** @relates Glib::Date */
404 inline bool operator>=(const Date& lhs, const Date& rhs)
405   { return (lhs.compare(rhs) >= 0); }
406
407 } // namespace Glib
408