Line data Source code
1 : /*
2 : * Present - Date/Time Library
3 : *
4 : * Implementation of the MonthDelta methods
5 : *
6 : * Licensed under the MIT License.
7 : * For details, see LICENSE.
8 : */
9 :
10 : #include <assert.h>
11 : #include <stddef.h>
12 :
13 : #include "present.h"
14 :
15 : #include "utils/constants.h"
16 : #include "utils/impl-utils.h"
17 : #include "utils/time-utils.h"
18 :
19 : /** Initialize a new MonthDelta based on the number of months. */
20 : static void
21 92 : init_month_delta(struct MonthDelta * const result, int_month_delta months)
22 : {
23 92 : assert(result != NULL);
24 92 : CLEAR(result);
25 :
26 92 : result->data_.delta_months = months;
27 92 : }
28 :
29 :
30 : struct MonthDelta
31 5 : MonthDelta_from_months(int_month_delta months)
32 : {
33 : struct MonthDelta result;
34 5 : init_month_delta(&result, months);
35 5 : return result;
36 : }
37 :
38 : void
39 55 : MonthDelta_ptr_from_months(
40 : struct MonthDelta * const result,
41 : int_month_delta months)
42 : {
43 55 : init_month_delta(result, months);
44 55 : }
45 :
46 : struct MonthDelta
47 5 : MonthDelta_from_years(int_year_delta years)
48 : {
49 : struct MonthDelta result;
50 5 : init_month_delta(&result, years * MONTHS_IN_YEAR);
51 5 : return result;
52 : }
53 :
54 : void
55 16 : MonthDelta_ptr_from_years(
56 : struct MonthDelta * const result,
57 : int_year_delta years)
58 : {
59 16 : init_month_delta(result, years * MONTHS_IN_YEAR);
60 16 : }
61 :
62 : struct MonthDelta
63 1 : MonthDelta_zero(void)
64 : {
65 : struct MonthDelta result;
66 1 : init_month_delta(&result, 0);
67 1 : return result;
68 : }
69 :
70 : void
71 10 : MonthDelta_ptr_zero(struct MonthDelta * const result)
72 : {
73 10 : init_month_delta(result, 0);
74 10 : }
75 :
76 : int_month_delta
77 75 : MonthDelta_months(const struct MonthDelta * const self)
78 : {
79 75 : assert(self != NULL);
80 :
81 75 : return self->data_.delta_months;
82 : }
83 :
84 : int_year_delta
85 11 : MonthDelta_years(const struct MonthDelta * const self)
86 : {
87 11 : assert(self != NULL);
88 :
89 11 : return self->data_.delta_months / MONTHS_IN_YEAR;
90 : }
91 :
92 : double
93 5 : MonthDelta_years_decimal(const struct MonthDelta * const self)
94 : {
95 5 : assert(self != NULL);
96 :
97 5 : return ((double)self->data_.delta_months) / (double)MONTHS_IN_YEAR;
98 : }
99 :
100 : present_bool
101 3 : MonthDelta_is_negative(const struct MonthDelta * const self)
102 : {
103 3 : assert(self != NULL);
104 :
105 3 : return self->data_.delta_months < 0;
106 : }
107 :
108 : void
109 9 : MonthDelta_negate(struct MonthDelta * const self)
110 : {
111 9 : assert(self != NULL);
112 :
113 9 : self->data_.delta_months = -self->data_.delta_months;
114 9 : }
115 :
116 : void
117 6 : MonthDelta_multiply_by(struct MonthDelta * const self, long scale_factor)
118 : {
119 6 : assert(self != NULL);
120 :
121 6 : self->data_.delta_months *= scale_factor;
122 6 : }
123 :
124 : void
125 6 : MonthDelta_divide_by(struct MonthDelta * const self, long scale_factor)
126 : {
127 6 : assert(self != NULL);
128 :
129 6 : self->data_.delta_months /= scale_factor;
130 6 : }
131 :
132 : void
133 4 : MonthDelta_add(
134 : struct MonthDelta * const self,
135 : const struct MonthDelta * const other)
136 : {
137 4 : assert(self != NULL);
138 4 : assert(other != NULL);
139 :
140 4 : self->data_.delta_months += other->data_.delta_months;
141 4 : }
142 :
143 : void
144 4 : MonthDelta_subtract(
145 : struct MonthDelta * const self,
146 : const struct MonthDelta * const other)
147 : {
148 4 : assert(self != NULL);
149 4 : assert(other != NULL);
150 :
151 4 : self->data_.delta_months -= other->data_.delta_months;
152 4 : }
153 :
154 : short
155 39 : MonthDelta_compare(
156 : const struct MonthDelta * const lhs,
157 : const struct MonthDelta * const rhs)
158 : {
159 39 : assert(lhs != NULL);
160 39 : assert(rhs != NULL);
161 :
162 39 : return STRUCT_COMPARE(delta_months, 0);
163 : }
164 :
165 33 : STRUCT_COMPARISON_OPERATORS(MonthDelta)
166 :
|