Present  0.9
 All Classes Files Functions Variables Typedefs Friends Macros Pages
date.h
Go to the documentation of this file.
1 /*
2  * Present - Date/Time Library
3  *
4  * Definition of the Date structure and declarations of the corresponding
5  * functions
6  *
7  * This file may be included individually ONLY if being used by a C compiler.
8  * However, it is recommended (and required for C++ projects) to include
9  * "present.h" rather than these individual header files.
10  *
11  * Licensed under the MIT License.
12  * For details, see LICENSE.
13  */
14 
15 #include <time.h>
16 
17 #include "present/internal/cpp-guard.h"
18 #include "present/internal/header-utils.h"
19 #include "present/internal/types.h"
20 
21 #include "present/internal/present-date-data.h"
22 
23 #ifndef _PRESENT_DATE_H_
24 #define _PRESENT_DATE_H_
25 
26 /*
27  * Forward Declaraions
28  */
29 
30 struct DayDelta;
31 struct MonthDelta;
32 
33 /*
34  * Helper Struct Definitions
35  */
36 
47 };
48 
49 /*
50  * C++ Class / C Struct Definition
51  */
52 
59 struct PRESENT_API Date {
66 
73  struct {
74  unsigned int month_out_of_range : 1,
75  day_out_of_range : 1,
76  week_of_year_out_of_range : 1,
77  day_of_week_out_of_range : 1;
78  } errors;
79 
80  /* Internal data representation */
81  struct PresentDateData data_;
82 
83 #ifdef __cplusplus
84 
85  static Date create(int_year year);
86 
88  static Date create(int_year year, int_month month);
89 
91  static Date create(int_year year, int_month month, int_day day);
92 
94  static Date from_year_day(
95  int_year year,
96  int_day_of_year day_of_year);
97 
99  static Date from_year_week_day(
100  int_year year,
101  int_week_of_year week_of_year,
102  int_day_of_week day_of_week);
103 
105  int_year year() const;
106 
108  int_month month() const;
109 
111  int_day day() const;
112 
114  int_day_of_year day_of_year() const;
115 
117  PresentWeekYear week_of_year() const;
118 
120  int_day_of_week day_of_week() const;
121 
123  DayDelta difference(const Date & other) const;
125  DayDelta absolute_difference(const Date & other) const;
126 
128  Date & operator+=(const DayDelta & delta);
130  Date & operator+=(const MonthDelta & delta);
132  Date & operator-=(const DayDelta & delta);
134  Date & operator-=(const MonthDelta & delta);
135 
137  friend const Date operator+(const Date & lhs, const DayDelta & rhs);
139  friend const Date operator+(const DayDelta & lhs, const Date & rhs);
140 
142  friend const Date operator+(const Date & lhs, const MonthDelta & rhs);
144  friend const Date operator+(const MonthDelta & lhs, const Date & rhs);
145 
147  friend const Date operator-(const Date & lhs, const DayDelta & rhs);
148 
150  friend const Date operator-(const Date & lhs, const MonthDelta & rhs);
151 
153  static short compare(const Date & lhs, const Date & rhs);
154 
156  friend bool operator==(const Date & lhs, const Date & rhs);
157  friend bool operator!=(const Date & lhs, const Date & rhs);
158 
160  friend bool operator<(const Date & lhs, const Date & rhs);
162  friend bool operator<=(const Date & lhs, const Date & rhs);
164  friend bool operator>(const Date & lhs, const Date & rhs);
166  friend bool operator>=(const Date & lhs, const Date & rhs);
167 #endif
168 };
169 
170 /*
171  * C Method Declarations
172  */
173 
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177 
184 PRESENT_API struct Date
186 
191 PRESENT_API void
192 Date_ptr_from_year(struct Date * const result, int_year year);
193 
206 PRESENT_API struct Date
208 
213 PRESENT_API void
215  struct Date * const result,
216  int_year year,
217  int_month month);
218 
232 PRESENT_API struct Date
234 
239 PRESENT_API void
241  struct Date * const result,
242  int_year year,
244  int_day day);
245 
256 #define Date_create(...) \
257  PRESENT_OVERLOAD_MAX_3(__VA_ARGS__, \
258  Date_from_year_month_day, \
259  Date_from_year_month, \
260  Date_from_year, \
261  dummy)(__VA_ARGS__)
262 
274 #define Date_ptr_create(result, ...) \
275  PRESENT_OVERLOAD_MAX_3(__VA_ARGS__, \
276  Date_ptr_from_year_month_day, \
277  Date_ptr_from_year_month, \
278  Date_ptr_from_year, \
279  dummy)(result, __VA_ARGS__)
280 
287 PRESENT_API struct Date
289 
294 PRESENT_API void
296  struct Date * const result,
297  int_year year,
299 
317 PRESENT_API struct Date
319  int_year year,
322 
327 PRESENT_API void
329  struct Date * const result,
330  int_year year,
333 
337 PRESENT_API int_year
338 Date_year(const struct Date * const self);
339 
343 PRESENT_API int_month
344 Date_month(const struct Date * const self);
345 
349 PRESENT_API int_day
350 Date_day(const struct Date * const self);
351 
355 PRESENT_API int_day_of_year
356 Date_day_of_year(const struct Date * const self);
357 
364 PRESENT_API struct PresentWeekYear
365 Date_week_of_year(const struct Date * const self);
366 
371 PRESENT_API int_day_of_week
372 Date_day_of_week(const struct Date * const self);
373 
377 PRESENT_API struct DayDelta
379  const struct Date * const self,
380  const struct Date * const other);
381 
385 PRESENT_API struct DayDelta
387  const struct Date * const self,
388  const struct Date * const other);
389 
393 PRESENT_API void
395  struct Date * const self,
396  const struct DayDelta * const delta);
397 
401 PRESENT_API void
403  struct Date * const self,
404  const struct MonthDelta * const delta);
405 
409 PRESENT_API void
411  struct Date * const self,
412  const struct DayDelta * const delta);
413 
417 PRESENT_API void
419  struct Date * const self,
420  const struct MonthDelta * const delta);
421 
429 PRESENT_API short
431  const struct Date * const lhs,
432  const struct Date * const rhs);
433 
437 PRESENT_API present_bool
438 Date_equal(const struct Date * const lhs, const struct Date * const rhs);
439 
443 PRESENT_API present_bool
444 Date_less_than(const struct Date * const lhs, const struct Date * const rhs);
445 
450 PRESENT_API present_bool
452  const struct Date * const lhs,
453  const struct Date * const rhs);
454 
458 PRESENT_API present_bool
460  const struct Date * const lhs,
461  const struct Date * const rhs);
462 
467 PRESENT_API present_bool
469  const struct Date * const lhs,
470  const struct Date * const rhs);
471 
472 #ifdef __cplusplus
473 }
474 #endif
475 
476 #endif /* _PRESENT_DATE_H_ */
477 
void Date_subtract_MonthDelta(struct Date *const self, const struct MonthDelta *const delta)
Subtract a MonthDelta from a Date.
present_int16 int_day
Definition: types.h:66
int_day_of_week day_of_week() const
Get the day of the week of a Date (1 to 7, inclusive, with 1 being Monday, 2 being Tuesday...
Struct containing a year and a week of the year.
Definition: date.h:44
int_month month() const
Get the month of a Date (1 to 12, inclusive).
int_day_of_year Date_day_of_year(const struct Date *const self)
Get the day of the year of a Date (1 to 366, inclusive).
int_year year
Definition: date.h:46
short Date_compare(const struct Date *const lhs, const struct Date *const rhs)
Compare two Date instances.
struct Date Date_from_year_month(int_year year, int_month month)
Create a new Date based on a year and a month.
struct Date Date_from_year_month_day(int_year year, int_month month, int_day day)
Create a new Date based on a year, a month, and a day.
int_year year() const
Get the year of a Date.
present_int32 int_year
Definition: types.h:64
void Date_ptr_from_year_week_day(struct Date *const result, int_year year, int_week_of_year week_of_year, int_day_of_week day_of_week)
Create a new Date based on a year, a week of that year, and a day of the week.
int_day_of_week Date_day_of_week(const struct Date *const self)
Get the day of the week of a Date (1 to 7, inclusive, with 1 being Monday, 2 being Tuesday...
present_bool Date_less_than_or_equal(const struct Date *const lhs, const struct Date *const rhs)
Determine whether a Date is earlier than or the same as another Date (lhs <= rhs).
void Date_subtract_DayDelta(struct Date *const self, const struct DayDelta *const delta)
Subtract a DayDelta from a Date.
int_day Date_day(const struct Date *const self)
Get the day of month of a Date (1 to 31, inclusive).
void Date_ptr_from_year_month(struct Date *const result, int_year year, int_month month)
Create a new Date based on a year and a month.
struct Date Date_from_year(int_year year)
Create a new Date based on a year.
void Date_add_MonthDelta(struct Date *const self, const struct MonthDelta *const delta)
Add a MonthDelta to a Date.
struct Date Date_from_year_week_day(int_year year, int_week_of_year week_of_year, int_day_of_week day_of_week)
Create a new Date based on a year, a week of that year, and a day of the week.
present_bool Date_greater_than_or_equal(const struct Date *const lhs, const struct Date *const rhs)
Determine whether a Date is later than or the same as another Date (lhs >= rhs).
present_uint8 int_day_of_week
Definition: types.h:70
present_int16 int_month
Definition: types.h:65
void Date_ptr_from_year_day(struct Date *const result, int_year year, int_day_of_year day_of_year)
Create a new Date based on a year and the day of that year.
present_bool Date_greater_than(const struct Date *const lhs, const struct Date *const rhs)
Determine whether a Date is later than another Date (lhs > rhs).
struct DayDelta Date_difference(const struct Date *const self, const struct Date *const other)
Get the difference between two Date instances.
int_day_of_year day_of_year() const
Get the day of the year of a Date (1 to 366, inclusive).
struct Date Date_from_year_day(int_year year, int_day_of_year day_of_year)
Create a new Date based on a year and the day of that year.
int_week_of_year week
Definition: date.h:45
present_bool has_error
This will be true if there were any errors when creating this Date.
Definition: date.h:65
present_bool Date_less_than(const struct Date *const lhs, const struct Date *const rhs)
Determine whether a Date is earlier than another Date (lhs < rhs).
int_month Date_month(const struct Date *const self)
Get the month of a Date (1 to 12, inclusive).
present_int8 int_week_of_year
Definition: types.h:68
int_day day() const
Get the day of month of a Date (1 to 31, inclusive).
Class or struct representing a positive or negative delta of a number of days or weeks.
Definition: day-delta.h:40
Class or struct representing a calendar date.
Definition: date.h:59
void Date_add_DayDelta(struct Date *const self, const struct DayDelta *const delta)
Add a DayDelta to a Date.
struct DayDelta Date_absolute_difference(const struct Date *const self, const struct Date *const other)
Get the absolute difference between two Date instances.
void Date_ptr_from_year_month_day(struct Date *const result, int_year year, int_month month, int_day day)
Create a new Date based on a year, a month, and a day.
struct PresentWeekYear Date_week_of_year(const struct Date *const self)
Get the week of the year of a Date (1 to 53, inclusive), and the year corresponding to that week...
void Date_ptr_from_year(struct Date *const result, int_year year)
Create a new Date based on a year.
present_bool Date_equal(const struct Date *const lhs, const struct Date *const rhs)
Determine whether two Date instances are equal (lhs == rhs).
bool present_bool
Definition: types.h:21
int_year Date_year(const struct Date *const self)
Get the year of a Date.
PresentWeekYear week_of_year() const
Get the week of the year of a Date (1 to 53, inclusive), and the year corresponding to that week...
present_int16 int_day_of_year
Definition: types.h:67
Class or struct representing a positive or negative delta of a number of months or years...
Definition: month-delta.h:38