/* Like g_date_add_years, but...
 *
 * 1. Do not spew criticals.
 * 2. Number of years is signed.
 */
void
gnm_date_add_years (GDate *d, int n)
{
	if (!g_date_valid (d))
		return;

	if (n >= 0) {
		int m = 65535 - g_date_get_year (d);

		if (n > m)
			goto bad;

		g_date_add_years (d, n);
	} else {
		int m = g_date_get_year (d) - 1;

		if (m + n <= 0)
			goto bad;

		g_date_subtract_years (d, -n);
	}

	return;

 bad:
	g_date_clear (d, 1);
}
Example #2
0
DATE*
julian_to_date (guint32 julian_day)
{
DATE *date;
GDate *cdate;
    
    date = g_new0 (DATE, 1);
    if (date == NULL) {
        return date;
    }

    if (g_date_valid_julian(julian_day)) {
        cdate = g_date_new_julian (julian_day);

        if (cdate != NULL) {
            if (g_date_valid (cdate)) {
                date->year = g_date_get_year (cdate);
                date->month = g_date_get_month (cdate);
                date->day = g_date_get_day (cdate);
            }
            g_date_free(cdate);
        }

        return date;    /* you have to free DATE struct after use! */

    } else {
        return NULL;
    }
}
/* Like g_date_add_months, but...
 *
 * 1. Do not spew criticals.
 * 2. Number of months is signed.
 */
