GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-gdate-utils.c
1 /*
2  * gnc-gdate-utils.c -- utility functions for manipulating
3  * GDate data structures from GLib
4  * Copyright (C) 2005 David Hampton <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, contact:
18  *
19  * Free Software Foundation Voice: +1-617-542-5942
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
21  * Boston, MA 02110-1301, USA [email protected]
22  */
23 
24 #include "config.h"
25 #include <glib.h>
26 
27 #include "gnc-gdate-utils.h"
28 
29 void
31 {
32  GDate *today = gnc_g_date_new_today ();
33  g_date_set_julian (gd, g_date_get_julian (today));
34  g_date_free (today);
35 }
36 
37 void
38 gnc_gdate_set_time64 (GDate* gd, time64 time)
39 {
40  GDateTime *gdt = gnc_g_date_time_new_from_unix_local (time);
41  gint y, m, d;
42  g_date_time_get_ymd (gdt, &y, &m, &d);
43  g_date_set_dmy (gd, d, m, y);
44  g_date_time_unref (gdt);
45 }
46 
47 gboolean
48 gnc_gdate_equal(gconstpointer gda, gconstpointer gdb)
49 {
50  return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
51 }
52 
53 guint
54 gnc_gdate_hash( gconstpointer gd )
55 {
56  gint val = (g_date_get_year( (GDate*)gd ) * 10000)
57  + (g_date_get_month( (GDate*)gd ) * 100)
58  + g_date_get_day( (GDate*)gd );
59  return g_int_hash( &val );
60 }
61 
62 
63 time64
64 gnc_time64_get_day_start_gdate (const GDate *date)
65 {
66  struct tm stm;
67  time64 secs;
68 
69  /* First convert to a 'struct tm' */
70  g_date_to_struct_tm (date, &stm);
71 
72  /* Then convert to number of seconds */
73  secs = gnc_mktime (&stm);
74  return secs;
75 }
76 
77 time64
78 gnc_time64_get_day_end_gdate (const GDate *date)
79 {
80  struct tm stm;
81  time64 secs;
82 
83  /* First convert to a 'struct tm' */
84  g_date_to_struct_tm(date, &stm);
85 
86  /* Force to th last second of the day */
87  stm.tm_hour = 23;
88  stm.tm_min = 59;
89  stm.tm_sec = 59;
90  stm.tm_isdst = -1;
91 
92  /* Then convert to number of seconds */
93  secs = gnc_mktime (&stm);
94  return secs;
95 }
96 
97 
98 void
100 {
101  g_date_set_day(date, 1);
102 }
103 
104 
112 void
114 {
115  /* First set the start of next month. */
116  g_date_set_day(date, 1);
117  g_date_add_months(date, 1);
118 
119  /* Then back up one day */
120  g_date_subtract_days(date, 1);
121 }
122 
123 
131 void
133 {
134  g_date_set_day(date, 1);
135  g_date_subtract_months(date, 1);
136 }
137 
138 
146 void
148 {
149  /* This will correctly handle the varying month lengths */
150  g_date_set_day(date, 1);
151  g_date_subtract_days(date, 1);
152 }
153 
154 /* ========== */
155 
156 void
158 {
159  gint months;
160 
161  /* Set the date to the first day of the specified month. */
162  g_date_set_day(date, 1);
163 
164  /* Back up 0-2 months */
165  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
166  g_date_subtract_months(date, months);
167 }
168 
169 
170 void
172 {
173  gint months;
174 
175  /* Set the date to the first day of the specified month. */
176  g_date_set_day(date, 1);
177 
178  /* Add 1-3 months to get the first day of the next quarter.*/
179  months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
180  g_date_add_months(date, 3 - months);
181 
182  /* Now back up one day */
183  g_date_subtract_days(date, 1);
184 }
185 
186 
187 void
189 {
191  g_date_subtract_months(date, 3);
192 }
193 
194 
195 void
197 {
199  g_date_subtract_months(date, 3);
200 }
201 
202 /* ========== */
203 
204 void
206 {
207  g_date_set_month(date, G_DATE_JANUARY);
208  g_date_set_day(date, 1);
209 }
210 
211 
212 void
214 {
215  g_date_set_month(date, G_DATE_DECEMBER);
216  g_date_set_day(date, 31);
217 }
218 
219 
220 void
222 {
224  g_date_subtract_years(date, 1);
225 }
226 
227 
228 void
230 {
232  g_date_subtract_years(date, 1);
233 }
234 
235 /* ========== */
236 
237 void
239  const GDate *fy_end)
240 {
241  GDate temp;
242  gboolean new_fy;
243 
244  g_return_if_fail(date);
245  g_return_if_fail(fy_end);
246 
247  /* Compute the FY end that occurred this CY */
248  temp = *fy_end;
249  g_date_set_year(&temp, g_date_get_year(date));
250 
251  /* Has it already passed? */
252  new_fy = (g_date_compare(date, &temp) > 0);
253 
254  /* Set start date */
255  *date = temp;
256  g_date_add_days(date, 1);
257  if (!new_fy)
258  g_date_subtract_years(date, 1);
259 }
260 
261 void
263  const GDate *fy_end)
264 {
265  GDate temp;
266  gboolean new_fy;
267 
268  g_return_if_fail(date);
269  g_return_if_fail(fy_end);
270 
271  /* Compute the FY end that occurred this CY */
272  temp = *fy_end;
273  g_date_set_year(&temp, g_date_get_year(date));
274 
275  /* Has it already passed? */
276  new_fy = (g_date_compare(date, &temp) > 0);
277 
278  /* Set end date */
279  *date = temp;
280  if (new_fy)
281  g_date_add_years(date, 1);
282 }
283 
284 void
286  const GDate *fy_end)
287 {
288  g_return_if_fail(date);
289  g_return_if_fail(fy_end);
290 
291  gnc_gdate_set_fiscal_year_start(date, fy_end);
292  g_date_subtract_years(date, 1);
293 }
294 
295 void
297  const GDate *fy_end)
298 {
299  g_return_if_fail(date);
300  g_return_if_fail(fy_end);
301 
302  gnc_gdate_set_fiscal_year_end(date, fy_end);
303  g_date_subtract_years(date, 1);
304 }
void gnc_gdate_set_today(GDate *gd)
void gnc_gdate_set_prev_quarter_start(GDate *date)
void gnc_gdate_set_prev_fiscal_year_end(GDate *date, const GDate *fy_end)
void gnc_gdate_set_month_end(GDate *date)
time64 gnc_time64_get_day_start_gdate(const GDate *date)
GDateTime * gnc_g_date_time_new_from_unix_local(time64 time)
void gnc_gdate_set_prev_year_end(GDate *date)
time64 gnc_time64_get_day_end_gdate(const GDate *date)
guint gnc_gdate_hash(gconstpointer gd)
void gnc_gdate_set_prev_quarter_end(GDate *date)
void gnc_gdate_set_month_start(GDate *date)
gboolean gnc_gdate_equal(gconstpointer gda, gconstpointer gdb)
void gnc_gdate_set_quarter_end(GDate *date)
void gnc_gdate_set_quarter_start(GDate *date)
void gnc_gdate_set_prev_year_start(GDate *date)
void gnc_gdate_set_fiscal_year_start(GDate *date, const GDate *fy_end)
void gnc_gdate_set_year_start(GDate *date)
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
GDate helper routines.
gint64 time64
Definition: gnc-date.h:83
void gnc_gdate_set_year_end(GDate *date)
void gnc_gdate_set_prev_month_end(GDate *date)
void gnc_gdate_set_prev_month_start(GDate *date)
void gnc_gdate_set_time64(GDate *gd, time64 time)
void gnc_gdate_set_fiscal_year_end(GDate *date, const GDate *fy_end)
void gnc_gdate_set_prev_fiscal_year_start(GDate *date, const GDate *fy_end)
GDate * gnc_g_date_new_today(void)