4 * Copyright (C) 2008 Jody Goldberg (jody@gnome.org)
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 the
9 * License, or (at your option) version 3.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #include <gnumeric-config.h>
24 #include <gnm-datetime.h>
26 #include <goffice/goffice.h>
27 #include <glib/gi18n-lib.h>
31 go_val_array_index_steal (GOValArray
*a
, int i
)
33 GOVal
*res
= go_val_array_index (a
, i
);
34 go_val_array_index (a
, i
) = NULL
;
39 go_val_array_free (GOValArray
*a
)
44 for (i
= (int)a
->len
; i
-- > 0 ; )
45 go_val_free (go_val_array_index (a
, i
));
46 g_ptr_array_free (a
, TRUE
);
51 go_val_bucketer_init (GOValBucketer
*bucketer
)
53 memset (bucketer
, 0, sizeof (GOValBucketer
));
54 bucketer
->type
= GO_VAL_BUCKET_NONE
;
58 go_val_bucketer_validate (GOValBucketer
*bucketer
)
60 GError
*failure
= NULL
;
61 if (bucketer
->type
>= GO_VAL_BUCKET_SERIES_LINEAR
) {
62 if (bucketer
->details
.dates
.minimum
>=
63 bucketer
->details
.dates
.maximum
)
64 failure
= g_error_new (go_error_invalid (), 0, _("minima must be < maxima"));
65 else if (bucketer
->details
.series
.step
<= 0)
66 failure
= g_error_new (go_error_invalid (), 0, _("step must be > 0"));
67 } else if (bucketer
->type
!= GO_VAL_BUCKET_NONE
) {
68 if (bucketer
->details
.dates
.minimum
>=
69 bucketer
->details
.dates
.maximum
)
70 failure
= g_error_new (go_error_invalid (), 0, _("minima must be < maxima"));
78 * go_val_bucketer_apply:
79 * @bucketer: #GOValBucketer
82 * Calculate which bucket @v falls into.
84 * Returns -1 on general failure, and 0 for out of range below the start of the domain.
85 * Some bucketer types will also create a bucket on the high end for out of range above.
88 go_val_bucketer_apply (GOValBucketer
const *bucketer
, GOVal
const *v
)
90 g_return_val_if_fail (bucketer
!= NULL
, 0);
91 g_return_val_if_fail (v
!= NULL
, 0);
93 if (bucketer
->type
== GO_VAL_BUCKET_NONE
)
97 if (bucketer
->type
<= GO_VAL_BUCKET_HOUR
) {
98 switch (bucketer
->type
) {
99 case GO_VAL_BUCKET_SECOND
:
101 case GO_VAL_BUCKET_MINUTE
:
103 default : g_assert_not_reached ();
107 if (bucketer
->type
<= GO_VAL_BUCKET_YEAR
) {
108 static GODateConventions
const default_conv
= {FALSE
};
110 if (!datetime_value_to_g (&d
, v
, &default_conv
))
113 switch (bucketer
->type
) {
114 case GO_VAL_BUCKET_DAY_OF_YEAR
: return 1 + g_date_get_day_of_year (&d
);
115 case GO_VAL_BUCKET_MONTH
: return g_date_get_month (&d
);
116 case GO_VAL_BUCKET_CALENDAR_QUARTER
: return 1 + ((g_date_get_month (&d
)-1) / 3);
117 case GO_VAL_BUCKET_YEAR
: return 1 + g_date_get_year (&d
);
118 default : g_assert_not_reached ();
122 /* >= GO_VAL_BUCKET_SERIES_LINEAR) */