void
gnm_date_add_months (GDate *d, int n)
{
	if (!g_date_valid (d))
		return;

	if (n >= 0) {
		int m = (65535 - g_date_get_year (d)) * 12 +
			(12 - g_date_get_month (d));

		if (n > m)
			goto bad;

		g_date_add_months (d, n);
	} else {
		int m = (g_date_get_year (d) - 1) * 12 +
			(g_date_get_month (d) - 1);

		if (m + n <= 0)
			goto bad;

		g_date_subtract_months (d, -n);
	}

	return;

 bad:
	g_date_clear (d, 1);
}
void
gnm_date_add_days (GDate *d, int n)
{
	if (!g_date_valid (d))
		return;

	if (n >= 0) {
		guint32 lim = 23936166;  /* 31-Dec-65535 */
		guint32 j = g_date_get_julian (d);

		if (j > lim || (unsigned)n > lim - j)
			goto bad;

		g_date_add_days (d, n);
	} else {
		int m = g_date_get_julian (d) - 1;

		if (m + n <= 0)
			goto bad;

		g_date_subtract_days (d, -n);
	}

	return;

 bad:
	g_date_clear (d, 1);
}
Example #5
0
/* Caller owns the returned memory */
gchar *
recurrenceToString(const Recurrence *r)
{
    gchar *tmpDate;
    gchar *tmpPeriod, *ret;

    g_return_val_if_fail(g_date_valid(&r->start), NULL);
    tmpDate = g_new0(gchar, MAX_DATE_LENGTH + 1);
    g_date_strftime(tmpDate, MAX_DATE_LENGTH, "%x", &r->start);

    if (r->ptype == PERIOD_ONCE)
    {
        ret = g_strdup_printf("once on %s", tmpDate);
        goto done;
    }

    tmpPeriod = period_type_strings[r->ptype];
    if (r->mult > 1)
        ret = g_strdup_printf("Every %d %ss beginning %s",
                              r->mult, tmpPeriod, tmpDate);
    else
        ret = g_strdup_printf("Every %s beginning %s",
                              tmpPeriod, tmpDate);
done:
    g_free(tmpDate);
    return ret;
}
static gint
_safe_invalidable_date_compare(const GDate *a, const GDate *b)
{
    if (!g_date_valid(a) && !g_date_valid(b))
    {
        return 0;
    }
    if (!g_date_valid(a))
    {
        return 1;
    }
    if (!g_date_valid(b))
    {
        return -1;
    }
    return g_date_compare(a, b);
}
static void
increment_sx_state(GncSxInstance *inst, GDate **last_occur_date, int *instance_count, int *remain_occur_count)
{
    if (!g_date_valid(*last_occur_date)
            || (g_date_valid(*last_occur_date)
                && g_date_compare(*last_occur_date, &inst->date) <= 0))
    {
        *last_occur_date = &inst->date;
    }

    *instance_count = gnc_sx_get_instance_count(inst->parent->sx, inst->temporal_state) + 1;

    if (*remain_occur_count > 0)
    {
        *remain_occur_count -= 1;
    }
}
Example #8
0
GDate* gnc_g_date_new_today ()
{
    GncDate gncd;
    auto ymd = gncd.year_month_day();
    auto month = static_cast<GDateMonth>(ymd.month);
    auto result = g_date_new_dmy (ymd.day, month, ymd.year);
    g_assert(g_date_valid (result));
    return result;
}void
Example #9
0
void
utl_date_get_dmy (const GDate *date, gint *day, gint *month, gint *year)
{
	g_return_if_fail (g_date_valid (date));

	if (day != NULL) *day = g_date_get_day (date);
	if (month != NULL) *month = g_date_get_month (date);
	if (year != NULL) *year = g_date_get_year (date);
}
Example #10
0
/* return an julian date ... 1 = no date set */
guint32 gtodo_todo_item_get_stop_date_as_julian(GTodoItem *item)
{
	if(item->stop == NULL || !g_date_valid(item->stop)) return 1;
	else
	{
		if(!g_date_valid_julian(g_date_get_julian(item->stop))) return 1;
		return g_date_get_julian(item->stop);
	}
}
Example #11
0
gboolean csv_import_parse_value_date ( struct struct_ope_importation * ope, gchar * string )
{
    g_return_val_if_fail ( string, FALSE );

    if (ope -> date_de_valeur )
        g_free ( ope -> date_de_valeur);
    ope -> date_de_valeur = gsb_parse_date_string ( string );

    return g_date_valid ( ope -> date_de_valeur );
}
Example #12
0
GDate
xaccSchedXactionGetNextInstance (const SchedXaction *sx, SXTmpStateData *tsd)
{
    GDate prev_occur, next_occur;

    g_date_clear( &prev_occur, 1 );
    if ( tsd != NULL )
        prev_occur = tsd->last_date;

    /* If prev_occur is in the "cleared" state and sx->start_date isn't, then
     * we're at the beginning. We want to pretend prev_occur is the day before
     * the start_date in case the start_date is today so that the SX will fire
     * today. If start_date isn't valid either then the SX will fire anyway, no
     * harm done.
     */
    if (! g_date_valid( &prev_occur ) && g_date_valid(&sx->start_date))
    {
        /* We must be at the beginning. */
        prev_occur = sx->start_date;
        g_date_subtract_days( &prev_occur, 1 );
    }

    recurrenceListNextInstance(sx->schedule, &prev_occur, &next_occur);

    if ( xaccSchedXactionHasEndDate( sx ) )
    {
        const GDate *end_date = xaccSchedXactionGetEndDate( sx );
        if ( g_date_compare( &next_occur, end_date ) > 0 )
        {
            g_date_clear( &next_occur, 1 );
        }
    }
    else if ( xaccSchedXactionHasOccurDef( sx ) )
    {
        if ((tsd && tsd->num_occur_rem == 0) ||
            (!tsd && sx->num_occurances_remain == 0 ))
        {
            g_date_clear( &next_occur, 1 );
        }
    }
    return next_occur;
}
gboolean
datetime_value_to_g (GDate *res, GnmValue const *v, GODateConventions const *conv)
{
	int serial = datetime_value_to_serial (v, conv);
	if (serial == G_MAXINT) {
		g_date_clear (res, 1);
		return FALSE;
	}
	go_date_serial_to_g (res, serial, conv);
	return g_date_valid (res);
}
Example #14
0
GDateDay     
g_date_get_day (const GDate *d)
{
  g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
  
  if (!d->dmy) 
    g_date_update_dmy (d);

  g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);  
  
  return d->day;
}
Example #15
0
GDateYear    
g_date_get_year (const GDate *d)
{
  g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
  
  if (!d->dmy) 
    g_date_update_dmy (d);

  g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);  
  
  return d->year;
}
Example #16
0
GDateMonth   
g_date_get_month (const GDate *d)
{
  g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
  
  if (!d->dmy) 
    g_date_update_dmy (d);

  g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
  
  return d->month;
}
Example #17
0
GDateWeekday 
g_date_get_weekday (const GDate *d)
{
  g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
  
  if (!d->julian) 
    g_date_update_julian (d);

  g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
  
  return ((d->julian_days - 1) % 7) + 1;
}
Example #18
0
/* return an julian date ... 1 = no date set */
guint32 gtodo_todo_item_get_due_date_as_julian(GTodoItem *item)
{
	if(item->due == NULL || !g_date_valid(item->due))
	{
		return GTODO_NO_DUE_DATE;
	}
	else
	{
		if(!g_date_valid_julian(g_date_get_julian(item->due))) return GTODO_NO_DUE_DATE;
		return g_date_get_julian(item->due);
	}
}
Example #19
0
gchar *
utl_date_print (const GDate *d, gint date_format, gint override_locale)
{
	gchar date_str[BUFFER_SIZE], *format;

	g_return_val_if_fail (g_date_valid (d), NULL);

	format = utl_date_get_format_str (date_format, override_locale);
	g_date_strftime (date_str, BUFFER_SIZE, format, d);

	return g_strdup (date_str);
}
static void
_format_conditional_date(const GDate *date, char *date_buf, int buf_max_length)
{
    if (date == NULL || !g_date_valid(date))
    {
        g_stpcpy(date_buf, _("never"));
    }
    else
    {
        qof_print_gdate(date_buf, buf_max_length, date);
    }
}
Example #21
0
void
utl_date_diff (const GDate *date1, const GDate *date2, gint *day, gint *month, gint *year)
{
	g_return_if_fail (g_date_valid (date1));
	g_return_if_fail (g_date_valid (date2));
	g_return_if_fail (g_date_compare (date1, date2) <= 0);

    *day = g_date_get_day (date2) - g_date_get_day (date1);
    *month = g_date_get_month (date2) - g_date_get_month (date1);
    *year = g_date_get_year (date2) - g_date_get_year (date1);

    if (*day < 0) {
        *day += utl_date_get_days_in_month (date2);
		*month -= 1;
    }

    if (*month < 0) {
        *month += 12;
		*year -= 1;
    }
}
Example #22
0
void
gnc_date_edit_set_gdate (GNCDateEdit *gde, const GDate *date)
{
    struct tm mytm;
    time64 t;

    g_return_if_fail(gde && GNC_IS_DATE_EDIT(gde) &&
                     date && g_date_valid(date));
    g_date_to_struct_tm(date, &mytm);
    t = gnc_mktime(&mytm);
    gnc_date_edit_set_time(gde, t);
}
Example #23
0
void
xaccSchedXactionSetLastOccurDate(SchedXaction *sx, const GDate* new_last_occur)
{
    g_return_if_fail (new_last_occur != NULL);
    if (g_date_valid(&sx->last_date)
            && g_date_compare(&sx->last_date, new_last_occur) == 0)
        return;
    gnc_sx_begin_edit(sx);
    sx->last_date = *new_last_occur;
    qof_instance_set_dirty(&sx->inst);
    gnc_sx_commit_edit(sx);
}
Example #24
0
void gncEntrySetDateGDate (GncEntry *entry, const GDate* date)
{
    if (!entry || !date || !g_date_valid(date))
        return;

    /* Watch out: Here we are deviating from the initial convention that a
    GDate always converts to the start time of the day. Instead, the GDate is
    converted to "noon" on the respective date. This is not nice, but this
    convention was used for the Timespec of GncEntry all the time, so we better
    stick to it.*/
    gncEntrySetDate(entry, timespecCanonicalDayTime(gdate_to_timespec(*date)));
}
Example #25
0
guint32      
g_date_get_julian (const GDate *d)
{
  g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
  
  if (!d->julian) 
    g_date_update_julian (d);

  g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);  
  
  return d->julian_days;
}
Example #26
0
void
recurrenceSet(Recurrence *r, guint16 mult, PeriodType pt, const GDate *_start, WeekendAdjust wadj)
{
    r->ptype = VALID_PERIOD_TYPE(pt) ? pt : PERIOD_MONTH;
    r->mult = (pt == PERIOD_ONCE) ? 0 : (mult > 0 ? mult : 1);

    if (_start && g_date_valid(_start))
    {
        r->start = *_start;
    }
    else
    {
        gnc_gdate_set_today (&r->start);
    }

    /* Some of the unusual period types also specify phase.  For those
       types, we ensure that the start date agrees with that phase. */
    switch (r->ptype)
    {
    case PERIOD_END_OF_MONTH:
        g_date_set_day(&r->start, g_date_get_days_in_month
                       (g_date_get_month(&r->start),
                        g_date_get_year(&r->start)));
        break;
    case PERIOD_LAST_WEEKDAY:
    {
        GDateDay dim;
        dim = g_date_get_days_in_month(g_date_get_month(&r->start),
                                       g_date_get_year(&r->start));
        while (dim - g_date_get_day(&r->start) >= 7)
            g_date_add_days(&r->start, 7);
    }
    break;
    case PERIOD_NTH_WEEKDAY:
        if ((g_date_get_day(&r->start) - 1) / 7 == 4) /* Fifth week */
            r->ptype = PERIOD_LAST_WEEKDAY;
        break;
    default:
        break;
    }

    switch (r->ptype)
    {
    case PERIOD_MONTH:
    case PERIOD_END_OF_MONTH:
    case PERIOD_YEAR:
        r->wadj = wadj;
        break;
    default:
        r->wadj = WEEKEND_ADJ_NONE;
        break;
    }
}
Example #27
0
/* The GDate setter functions all in the end use g_date_set_time_t,
 * which in turn relies on localtime and is therefore subject to the
 * 2038 bug.
 */
