summaryrefslogtreecommitdiff
path: root/ext/calendar/calendar.c
blob: b681ee94200d0443cbe3c726c5870d83a6d260a9 (plain)
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
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group                   |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Shane Caraveo             <shane@caraveo.com>               | 
   |          Colin Viebrock            <cmv@easydns.com>                 |
   |          Hartmut Holzgraefe        <hartmut@six.de>                  |
   +----------------------------------------------------------------------+
 */
/* $Id: */

#include "php.h"
#include "ext/standard/info.h"
#include "php_calendar.h"
#include "sdncal.h"

function_entry calendar_functions[] = {
	PHP_FE(jdtogregorian, NULL)
	PHP_FE(gregoriantojd, NULL)
	PHP_FE(jdtojulian, NULL)
	PHP_FE(juliantojd, NULL)
	PHP_FE(jdtojewish, NULL)
	PHP_FE(jewishtojd, NULL)
	PHP_FE(jdtofrench, NULL)
	PHP_FE(frenchtojd, NULL)
	PHP_FE(jddayofweek, NULL)
	PHP_FE(jdmonthname, NULL)
	PHP_FE(easter_date, NULL)
	PHP_FE(easter_days, NULL)
	PHP_FE(unixtojd,    NULL)
	PHP_FE(jdtounix,    NULL)
	{NULL, NULL, NULL}
};


zend_module_entry calendar_module_entry = {
  "Calendar", 
  calendar_functions, 
  NULL, /*PHP_MINIT(calendar),*/
  NULL,
  NULL,
  NULL,
  PHP_MINFO(calendar),
  STANDARD_MODULE_PROPERTIES,
};

#ifdef COMPILE_DL_CALENDAR
ZEND_GET_MODULE(calendar)
#endif

PHP_MINIT_FUNCTION(calendar)
{
  /*
  REGISTER_INT_CONSTANT("CAL_EASTER_TO_xxx",0, CONST_CS | CONST_PERSISTENT);
  */
  return SUCCESS;
}

PHP_MINFO_FUNCTION(calendar)
{
  php_info_print_table_start();
  php_info_print_table_row(2, "Calendar support", "enabled");
  php_info_print_table_end();
}


/* {{{ proto string jdtogregorian(int juliandaycount)
   Convert a julian day count to a gregorian calendar date */
