GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Recurrence.h
1 /* Recurrence.h:
2  *
3  * A Recurrence represents the periodic occurrence of dates, with a
4  * beginning point. For example, "Every Friday, beginning April 15,
5  * 2005" or "The 1st of every 3rd month, beginning April 1, 2001."
6  *
7  * Technically, a Recurrence can also represent certain useful
8  * "almost periodic" date sequences. For example, "The last day of
9  * every month, beginning Feb. 28, 2005."
10  *
11  * The main operation you can perform on a Recurrence is to find the
12  * earliest date in the sequence of occurrences that is after some
13  * specified date (often the "previous" occurrence).
14  *
15  * In addition, you can use a GList of Recurrences to represent a
16  * sequence containing all the dates in each Recurrence in the list,
17  * and perform the same "next instance" computation for this
18  * sequence.
19  *
20  * Copyright (C) 2005, Chris Shoemaker <[email protected]>
21  *
22  * This program is free software; you can redistribute it and/or
23  * modify it under the terms of the GNU General Public License as
24  * published by the Free Software Foundation; either version 2 of
25  * the License, or (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program; if not, contact:
34  *
35  * Free Software Foundation Voice: +1-617-542-5942
36  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
37  * Boston, MA 02110-1301, USA [email protected]
38  */
39 
40 #ifndef RECURRENCE_H
41 #define RECURRENCE_H
42 
43 #include <glib.h>
44 #include "Account.h"
45 #include "gnc-numeric.h"
46 
47 typedef enum
48 {
49  PERIOD_ONCE, /* Not a true period at all, but convenient here. */
50  PERIOD_DAY,
51  PERIOD_WEEK,
52  PERIOD_MONTH,
53  PERIOD_END_OF_MONTH, /* This is actually a period plus a phase. */
54  PERIOD_NTH_WEEKDAY, /* Also a phase, e.g. Second Tueday. */
55  PERIOD_LAST_WEEKDAY, /* Also a phase. */
56  PERIOD_YEAR,
57  NUM_PERIOD_TYPES,
58  PERIOD_INVALID = -1,
59 } PeriodType;
60 
61 typedef enum
62 {
63  WEEKEND_ADJ_NONE,
64  WEEKEND_ADJ_BACK, /* Previous weekday */
65  WEEKEND_ADJ_FORWARD, /* Next weekday */
66  NUM_WEEKEND_ADJS,
67  WEEKEND_ADJ_INVALID = -1,
68 } WeekendAdjust;
69 
70 /* Recurrences represent both the phase and period of a recurring event. */
71 
72 typedef struct
73 {
74  GDate start; /* First date in the recurrence; specifies phase. */
75  PeriodType ptype; /* see PeriodType enum */
76  guint16 mult; /* a period multiplier */
77  WeekendAdjust wadj; /* see WeekendAdjust enum */
78 } Recurrence;
79 
80 
81 /* recurrenceSet() will enforce internal consistency by overriding
82  inconsistent inputs so that 'r' will _always_ end up being a valid
83  recurrence.
84 
85  - if the period type is invalid, PERIOD_MONTH is used.
86 
87  - if the period type is PERIOD_ONCE, then mult is ignored,
88  otherwise, if mult is zero, then mult of 1 is used.
89 
90  - if the date is invalid, the current date is used.
91 
92  - if the period type specifies phase, the date is made to agree
93  with that phase:
94 
95  - for PERIOD_END_OF_MONTH, the last day of date's month is used.
96 
97  - for PERIOD_NTH_WEEKDAY, a fifth weekday converts to a
98  PERIOD_LAST_WEEKDAY
99 
100  - for PERIOD_LAST_WEEKDAY, the last day in date's month with
101  date's day-of-week is used.
102 
103 */
104 void recurrenceSet(Recurrence *r, guint16 mult, PeriodType pt,
105  const GDate *date, WeekendAdjust wadj);
106 
107 /* get the fields */
108 PeriodType recurrenceGetPeriodType(const Recurrence *r);
109 guint recurrenceGetMultiplier(const Recurrence *r);
110 GDate recurrenceGetDate(const Recurrence *r);
111 WeekendAdjust recurrenceGetWeekendAdjust(const Recurrence *r);
112 
113 /* Get the occurence immediately after refDate.
114  *
115  * This function has strict and precise post-conditions:
116  *
117  * Given a valid recurrence and a valid 'refDate', 'nextDate' will be
118  * *IN*valid IFF the period_type is PERIOD_ONCE, and 'refDate' is
119  * later-than or equal to the single occurrence (start_date).
120  *
121  * A valid 'nextDate' will _always_ be:
122  * - strictly later than the 'refDate', AND
123  * - later than or equal to the start date of the recurrence, AND
124  * - exactly an integral number of periods away from the start date
125  *
126  * Furthermore, there will be no date _earlier_ than 'nextDate' for
127  * which the three things above are true.
128  *
129  */
130 void recurrenceNextInstance(const Recurrence *r, const GDate *refDate,
131  GDate *nextDate);
132 
133 /* Zero-based. n == 1 gets the instance after the start date. */
134 void recurrenceNthInstance(const Recurrence *r, guint n, GDate *date);
135 
136 /* Get a time coresponding to the beginning (or end if 'end' is true)
137  of the nth instance of the recurrence. Also zero-based. */
138 time64 recurrenceGetPeriodTime(const Recurrence *r, guint n, gboolean end);
139 
144 gnc_numeric recurrenceGetAccountPeriodValue(const Recurrence *r,
145  Account *acct, guint n);
146 
148 void recurrenceListNextInstance(const GList *r, const GDate *refDate,
149  GDate *nextDate);
150 
151 /* These four functions are only for xml storage, not user presentation. */
152 gchar *recurrencePeriodTypeToString(PeriodType pt);
153 PeriodType recurrencePeriodTypeFromString(const gchar *str);
154 gchar *recurrenceWeekendAdjustToString(WeekendAdjust wadj);
155 WeekendAdjust recurrenceWeekendAdjustFromString(const gchar *str);
156 
157 /* For debugging. Caller owns the returned string. Not intl. */
158 gchar *recurrenceToString(const Recurrence *r);
159 gchar *recurrenceListToString(const GList *rlist);
160 
162 gboolean recurrenceListIsSemiMonthly(GList *recurrences);
164 gboolean recurrenceListIsWeeklyMultiple(const GList *recurrences);
165 
178 gchar *recurrenceListToCompactString(GList *recurrence_list);
179 
181 int recurrenceCmp(Recurrence *a, Recurrence *b);
182 int recurrenceListCmp(GList *a, GList *b);
183 
184 void recurrenceListFree(GList **recurrence);
185 
186 #endif /* RECURRENCE_H */
An exact-rational-number library for gnucash. (to be renamed qofnumeric.h in libqof2) ...
Account handling public routines.
gint64 time64
Definition: gnc-date.h:83