GDate timespec_to_gdate (Timespec ts)
{
    GDate result;
    gint day, month, year;

    g_date_clear (&result, 1);
    gnc_timespec2dmy (ts, &day, &month, &year);
    g_date_set_dmy (&result, day, static_cast<GDateMonth>(month), year);
    g_assert(g_date_valid (&result));

    return result;
}
Example #28
0
void         
g_date_subtract_months (GDate       *d, 
                        guint        nmonths)
{
  guint years, months;
  gint index;
  
  g_return_if_fail (d != NULL);
  g_return_if_fail (g_date_valid (d));
  
  if (!d->dmy) 
    {
      g_date_update_dmy (d);
    }
  g_return_if_fail (d->dmy);  
  
  years  = nmonths/12;
  months = nmonths%12;
  
  g_return_if_fail (d->year > years);
  
  d->year  -= years;
  
  if (d->month > months) d->month -= months;
  else 
    {
      months -= d->month;
      d->month = 12 - months;
      d->year -= 1;
    }
  
  index = g_date_is_leap_year (d->year) ? 1 : 0;
  
  if (d->day > days_in_months[index][d->month])
    d->day = days_in_months[index][d->month];
  
  d->julian = FALSE;
  
  g_return_if_fail (g_date_valid (d));
}
Example #29
0
/**
 * set the next date in the scheduled transaction
 * if it's above the limit date, that transaction is deleted
 * if it's a split, the children are updated too
 * if the scheduled transaction is finished, it's removed from the list and from the scheduled transactions
 * 
 * \param scheduled_number the scheduled transaction we want to increase
 * 
 * \return FALSE if the scheduled transaction is finished, TRUE else
 * */
