GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Files
GDate Utilities

Files

file  gnc-gdate-utils.h
 GDate helper routines.
 

GDate time64 setters

void gnc_gdate_set_today (GDate *gd)
 
void gnc_gdate_set_time64 (GDate *gd, time64 time)
 

GDate hash table support

gint gnc_gdate_equal (gconstpointer gda, gconstpointer gdb)
 
guint gnc_gdate_hash (gconstpointer gd)
 

GDate to time64 conversions

time64 gnc_time64_get_day_start_gdate (const GDate *date)
 
time64 gnc_time64_get_day_end_gdate (const GDate *date)
 

Date Manipulation

void gnc_gdate_set_month_start (GDate *date)
 
void gnc_gdate_set_month_end (GDate *date)
 
void gnc_gdate_set_prev_month_start (GDate *date)
 
void gnc_gdate_set_prev_month_end (GDate *date)
 
void gnc_gdate_set_quarter_start (GDate *date)
 
void gnc_gdate_set_quarter_end (GDate *date)
 
void gnc_gdate_set_prev_quarter_start (GDate *date)
 
void gnc_gdate_set_prev_quarter_end (GDate *date)
 
void gnc_gdate_set_year_start (GDate *date)
 
void gnc_gdate_set_year_end (GDate *date)
 
void gnc_gdate_set_prev_year_start (GDate *date)
 
void gnc_gdate_set_prev_year_end (GDate *date)
 
void gnc_gdate_set_fiscal_year_start (GDate *date, const GDate *year_end)
 
void gnc_gdate_set_fiscal_year_end (GDate *date, const GDate *year_end)
 
void gnc_gdate_set_prev_fiscal_year_start (GDate *date, const GDate *year_end)
 
void gnc_gdate_set_prev_fiscal_year_end (GDate *date, const GDate *year_end)
 

Detailed Description

This file provides routines that help make it easier to use GDates from within Gnucash. A GDate is a data strucutre provided by GLib that handles dates from year 1 to year 9999.

Function Documentation

gint gnc_gdate_equal ( gconstpointer  gda,
gconstpointer  gdb 
)

Compares two GDate*'s for equality; useful for using GDate*'s as GHashTable keys.

Definition at line 48 of file gnc-gdate-utils.c.

49 {
50  return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
51 }
guint gnc_gdate_hash ( gconstpointer  gd)

Provides a "hash" of a GDate* value; useful for using GDate*'s as GHashTable keys.

Definition at line 54 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_fiscal_year_end ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the last day of the fiscal year in which it falls. For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2004-07-31.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 262 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_fiscal_year_start ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the first day of the fiscal year in which it falls. For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2003-08-01.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 238 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_month_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the month in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-09-30.

Parameters
dateThe GDate to modify.

Convert a GDate to the last day of the month. This routine has no knowledge of how many days are in a month, whether its a leap year, etc. All that information is contained in the glib date functions.

Parameters
dateThe GDate to modify.

Definition at line 113 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_month_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the month in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-09-01.

Parameters
dateThe GDate to modify.

Definition at line 99 of file gnc-gdate-utils.c.

100 {
101  g_date_set_day(date, 1);
102 }
void gnc_gdate_set_prev_fiscal_year_end ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the last day of the fiscal year prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2003-07-31.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 296 of file gnc-gdate-utils.c.

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_fiscal_year_end(GDate *date, const GDate *fy_end)
void gnc_gdate_set_prev_fiscal_year_start ( GDate *  date,
const GDate *  year_end 
)

This function modifies a GDate to set it to the first day of the fiscal year prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 and a fiscal year ending July 31st, the date will be modified to 2002-08-01.

Parameters
dateThe GDate to modify.
year_endA GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

Definition at line 285 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_fiscal_year_start(GDate *date, const GDate *fy_end)
void gnc_gdate_set_prev_month_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the month prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-08-31.

Parameters
dateThe GDate to modify.

Convert a GDate to the last day of the prebvious month. This routine has no knowledge of how many days are in a month, whether its a leap year, etc. All that information is contained in the glib date functions.

Parameters
dateThe GDate to modify.

