File: functions.c

package info (click to toggle)
gnumeric 1.10.8-1squeeze5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 90,968 kB
  • ctags: 23,303
  • sloc: ansic: 248,235; xml: 51,894; sh: 10,491; makefile: 2,822; perl: 2,466; yacc: 1,272; python: 205
file content (414 lines) | stat: -rw-r--r-- 13,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * fn-hebrew-date.c:  Built in hebrew date functions.
 *
 * Author:
 *   Yaacov Zamir <kzamir@walla.co.il>
 *
 * Based on Date functions by:
 *   Miguel de Icaza (miguel@gnu.org)
 *   Jukka-Pekka Iivonen (iivonen@iki.fi)
 *   Morten Welinder <terra@diku.dk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <gnumeric-config.h>
#include <gnumeric.h>
#include <gnm-i18n.h>
#include <func.h>
#include <value.h>

#include <parse-util.h>
#include <cell.h>
#include <value.h>
#include <mathfunc.h>
#include <workbook.h>
#include <sheet.h>
#include <gnm-datetime.h>

#include <math.h>

#include <glib.h>
#include "hdate.h"
#include <goffice/goffice.h>
#include <gnm-plugin.h>

GNM_PLUGIN_MODULE_HEADER;

#define DATE_CONV(ep)               workbook_date_conv ((ep)->sheet->workbook)
#define UNICODE_MONTH_PREFIX "\xd7\x91\xd6\xbc\xd6\xb0"

static void
gnumeric_hdate_get_date (GnmValue const * const *arg, int *year, int *month, int *day)
{
	GDate date;

	if (arg[0] == NULL || arg[1]  == NULL || arg[2] == NULL)
		g_date_set_time_t (&date, time (NULL));

	*year = (arg[0]) ? value_get_as_int (arg[0])
		: g_date_get_year (&date);
	*month = (arg[1]) ? value_get_as_int (arg[1]) :
		(int)g_date_get_month (&date);
	*day = (arg[2]) ? value_get_as_int (arg[2]) :
		g_date_get_day (&date);

	return;
}

static GnmValue *
gnumeric_date_get_date (GnmFuncEvalInfo * ei, GnmValue const * const val, 
			int *year, int *month, int *day)
{
	GDate date;

	if (val == NULL)
		g_date_set_time_t (&date, time (NULL));
	else if (!datetime_value_to_g (&date, val, DATE_CONV (ei->pos)))
		return value_new_error_NUM (ei->pos);

	*year = g_date_get_year (&date);
	*month = g_date_get_month (&date);
	*day = g_date_get_day (&date);

	return NULL;
}

/***************************************************************************/

static GnmFuncHelp const help_hdate[] = {
	{ GNM_FUNC_HELP_NAME, F_("HDATE:Hebrew date") },
        { GNM_FUNC_HELP_ARG, F_("year:Gregorian year of date, defaults to the current year")},
        { GNM_FUNC_HELP_ARG, F_("month:Gregorian month of year, defaults to the current month")},
        { GNM_FUNC_HELP_ARG, F_("day:Gregorian day of month, defaults to the current day")},
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE(2001,3,30)" },
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE_HEB,DATE"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_hdate (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int hyear, hmonth, hday;
	char *res;

	gnumeric_hdate_get_date (argv, &year, &month, &day);

	if (0 != hdate_gdate_to_hdate (day, month, year, &hday, &hmonth, &hyear))
		return value_new_error_VALUE (ei->pos);

	res = g_strdup_printf ("%d %s %d",
			       hday + 1,
			       hdate_get_hebrew_month_name (hmonth),
			       hyear);

	return value_new_string_nocopy (res);
}

/***************************************************************************/



