summaryrefslogtreecommitdiff
path: root/glib/src/date.hg
blob: 472dc60987df96884e78c08b13eeebf3ecf5f61a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

_DEFS(glibmm,glib)

#m4 _PUSH(SECTION_CC_PRE_INCLUDES)
/* So we can use deprecated functions in our deprecated methods */
#undef G_DISABLE_DEPRECATED
#define GLIB_DISABLE_DEPRECATION_WARNINGS 1
#m4 _POP()

#include <glibmmconfig.h>
#include <glibmm/ustring.h>
#include <glib.h>
#include <ctime>

#ifndef DOXYGEN_SHOULD_SKIP_THIS
extern "C" { struct tm; }
#endif

namespace Glib
{

/** Julian calendar date.
 */
class Date
{
public:
  typedef guint8  Day;
  typedef guint16 Year;

  _WRAP_ENUM(Month, GDateMonth, s#^DATE_##, NO_GTYPE, get_type_func=)
  _WRAP_ENUM(Weekday, GDateWeekday, s#^DATE_##, NO_GTYPE)
  _WRAP_ENUM(DMY, GDateDMY, s#^DATE_##, NO_GTYPE)

  static const Day     BAD_DAY    = 0;
  static const Year    BAD_YEAR   = 0;
  static const guint32 BAD_JULIAN = 0;

  /** Construct an undefined date.
   */
  Date();

  /** Construct a date with the given day, month and year.
   * @param day The day.
   * @param month The month.
   * @param year The year.
   */
  Date(Day day, Month month, Year year);

  /** Construct a date from a julian day.
   * @param julian_day The julian day (guint32).
   */
  explicit Date(guint32 julian_day);

  /** Construct a Glib::Date by copying the contents of a GDate.
   * @param castitem The GDate.
   *
   * @newin{2,18}
   */
  explicit Date(const GDate& castitem);

  /** Construct a Glib::Date from another.
   * @param other the other Glib::Date.
   *
   * @newin{2,18}
   */
  Date(const Date& other);

  /** Assign another date to this one. For example:
   * @code
   * ...
   * Glib::Date my_date;
   * my_date = other_date;
   * @endcode
   *
   * @param other The other Glib::Date.
   *
   * @newin{2,18}
   */
  Date& operator=(const Date& other);

  /// Provides access to the underlying C instance.
  GDate*       gobj()       { return &gobject_; }

  /// Provides access to the underlying C instance.
  const GDate* gobj() const { return &gobject_; }

private:
  GDate gobject_;

public:
  /** Clear the date. The cleared dates will not represent an existing date,
   * but will not contain garbage.
   */
  void clear();

  /** 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.
   * 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).
   * @param str String to parse.
   */
  void set_parse (const Glib::ustring& str);


  _DEPRECATE_IFDEF_START

  //Avoid a build problem in the case that std::time_t is equivalent to gint32 (GTime is also gint32)
  //That would make the set_time() method overload impossible.
  #ifdef GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32
  /** Sets the value of a date from a GTime value.
   *
   * @param time GTime value to set.
   *
   * @deprecated Please use set_time(std::time_t) or set_time(const GTimeVal&).
   */
  void set_time(GTime time);
  #endif //GLIBMM_HAVE_C_STD_TIME_T_IS_NOT_INT32

  _DEPRECATE_IFDEF_END


  /** Sets the value of a date from a <type>std::time_t</type> value.
   *
   * @param timet std::time_t value to set
   *
   * @see set_time_current()
   *
   * Since: 2.10
   */
  void set_time(std::time_t timet);

  /** Sets the value of a date from a GTimeVal value.  Note that the
   * tv_usec member is ignored, because Glib::Date can't make use of the
   * additional precision.
   *
   * @see set_time_current()
   *
   * @param timeval GTimeVal value to set
   *
   * Since: 2.10
   */
  void set_time(const GTimeVal& timeval);

  /** Set this Glib::Date to the current time.
   */
  void set_time_current();

  /** Sets the month of the year. If the resulting day-month-year triplet is invalid, the date will be invalid.
   * @param month Month to set.
   */
  void set_month(Month month);

  /** Sets the day of the month. If the resulting day-month-year triplet is invalid, the date will be invalid.
   * @param day Day to set.
   */
  void set_day(Day day);

  /** Sets the year. If the resulting day-month-year triplet is invalid, the date will be invalid.
   * @param year Year to set.
   */
  void set_year(Year year);

  /** Sets the value of a day, month, and year.. If the resulting day-month-year triplet is invalid, the date will be invalid.
   * @param day Day to set.
   * @param month Month to set.
   * @param year Year to set.
   */
  void set_dmy(Day day, Month month, Year year);

  /** Sets the value of a GDate from a Julian day number.
   * @param julian_day Julian day to set.
   */
   void set_julian(guint32 julian_day);

  /** Add a number of days to a Date.
   * This method returns a reference to the object itself, so you can write code such as:
   * @code
   * somedate.add_days(2).add_months(3).add_years(17);
   * @endcode
   *
   * @param n_days Days to add.
   * @return Resulting Date
   */
  Date& add_days(int n_days);

  /** Subtract n_days to a Date.
   * This method returns a reference to the object itself, so you can write code such as:
   * @code
   * somedate.subtract_days(2).subtract_months(3).subtract_years(17);
   * @endcode
   *
   * @param n_days Days to subtract.
   * @return Resulting Date
   */
  Date& subtract_days(int n_days);

  /** Add n_months to a Date.
   * This method returns a reference to the object itself, so you can write code such as:
   * @code
   * somedate.add_years(2).add_months(3).add_days(17);
   * @endcode
   *
   * @param n_months Months to add.
   * @return Resulting Date
   */
  Date& add_months(int n_months);

  /** Subtract n_months to a Date.
   * This method returns a reference to the object itself, so you can write code such as:
   * @code
   * somedate.subtract_years(2).subtract_months(3).subtract_days(17);
   * @endcode
   *
   * @param n_months Months to subtract.
   * @return Resulting Date
   */
  Date& subtract_months(int n_months);

  /** Add n_days to a Date.
   * This method returns a reference to the object itself, so you can write code such as:
   * @code
   * somedate.add_years(2).add_months(3).add_days(17);
   * @endcode
   *
   * @param n_years Years to add.
   * @return Resulting Date
   */
  Date& add_years(int n_years);

  /** Subtract n_years to a Date.
   * This method returns a reference to the object itself, so you can write code such as:
   * @code
   * somedate.subtract_years(2).subtract_months(3).subtract_days(17);
   * @endcode
   *
   * @param n_years Years to subtract.
   * @return Resulting Date
   */
  Date& subtract_years(int n_years);

  /** Calculate days between two dates.
   * @param rhs Date.
   * @return Numbers of days.
   */
  int days_between(const Date& rhs) const;

  /** Compare two dates.
   * @param rhs Date to compare.
   * @return Result of comparition.
   */
  int compare(const Date& rhs) const;

  /** If date is prior to min_date, sets date equal to min_date.
   * If date falls after max_date, sets date equal to max_date. All dates must be valid.
   * See also clamp_min() and clamp_max().
   * This method returns a reference to the object itself.
   * @param min_date Date minimum value.
   * @param max_date Date maximum value.
   * @return Date in interval.
   */
  Date& clamp(const Date& min_date, const Date& max_date);

  /** If date is prior to min_date, sets date equal to min_date.
   * See also clamp(), and clamp_max().
   * This method returns a reference to the object itself.
   * @param min_date Date minimum value.
   * @return Date in interval.
   */
  Date& clamp_min(const Date& min_date);

  /** If date falls after max_date, sets date equal to max_date.
   * See also clamp(), and clamp_min().
   * This method returns a reference to the object itself.
   * @param max_date Date maximum value.
   * @return Date in interval.
   */
  Date& clamp_max(const Date& max_date);

  /** Checks if date is less than or equal to other date, and swap the values if this is not the case.
   * @param other Date ro compare.
   * @return Date.
   */
  void order(Date& other);

  /** Returns the day of the week for a Date. The date must be valid.
   * @return Day of the week as a Date::Weekday.
   */
  Weekday get_weekday() const;

  /** Returns the month of the year. The date must be valid.
   * @return Month of the year as a Date::Month.
   */
  Month        get_month()               const;

  /** Returns the year of a Date. The date must be valid.
   * @return Year in which the date falls.
   */
  Year         get_year()                const;

  /** Returns the day of the month. The date must be valid.
   * @return Day of the month..
   */
  Day          get_day()                 const;

  /** Returns the Julian day or "serial number" of the Date.
   * The Julian day is simply the number of days since January 1, Year 1;
   * i.e., January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2, etc.
   * The date must be valid.
   * @return Julian day.
   */
  guint32      get_julian()              const;

  /** Returns the day of the year, where Jan 1 is the first day of the year.
   * The date must be valid.
   * @return Julian day.
   */
  unsigned int get_day_of_year()         const;

  /** Returns the week of the year, where weeks are understood to start on Monday.
   * If the date is before the first Monday of the year, return 0.
   * The date must be valid.
   * @return Week of the year.
   */
  unsigned int get_monday_week_of_year() const;

  /** Returns the week of the year during which this date falls, if weeks are understood to being on Sunday.
   * Can return 0 if the day is before the first Sunday of the year.
   * The date must be valid.
   * @return Week of the year.
   */
  unsigned int get_sunday_week_of_year() const;

  /** Returns the week of the year, where weeks are interpreted according to ISO 8601.
   * The date must be valid.
   * @return ISO 8601 week number of the year.
   *
   * @newin{2,22}
   */
  unsigned int get_iso8601_week_of_year() const;

  /** Returns true if the date is on the first of a month.
   * The date must be valid.
   * @return true if the date is the first of the month.
   */
  bool         is_first_of_month()       const;

  /** Returns true if the date is the last day of the month.
   * The date must be valid.
   * @return true if the date is the last day of the month.
   */
  bool         is_last_of_month()        const;

  /** Returns the number of days in a month, taking leap years into account.
   * @param month Month.
   * @param year Year.
   * @return Number of days in month during the year.
   */
  static guint8 get_days_in_month(Month month, Year year);

  /** Returns the number of weeks in the year, where weeks are taken to start on Monday. Will be 52 or 53.
   * (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.)
   * @param year Year to count weeks in.
   * @return Number of weeks.
   */
  static guint8 get_monday_weeks_in_year(Year year);

  /** Returns the number of weeks in the year, where weeks are taken to start on Sunday. Will be 52 or 53.
   * (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.)
   * @param year Year to count weeks in.
   * @return Number of weeks.
   */
  static guint8 get_sunday_weeks_in_year(Year year);

  /** Returns true if the year is a leap year.
   * @param year Year to check.
   * @return true if the year is a leap year.
   */
  static bool   is_leap_year(Year year);

  /** Convert date to string.
   * @param format A format string as used by @c strftime(), in UTF-8
   * encoding.  Only date formats are allowed, the result of time formats
   * is undefined.
   * @return The formatted date string.
   * @throw Glib::ConvertError
   */
  Glib::ustring format_string(const Glib::ustring& format) const;

  /** Fills in the date-related bits of a struct tm using the date value. Initializes the non-date parts with something sane but meaningless.
   * @param dest Struct tm to fill.
   */
  void to_struct_tm(struct tm& dest) const;

  /** Returns true if the Date represents an existing day.
   * @return true if the date is valid.
   */
  bool valid() const;

  /** Returns true if the day of the month is valid (a day is valid if it's between 1 and 31 inclusive).
   * @param day Day to check.
   * @return true if the day is valid.
   */
  static bool valid_day(Day day);

  /** Returns true if the month value is valid. The 12 Date::Month enumeration values are the only valid months.
   * @param month Month to check.
   * @return true if the month is valid.
   */
  static bool valid_month(Month month);


  /** Returns true if the year is valid.
   * Any year greater than 0 is valid, though there is a 16-bit limit to what Date will understand.
   * @param year Year to check.
   * @return true if the year is valid.
   */
  static bool valid_year(Year year);

  /** Returns true if the weekday is valid.
   * The 7 Date::Weekday enumeration values are the only valid.
   * @param weekday Weekday to check.
   * @return true if the weekday is valid.
   */
  static bool valid_weekday(Weekday weekday);

  /** Returns true if the Julian day is valid.
   * Anything greater than zero is basically a valid Julian, though there is a 32-bit limit.
   * @param julian_day Julian day to check.
   * @return true if the Julian day is valid.
   */
  static bool valid_julian(guint32 julian_day);


  /** 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).
   * @param day Day to check.
   * @param month Month to check.
   * @param year Year to check.
   * @return true if the date is a valid one.
   */
  static bool valid_dmy(Day day, Month month, Year year);
};


/** @relates Glib::Date */
inline bool operator==(const Date& lhs, const Date& rhs)
  { return (lhs.compare(rhs) == 0); }

/** @relates Glib::Date */
inline bool operator!=(const Date& lhs, const Date& rhs)
  { return (lhs.compare(rhs) != 0); }

/** @relates Glib::Date */
inline bool operator<(const Date& lhs, const Date& rhs)
  { return (lhs.compare(rhs) < 0); }

/** @relates Glib::Date */
inline bool operator>(const Date& lhs, const Date& rhs)
  { return (lhs.compare(rhs) > 0); }

/** @relates Glib::Date */
inline bool operator<=(const Date& lhs, const Date& rhs)
  { return (lhs.compare(rhs) <= 0); }

/** @relates Glib::Date */
inline bool operator>=(const Date& lhs, const Date& rhs)
  { return (lhs.compare(rhs) >= 0); }

} // namespace Glib