PHP_FUNCTION(jdtogregorian)
{
	pval **julday;
	int year, month, day;
	char date[10];

	if (zend_get_parameters_ex(1, &julday) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long_ex(julday);
	SdnToGregorian((*julday)->value.lval, &year, &month, &day);
	sprintf(date, "%i/%i/%i", month, day, year);

	RETURN_STRING(date,1);
}
/* }}} */

/* {{{ proto int gregoriantojd(int month, int day, int year)
   Convert a gregorian calendar date to julian day count */
 PHP_FUNCTION(gregoriantojd)
{
	pval **year, **month, **day;
	int jdate;

	if (zend_get_parameters_ex(3, &month, &day, &year) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}
	
	convert_to_long_ex(month);
	convert_to_long_ex(day);
	convert_to_long_ex(year);

	jdate = GregorianToSdn((*year)->value.lval, (*month)->value.lval,(*day)->value.lval);

	RETURN_LONG(jdate);
}
/* }}} */

/* {{{ proto string jdtojulian(int juliandaycount)
   Convert a julian day count to a julian calendar date */
 PHP_FUNCTION(jdtojulian) 
{
	pval **julday;
	int year, month, day;
	char date[10];

	if (zend_get_parameters_ex(1, &julday) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long_ex(julday);
	SdnToJulian((*julday)->value.lval, &year, &month, &day);
	sprintf(date, "%i/%i/%i", month, day, year);

	RETURN_STRING(date,1);
}
/* }}} */


/* {{{ proto int juliantojd(int month, int day, int year)
   Convert a julian calendar date to julian day count */
 PHP_FUNCTION(juliantojd)
{
	pval **year, **month, **day;
	int jdate;

	if (zend_get_parameters_ex(3, &month, &day, &year) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}
	
	convert_to_long_ex(month);
	convert_to_long_ex(day);
	convert_to_long_ex(year);

	jdate = JulianToSdn((*year)->value.lval,(*month)->value.lval, (*day)->value.lval);

	RETURN_LONG(jdate);
}
/* }}} */


/* {{{ proto string jdtojewish(int juliandaycount)
   Convert a julian day count to a jewish calendar date */
 PHP_FUNCTION(jdtojewish) 
{
	pval **julday;
	int year, month, day;
	char date[10];

	if (zend_get_parameters_ex(1, &julday) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}
	
	convert_to_long_ex(julday);
	
	SdnToJewish((*julday)->value.lval, &year, &month, &day);
	sprintf(date, "%i/%i/%i", month, day, year);

	RETURN_STRING(date,1);
}
/* }}} */


/* {{{ proto int jewishtojd(int month, int day, int year)
   Convert a jewish calendar date to a julian day count */
 PHP_FUNCTION(jewishtojd)
{
	pval **year, **month, **day;
	int jdate;

	if (zend_get_parameters_ex(3, &month, &day, &year) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long_ex(month);
	convert_to_long_ex(day);
	convert_to_long_ex(year);

	jdate = JewishToSdn((*year)->value.lval,(*month)->value.lval, (*day)->value.lval);

	RETURN_LONG(jdate);
}
/* }}} */


/* {{{ proto string jdtofrench(int juliandaycount)
   Convert a julian day count to a french republic calendar date */
 PHP_FUNCTION(jdtofrench)
{
	pval **julday;
	int year, month, day;
	char date[10];

	if (zend_get_parameters_ex(1, &julday) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long_ex(julday);
	
	SdnToFrench((*julday)->value.lval, &year, &month, &day);
	sprintf(date, "%i/%i/%i", month, day, year);

	RETURN_STRING(date,1);
}
/* }}} */


/* {{{ proto int frenchtojd(int month, int day, int year)
   Convert a french republic calendar date to julian day count */
 PHP_FUNCTION(frenchtojd)
{
	pval **year, **month, **day;
	int jdate;

	if (zend_get_parameters_ex(3, &month, &day, &year) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}
	
	convert_to_long_ex(month);
	convert_to_long_ex(day);
	convert_to_long_ex(year);

	jdate = FrenchToSdn((*year)->value.lval,(*month)->value.lval,(*day)->value.lval);

	RETURN_LONG(jdate);
}
/* }}} */

/* {{{ proto mixed jddayofweek(int juliandaycount [, int mode])
   Returns name or number of day of week from julian day count */
 PHP_FUNCTION(jddayofweek)
{
	pval *julday, *mode;
	int day;
	char *daynamel, *daynames;
	int myargc=ZEND_NUM_ARGS(),mymode=0;
	
	if ((myargc < 1) || (myargc > 2) || (zend_get_parameters(ht,myargc, &julday, &mode) != SUCCESS)) {
		WRONG_PARAM_COUNT;
	}
	
	convert_to_long(julday);
	if(myargc==2) {
	  convert_to_long(mode);
	  mymode = mode->value.lval;
	} 

	day = DayOfWeek(julday->value.lval);
	daynamel = DayNameLong[day];
	daynames = DayNameShort[day];

		switch (mymode) {
			case 0L:
				RETURN_LONG(day);
				break;
			case 1L:
				RETURN_STRING(daynamel,1);
				break;
			case 2L:
				RETURN_STRING(daynames,1);
				break;
			default:
				RETURN_LONG(day);
				break;
		}
}
/* }}} */


/* {{{ proto string jdmonthname(int juliandaycount, int mode)
   Returns name of month for julian day count */
 PHP_FUNCTION(jdmonthname)
{
	pval **julday, **mode;
	char *monthname = NULL;
	int month, day, year;

	if (zend_get_parameters_ex(2, &julday, &mode) != SUCCESS) {
		WRONG_PARAM_COUNT;
	}
	
	convert_to_long_ex(julday);
	convert_to_long_ex(mode);

		switch((*mode)->value.lval) {
			case 0L:			/* gregorian or julian month */
				SdnToGregorian((*julday)->value.lval,&year, &month, &day);
				monthname = MonthNameShort[month];
				break;
			case 1L:			/* gregorian or julian month */
				SdnToGregorian((*julday)->value.lval,&year, &month, &day);
				monthname = MonthNameLong[month];
				break;
			case 2L:			/* gregorian or julian month */
				SdnToJulian((*julday)->value.lval, &year,&month, &day);
				monthname = MonthNameShort[month];
				break;
			case 3L:			/* gregorian or julian month */
				SdnToJulian((*julday)->value.lval, &year,&month, &day);
				monthname = MonthNameLong[month];
				break;
			case 4L:			/* jewish month */
				SdnToJewish((*julday)->value.lval, &year,&month, &day);
				monthname = JewishMonthName[month];
				break;
			case 5L:			/* french month */
				SdnToFrench((*julday)->value.lval, &year,&month, &day);
				monthname = FrenchMonthName[month];
				break;
			default:			/* default gregorian */
				/* FIXME - need to set monthname to something here ?? */
				break;
		}

	RETURN_STRING(monthname,1);
}
/* }}} */