static GnmFuncHelp const help_date2hdate[] = {
	{ GNM_FUNC_HELP_NAME, F_("DATE2HDATE:Hebrew date") },
        { GNM_FUNC_HELP_ARG, F_("date:Gregorian date, defaults to today")},
        { GNM_FUNC_HELP_EXAMPLES, "=DATE2HDATE(DATE(2001,3,30))" },
        { GNM_FUNC_HELP_EXAMPLES, "=DATE2HDATE()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE,DATE2HDATE_HEB"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_date2hdate (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int hyear, hmonth, hday;
	char *res;
	GnmValue *val;

	val = gnumeric_date_get_date (ei,argv[0], &year, &month, &day);
	if (val != NULL)
		return val;

	if (0 != hdate_gdate_to_hdate (day, month, year, &hday, &hmonth, &hyear))
		return value_new_error_VALUE (ei->pos);

	res = g_strdup_printf ("%d %s %d",
			       hday + 1,
			       hdate_get_hebrew_month_name (hmonth),
			       hyear);

	return value_new_string_nocopy (res);
}

/***************************************************************************/

static GnmFuncHelp const help_hdate_heb[] = {
	{ GNM_FUNC_HELP_NAME, F_("HDATE_HEB:Hebrew date in Hebrew") },
        { GNM_FUNC_HELP_ARG, F_("year:Gregorian year of date, defaults to the current year")},
        { GNM_FUNC_HELP_ARG, F_("month:Gregorian month of year, defaults to the current month")},
        { GNM_FUNC_HELP_ARG, F_("day:Gregorian day of month, defaults to the current day")},
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_HEB(2001,3,30)" },
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_HEB()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE,DATE"},
	{ GNM_FUNC_HELP_END }
};

static void
build_hdate (GString *res, int hyear, int hmonth, int hday)
{
	hdate_int_to_hebrew (res, hday + 1);
	g_string_append (res, " " UNICODE_MONTH_PREFIX);
	g_string_append (res, hdate_get_hebrew_month_name_heb (hmonth));
	g_string_append_c (res, ' ');
	hdate_int_to_hebrew (res, hyear);
}

static GnmValue *
gnumeric_hdate_heb (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int hyear, hmonth, hday;
	GString *res;

	gnumeric_hdate_get_date (argv, &year, &month, &day);

	if (0 != hdate_gdate_to_hdate (day, month, year, &hday, &hmonth, &hyear))
		return value_new_error_VALUE (ei->pos);

	res = g_string_new (NULL);
	build_hdate (res, hyear, hmonth, hday);

	return value_new_string_nocopy (g_string_free (res, FALSE));
}

/***************************************************************************/

static GnmFuncHelp const help_date2hdate_heb[] = {
	{ GNM_FUNC_HELP_NAME, F_("DATE2HDATE_HEB:Hebrew date in Hebrew") },
        { GNM_FUNC_HELP_ARG, F_("date:Gregorian date, defaults to today")},
        { GNM_FUNC_HELP_EXAMPLES, "=DATE2HDATE_HEB(DATE(2001,3,30))" },
        { GNM_FUNC_HELP_EXAMPLES, "=DATE2HDATE_HEB()" },
        { GNM_FUNC_HELP_SEEALSO, "DATE2HDATE,HDATE_HEB"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_date2hdate_heb (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int hyear, hmonth, hday;
	GString *res;
	GnmValue *val;

	val = gnumeric_date_get_date (ei,argv[0], &year, &month, &day);
	if (val != NULL)
		return val;

	if (0 != hdate_gdate_to_hdate (day, month, year, &hday, &hmonth, &hyear))
		return value_new_error_VALUE (ei->pos);

	res = g_string_new (NULL);
	build_hdate (res, hyear, hmonth, hday);

	return value_new_string_nocopy (g_string_free (res, FALSE));
}

/***************************************************************************/

static GnmFuncHelp const help_hdate_month[] = {
	{ GNM_FUNC_HELP_NAME, F_("HDATE_MONTH:Hebrew month of Gregorian date") },
        { GNM_FUNC_HELP_ARG, F_("year:Gregorian year of date, defaults to the current year")},
        { GNM_FUNC_HELP_ARG, F_("month:Gregorian month of year, defaults to the current month")},
        { GNM_FUNC_HELP_ARG, F_("day:Gregorian day of month, defaults to the current day")},
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_MONTH(2001,3,30)" },
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_MONTH()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE_JULIAN"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_hdate_month (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int hyear, hmonth, hday;

	gnumeric_hdate_get_date (argv, &year, &month, &day);

	if (0 != hdate_gdate_to_hdate (day, month, year, &hday, &hmonth, &hyear))
		return value_new_error_VALUE (ei->pos);

	return value_new_int (hmonth);
}

/***************************************************************************/

static GnmFuncHelp const help_hdate_day[] = {
	{ GNM_FUNC_HELP_NAME, F_("HDATE_DAY:Hebrew day of Gregorian date") },
        { GNM_FUNC_HELP_ARG, F_("year:Gregorian year of date, defaults to the current year")},
        { GNM_FUNC_HELP_ARG, F_("month:Gregorian month of year, defaults to the current month")},
        { GNM_FUNC_HELP_ARG, F_("day:Gregorian day of month, defaults to the current day")},
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_DAY(2001,3,30)" },
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_DAY()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE_JULIAN"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_hdate_day (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int hyear, hmonth, hday;

	gnumeric_hdate_get_date (argv, &year, &month, &day);

	if (0 != hdate_gdate_to_hdate (day, month, year, &hday, &hmonth, &hyear))
		return value_new_error_VALUE (ei->pos);

	return value_new_int (hday + 1);
}

/***************************************************************************/

static GnmFuncHelp const help_hdate_year[] = {
	{ GNM_FUNC_HELP_NAME, F_("HDATE_YEAR:Hebrew year of Gregorian date") },
        { GNM_FUNC_HELP_ARG, F_("year:Gregorian year of date, defaults to the current year")},
        { GNM_FUNC_HELP_ARG, F_("month:Gregorian month of year, defaults to the current month")},
        { GNM_FUNC_HELP_ARG, F_("day:Gregorian day of month, defaults to the current day")},
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_YEAR(2001,3,30)" },
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_YEAR()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE_JULIAN"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_hdate_year (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int hyear, hmonth, hday;

	gnumeric_hdate_get_date (argv, &year, &month, &day);

	if (0 != hdate_gdate_to_hdate (day, month, year, &hday, &hmonth, &hyear))
		return value_new_error_VALUE (ei->pos);

	return value_new_int (hyear);
}

/***************************************************************************/

static GnmFuncHelp const help_hdate_julian[] = {
	{ GNM_FUNC_HELP_NAME, F_("HDATE_JULIAN:Julian day number for given Gregorian date") },
        { GNM_FUNC_HELP_ARG, F_("year:Gregorian year of date, defaults to the current year")},
        { GNM_FUNC_HELP_ARG, F_("month:Gregorian month of year, defaults to the current month")},
        { GNM_FUNC_HELP_ARG, F_("day:Gregorian day of month, defaults to the current day")},
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_JULIAN(2001,3,30)" },
        { GNM_FUNC_HELP_EXAMPLES, "=HDATE_JULIAN()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_hdate_julian (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int julian;

	gnumeric_hdate_get_date (argv, &year, &month, &day);

	julian = hdate_gdate_to_jd (day, month, year);

	return value_new_int (julian);
}

/***************************************************************************/

static GnmFuncHelp const help_date2julian[] = {
	{ GNM_FUNC_HELP_NAME, F_("DATE2JULIAN:Julian day number for given Gregorian date") },
        { GNM_FUNC_HELP_ARG, F_("date:Gregorian date, defaults to today")},
        { GNM_FUNC_HELP_EXAMPLES, "=DATE2JULIAN(DATE(2001,3,30))" },
        { GNM_FUNC_HELP_EXAMPLES, "=DATE2JULIAN()" },
        { GNM_FUNC_HELP_SEEALSO, "HDATE_JULIAN"},
	{ GNM_FUNC_HELP_END }
};

static GnmValue *
gnumeric_date2julian (GnmFuncEvalInfo * ei, GnmValue const * const *argv)
{
	int year, month, day;
	int julian;
	GnmValue *val;

	val = gnumeric_date_get_date (ei, argv[0], &year, &month, &day);
	if (val != NULL)
		return val;

	julian = hdate_gdate_to_jd (day, month, year);

	return value_new_int (julian);
}

/***************************************************************************/

GnmFuncDescriptor const datetime_functions[] = {
	{"hdate", "|fff", help_hdate,
	 gnumeric_hdate, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	{"hdate_heb", "|fff", help_hdate_heb,
	 gnumeric_hdate_heb, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	 {"hdate_day", "|fff", help_hdate_day,
	 gnumeric_hdate_day, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	 {"hdate_month", "|fff", help_hdate_month,
	 gnumeric_hdate_month, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	 {"hdate_year", "|fff", help_hdate_year,
	 gnumeric_hdate_year, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	 {"hdate_julian", "|fff", help_hdate_julian,
	 gnumeric_hdate_julian, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	 {"date2hdate", "|f", help_date2hdate,
	 gnumeric_date2hdate, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	 {"date2hdate_heb", "|f", help_date2hdate_heb,
	 gnumeric_date2hdate_heb, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	 {"date2julian", "|f", help_date2julian,
	 gnumeric_date2julian, NULL, NULL, NULL, NULL,
	 GNM_FUNC_SIMPLE + GNM_FUNC_AUTO_UNITLESS,
	 GNM_FUNC_IMPL_STATUS_UNIQUE_TO_GNUMERIC,
	 GNM_FUNC_TEST_STATUS_NO_TESTSUITE},
	{NULL}
};