Definition at line 147 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_prev_month_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the month prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-08-01.

Parameters
dateThe GDate to modify.

Convert a GDate to the first day of the prebvious month. This routine has no knowledge of how many days are in a month, whether its a leap year, etc. All that information is contained in the glib date functions.

Parameters
dateThe GDate to modify.

Definition at line 132 of file gnc-gdate-utils.c.

133 {
134  g_date_set_day(date, 1);
135  g_date_subtract_months(date, 1);
136 }
void gnc_gdate_set_prev_quarter_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the quarter prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-07-31.

Parameters
dateThe GDate to modify.

Definition at line 196 of file gnc-gdate-utils.c.

197 {
199  g_date_subtract_months(date, 3);
200 }
void gnc_gdate_set_quarter_end(GDate *date)
void gnc_gdate_set_prev_quarter_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the quarter prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-06-01.

Parameters
dateThe GDate to modify.

Definition at line 188 of file gnc-gdate-utils.c.

189 {
191  g_date_subtract_months(date, 3);
192 }
void gnc_gdate_set_quarter_start(GDate *date)
void gnc_gdate_set_prev_year_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the year prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2002-12-31.

Parameters
dateThe GDate to modify.

Definition at line 229 of file gnc-gdate-utils.c.

230 {
232  g_date_subtract_years(date, 1);
233 }
void gnc_gdate_set_year_end(GDate *date)
void gnc_gdate_set_prev_year_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the year prior to the one in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2002-01-01.

Parameters
dateThe GDate to modify.

Definition at line 221 of file gnc-gdate-utils.c.

222 {
224  g_date_subtract_years(date, 1);
225 }
void gnc_gdate_set_year_start(GDate *date)
void gnc_gdate_set_quarter_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the quarter in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-12-31.

Parameters
dateThe GDate to modify.

Definition at line 171 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_quarter_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the quarter in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-09-01.

Parameters
dateThe GDate to modify.

Definition at line 157 of file gnc-gdate-utils.c.

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 }
void gnc_gdate_set_time64 ( GDate *  gd,
time64  time 
)

Set a GDate to a time64

Parameters
theGDatethe date to act on
timethe time to set it to.

Definition at line 38 of file gnc-gdate-utils.c.

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 }
GDateTime * gnc_g_date_time_new_from_unix_local(time64 time)
void gnc_gdate_set_today ( GDate *  gd)

Set a GDate to the current day

Parameters
theGDateThe date to act on

Definition at line 30 of file gnc-gdate-utils.c.

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 }
GDate * gnc_g_date_new_today(void)
void gnc_gdate_set_year_end ( GDate *  date)

This function modifies a GDate to set it to the last day of the year in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-12-31.

Parameters
dateThe GDate to modify.

Definition at line 213 of file gnc-gdate-utils.c.

214 {
215  g_date_set_month(date, G_DATE_DECEMBER);
216  g_date_set_day(date, 31);
217 }
void gnc_gdate_set_year_start ( GDate *  date)

This function modifies a GDate to set it to the first day of the year in which it falls. For example, if this function is called with a date of 2003-09-24 the date will be modified to 2003-01-01.

Parameters
dateThe GDate to modify.

Definition at line 205 of file gnc-gdate-utils.c.

206 {
207  g_date_set_month(date, G_DATE_JANUARY);
208  g_date_set_day(date, 1);
209 }
time64 gnc_time64_get_day_end_gdate ( const GDate *  date)

The gnc_time64_get_day_end() routine will take the given time in GLib GDate format and adjust it to the last second of that day.

Definition at line 78 of file gnc-gdate-utils.c.

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 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
gint64 time64
Definition: gnc-date.h:83
time64 gnc_time64_get_day_start_gdate ( const GDate *  date)

The gnc_time64_get_day_start() routine will take the given time in GLib GDate format and adjust it to the first second of that day.

Definition at line 64 of file gnc-gdate-utils.c.

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 }
time64 gnc_mktime(struct tm *time)
calculate seconds from the epoch given a time struct
gint64 time64
Definition: gnc-date.h:83