gboolean gsb_scheduler_increase_scheduled ( gint scheduled_number )
{
    GDate *new_date;

    g_return_val_if_fail ( g_date_valid (gsb_data_scheduled_get_date (scheduled_number)), TRUE );

    /* increase the date of the scheduled_transaction */
    new_date = gsb_scheduler_get_next_date ( scheduled_number,
					     gsb_data_scheduled_get_date (scheduled_number));

    /* we continue to work only if new_date is not null (null mean reach the end) */
    if (new_date)
    {
	/* set the new date */
	gsb_data_scheduled_set_date ( scheduled_number, new_date);

	if ( gsb_data_scheduled_get_split_of_scheduled ( scheduled_number ))
	{
	    GSList *children_numbers_list;

	    /* if there is some children, set the new date too */
	    children_numbers_list = gsb_data_scheduled_get_children ( scheduled_number, TRUE );

	    while ( children_numbers_list )
	    {
		gint child_number;

		child_number = GPOINTER_TO_INT ( children_numbers_list -> data );

		gsb_data_scheduled_set_date ( child_number,
					      new_date );

		children_numbers_list = children_numbers_list -> next;
	    }
	    g_slist_free (children_numbers_list);
	}
	g_date_free (new_date);
    }
    else
    {
	/* the scheduled transaction is over, we remove it */
	/* update the main page */
	gsb_main_page_update_finished_scheduled_transactions (scheduled_number);

	/* remove the scheduled transaction */
	/* !! important to remove first from the list... */
	gsb_scheduler_list_remove_transaction_from_list ( scheduled_number );
	gsb_data_scheduled_remove_scheduled (scheduled_number);
	return FALSE;
    }
    return TRUE;
}
Example #30
0
/* The GDate setter functions all in the end use g_date_set_time_t,
 * which in turn relies on localtime and is therefore subject to the
 * 2038 bug.
 */
GDate timespec_to_gdate (Timespec ts)
{
    GDate result;

    g_date_clear (&result, 1);
    GncDateTime time(ts.tv_sec);
    auto date = time.date().year_month_day();
    g_date_set_dmy (&result, date.day, static_cast<GDateMonth>(date.month),
                    date.year);
    g_assert(g_date_valid (&result));

    return result;
}