GDate Utilities
[GLib]


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.


Files

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

GDate hash table support

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

GDate to time_t conversions

time_t gnc_timet_get_day_start_gdate (GDate *date)
time_t gnc_timet_get_day_end_gdate (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)


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 32 of file gnc-gdate-utils.c.

00033 {
00034   return (g_date_compare( (GDate*)gda, (GDate*)gdb ) == 0 ? TRUE : FALSE);
00035 }

guint gnc_gdate_hash ( gconstpointer  gd  ) 

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

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

00039 {
00040   gint val = (g_date_get_year( (GDate*)gd ) * 10000)
00041     + (g_date_get_month( (GDate*)gd ) * 100)
00042     + g_date_get_day( (GDate*)gd );
00043   return g_int_hash( &val );
00044 }

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:
date The GDate to modify.
year_end A GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

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

00248 {
00249   GDate temp;
00250   gboolean new_fy;
00251 
00252   g_return_if_fail(date);
00253   g_return_if_fail(fy_end);
00254 
00255   /* Compute the FY end that occurred this CY */
00256   temp = *fy_end;
00257   g_date_set_year(&temp, g_date_get_year(date));
00258 
00259   /* Has it already passed? */
00260   new_fy = (g_date_compare(date, &temp) > 0);
00261 
00262   /* Set end date */
00263   *date = temp;
00264   if (new_fy)
00265     g_date_add_years(date, 1);
00266 }

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:
date The GDate to modify.
year_end A GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

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

00224 {
00225   GDate temp;
00226   gboolean new_fy;
00227 
00228   g_return_if_fail(date);
00229   g_return_if_fail(fy_end);
00230 
00231   /* Compute the FY end that occurred this CY */
00232   temp = *fy_end;
00233   g_date_set_year(&temp, g_date_get_year(date));
00234 
00235   /* Has it already passed? */
00236   new_fy = (g_date_compare(date, &temp) > 0);
00237 
00238   /* Set start date */
00239   *date = temp;
00240   g_date_add_days(date, 1);
00241   if (!new_fy)
00242     g_date_subtract_years(date, 1);
00243 }

void gnc_gdate_set_month_end ( GDate *  date  ) 

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:
date The GDate to modify.

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

00098 {
00099   /* First set the start of next month. */
00100   g_date_set_day(date, 1);
00101   g_date_add_months(date, 1);
00102 
00103   /* Then back up one day */
00104   g_date_subtract_days(date, 1);
00105 }

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:
date The GDate to modify.

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

00084 {
00085   g_date_set_day(date, 1);
00086 }

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:
date The GDate to modify.
year_end A GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

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

00282 {
00283   g_return_if_fail(date);
00284   g_return_if_fail(fy_end);
00285 
00286   gnc_gdate_set_fiscal_year_end(date, fy_end);
00287   g_date_subtract_years(date, 1);
00288 }

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:
date The GDate to modify.
year_end A GDate containing the last month and day of the fiscal year. The year field of this argument is ignored.

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

00271 {
00272   g_return_if_fail(date);
00273   g_return_if_fail(fy_end);
00274 
00275   gnc_gdate_set_fiscal_year_start(date, fy_end);
00276   g_date_subtract_years(date, 1);
00277 }

void gnc_gdate_set_prev_month_end ( GDate *  date  ) 

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:
date The GDate to modify.

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

00132 {
00133   /* This will correctly handle the varying month lengths */
00134   g_date_set_day(date, 1);
00135   g_date_subtract_days(date, 1);
00136 }

void gnc_gdate_set_prev_month_start ( GDate *  date  ) 

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:
date The GDate to modify.

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

00117 {
00118   g_date_set_day(date, 1);
00119   g_date_subtract_months(date, 1);
00120 }

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:
date The GDate to modify.

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

00181 {
00182   gnc_gdate_set_quarter_end(date);
00183   g_date_subtract_months(date, 3);
00184 }

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:
date The GDate to modify.

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

00173 {
00174   gnc_gdate_set_quarter_start(date);
00175   g_date_subtract_months(date, 3);
00176 }

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:
date The GDate to modify.

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

00214 {
00215   gnc_gdate_set_year_end(date);
00216   g_date_subtract_years(date, 1);
00217 }

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:
date The GDate to modify.

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

00206 {
00207   gnc_gdate_set_year_start(date);
00208   g_date_subtract_years(date, 1);
00209 }

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:
date The GDate to modify.

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

00156 {
00157   gint months;
00158 
00159   /* Set the date to the first day of the specified month. */
00160   g_date_set_day(date, 1);
00161 
00162   /* Add 1-3 months to get the first day of the next quarter.*/
00163   months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
00164   g_date_add_months(date, 3 - months);
00165 
00166   /* Now back up one day */
00167   g_date_subtract_days(date, 1);
00168 }

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:
date The GDate to modify.

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

00142 {
00143   gint months;
00144 
00145   /* Set the date to the first day of the specified month. */
00146   g_date_set_day(date, 1);
00147 
00148   /* Back up 0-2 months */ 
00149   months = (g_date_get_month(date) - G_DATE_JANUARY) % 3;
00150   g_date_subtract_months(date, months);
00151 }

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:
date The GDate to modify.

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

00198 {
00199   g_date_set_month(date, G_DATE_DECEMBER);
00200   g_date_set_day(date, 31);
00201 }

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:
date The GDate to modify.

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

00190 {
00191   g_date_set_month(date, G_DATE_JANUARY);
00192   g_date_set_day(date, 1);
00193 }

time_t gnc_timet_get_day_end_gdate ( GDate *  date  ) 

The gnc_timet_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 62 of file gnc-gdate-utils.c.

00063 {
00064   struct tm stm;
00065   time_t secs;
00066 
00067   /* First convert to a 'struct tm' */
00068   g_date_to_struct_tm(date, &stm);
00069 
00070   /* Force to th last second of the day */
00071   stm.tm_hour = 23;
00072   stm.tm_min = 59;
00073   stm.tm_sec = 59;
00074   stm.tm_isdst = -1;
00075 
00076   /* Then convert to number of seconds */
00077   secs = mktime (&stm);
00078   return secs;
00079 }

time_t gnc_timet_get_day_start_gdate ( GDate *  date  ) 

The gnc_timet_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 48 of file gnc-gdate-utils.c.

00049 {
00050   struct tm stm;
00051   time_t secs;
00052 
00053   /* First convert to a 'struct tm' */
00054   g_date_to_struct_tm(date, &stm);
00055 
00056   /* Then convert to number of seconds */
00057   secs = mktime (&stm);
00058   return secs;
00059 }


Generated on Thu Jul 3 05:07:15 2008 for GnuCash by  doxygen 1.5.2