Coverage Report

Created: 2023-12-06 19:46

/src/libxslt/libexslt/date.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * date.c: Implementation of the EXSLT -- Dates and Times module
3
 *
4
 * References:
5
 *   http://www.exslt.org/date/date.html
6
 *
7
 * See Copyright for the status of this software.
8
 *
9
 * Authors:
10
 *   Charlie Bozeman <cbozeman@HiWAAY.net>
11
 *   Thomas Broyer <tbroyer@ltgt.net>
12
 *
13
 * TODO:
14
 * elements:
15
 *   date-format
16
 * functions:
17
 *   format-date
18
 *   parse-date
19
 *   sum
20
 */
21
22
#define IN_LIBEXSLT
23
#include "libexslt/libexslt.h"
24
25
#if defined(HAVE_LOCALTIME_R) && defined(__GLIBC__) /* _POSIX_SOURCE required by gnu libc */
26
#ifndef _AIX51    /* but on AIX we're not using gnu libc */
27
#define _POSIX_SOURCE
28
#endif
29
#endif
30
31
#include <libxml/tree.h>
32
#include <libxml/xpath.h>
33
#include <libxml/xpathInternals.h>
34
35
#include <libxslt/xsltutils.h>
36
#include <libxslt/xsltInternals.h>
37
#include <libxslt/extensions.h>
38
39
#include "exslt.h"
40
41
#include <string.h>
42
#include <limits.h>
43
#include <errno.h>
44
#include <math.h>
45
46
/* needed to get localtime_r on Solaris */
47
#ifdef __sun
48
#ifndef __EXTENSIONS__
49
#define __EXTENSIONS__
50
#endif
51
#endif
52
53
#include <time.h>
54
55
/*
56
 * types of date and/or time (from schema datatypes)
57
 *   somewhat ordered from least specific to most specific (i.e.
58
 *   most truncated to least truncated).
59
 */
60
typedef enum {
61
    EXSLT_UNKNOWN  =    0,
62
    XS_TIME        =    1,       /* time is left-truncated */
63
    XS_GDAY        = (XS_TIME   << 1),
64
    XS_GMONTH      = (XS_GDAY   << 1),
65
    XS_GMONTHDAY   = (XS_GMONTH | XS_GDAY),
66
    XS_GYEAR       = (XS_GMONTH << 1),
67
    XS_GYEARMONTH  = (XS_GYEAR  | XS_GMONTH),
68
    XS_DATE        = (XS_GYEAR  | XS_GMONTH | XS_GDAY),
69
    XS_DATETIME    = (XS_DATE   | XS_TIME)
70
} exsltDateType;
71
72
/* Date value */
73
typedef struct _exsltDateVal exsltDateVal;
74
typedef exsltDateVal *exsltDateValPtr;
75
struct _exsltDateVal {
76
    exsltDateType type;
77
    long    year;
78
    unsigned int  mon :4; /* 1 <=  mon    <= 12   */
79
    unsigned int  day :5; /* 1 <=  day    <= 31   */
80
    unsigned int  hour  :5; /* 0 <=  hour   <= 23   */
81
    unsigned int  min :6; /* 0 <=  min    <= 59 */
82
    double    sec;
83
    unsigned int  tz_flag :1; /* is tzo explicitely set? */
84
    signed int    tzo :12;  /* -1440 <= tzo <= 1440 currently only -840 to +840 are needed */
85
};
86
87
/* Duration value */
88
typedef struct _exsltDateDurVal exsltDateDurVal;
89
typedef exsltDateDurVal *exsltDateDurValPtr;
90
struct _exsltDateDurVal {
91
    long  mon;  /* mon stores years also */
92
    long  day;
93
    double  sec;  /* sec stores min and hour also
94
         0 <= sec < SECS_PER_DAY */
95
};
96
97
/****************************************************************
98
 *                *
99
 *    Convenience macros and functions    *
100
 *                *
101
 ****************************************************************/
102
103
#define IS_TZO_CHAR(c)            \
104
5.77M
  ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
105
106
568k
#define VALID_ALWAYS(num) (num >= 0)
107
5.39M
#define VALID_MONTH(mon)        ((mon >= 1) && (mon <= 12))
108
/* VALID_DAY should only be used when month is unknown */
109
1.21M
#define VALID_DAY(day)          ((day >= 1) && (day <= 31))
110
6.69M
#define VALID_HOUR(hr)          ((hr >= 0) && (hr <= 23))
111
1.02M
#define VALID_MIN(min)          ((min >= 0) && (min <= 59))
112
2.44M
#define VALID_SEC(sec)          ((sec >= 0) && (sec < 60))
113
1.39M
#define VALID_TZO(tzo)          ((tzo > -1440) && (tzo < 1440))
114
#define IS_LEAP(y)            \
115
122M
  (((y & 3) == 0) && ((y % 25 != 0) || ((y & 15) == 0)))
116
117
static const long daysInMonth[12] =
118
  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
119
static const long daysInMonthLeap[12] =
120
  { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
121
122
#define MAX_DAYINMONTH(yr,mon)                                  \
123
120M
        (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
124
125
#define VALID_MDAY(dt)            \
126
1.74M
  (IS_LEAP(dt->year) ?                \
127
1.74M
      (dt->day <= daysInMonthLeap[dt->mon - 1]) :          \
128
1.74M
      (dt->day <= daysInMonth[dt->mon - 1]))
129
130
#define VALID_DATE(dt)            \
131
2.40M
  (VALID_MONTH(dt->mon) && VALID_MDAY(dt))
132
133
/*
134
    hour and min structure vals are unsigned, so normal macros give
135
    warnings on some compilers.
136
*/
137
#define VALID_TIME(dt)            \
138
1.22M
  ((dt->hour <=23 ) && (dt->min <= 59) &&     \
139
1.22M
   VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
140
141
#define VALID_DATETIME(dt)          \
142
662k
  (VALID_DATE(dt) && VALID_TIME(dt))
143
144
12.1M
#define SECS_PER_MIN            60
145
10.4M
#define MINS_PER_HOUR           60
146
9.55M
#define HOURS_PER_DAY           24
147
9.15M
#define SECS_PER_HOUR           (MINS_PER_HOUR * SECS_PER_MIN)
148
6.64M
#define SECS_PER_DAY            (HOURS_PER_DAY * SECS_PER_HOUR)
149
1.30M
#define MINS_PER_DAY            (HOURS_PER_DAY * MINS_PER_HOUR)
150
534k
#define DAYS_PER_EPOCH          (400 * 365 + 100 - 4 + 1)
151
267k
#define YEARS_PER_EPOCH         400
152
153
static const long dayInYearByMonth[12] =
154
  { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
155
static const long dayInLeapYearByMonth[12] =
156
  { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
157
158
#define DAY_IN_YEAR(day, month, year)       \
159
578k
        ((IS_LEAP(year) ?          \
160
578k
                dayInLeapYearByMonth[month - 1] :    \
161
578k
                dayInYearByMonth[month - 1]) + day)
162
163
28.2M
#define YEAR_MAX LONG_MAX
164
5.73M
#define YEAR_MIN (-LONG_MAX + 1)
165
166
/**
167
 * _exsltDateParseGYear:
168
 * @dt:  pointer to a date structure
169
 * @str: pointer to the string to analyze
170
 *
171
 * Parses a xs:gYear without time zone and fills in the appropriate
172
 * field of the @dt structure. @str is updated to point just after the
173
 * xs:gYear. It is supposed that @dt->year is big enough to contain
174
 * the year.
175
 *
176
 * According to XML Schema Part 2, the year "0000" is an illegal year value
177
 * which probably means that the year preceding AD 1 is BC 1. Internally,
178
 * we allow a year 0 and adjust the value when parsing and formatting.
179
 *
180
 * Returns 0 or the error code
181
 */
182
static int
183
_exsltDateParseGYear (exsltDateValPtr dt, const xmlChar **str)
184
3.64M
{
185
3.64M
    const xmlChar *cur = *str, *firstChar;
186
3.64M
    int isneg = 0, digcnt = 0;
187
188
3.64M
    if (((*cur < '0') || (*cur > '9')) &&
189
3.64M
  (*cur != '-') && (*cur != '+'))
190
374k
  return -1;
191
192
3.27M
    if (*cur == '-') {
193
202k
  isneg = 1;
194
202k
  cur++;
195
202k
    }
196
197
3.27M
    firstChar = cur;
198
199
29.2M
    while ((*cur >= '0') && (*cur <= '9')) {
200
25.9M
        if (dt->year >= YEAR_MAX / 10) /* Not really exact */
201
13.2k
            return -1;
202
25.9M
  dt->year = dt->year * 10 + (*cur - '0');
203
25.9M
  cur++;
204
25.9M
  digcnt++;
205
25.9M
    }
206
207
    /* year must be at least 4 digits (CCYY); over 4
208
     * digits cannot have a leading zero. */
209
3.26M
    if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
210
426k
  return 1;
211
212
2.83M
    if (dt->year == 0)
213
6.30k
  return 2;
214
215
    /* The internal representation of negative years is continuous. */
216
2.82M
    if (isneg)
217
157k
  dt->year = -dt->year + 1;
218
219
2.82M
    *str = cur;
220
221
#ifdef DEBUG_EXSLT_DATE
222
    xsltGenericDebug(xsltGenericDebugContext,
223
         "Parsed year %04ld\n", dt->year);
224
#endif
225
226
2.82M
    return 0;
227
2.83M
}
228
229
/**
230
 * FORMAT_GYEAR:
231
 * @yr:  the year to format
232
 * @cur: a pointer to an allocated buffer
233
 *
234
 * Formats @yr in xsl:gYear format. Result is appended to @cur and
235
 * @cur is updated to point after the xsl:gYear.
236
 */
237
#define FORMAT_GYEAR(yr, cur)         \
238
344k
  if (yr <= 0) {                 \
239
29.5k
      *cur = '-';           \
240
29.5k
      cur++;            \
241
29.5k
  }              \
242
344k
  {             \
243
344k
      long year = (yr <= 0) ? -yr + 1 : yr;               \
244
344k
      xmlChar tmp_buf[100], *tmp = tmp_buf;   \
245
344k
      /* result is in reverse-order */      \
246
3.16M
      while (year > 0) {         \
247
2.81M
    *tmp = '0' + (xmlChar)(year % 10);    \
248
2.81M
    year /= 10;         \
249
2.81M
    tmp++;            \
250
2.81M
      }              \
251
344k
      /* virtually adds leading zeros */      \
252
370k
      while ((tmp - tmp_buf) < 4)       \
253
344k
    *tmp++ = '0';         \
254
344k
      /* restore the correct order */     \
255
3.18M
      while (tmp > tmp_buf) {       \
256
2.84M
    tmp--;            \
257
2.84M
    *cur = *tmp;          \
258
2.84M
    cur++;            \
259
2.84M
      }              \
260
344k
  }
261
262
/**
263
 * PARSE_2_DIGITS:
264
 * @num:  the integer to fill in
265
 * @cur:  an #xmlChar *
266
 * @func: validation function for the number
267
 * @invalid: an integer
268
 *
269
 * Parses a 2-digits integer and updates @num with the value. @cur is
270
 * updated to point just after the integer.
271
 * In case of error, @invalid is set to %TRUE, values of @num and
272
 * @cur are undefined.
273
 */
274
#define PARSE_2_DIGITS(num, cur, func, invalid)     \
275
12.7M
  if ((cur[0] < '0') || (cur[0] > '9') ||     \
276
12.7M
      (cur[1] < '0') || (cur[1] > '9'))     \
277
12.7M
      invalid = 1;         \
278
12.7M
  else {             \
279
11.4M
      int val;            \
280
11.4M
      val = (cur[0] - '0') * 10 + (cur[1] - '0');   \
281
11.4M
      if (!func(val))          \
282
11.4M
          invalid = 2;         \
283
11.4M
      else            \
284
11.4M
          num = val;         \
285
11.4M
  }              \
286
12.7M
  cur += 2;
287
288
/**
289
 * FORMAT_2_DIGITS:
290
 * @num:  the integer to format
291
 * @cur: a pointer to an allocated buffer
292
 *
293
 * Formats a 2-digits integer. Result is appended to @cur and
294
 * @cur is updated to point after the integer.
295
 */
296
#define FORMAT_2_DIGITS(num, cur)       \
297
773k
  *cur = '0' + ((num / 10) % 10);       \
298
773k
  cur++;              \
299
773k
  *cur = '0' + (num % 10);        \
300
773k
  cur++;
301
302
/**
303
 * PARSE_FLOAT:
304
 * @num:  the double to fill in
305
 * @cur:  an #xmlChar *
306
 * @invalid: an integer
307
 *
308
 * Parses a float and updates @num with the value. @cur is
309
 * updated to point just after the float. The float must have a
310
 * 2-digits integer part and may or may not have a decimal part.
311
 * In case of error, @invalid is set to %TRUE, values of @num and
312
 * @cur are undefined.
313
 */
314
#define PARSE_FLOAT(num, cur, invalid)        \
315
666k
  PARSE_2_DIGITS(num, cur, VALID_ALWAYS, invalid); \
316
666k
  if (!invalid && (*cur == '.')) {     \
317
37.3k
      double mult = 1;                \
318
37.3k
      cur++;            \
319
37.3k
      if ((*cur < '0') || (*cur > '9'))     \
320
37.3k
    invalid = 1;         \
321
190k
      while ((*cur >= '0') && (*cur <= '9')) {   \
322
153k
    mult /= 10;         \
323
153k
    num += (*cur - '0') * mult;     \
324
153k
    cur++;            \
325
153k
      }              \
326
37.3k
  }
327
328
/**
329
 * _exsltDateParseGMonth:
330
 * @dt:  pointer to a date structure
331
 * @str: pointer to the string to analyze
332
 *
333
 * Parses a xs:gMonth without time zone and fills in the appropriate
334
 * field of the @dt structure. @str is updated to point just after the
335
 * xs:gMonth.
336
 *
337
 * Returns 0 or the error code
338
 */
339
static int
340
_exsltDateParseGMonth (exsltDateValPtr dt, const xmlChar **str)
341
2.24M
{
342
2.24M
    const xmlChar *cur = *str;
343
2.24M
    int ret = 0;
344
345
2.24M
    PARSE_2_DIGITS(dt->mon, cur, VALID_MONTH, ret);
346
2.24M
    if (ret != 0)
347
480k
  return ret;
348
349
1.75M
    *str = cur;
350
351
#ifdef DEBUG_EXSLT_DATE
352
    xsltGenericDebug(xsltGenericDebugContext,
353
         "Parsed month %02i\n", dt->mon);
354
#endif
355
356
1.75M
    return 0;
357
2.24M
}
358
359
/**
360
 * FORMAT_GMONTH:
361
 * @mon:  the month to format
362
 * @cur: a pointer to an allocated buffer
363
 *
364
 * Formats @mon in xsl:gMonth format. Result is appended to @cur and
365
 * @cur is updated to point after the xsl:gMonth.
366
 */
367
#define FORMAT_GMONTH(mon, cur)         \
368
324k
  FORMAT_2_DIGITS(mon, cur)
369
370
/**
371
 * _exsltDateParseGDay:
372
 * @dt:  pointer to a date structure
373
 * @str: pointer to the string to analyze
374
 *
375
 * Parses a xs:gDay without time zone and fills in the appropriate
376
 * field of the @dt structure. @str is updated to point just after the
377
 * xs:gDay.
378
 *
379
 * Returns 0 or the error code
380
 */
381
static int
382
_exsltDateParseGDay (exsltDateValPtr dt, const xmlChar **str)
383
1.33M
{
384
1.33M
    const xmlChar *cur = *str;
385
1.33M
    int ret = 0;
386
387
1.33M
    PARSE_2_DIGITS(dt->day, cur, VALID_DAY, ret);
388
1.33M
    if (ret != 0)
389
211k
  return ret;
390
391
1.12M
    *str = cur;
392
393
#ifdef DEBUG_EXSLT_DATE
394
    xsltGenericDebug(xsltGenericDebugContext,
395
         "Parsed day %02i\n", dt->day);
396
#endif
397
398
1.12M
    return 0;
399
1.33M
}
400
401
/**
402
 * FORMAT_GDAY:
403
 * @dt:  the #exsltDateVal to format
404
 * @cur: a pointer to an allocated buffer
405
 *
406
 * Formats @dt in xsl:gDay format. Result is appended to @cur and
407
 * @cur is updated to point after the xsl:gDay.
408
 */
409
#define FORMAT_GDAY(dt, cur)          \
410
281k
  FORMAT_2_DIGITS(dt->day, cur)
411
412
/**
413
 * FORMAT_DATE:
414
 * @dt:  the #exsltDateVal to format
415
 * @cur: a pointer to an allocated buffer
416
 *
417
 * Formats @dt in xsl:date format. Result is appended to @cur and
418
 * @cur is updated to point after the xsl:date.
419
 */
420
#define FORMAT_DATE(dt, cur)          \
421
281k
  FORMAT_GYEAR(dt->year, cur);        \
422
281k
  *cur = '-';           \
423
281k
  cur++;              \
424
281k
  FORMAT_GMONTH(dt->mon, cur);       \
425
281k
  *cur = '-';           \
426
281k
  cur++;              \
427
281k
  FORMAT_GDAY(dt, cur);
428
429
/**
430
 * _exsltDateParseTime:
431
 * @dt:  pointer to a date structure
432
 * @str: pointer to the string to analyze
433
 *
434
 * Parses a xs:time without time zone and fills in the appropriate
435
 * fields of the @dt structure. @str is updated to point just after the
436
 * xs:time.
437
 * In case of error, values of @dt fields are undefined.
438
 *
439
 * Returns 0 or the error code
440
 */
441
static int
442
_exsltDateParseTime (exsltDateValPtr dt, const xmlChar **str)
443
3.77M
{
444
3.77M
    const xmlChar *cur = *str;
445
3.77M
    unsigned int hour = 0; /* use temp var in case str is not xs:time */
446
3.77M
    int ret = 0;
447
448
3.77M
    PARSE_2_DIGITS(hour, cur, VALID_HOUR, ret);
449
3.77M
    if (ret != 0)
450
394k
  return ret;
451
452
3.38M
    if (*cur != ':')
453
2.52M
  return 1;
454
855k
    cur++;
455
456
    /* the ':' insures this string is xs:time */
457
855k
    dt->hour = hour;
458
459
855k
    PARSE_2_DIGITS(dt->min, cur, VALID_MIN, ret);
460
855k
    if (ret != 0)
461
67.1k
  return ret;
462
463
788k
    if (*cur != ':')
464
122k
  return 1;
465
666k
    cur++;
466
467
666k
    PARSE_FLOAT(dt->sec, cur, ret);
468
666k
    if (ret != 0)
469
114k
  return ret;
470
471
551k
    if (!VALID_TIME(dt))
472
9.27k
  return 2;
473
474
542k
    *str = cur;
475
476
#ifdef DEBUG_EXSLT_DATE
477
    xsltGenericDebug(xsltGenericDebugContext,
478
         "Parsed time %02i:%02i:%02.f\n",
479
         dt->hour, dt->min, dt->sec);
480
#endif
481
482
542k
    return 0;
483
551k
}
484
485
/**
486
 * _exsltDateParseTimeZone:
487
 * @dt:  pointer to a date structure
488
 * @str: pointer to the string to analyze
489
 *
490
 * Parses a time zone without time zone and fills in the appropriate
491
 * field of the @dt structure. @str is updated to point just after the
492
 * time zone.
493
 *
494
 * Returns 0 or the error code
495
 */
496
static int
497
_exsltDateParseTimeZone (exsltDateValPtr dt, const xmlChar **str)
498
4.94M
{
499
4.94M
    const xmlChar *cur;
500
4.94M
    int ret = 0;
501
502
4.94M
    if (str == NULL)
503
0
  return -1;
504
4.94M
    cur = *str;
505
4.94M
    switch (*cur) {
506
1.19M
    case 0:
507
1.19M
  dt->tz_flag = 0;
508
1.19M
  dt->tzo = 0;
509
1.19M
  break;
510
511
108k
    case 'Z':
512
108k
  dt->tz_flag = 1;
513
108k
  dt->tzo = 0;
514
108k
  cur++;
515
108k
  break;
516
517
226k
    case '+':
518
3.60M
    case '-': {
519
3.60M
  int isneg = 0, tmp = 0;
520
3.60M
  isneg = (*cur == '-');
521
522
3.60M
  cur++;
523
524
3.60M
  PARSE_2_DIGITS(tmp, cur, VALID_HOUR, ret);
525
3.60M
  if (ret != 0)
526
575k
      return ret;
527
528
3.03M
  if (*cur != ':')
529
2.76M
      return 1;
530
262k
  cur++;
531
532
262k
  dt->tzo = tmp * 60;
533
534
262k
  PARSE_2_DIGITS(tmp, cur, VALID_MIN, ret);
535
262k
  if (ret != 0)
536
83.7k
      return ret;
537
538
179k
  dt->tzo += tmp;
539
179k
  if (isneg)
540
132k
      dt->tzo = - dt->tzo;
541
542
179k
  if (!VALID_TZO(dt->tzo))
543
0
      return 2;
544
545
179k
  break;
546
179k
      }
547
179k
    default:
548
29.0k
  return 1;
549
4.94M
    }
550
551
1.48M
    *str = cur;
552
553
#ifdef DEBUG_EXSLT_DATE
554
    xsltGenericDebug(xsltGenericDebugContext,
555
         "Parsed time zone offset (%s) %i\n",
556
         dt->tz_flag ? "explicit" : "implicit", dt->tzo);
557
#endif
558
559
1.48M
    return 0;
560
4.94M
}
561
562
/**
563
 * FORMAT_TZ:
564
 * @tzo:  the timezone offset to format
565
 * @cur: a pointer to an allocated buffer
566
 *
567
 * Formats @tzo timezone. Result is appended to @cur and
568
 * @cur is updated to point after the timezone.
569
 */
570
#define FORMAT_TZ(tzo, cur)         \
571
208k
  if (tzo == 0) {                 \
572
125k
      *cur = 'Z';           \
573
125k
      cur++;            \
574
125k
  } else {           \
575
83.8k
      int aTzo = (tzo < 0) ? - tzo : tzo;                 \
576
83.8k
      int tzHh = aTzo / 60, tzMm = aTzo % 60;   \
577
83.8k
      *cur = (tzo < 0) ? '-' : '+' ;      \
578
83.8k
      cur++;            \
579
83.8k
      FORMAT_2_DIGITS(tzHh, cur);        \
580
83.8k
      *cur = ':';           \
581
83.8k
      cur++;            \
582
83.8k
      FORMAT_2_DIGITS(tzMm, cur);        \
583
83.8k
  }
584
585
/****************************************************************
586
 *                *
587
 *  XML Schema Dates/Times Datatypes Handling   *
588
 *                *
589
 ****************************************************************/
590
591
/**
592
 * exsltDateCreateDate:
593
 * @type:       type to create
594
 *
595
 * Creates a new #exsltDateVal, uninitialized.
596
 *
597
 * Returns the #exsltDateValPtr
598
 */
599
static exsltDateValPtr
600
exsltDateCreateDate (exsltDateType type)
601
4.69M
{
602
4.69M
    exsltDateValPtr ret;
603
604
4.69M
    ret = (exsltDateValPtr) xmlMalloc(sizeof(exsltDateVal));
605
4.69M
    if (ret == NULL) {
606
0
  xsltGenericError(xsltGenericErrorContext,
607
0
       "exsltDateCreateDate: out of memory\n");
608
0
  return (NULL);
609
0
    }
610
4.69M
    memset (ret, 0, sizeof(exsltDateVal));
611
612
4.69M
    ret->mon = 1;
613
4.69M
    ret->day = 1;
614
615
4.69M
    if (type != EXSLT_UNKNOWN)
616
674k
        ret->type = type;
617
618
4.69M
    return ret;
619
4.69M
}
620
621
/**
622
 * exsltDateFreeDate:
623
 * @date: an #exsltDateValPtr
624
 *
625
 * Frees up the @date
626
 */
627
static void
628
4.69M
exsltDateFreeDate (exsltDateValPtr date) {
629
4.69M
    if (date == NULL)
630
0
  return;
631
632
4.69M
    xmlFree(date);
633
4.69M
}
634
635
/**
636
 * exsltDateCreateDuration:
637
 *
638
 * Creates a new #exsltDateDurVal, uninitialized.
639
 *
640
 * Returns the #exsltDateDurValPtr
641
 */
642
static exsltDateDurValPtr
643
exsltDateCreateDuration (void)
644
2.95M
{
645
2.95M
    exsltDateDurValPtr ret;
646
647
2.95M
    ret = (exsltDateDurValPtr) xmlMalloc(sizeof(exsltDateDurVal));
648
2.95M
    if (ret == NULL) {
649
0
  xsltGenericError(xsltGenericErrorContext,
650
0
       "exsltDateCreateDuration: out of memory\n");
651
0
  return (NULL);
652
0
    }
653
2.95M
    memset (ret, 0, sizeof(exsltDateDurVal));
654
655
2.95M
    return ret;
656
2.95M
}
657
658
/**
659
 * exsltDateFreeDuration:
660
 * @date: an #exsltDateDurValPtr
661
 *
662
 * Frees up the @duration
663
 */
664
static void
665
2.95M
exsltDateFreeDuration (exsltDateDurValPtr duration) {
666
2.95M
    if (duration == NULL)
667
0
  return;
668
669
2.95M
    xmlFree(duration);
670
2.95M
}
671
672
/**
673
 * exsltDateCurrent:
674
 *
675
 * Returns the current date and time.
676
 */
677
static exsltDateValPtr
678
exsltDateCurrent (void)
679
173k
{
680
173k
    struct tm localTm, gmTm;
681
#if !defined(HAVE_GMTIME_R) && !defined(_WIN32)
682
    struct tm *tb = NULL;
683
#endif
684
173k
    time_t secs;
685
173k
    int local_s, gm_s;
686
173k
    exsltDateValPtr ret;
687
173k
    char *source_date_epoch;
688
173k
    int override = 0;
689
690
173k
    ret = exsltDateCreateDate(XS_DATETIME);
691
173k
    if (ret == NULL)
692
0
        return NULL;
693
694
    /*
695
     * Allow the date and time to be set externally by an exported
696
     * environment variable to enable reproducible builds.
697
     */
698
173k
    source_date_epoch = getenv("SOURCE_DATE_EPOCH");
699
173k
    if (source_date_epoch) {
700
0
        errno = 0;
701
0
  secs = (time_t) strtol (source_date_epoch, NULL, 10);
702
0
  if (errno == 0) {
703
#ifdef _WIN32
704
      struct tm *gm = gmtime_s(&localTm, &secs) ? NULL : &localTm;
705
      if (gm != NULL)
706
          override = 1;
707
#elif HAVE_GMTIME_R
708
0
      if (gmtime_r(&secs, &localTm) != NULL)
709
0
          override = 1;
710
#else
711
      tb = gmtime(&secs);
712
      if (tb != NULL) {
713
          localTm = *tb;
714
    override = 1;
715
      }
716
#endif
717
0
        }
718
0
    }
719
720
173k
    if (override == 0) {
721
    /* get current time */
722
173k
  secs    = time(NULL);
723
724
#ifdef _WIN32
725
  localtime_s(&localTm, &secs);
726
#elif HAVE_LOCALTIME_R
727
173k
  localtime_r(&secs, &localTm);
728
#else
729
  localTm = *localtime(&secs);
730
#endif
731
173k
    }
732
733
    /* get real year, not years since 1900 */
734
173k
    ret->year = localTm.tm_year + 1900;
735
736
173k
    ret->mon  = localTm.tm_mon + 1;
737
173k
    ret->day  = localTm.tm_mday;
738
173k
    ret->hour = localTm.tm_hour;
739
173k
    ret->min  = localTm.tm_min;
740
741
    /* floating point seconds */
742
173k
    ret->sec  = (double) localTm.tm_sec;
743
744
    /* determine the time zone offset from local to gm time */
745
#ifdef _WIN32
746
    gmtime_s(&gmTm, &secs);
747
#elif HAVE_GMTIME_R
748
173k
    gmtime_r(&secs, &gmTm);
749
#else
750
    tb = gmtime(&secs);
751
    if (tb == NULL)
752
        return NULL;
753
    gmTm = *tb;
754
#endif
755
173k
    ret->tz_flag = 0;
756
#if 0
757
    ret->tzo = (((ret->day * 1440) +
758
                 (ret->hour * 60) +
759
                  ret->min) -
760
                ((gmTm.tm_mday * 1440) + (gmTm.tm_hour * 60) +
761
                  gmTm.tm_min));
762
#endif
763
173k
    local_s = localTm.tm_hour * SECS_PER_HOUR +
764
173k
        localTm.tm_min * SECS_PER_MIN +
765
173k
        localTm.tm_sec;
766
767
173k
    gm_s = gmTm.tm_hour * SECS_PER_HOUR +
768
173k
        gmTm.tm_min * SECS_PER_MIN +
769
173k
        gmTm.tm_sec;
770
771
173k
    if (localTm.tm_year < gmTm.tm_year) {
772
0
  ret->tzo = -((SECS_PER_DAY - local_s) + gm_s)/60;
773
173k
    } else if (localTm.tm_year > gmTm.tm_year) {
774
0
  ret->tzo = ((SECS_PER_DAY - gm_s) + local_s)/60;
775
173k
    } else if (localTm.tm_mon < gmTm.tm_mon) {
776
0
  ret->tzo = -((SECS_PER_DAY - local_s) + gm_s)/60;
777
173k
    } else if (localTm.tm_mon > gmTm.tm_mon) {
778
0
  ret->tzo = ((SECS_PER_DAY - gm_s) + local_s)/60;
779
173k
    } else if (localTm.tm_mday < gmTm.tm_mday) {
780
0
  ret->tzo = -((SECS_PER_DAY - local_s) + gm_s)/60;
781
173k
    } else if (localTm.tm_mday > gmTm.tm_mday) {
782
0
  ret->tzo = ((SECS_PER_DAY - gm_s) + local_s)/60;
783
173k
    } else  {
784
173k
  ret->tzo = (local_s - gm_s)/60;
785
173k
    }
786
787
173k
    return ret;
788
173k
}
789
790
/**
791
 * exsltDateParse:
792
 * @dateTime:  string to analyze
793
 *
794
 * Parses a date/time string
795
 *
796
 * Returns a newly built #exsltDateValPtr of NULL in case of error
797
 */
798
static exsltDateValPtr
799
exsltDateParse (const xmlChar *dateTime)
800
4.02M
{
801
4.02M
    exsltDateValPtr dt;
802
4.02M
    int ret;
803
4.02M
    const xmlChar *cur = dateTime;
804
805
4.02M
#define RETURN_TYPE_IF_VALID(t)         \
806
5.77M
    if (IS_TZO_CHAR(*cur)) {         \
807
4.52M
  ret = _exsltDateParseTimeZone(dt, &cur);    \
808
4.52M
  if (ret == 0) {           \
809
1.10M
      if (*cur != 0)         \
810
1.10M
    goto error;         \
811
1.10M
      dt->type = t;         \
812
1.01M
      return dt;           \
813
1.10M
  }              \
814
4.52M
    }
815
816
4.02M
    if (dateTime == NULL)
817
0
  return NULL;
818
819
4.02M
    if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
820
0
  return NULL;
821
822
4.02M
    dt = exsltDateCreateDate(EXSLT_UNKNOWN);
823
4.02M
    if (dt == NULL)
824
0
  return NULL;
825
826
4.02M
    if ((cur[0] == '-') && (cur[1] == '-')) {
827
  /*
828
   * It's an incomplete date (xs:gMonthDay, xs:gMonth or
829
   * xs:gDay)
830
   */
831
342k
  cur += 2;
832
833
  /* is it an xs:gDay? */
834
342k
  if (*cur == '-') {
835
98.5k
    ++cur;
836
98.5k
      ret = _exsltDateParseGDay(dt, &cur);
837
98.5k
      if (ret != 0)
838
70.1k
    goto error;
839
840
28.4k
      RETURN_TYPE_IF_VALID(XS_GDAY);
841
842
14.7k
      goto error;
843
28.4k
  }
844
845
  /*
846
   * it should be an xs:gMonthDay or xs:gMonth
847
   */
848
244k
  ret = _exsltDateParseGMonth(dt, &cur);
849
244k
  if (ret != 0)
850
74.9k
      goto error;
851
852
169k
  if (*cur != '-')
853
33.7k
      goto error;
854
135k
  cur++;
855
856
  /* is it an xs:gMonth? */
857
135k
  if (*cur == '-') {
858
105k
      cur++;
859
105k
      RETURN_TYPE_IF_VALID(XS_GMONTH);
860
35.2k
      goto error;
861
105k
  }
862
863
  /* it should be an xs:gMonthDay */
864
30.0k
  ret = _exsltDateParseGDay(dt, &cur);
865
30.0k
  if (ret != 0)
866
13.1k
      goto error;
867
868
16.8k
  RETURN_TYPE_IF_VALID(XS_GMONTHDAY);
869
870
11.7k
  goto error;
871
16.8k
    }
872
873
    /*
874
     * It's a right-truncated date or an xs:time.
875
     * Try to parse an xs:time then fallback on right-truncated dates.
876
     */
877
3.68M
    if ((*cur >= '0') && (*cur <= '9')) {
878
3.08M
  ret = _exsltDateParseTime(dt, &cur);
879
3.08M
  if (ret == 0) {
880
      /* it's an xs:time */
881
126k
      RETURN_TYPE_IF_VALID(XS_TIME);
882
95.1k
  }
883
3.08M
    }
884
885
    /* fallback on date parsing */
886
3.64M
    cur = dateTime;
887
888
3.64M
    ret = _exsltDateParseGYear(dt, &cur);
889
3.64M
    if (ret != 0)
890
820k
  goto error;
891
892
    /* is it an xs:gYear? */
893
2.82M
    RETURN_TYPE_IF_VALID(XS_GYEAR);
894
895
2.26M
    if (*cur != '-')
896
271k
  goto error;
897
1.99M
    cur++;
898
899
1.99M
    ret = _exsltDateParseGMonth(dt, &cur);
900
1.99M
    if (ret != 0)
901
405k
  goto error;
902
903
    /* is it an xs:gYearMonth? */
904
1.59M
    RETURN_TYPE_IF_VALID(XS_GYEARMONTH);
905
906
1.34M
    if (*cur != '-')
907
131k
  goto error;
908
1.20M
    cur++;
909
910
1.20M
    ret = _exsltDateParseGDay(dt, &cur);
911
1.20M
    if ((ret != 0) || !VALID_DATE(dt))
912
133k
  goto error;
913
914
    /* is it an xs:date? */
915
1.07M
    RETURN_TYPE_IF_VALID(XS_DATE);
916
917
903k
    if (*cur != 'T')
918
209k
  goto error;
919
693k
    cur++;
920
921
    /* it should be an xs:dateTime */
922
693k
    ret = _exsltDateParseTime(dt, &cur);
923
693k
    if (ret != 0)
924
278k
  goto error;
925
926
415k
    ret = _exsltDateParseTimeZone(dt, &cur);
927
415k
    if ((ret != 0) || (*cur != 0) || !VALID_DATETIME(dt))
928
33.8k
  goto error;
929
930
381k
    dt->type = XS_DATETIME;
931
932
381k
    return dt;
933
934
2.63M
error:
935
2.63M
    if (dt != NULL)
936
2.63M
  exsltDateFreeDate(dt);
937
2.63M
    return NULL;
938
415k
}
939
940
/**
941
 * exsltDateParseDuration:
942
 * @duration:  string to analyze
943
 *
944
 * Parses a duration string
945
 *
946
 * Returns a newly built #exsltDateDurValPtr of NULL in case of error
947
 */
948
static exsltDateDurValPtr
949
exsltDateParseDuration (const xmlChar *duration)
950
2.01M
{
951
2.01M
    const xmlChar  *cur = duration;
952
2.01M
    exsltDateDurValPtr dur;
953
2.01M
    int isneg = 0;
954
2.01M
    unsigned int seq = 0;
955
2.01M
    long days, secs = 0;
956
2.01M
    double sec_frac = 0.0;
957
958
2.01M
    if (duration == NULL)
959
0
  return NULL;
960
961
2.01M
    if (*cur == '-') {
962
880k
        isneg = 1;
963
880k
        cur++;
964
880k
    }
965
966
    /* duration must start with 'P' (after sign) */
967
2.01M
    if (*cur++ != 'P')
968
355k
  return NULL;
969
970
1.66M
    if (*cur == 0)
971
45.8k
  return NULL;
972
973
1.61M
    dur = exsltDateCreateDuration();
974
1.61M
    if (dur == NULL)
975
0
  return NULL;
976
977
7.56M
    while (*cur != 0) {
978
6.54M
        long           num = 0;
979
6.54M
        size_t         has_digits = 0;
980
6.54M
        int            has_frac = 0;
981
6.54M
        const xmlChar  desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
982
983
        /* input string should be empty or invalid date/time item */
984
6.54M
        if (seq >= sizeof(desig))
985
21.1k
            goto error;
986
987
        /* T designator must be present for time items */
988
6.51M
        if (*cur == 'T') {
989
1.09M
            if (seq > 3)
990
4.21k
                goto error;
991
1.08M
            cur++;
992
1.08M
            seq = 3;
993
5.42M
        } else if (seq == 3)
994
52.7k
            goto error;
995
996
        /* Parse integral part. */
997
19.1M
        while (*cur >= '0' && *cur <= '9') {
998
12.6M
            long digit = *cur - '0';
999
1000
12.6M
            if (num > LONG_MAX / 10)
1001
22.2k
                goto error;
1002
12.6M
            num *= 10;
1003
12.6M
            if (num > LONG_MAX - digit)
1004
0
                goto error;
1005
12.6M
            num += digit;
1006
1007
12.6M
            has_digits = 1;
1008
12.6M
            cur++;
1009
12.6M
        }
1010
1011
6.43M
        if (*cur == '.') {
1012
            /* Parse fractional part. */
1013
179k
            double mult = 1.0;
1014
179k
            cur++;
1015
179k
            has_frac = 1;
1016
859k
            while (*cur >= '0' && *cur <= '9') {
1017
679k
                mult /= 10.0;
1018
679k
                sec_frac += (*cur - '0') * mult;
1019
679k
                has_digits = 1;
1020
679k
                cur++;
1021
679k
            }
1022
179k
        }
1023
1024
7.85M
        while (*cur != desig[seq]) {
1025
1.85M
            seq++;
1026
            /* No T designator or invalid char. */
1027
1.85M
            if (seq == 3 || seq == sizeof(desig))
1028
446k
                goto error;
1029
1.85M
        }
1030
5.99M
        cur++;
1031
1032
5.99M
        if (!has_digits || (has_frac && (seq != 5)))
1033
42.6k
            goto error;
1034
1035
5.95M
        switch (seq) {
1036
1.23M
            case 0:
1037
                /* Year */
1038
1.23M
                if (num > LONG_MAX / 12)
1039
1.54k
                    goto error;
1040
1.23M
                dur->mon = num * 12;
1041
1.23M
                break;
1042
1.18M
            case 1:
1043
                /* Month */
1044
1.18M
                if (dur->mon > LONG_MAX - num)
1045
0
                    goto error;
1046
1.18M
                dur->mon += num;
1047
1.18M
                break;
1048
1.19M
            case 2:
1049
                /* Day */
1050
1.19M
                dur->day = num;
1051
1.19M
                break;
1052
800k
            case 3:
1053
                /* Hour */
1054
800k
                days = num / HOURS_PER_DAY;
1055
800k
                if (dur->day > LONG_MAX - days)
1056
0
                    goto error;
1057
800k
                dur->day += days;
1058
800k
                secs = (num % HOURS_PER_DAY) * SECS_PER_HOUR;
1059
800k
                break;
1060
651k
            case 4:
1061
                /* Minute */
1062
651k
                days = num / MINS_PER_DAY;
1063
651k
                if (dur->day > LONG_MAX - days)
1064
0
                    goto error;
1065
651k
                dur->day += days;
1066
651k
                secs += (num % MINS_PER_DAY) * SECS_PER_MIN;
1067
651k
                break;
1068
883k
            case 5:
1069
                /* Second */
1070
883k
                days = num / SECS_PER_DAY;
1071
883k
                if (dur->day > LONG_MAX - days)
1072
0
                    goto error;
1073
883k
                dur->day += days;
1074
883k
                secs += num % SECS_PER_DAY;
1075
883k
                break;
1076
5.95M
        }
1077
1078
5.94M
        seq++;
1079
5.94M
    }
1080
1081
1.02M
    days = secs / SECS_PER_DAY;
1082
1.02M
    if (dur->day > LONG_MAX - days)
1083
0
        goto error;
1084
1.02M
    dur->day += days;
1085
1.02M
    dur->sec = (secs % SECS_PER_DAY) + sec_frac;
1086
1087
1.02M
    if (isneg) {
1088
587k
        dur->mon = -dur->mon;
1089
587k
        dur->day = -dur->day;
1090
587k
        if (dur->sec != 0.0) {
1091
505k
            dur->sec = SECS_PER_DAY - dur->sec;
1092
505k
            dur->day -= 1;
1093
505k
        }
1094
587k
    }
1095
1096
#ifdef DEBUG_EXSLT_DATE
1097
    xsltGenericDebug(xsltGenericDebugContext,
1098
         "Parsed duration %f\n", dur->sec);
1099
#endif
1100
1101
1.02M
    return dur;
1102
1103
590k
error:
1104
590k
    if (dur != NULL)
1105
590k
  exsltDateFreeDuration(dur);
1106
590k
    return NULL;
1107
1.02M
}
1108
1109
static void
1110
1.40M
exsltFormatLong(xmlChar **cur, xmlChar *end, long num) {
1111
1.40M
    xmlChar buf[20];
1112
1.40M
    int i = 0;
1113
1114
3.22M
    while (i < 20) {
1115
3.22M
        buf[i++] = '0' + num % 10;
1116
3.22M
        num /= 10;
1117
3.22M
        if (num == 0)
1118
1.40M
            break;
1119
3.22M
    }
1120
1121
4.62M
    while (i > 0) {
1122
3.22M
        if (*cur < end)
1123
3.22M
            *(*cur)++ = buf[--i];
1124
3.22M
    }
1125
1.40M
}
1126
1127
static void
1128
532k
exsltFormatNanoseconds(xmlChar **cur, xmlChar *end, long nsecs) {
1129
532k
    long p10, digit;
1130
1131
532k
    if (nsecs > 0) {
1132
100k
        if (*cur < end)
1133
100k
            *(*cur)++ = '.';
1134
100k
        p10 = 100000000;
1135
382k
        while (nsecs > 0) {
1136
281k
            digit = nsecs / p10;
1137
281k
            if (*cur < end)
1138
281k
                *(*cur)++ = '0' + digit;
1139
281k
            nsecs -= digit * p10;
1140
281k
            p10 /= 10;
1141
281k
        }
1142
100k
    }
1143
532k
}
1144
1145
/**
1146
 * exsltDateFormatDuration:
1147
 * @dur: an #exsltDateDurValPtr
1148
 *
1149
 * Formats the duration.
1150
 *
1151
 * Returns a newly allocated string, or NULL in case of error
1152
 */
1153
static xmlChar *
1154
exsltDateFormatDuration (const exsltDateDurValPtr dur)
1155
655k
{
1156
655k
    xmlChar buf[100], *cur = buf, *end = buf + 99;
1157
655k
    double secs, tmp;
1158
655k
    long days, months, intSecs, nsecs;
1159
1160
655k
    if (dur == NULL)
1161
0
  return NULL;
1162
1163
    /* quick and dirty check */
1164
655k
    if ((dur->sec == 0.0) && (dur->day == 0) && (dur->mon == 0))
1165
65.8k
        return xmlStrdup((xmlChar*)"P0D");
1166
1167
589k
    secs   = dur->sec;
1168
589k
    days   = dur->day;
1169
589k
    months = dur->mon;
1170
1171
589k
    *cur = '\0';
1172
589k
    if (days < 0) {
1173
40.6k
        if (secs != 0.0) {
1174
36.8k
            secs = SECS_PER_DAY - secs;
1175
36.8k
            days += 1;
1176
36.8k
        }
1177
40.6k
        days = -days;
1178
40.6k
        *cur = '-';
1179
40.6k
    }
1180
589k
    if (months < 0) {
1181
35.1k
        months = -months;
1182
35.1k
        *cur = '-';
1183
35.1k
    }
1184
589k
    if (*cur == '-')
1185
43.5k
  cur++;
1186
1187
589k
    *cur++ = 'P';
1188
1189
589k
    if (months >= 12) {
1190
135k
        long years = months / 12;
1191
1192
135k
        months -= years * 12;
1193
135k
        exsltFormatLong(&cur, end, years);
1194
135k
        if (cur < end)
1195
135k
            *cur++ = 'Y';
1196
135k
    }
1197
1198
589k
    if (months != 0) {
1199
81.9k
        exsltFormatLong(&cur, end, months);
1200
81.9k
        if (cur < end)
1201
81.9k
            *cur++ = 'M';
1202
81.9k
    }
1203
1204
589k
    if (days != 0) {
1205
208k
        exsltFormatLong(&cur, end, days);
1206
208k
        if (cur < end)
1207
208k
            *cur++ = 'D';
1208
208k
    }
1209
1210
589k
    tmp = floor(secs);
1211
589k
    intSecs = (long) tmp;
1212
    /* Round to nearest to avoid issues with floating point precision */
1213
589k
    nsecs = (long) floor((secs - tmp) * 1000000000 + 0.5);
1214
589k
    if (nsecs >= 1000000000) {
1215
7.55k
        nsecs -= 1000000000;
1216
7.55k
        intSecs += 1;
1217
7.55k
    }
1218
1219
589k
    if ((intSecs > 0) || (nsecs > 0)) {
1220
490k
        if (cur < end)
1221
490k
            *cur++ = 'T';
1222
1223
490k
        if (intSecs >= SECS_PER_HOUR) {
1224
193k
            long hours = intSecs / SECS_PER_HOUR;
1225
1226
193k
            intSecs -= hours * SECS_PER_HOUR;
1227
193k
            exsltFormatLong(&cur, end, hours);
1228
193k
            if (cur < end)
1229
193k
                *cur++ = 'H';
1230
193k
        }
1231
1232
490k
        if (intSecs >= SECS_PER_MIN) {
1233
415k
            long mins = intSecs / SECS_PER_MIN;
1234
1235
415k
            intSecs -= mins * SECS_PER_MIN;
1236
415k
            exsltFormatLong(&cur, end, mins);
1237
415k
            if (cur < end)
1238
415k
                *cur++ = 'M';
1239
415k
        }
1240
1241
490k
        if ((intSecs > 0) || (nsecs > 0)) {
1242
368k
            exsltFormatLong(&cur, end, intSecs);
1243
368k
            exsltFormatNanoseconds(&cur, end, nsecs);
1244
368k
            if (cur < end)
1245
368k
                *cur++ = 'S';
1246
368k
        }
1247
490k
    }
1248
1249
589k
    *cur = 0;
1250
1251
589k
    return xmlStrdup(buf);
1252
655k
}
1253
1254
static void
1255
491k
exsltFormatTwoDigits(xmlChar **cur, xmlChar *end, int num) {
1256
491k
    if (num < 0 || num >= 100)
1257
0
        return;
1258
491k
    if (*cur < end)
1259
491k
        *(*cur)++ = '0' + num / 10;
1260
491k
    if (*cur < end)
1261
491k
        *(*cur)++ = '0' + num % 10;
1262
491k
}
1263
1264
static void
1265
163k
exsltFormatTime(xmlChar **cur, xmlChar *end, exsltDateValPtr dt) {
1266
163k
    double tmp;
1267
163k
    long intSecs, nsecs;
1268
1269
163k
    exsltFormatTwoDigits(cur, end, dt->hour);
1270
163k
    if (*cur < end)
1271
163k
        *(*cur)++ = ':';
1272
1273
163k
    exsltFormatTwoDigits(cur, end, dt->min);
1274
163k
    if (*cur < end)
1275
163k
        *(*cur)++ = ':';
1276
1277
163k
    tmp = floor(dt->sec);
1278
163k
    intSecs = (long) tmp;
1279
    /*
1280
     * Round to nearest to avoid issues with floating point precision,
1281
     * but don't carry over so seconds stay below 60.
1282
     */
1283
163k
    nsecs = (long) floor((dt->sec - tmp) * 1000000000 + 0.5);
1284
163k
    if (nsecs > 999999999)
1285
45
        nsecs = 999999999;
1286
163k
    exsltFormatTwoDigits(cur, end, intSecs);
1287
163k
    exsltFormatNanoseconds(cur, end, nsecs);
1288
163k
}
1289
1290
/**
1291
 * exsltDateFormatDateTime:
1292
 * @dt: an #exsltDateValPtr
1293
 *
1294
 * Formats @dt in xs:dateTime format.
1295
 *
1296
 * Returns a newly allocated string, or NULL in case of error
1297
 */
1298
static xmlChar *
1299
exsltDateFormatDateTime (const exsltDateValPtr dt)
1300
153k
{
1301
153k
    xmlChar buf[100], *cur = buf, *end = buf + 99;
1302
1303
153k
    if ((dt == NULL) || !VALID_DATETIME(dt))
1304
0
  return NULL;
1305
1306
153k
    FORMAT_DATE(dt, cur);
1307
153k
    *cur = 'T';
1308
153k
    cur++;
1309
153k
    exsltFormatTime(&cur, end, dt);
1310
153k
    FORMAT_TZ(dt->tzo, cur);
1311
153k
    *cur = 0;
1312
1313
153k
    return xmlStrdup(buf);
1314
153k
}
1315
1316
/**
1317
 * exsltDateFormatDate:
1318
 * @dt: an #exsltDateValPtr
1319
 *
1320
 * Formats @dt in xs:date format.
1321
 *
1322
 * Returns a newly allocated string, or NULL in case of error
1323
 */
1324
static xmlChar *
1325
exsltDateFormatDate (const exsltDateValPtr dt)
1326
127k
{
1327
127k
    xmlChar buf[100], *cur = buf;
1328
1329
127k
    if ((dt == NULL) || !VALID_DATETIME(dt))
1330
0
  return NULL;
1331
1332
127k
    FORMAT_DATE(dt, cur);
1333
127k
    if (dt->tz_flag || (dt->tzo != 0)) {
1334
36.0k
  FORMAT_TZ(dt->tzo, cur);
1335
36.0k
    }
1336
127k
    *cur = 0;
1337
1338
127k
    return xmlStrdup(buf);
1339
127k
}
1340
1341
/**
1342
 * exsltDateFormatTime:
1343
 * @dt: an #exsltDateValPtr
1344
 *
1345
 * Formats @dt in xs:time format.
1346
 *
1347
 * Returns a newly allocated string, or NULL in case of error
1348
 */
1349
static xmlChar *
1350
exsltDateFormatTime (const exsltDateValPtr dt)
1351
10.1k
{
1352
10.1k
    xmlChar buf[100], *cur = buf, *end = buf + 99;
1353
1354
10.1k
    if ((dt == NULL) || !VALID_TIME(dt))
1355
0
  return NULL;
1356
1357
10.1k
    exsltFormatTime(&cur, end, dt);
1358
10.1k
    if (dt->tz_flag || (dt->tzo != 0)) {
1359
129
  FORMAT_TZ(dt->tzo, cur);
1360
129
    }
1361
10.1k
    *cur = 0;
1362
1363
10.1k
    return xmlStrdup(buf);
1364
10.1k
}
1365
1366
/**
1367
 * exsltDateFormat:
1368
 * @dt: an #exsltDateValPtr
1369
 *
1370
 * Formats @dt in the proper format.
1371
 * Note: xs:gmonth and xs:gday are not formatted as there are no
1372
 * routines that output them.
1373
 *
1374
 * Returns a newly allocated string, or NULL in case of error
1375
 */
1376
static xmlChar *
1377
exsltDateFormat (const exsltDateValPtr dt)
1378
267k
{
1379
1380
267k
    if (dt == NULL)
1381
0
  return NULL;
1382
1383
267k
    switch (dt->type) {
1384
145k
    case XS_DATETIME:
1385
145k
        return exsltDateFormatDateTime(dt);
1386
58.6k
    case XS_DATE:
1387
58.6k
        return exsltDateFormatDate(dt);
1388
0
    case XS_TIME:
1389
0
        return exsltDateFormatTime(dt);
1390
62.5k
    default:
1391
62.5k
        break;
1392
267k
    }
1393
1394
62.5k
    if (dt->type & XS_GYEAR) {
1395
62.5k
        xmlChar buf[100], *cur = buf;
1396
1397
62.5k
        FORMAT_GYEAR(dt->year, cur);
1398
62.5k
        if (dt->type == XS_GYEARMONTH) {
1399
42.7k
      *cur = '-';
1400
42.7k
      cur++;
1401
42.7k
      FORMAT_GMONTH(dt->mon, cur);
1402
42.7k
        }
1403
1404
62.5k
        if (dt->tz_flag || (dt->tzo != 0)) {
1405
18.8k
      FORMAT_TZ(dt->tzo, cur);
1406
18.8k
        }
1407
62.5k
        *cur = 0;
1408
62.5k
        return xmlStrdup(buf);
1409
62.5k
    }
1410
1411
0
    return NULL;
1412
62.5k
}
1413
1414
/**
1415
 * _exsltDateCastYMToDays:
1416
 * @dt: an #exsltDateValPtr
1417
 *
1418
 * Convert mon and year of @dt to total number of days. Take the
1419
 * number of years since (or before) 1 AD and add the number of leap
1420
 * years. This is a function  because negative
1421
 * years must be handled a little differently.
1422
 *
1423
 * Returns number of days.
1424
 */
1425
static long
1426
_exsltDateCastYMToDays (const exsltDateValPtr dt)
1427
479k
{
1428
479k
    long ret;
1429
1430
479k
    if (dt->year <= 0)
1431
5.65k
        ret = ((dt->year-1) * 365) +
1432
5.65k
              (((dt->year)/4)-((dt->year)/100)+
1433
5.65k
               ((dt->year)/400)) +
1434
5.65k
              DAY_IN_YEAR(0, dt->mon, dt->year) - 1;
1435
474k
    else
1436
474k
        ret = ((dt->year-1) * 365) +
1437
474k
              (((dt->year-1)/4)-((dt->year-1)/100)+
1438
474k
               ((dt->year-1)/400)) +
1439
474k
              DAY_IN_YEAR(0, dt->mon, dt->year);
1440
1441
479k
    return ret;
1442
479k
}
1443
1444
/**
1445
 * TIME_TO_NUMBER:
1446
 * @dt:  an #exsltDateValPtr
1447
 *
1448
 * Calculates the number of seconds in the time portion of @dt.
1449
 *
1450
 * Returns seconds.
1451
 */
1452
#define TIME_TO_NUMBER(dt)                              \
1453
479k
    ((double)((dt->hour * SECS_PER_HOUR) +   \
1454
479k
              (dt->min * SECS_PER_MIN)) + dt->sec)
1455
1456
/**
1457
 * _exsltDateTruncateDate:
1458
 * @dt: an #exsltDateValPtr
1459
 * @type: dateTime type to set to
1460
 *
1461
 * Set @dt to truncated @type.
1462
 *
1463
 * Returns 0 success, non-zero otherwise.
1464
 */
1465
static int
1466
_exsltDateTruncateDate (exsltDateValPtr dt, exsltDateType type)
1467
167k
{
1468
167k
    if (dt == NULL)
1469
0
        return 1;
1470
1471
167k
    if ((type & XS_TIME) != XS_TIME) {
1472
167k
        dt->hour = 0;
1473
167k
        dt->min  = 0;
1474
167k
        dt->sec  = 0.0;
1475
167k
    }
1476
1477
167k
    if ((type & XS_GDAY) != XS_GDAY)
1478
163k
        dt->day = 1;
1479
1480
167k
    if ((type & XS_GMONTH) != XS_GMONTH)
1481
153k
        dt->mon = 1;
1482
1483
167k
    if ((type & XS_GYEAR) != XS_GYEAR)
1484
0
        dt->year = 0;
1485
1486
167k
    dt->type = type;
1487
1488
167k
    return 0;
1489
167k
}
1490
1491
/**
1492
 * _exsltDayInWeek:
1493
 * @yday: year day (1-366)
1494
 * @yr: year
1495
 *
1496
 * Determine the day-in-week from @yday and @yr. 0001-01-01 was
1497
 * a Monday so all other days are calculated from there. Take the
1498
 * number of years since (or before) add the number of leap years and
1499
 * the day-in-year and mod by 7. This is a function  because negative
1500
 * years must be handled a little differently.
1501
 *
1502
 * Returns day in week (Sunday = 0).
1503
 */
1504
static long
1505
_exsltDateDayInWeek(long yday, long yr)
1506
82.6k
{
1507
82.6k
    long ret;
1508
1509
82.6k
    if (yr <= 0) {
1510
        /* Compute modulus twice to avoid integer overflow */
1511
44.0k
        ret = ((yr%7-2 + ((yr/4)-(yr/100)+(yr/400)) + yday) % 7);
1512
44.0k
        if (ret < 0)
1513
39.7k
            ret += 7;
1514
44.0k
    } else
1515
38.6k
        ret = (((yr%7-1) + (((yr-1)/4)-((yr-1)/100)+((yr-1)/400)) + yday) % 7);
1516
1517
82.6k
    return ret;
1518
82.6k
}
1519
1520
/**
1521
 * _exsltDateAdd:
1522
 * @dt: an #exsltDateValPtr
1523
 * @dur: an #exsltDateDurValPtr
1524
 *
1525
 * Compute a new date/time from @dt and @dur. This function assumes @dt
1526
 * is either #XS_DATETIME, #XS_DATE, #XS_GYEARMONTH, or #XS_GYEAR.
1527
 *
1528
 * Returns date/time pointer or NULL.
1529
 */
1530
static exsltDateValPtr
1531
_exsltDateAdd (exsltDateValPtr dt, exsltDateDurValPtr dur)
1532
267k
{
1533
267k
    exsltDateValPtr ret;
1534
267k
    long carry, temp;
1535
267k
    double sum;
1536
1537
267k
    if ((dt == NULL) || (dur == NULL))
1538
0
        return NULL;
1539
1540
267k
    ret = exsltDateCreateDate(dt->type);
1541
267k
    if (ret == NULL)
1542
0
        return NULL;
1543
1544
    /*
1545
     * Note that temporary values may need more bits than the values in
1546
     * bit field.
1547
     */
1548
1549
    /* month */
1550
267k
    temp  = dt->mon + dur->mon % 12;
1551
267k
    carry = dur->mon / 12;
1552
267k
    if (temp < 1) {
1553
94.3k
        temp  += 12;
1554
94.3k
        carry -= 1;
1555
94.3k
    }
1556
172k
    else if (temp > 12) {
1557
7.25k
        temp  -= 12;
1558
7.25k
        carry += 1;
1559
7.25k
    }
1560
267k
    ret->mon = temp;
1561
1562
    /*
1563
     * year (may be modified later)
1564
     *
1565
     * Add epochs from dur->day now to avoid overflow later and to speed up
1566
     * pathological cases.
1567
     */
1568
267k
    carry += (dur->day / DAYS_PER_EPOCH) * YEARS_PER_EPOCH;
1569
267k
    if ((carry > 0 && dt->year > YEAR_MAX - carry) ||
1570
267k
        (carry < 0 && dt->year < YEAR_MIN - carry)) {
1571
        /* Overflow */
1572
0
        exsltDateFreeDate(ret);
1573
0
        return NULL;
1574
0
    }
1575
267k
    ret->year = dt->year + carry;
1576
1577
    /* time zone */
1578
267k
    ret->tzo     = dt->tzo;
1579
267k
    ret->tz_flag = dt->tz_flag;
1580
1581
    /* seconds */
1582
267k
    sum    = dt->sec + dur->sec;
1583
267k
    ret->sec = fmod(sum, 60.0);
1584
267k
    carry  = (long)(sum / 60.0);
1585
1586
    /* minute */
1587
267k
    temp  = dt->min + carry % 60;
1588
267k
    carry = carry / 60;
1589
267k
    if (temp >= 60) {
1590
3.49k
        temp  -= 60;
1591
3.49k
        carry += 1;
1592
3.49k
    }
1593
267k
    ret->min = temp;
1594
1595
    /* hours */
1596
267k
    temp  = dt->hour + carry % 24;
1597
267k
    carry = carry / 24;
1598
267k
    if (temp >= 24) {
1599
50.6k
        temp  -= 24;
1600
50.6k
        carry += 1;
1601
50.6k
    }
1602
267k
    ret->hour = temp;
1603
1604
    /* days */
1605
267k
    if (dt->day > MAX_DAYINMONTH(ret->year, ret->mon))
1606
3.75k
        temp = MAX_DAYINMONTH(ret->year, ret->mon);
1607
263k
    else if (dt->day < 1)
1608
0
        temp = 1;
1609
263k
    else
1610
263k
        temp = dt->day;
1611
1612
267k
    temp += dur->day % DAYS_PER_EPOCH + carry;
1613
1614
93.6M
    while (1) {
1615
93.6M
        if (temp < 1) {
1616
67.1M
            if (ret->mon > 1) {
1617
61.6M
                ret->mon -= 1;
1618
61.6M
            }
1619
5.59M
            else {
1620
5.59M
                if (ret->year == YEAR_MIN) {
1621
0
                    exsltDateFreeDate(ret);
1622
0
                    return NULL;
1623
0
                }
1624
5.59M
                ret->mon   = 12;
1625
5.59M
                ret->year -= 1;
1626
5.59M
            }
1627
67.1M
            temp += MAX_DAYINMONTH(ret->year, ret->mon);
1628
67.1M
        } else if (temp > (long)MAX_DAYINMONTH(ret->year, ret->mon)) {
1629
26.1M
            temp -= MAX_DAYINMONTH(ret->year, ret->mon);
1630
26.1M
            if (ret->mon < 12) {
1631
24.0M
                ret->mon += 1;
1632
24.0M
            }
1633
2.18M
            else {
1634
2.18M
                if (ret->year == YEAR_MAX) {
1635
0
                    exsltDateFreeDate(ret);
1636
0
                    return NULL;
1637
0
                }
1638
2.18M
                ret->mon   = 1;
1639
2.18M
                ret->year += 1;
1640
2.18M
            }
1641
26.1M
        } else
1642
267k
            break;
1643
93.6M
    }
1644
1645
267k
    ret->day = temp;
1646
1647
    /*
1648
     * adjust the date/time type to the date values
1649
     */
1650
267k
    if (ret->type != XS_DATETIME) {
1651
187k
        if ((ret->hour) || (ret->min) || (ret->sec))
1652
66.6k
            ret->type = XS_DATETIME;
1653
121k
        else if (ret->type != XS_DATE) {
1654
118k
            if (ret->day != 1)
1655
56.4k
                ret->type = XS_DATE;
1656
62.5k
            else if ((ret->type != XS_GYEARMONTH) && (ret->mon != 1))
1657
33.2k
                ret->type = XS_GYEARMONTH;
1658
118k
        }
1659
187k
    }
1660
1661
267k
    return ret;
1662
267k
}
1663
1664
/**
1665
 * _exsltDateDifference:
1666
 * @x: an #exsltDateValPtr
1667
 * @y: an #exsltDateValPtr
1668
 * @flag: force difference in days
1669
 *
1670
 * Calculate the difference between @x and @y as a duration
1671
 * (i.e. y - x). If the @flag is set then even if the least specific
1672
 * format of @x or @y is xs:gYear or xs:gYearMonth.
1673
 *
1674
 * Returns a duration pointer or NULL.
1675
 */
1676
static exsltDateDurValPtr
1677
_exsltDateDifference (exsltDateValPtr x, exsltDateValPtr y, int flag)
1678
338k
{
1679
338k
    exsltDateDurValPtr ret;
1680
1681
338k
    if ((x == NULL) || (y == NULL))
1682
0
        return NULL;
1683
1684
338k
    if (((x->type < XS_GYEAR) || (x->type > XS_DATETIME)) ||
1685
338k
        ((y->type < XS_GYEAR) || (y->type > XS_DATETIME)))
1686
0
        return NULL;
1687
1688
    /*
1689
     * the operand with the most specific format must be converted to
1690
     * the same type as the operand with the least specific format.
1691
     */
1692
338k
    if (x->type != y->type) {
1693
167k
        if (x->type < y->type) {
1694
2.36k
            _exsltDateTruncateDate(y, x->type);
1695
165k
        } else {
1696
165k
            _exsltDateTruncateDate(x, y->type);
1697
165k
        }
1698
167k
    }
1699
1700
338k
    ret = exsltDateCreateDuration();
1701
338k
    if (ret == NULL)
1702
0
        return NULL;
1703
1704
338k
    if (((x->type == XS_GYEAR) || (x->type == XS_GYEARMONTH)) && (!flag)) {
1705
        /* compute the difference in months */
1706
93.8k
        if ((x->year >= LONG_MAX / 24) || (x->year <= LONG_MIN / 24) ||
1707
93.8k
            (y->year >= LONG_MAX / 24) || (y->year <= LONG_MIN / 24)) {
1708
            /* Possible overflow. */
1709
4.53k
            exsltDateFreeDuration(ret);
1710
4.53k
            return NULL;
1711
4.53k
        }
1712
89.3k
        ret->mon = (y->year - x->year) * 12 + (y->mon - x->mon);
1713
244k
    } else {
1714
244k
        long carry;
1715
1716
244k
        if ((x->year > LONG_MAX / 731) || (x->year < LONG_MIN / 731) ||
1717
244k
            (y->year > LONG_MAX / 731) || (y->year < LONG_MIN / 731)) {
1718
            /* Possible overflow. */
1719
5.03k
            exsltDateFreeDuration(ret);
1720
5.03k
            return NULL;
1721
5.03k
        }
1722
1723
239k
        ret->sec  = TIME_TO_NUMBER(y) - TIME_TO_NUMBER(x);
1724
239k
        ret->sec += (x->tzo - y->tzo) * SECS_PER_MIN;
1725
239k
        carry    = (long)floor(ret->sec / SECS_PER_DAY);
1726
239k
        ret->sec  = ret->sec - carry * SECS_PER_DAY;
1727
1728
239k
        ret->day  = _exsltDateCastYMToDays(y) - _exsltDateCastYMToDays(x);
1729
239k
        ret->day += y->day - x->day;
1730
239k
        ret->day += carry;
1731
239k
    }
1732
1733
329k
    return ret;
1734
338k
}
1735
1736
/**
1737
 * _exsltDateAddDurCalc
1738
 * @ret: an exsltDateDurValPtr for the return value:
1739
 * @x: an exsltDateDurValPtr for the first operand
1740
 * @y: an exsltDateDurValPtr for the second operand
1741
 *
1742
 * Add two durations, catering for possible negative values.
1743
 * The sum is placed in @ret.
1744
 *
1745
 * Returns 1 for success, 0 if error detected.
1746
 */
1747
static int
1748
_exsltDateAddDurCalc (exsltDateDurValPtr ret, exsltDateDurValPtr x,
1749
          exsltDateDurValPtr y)
1750
619k
{
1751
    /* months */
1752
619k
    if ((x->mon > 0 && y->mon > LONG_MAX - x->mon) ||
1753
619k
        (x->mon < 0 && y->mon < LONG_MIN - x->mon)) {
1754
        /* Overflow */
1755
0
        return 0;
1756
0
    }
1757
619k
    ret->mon = x->mon + y->mon;
1758
1759
    /* days */
1760
619k
    if ((x->day > 0 && y->day > LONG_MAX - x->day) ||
1761
619k
        (x->day < 0 && y->day < LONG_MIN - x->day)) {
1762
        /* Overflow */
1763
0
        return 0;
1764
0
    }
1765
619k
    ret->day = x->day + y->day;
1766
1767
    /* seconds */
1768
619k
    ret->sec = x->sec + y->sec;
1769
619k
    if (ret->sec >= SECS_PER_DAY) {
1770
104k
        if (ret->day == LONG_MAX) {
1771
            /* Overflow */
1772
0
            return 0;
1773
0
        }
1774
104k
        ret->sec -= SECS_PER_DAY;
1775
104k
        ret->day += 1;
1776
104k
    }
1777
1778
    /*
1779
     * are the results indeterminate? i.e. how do you subtract days from
1780
     * months or years?
1781
     */
1782
619k
    if (ret->day >= 0) {
1783
172k
        if (((ret->day > 0) || (ret->sec > 0)) && (ret->mon < 0))
1784
15.9k
            return 0;
1785
172k
    }
1786
447k
    else {
1787
447k
        if (ret->mon > 0)
1788
30.1k
            return 0;
1789
447k
    }
1790
573k
    return 1;
1791
619k
}
1792
1793
/**
1794
 * _exsltDateAddDuration:
1795
 * @x: an #exsltDateDurValPtr
1796
 * @y: an #exsltDateDurValPtr
1797
 *
1798
 * Compute a new duration from @x and @y.
1799
 *
1800
 * Returns a duration pointer or NULL.
1801
 */
1802
static exsltDateDurValPtr
1803
_exsltDateAddDuration (exsltDateDurValPtr x, exsltDateDurValPtr y)
1804
76.5k
{
1805
76.5k
    exsltDateDurValPtr ret;
1806
1807
76.5k
    if ((x == NULL) || (y == NULL))
1808
0
        return NULL;
1809
1810
76.5k
    ret = exsltDateCreateDuration();
1811
76.5k
    if (ret == NULL)
1812
0
        return NULL;
1813
1814
76.5k
    if (_exsltDateAddDurCalc(ret, x, y))
1815
60.9k
        return ret;
1816
1817
15.5k
    exsltDateFreeDuration(ret);
1818
15.5k
    return NULL;
1819
76.5k
}
1820
1821
/****************************************************************
1822
 *                *
1823
 *    EXSLT - Dates and Times functions   *
1824
 *                *
1825
 ****************************************************************/
1826
1827
/**
1828
 * exsltDateDateTime:
1829
 *
1830
 * Implements the EXSLT - Dates and Times date-time() function:
1831
 *     string date:date-time()
1832
 *
1833
 * Returns the current date and time as a date/time string.
1834
 */
1835
static xmlChar *
1836
exsltDateDateTime (void)
1837
7.91k
{
1838
7.91k
    xmlChar *ret = NULL;
1839
7.91k
    exsltDateValPtr cur;
1840
1841
7.91k
    cur = exsltDateCurrent();
1842
7.91k
    if (cur != NULL) {
1843
7.91k
  ret = exsltDateFormatDateTime(cur);
1844
7.91k
  exsltDateFreeDate(cur);
1845
7.91k
    }
1846
1847
7.91k
    return ret;
1848
7.91k
}
1849
1850
/**
1851
 * exsltDateDate:
1852
 * @dateTime: a date/time string
1853
 *
1854
 * Implements the EXSLT - Dates and Times date() function:
1855
 *     string date:date (string?)
1856
 *
1857
 * Returns the date specified in the date/time string given as the
1858
 * argument.  If no argument is given, then the current local
1859
 * date/time, as returned by date:date-time is used as a default
1860
 * argument.
1861
 * The date/time string specified as an argument must be a string in
1862
 * the format defined as the lexical representation of either
1863
 * xs:dateTime or xs:date.  If the argument is not in either of these
1864
 * formats, returns NULL.
1865
 */
1866
static xmlChar *
1867
exsltDateDate (const xmlChar *dateTime)
1868
924k
{
1869
924k
    exsltDateValPtr dt = NULL;
1870
924k
    xmlChar *ret = NULL;
1871
1872
924k
    if (dateTime == NULL) {
1873
9.63k
  dt = exsltDateCurrent();
1874
9.63k
  if (dt == NULL)
1875
0
      return NULL;
1876
915k
    } else {
1877
915k
  dt = exsltDateParse(dateTime);
1878
915k
  if (dt == NULL)
1879
832k
      return NULL;
1880
82.5k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
1881
22.9k
      exsltDateFreeDate(dt);
1882
22.9k
      return NULL;
1883
22.9k
  }
1884
82.5k
    }
1885
1886
69.2k
    ret = exsltDateFormatDate(dt);
1887
69.2k
    exsltDateFreeDate(dt);
1888
1889
69.2k
    return ret;
1890
924k
}
1891
1892
/**
1893
 * exsltDateTime:
1894
 * @dateTime: a date/time string
1895
 *
1896
 * Implements the EXSLT - Dates and Times time() function:
1897
 *     string date:time (string?)
1898
 *
1899
 * Returns the time specified in the date/time string given as the
1900
 * argument.  If no argument is given, then the current local
1901
 * date/time, as returned by date:date-time is used as a default
1902
 * argument.
1903
 * The date/time string specified as an argument must be a string in
1904
 * the format defined as the lexical representation of either
1905
 * xs:dateTime or xs:time.  If the argument is not in either of these
1906
 * formats, returns NULL.
1907
 */
1908
static xmlChar *
1909
exsltDateTime (const xmlChar *dateTime)
1910
77.6k
{
1911
77.6k
    exsltDateValPtr dt = NULL;
1912
77.6k
    xmlChar *ret = NULL;
1913
1914
77.6k
    if (dateTime == NULL) {
1915
7.58k
  dt = exsltDateCurrent();
1916
7.58k
  if (dt == NULL)
1917
0
      return NULL;
1918
70.0k
    } else {
1919
70.0k
  dt = exsltDateParse(dateTime);
1920
70.0k
  if (dt == NULL)
1921
60.3k
      return NULL;
1922
9.68k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
1923
7.16k
      exsltDateFreeDate(dt);
1924
7.16k
      return NULL;
1925
7.16k
  }
1926
9.68k
    }
1927
1928
10.1k
    ret = exsltDateFormatTime(dt);
1929
10.1k
    exsltDateFreeDate(dt);
1930
1931
10.1k
    return ret;
1932
77.6k
}
1933
1934
/**
1935
 * exsltDateYear:
1936
 * @dateTime: a date/time string
1937
 *
1938
 * Implements the EXSLT - Dates and Times year() function
1939
 *    number date:year (string?)
1940
 * Returns the year of a date as a number.  If no argument is given,
1941
 * then the current local date/time, as returned by date:date-time is
1942
 * used as a default argument.
1943
 * The date/time string specified as the first argument must be a
1944
 * right-truncated string in the format defined as the lexical
1945
 * representation of xs:dateTime in one of the formats defined in [XML
1946
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
1947
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
1948
 *  - xs:date (CCYY-MM-DD)
1949
 *  - xs:gYearMonth (CCYY-MM)
1950
 *  - xs:gYear (CCYY)
1951
 * If the date/time string is not in one of these formats, then NaN is
1952
 * returned.
1953
 */
1954
static double
1955
exsltDateYear (const xmlChar *dateTime)
1956
365k
{
1957
365k
    exsltDateValPtr dt;
1958
365k
    long year;
1959
365k
    double ret;
1960
1961
365k
    if (dateTime == NULL) {
1962
7.59k
  dt = exsltDateCurrent();
1963
7.59k
  if (dt == NULL)
1964
0
      return xmlXPathNAN;
1965
357k
    } else {
1966
357k
  dt = exsltDateParse(dateTime);
1967
357k
  if (dt == NULL)
1968
281k
      return xmlXPathNAN;
1969
76.5k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
1970
76.5k
      (dt->type != XS_GYEARMONTH) && (dt->type != XS_GYEAR)) {
1971
11.7k
      exsltDateFreeDate(dt);
1972
11.7k
      return xmlXPathNAN;
1973
11.7k
  }
1974
76.5k
    }
1975
1976
72.3k
    year = dt->year;
1977
72.3k
    if (year <= 0) year -= 1; /* Adjust for missing year 0. */
1978
72.3k
    ret = (double) year;
1979
72.3k
    exsltDateFreeDate(dt);
1980
1981
72.3k
    return ret;
1982
365k
}
1983
1984
/**
1985
 * exsltDateLeapYear:
1986
 * @dateTime: a date/time string
1987
 *
1988
 * Implements the EXSLT - Dates and Times leap-year() function:
1989
 *    boolean date:leap-yea (string?)
1990
 * Returns true if the year given in a date is a leap year.  If no
1991
 * argument is given, then the current local date/time, as returned by
1992
 * date:date-time is used as a default argument.
1993
 * The date/time string specified as the first argument must be a
1994
 * right-truncated string in the format defined as the lexical
1995
 * representation of xs:dateTime in one of the formats defined in [XML
1996
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
1997
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
1998
 *  - xs:date (CCYY-MM-DD)
1999
 *  - xs:gYearMonth (CCYY-MM)
2000
 *  - xs:gYear (CCYY)
2001
 * If the date/time string is not in one of these formats, then NaN is
2002
 * returned.
2003
 */
2004
static xmlXPathObjectPtr
2005
exsltDateLeapYear (const xmlChar *dateTime)
2006
112k
{
2007
112k
    exsltDateValPtr dt = NULL;
2008
112k
    xmlXPathObjectPtr ret;
2009
2010
112k
    if (dateTime == NULL) {
2011
285
  dt = exsltDateCurrent();
2012
112k
    } else {
2013
112k
  dt = exsltDateParse(dateTime);
2014
112k
  if ((dt != NULL) &&
2015
112k
            (dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
2016
112k
      (dt->type != XS_GYEARMONTH) && (dt->type != XS_GYEAR)) {
2017
9.70k
      exsltDateFreeDate(dt);
2018
9.70k
      dt = NULL;
2019
9.70k
  }
2020
112k
    }
2021
2022
112k
    if (dt == NULL) {
2023
50.0k
        ret = xmlXPathNewFloat(xmlXPathNAN);
2024
50.0k
    }
2025
62.7k
    else {
2026
62.7k
        ret = xmlXPathNewBoolean(IS_LEAP(dt->year));
2027
62.7k
        exsltDateFreeDate(dt);
2028
62.7k
    }
2029
2030
112k
    return ret;
2031
112k
}
2032
2033
/**
2034
 * exsltDateMonthInYear:
2035
 * @dateTime: a date/time string
2036
 *
2037
 * Implements the EXSLT - Dates and Times month-in-year() function:
2038
 *    number date:month-in-year (string?)
2039
 * Returns the month of a date as a number.  If no argument is given,
2040
 * then the current local date/time, as returned by date:date-time is
2041
 * used the default argument.
2042
 * The date/time string specified as the argument is a left or
2043
 * right-truncated string in the format defined as the lexical
2044
 * representation of xs:dateTime in one of the formats defined in [XML
2045
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2046
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2047
 *  - xs:date (CCYY-MM-DD)
2048
 *  - xs:gYearMonth (CCYY-MM)
2049
 *  - xs:gMonth (--MM--)
2050
 *  - xs:gMonthDay (--MM-DD)
2051
 * If the date/time string is not in one of these formats, then NaN is
2052
 * returned.
2053
 */
2054
static double
2055
exsltDateMonthInYear (const xmlChar *dateTime)
2056
372k
{
2057
372k
    exsltDateValPtr dt;
2058
372k
    double ret;
2059
2060
372k
    if (dateTime == NULL) {
2061
2.65k
  dt = exsltDateCurrent();
2062
2.65k
  if (dt == NULL)
2063
0
      return xmlXPathNAN;
2064
369k
    } else {
2065
369k
  dt = exsltDateParse(dateTime);
2066
369k
  if (dt == NULL)
2067
256k
      return xmlXPathNAN;
2068
113k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
2069
113k
      (dt->type != XS_GYEARMONTH) && (dt->type != XS_GMONTH) &&
2070
113k
      (dt->type != XS_GMONTHDAY)) {
2071
51.7k
      exsltDateFreeDate(dt);
2072
51.7k
      return xmlXPathNAN;
2073
51.7k
  }
2074
113k
    }
2075
2076
64.0k
    ret = (double) dt->mon;
2077
64.0k
    exsltDateFreeDate(dt);
2078
2079
64.0k
    return ret;
2080
372k
}
2081
2082
/**
2083
 * exsltDateMonthName:
2084
 * @dateTime: a date/time string
2085
 *
2086
 * Implements the EXSLT - Dates and Time month-name() function
2087
 *    string date:month-name (string?)
2088
 * Returns the full name of the month of a date.  If no argument is
2089
 * given, then the current local date/time, as returned by
2090
 * date:date-time is used the default argument.
2091
 * The date/time string specified as the argument is a left or
2092
 * right-truncated string in the format defined as the lexical
2093
 * representation of xs:dateTime in one of the formats defined in [XML
2094
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2095
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2096
 *  - xs:date (CCYY-MM-DD)
2097
 *  - xs:gYearMonth (CCYY-MM)
2098
 *  - xs:gMonth (--MM--)
2099
 * If the date/time string is not in one of these formats, then an
2100
 * empty string ('') is returned.
2101
 * The result is an English month name: one of 'January', 'February',
2102
 * 'March', 'April', 'May', 'June', 'July', 'August', 'September',
2103
 * 'October', 'November' or 'December'.
2104
 */
2105
static const xmlChar *
2106
exsltDateMonthName (const xmlChar *dateTime)
2107
61.9k
{
2108
61.9k
    static const xmlChar monthNames[13][10] = {
2109
61.9k
        { 0 },
2110
61.9k
  { 'J', 'a', 'n', 'u', 'a', 'r', 'y', 0 },
2111
61.9k
  { 'F', 'e', 'b', 'r', 'u', 'a', 'r', 'y', 0 },
2112
61.9k
  { 'M', 'a', 'r', 'c', 'h', 0 },
2113
61.9k
  { 'A', 'p', 'r', 'i', 'l', 0 },
2114
61.9k
  { 'M', 'a', 'y', 0 },
2115
61.9k
  { 'J', 'u', 'n', 'e', 0 },
2116
61.9k
  { 'J', 'u', 'l', 'y', 0 },
2117
61.9k
  { 'A', 'u', 'g', 'u', 's', 't', 0 },
2118
61.9k
  { 'S', 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r', 0 },
2119
61.9k
  { 'O', 'c', 't', 'o', 'b', 'e', 'r', 0 },
2120
61.9k
  { 'N', 'o', 'v', 'e', 'm', 'b', 'e', 'r', 0 },
2121
61.9k
  { 'D', 'e', 'c', 'e', 'm', 'b', 'e', 'r', 0 }
2122
61.9k
    };
2123
61.9k
    double month;
2124
61.9k
    int index = 0;
2125
61.9k
    month = exsltDateMonthInYear(dateTime);
2126
61.9k
    if (!xmlXPathIsNaN(month) && (month >= 1.0) && (month <= 12.0))
2127
8.39k
      index = (int) month;
2128
61.9k
    return monthNames[index];
2129
61.9k
}
2130
2131
/**
2132
 * exsltDateMonthAbbreviation:
2133
 * @dateTime: a date/time string
2134
 *
2135
 * Implements the EXSLT - Dates and Time month-abbreviation() function
2136
 *    string date:month-abbreviation (string?)
2137
 * Returns the abbreviation of the month of a date.  If no argument is
2138
 * given, then the current local date/time, as returned by
2139
 * date:date-time is used the default argument.
2140
 * The date/time string specified as the argument is a left or
2141
 * right-truncated string in the format defined as the lexical
2142
 * representation of xs:dateTime in one of the formats defined in [XML
2143
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2144
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2145
 *  - xs:date (CCYY-MM-DD)
2146
 *  - xs:gYearMonth (CCYY-MM)
2147
 *  - xs:gMonth (--MM--)
2148
 * If the date/time string is not in one of these formats, then an
2149
 * empty string ('') is returned.
2150
 * The result is an English month abbreviation: one of 'Jan', 'Feb',
2151
 * 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov' or
2152
 * 'Dec'.
2153
 */
2154
static const xmlChar *
2155
exsltDateMonthAbbreviation (const xmlChar *dateTime)
2156
4.51k
{
2157
4.51k
    static const xmlChar monthAbbreviations[13][4] = {
2158
4.51k
        { 0 },
2159
4.51k
  { 'J', 'a', 'n', 0 },
2160
4.51k
  { 'F', 'e', 'b', 0 },
2161
4.51k
  { 'M', 'a', 'r', 0 },
2162
4.51k
  { 'A', 'p', 'r', 0 },
2163
4.51k
  { 'M', 'a', 'y', 0 },
2164
4.51k
  { 'J', 'u', 'n', 0 },
2165
4.51k
  { 'J', 'u', 'l', 0 },
2166
4.51k
  { 'A', 'u', 'g', 0 },
2167
4.51k
  { 'S', 'e', 'p', 0 },
2168
4.51k
  { 'O', 'c', 't', 0 },
2169
4.51k
  { 'N', 'o', 'v', 0 },
2170
4.51k
  { 'D', 'e', 'c', 0 }
2171
4.51k
    };
2172
4.51k
    double month;
2173
4.51k
    int index = 0;
2174
4.51k
    month = exsltDateMonthInYear(dateTime);
2175
4.51k
    if (!xmlXPathIsNaN(month) && (month >= 1.0) && (month <= 12.0))
2176
1.22k
      index = (int) month;
2177
4.51k
    return monthAbbreviations[index];
2178
4.51k
}
2179
2180
/**
2181
 * exsltDateWeekInYear:
2182
 * @dateTime: a date/time string
2183
 *
2184
 * Implements the EXSLT - Dates and Times week-in-year() function
2185
 *    number date:week-in-year (string?)
2186
 * Returns the week of the year as a number.  If no argument is given,
2187
 * then the current local date/time, as returned by date:date-time is
2188
 * used as the default argument.  For the purposes of numbering,
2189
 * counting follows ISO 8601: week 1 in a year is the week containing
2190
 * the first Thursday of the year, with new weeks beginning on a
2191
 * Monday.
2192
 * The date/time string specified as the argument is a right-truncated
2193
 * string in the format defined as the lexical representation of
2194
 * xs:dateTime in one of the formats defined in [XML Schema Part 2:
2195
 * Datatypes].  The permitted formats are as follows:
2196
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2197
 *  - xs:date (CCYY-MM-DD)
2198
 * If the date/time string is not in one of these formats, then NaN is
2199
 * returned.
2200
 */
2201
static double
2202
exsltDateWeekInYear (const xmlChar *dateTime)
2203
103k
{
2204
103k
    exsltDateValPtr dt;
2205
103k
    long diy, diw, year, ret;
2206
2207
103k
    if (dateTime == NULL) {
2208
237
  dt = exsltDateCurrent();
2209
237
  if (dt == NULL)
2210
0
      return xmlXPathNAN;
2211
103k
    } else {
2212
103k
  dt = exsltDateParse(dateTime);
2213
103k
  if (dt == NULL)
2214
88.4k
      return xmlXPathNAN;
2215
14.5k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2216
3.85k
      exsltDateFreeDate(dt);
2217
3.85k
      return xmlXPathNAN;
2218
3.85k
  }
2219
14.5k
    }
2220
2221
10.9k
    diy = DAY_IN_YEAR(dt->day, dt->mon, dt->year);
2222
2223
    /*
2224
     * Determine day-in-week (0=Sun, 1=Mon, etc.) then adjust so Monday
2225
     * is the first day-in-week
2226
     */
2227
10.9k
    diw = (_exsltDateDayInWeek(diy, dt->year) + 6) % 7;
2228
2229
    /* ISO 8601 adjustment, 3 is Thu */
2230
10.9k
    diy += (3 - diw);
2231
10.9k
    if(diy < 1) {
2232
5.79k
  year = dt->year - 1;
2233
5.79k
  if(year == 0) year--;
2234
5.79k
  diy = DAY_IN_YEAR(31, 12, year) + diy;
2235
5.79k
    } else if (diy > (long)DAY_IN_YEAR(31, 12, dt->year)) {
2236
116
  diy -= DAY_IN_YEAR(31, 12, dt->year);
2237
116
    }
2238
2239
10.9k
    ret = ((diy - 1) / 7) + 1;
2240
2241
10.9k
    exsltDateFreeDate(dt);
2242
2243
10.9k
    return (double) ret;
2244
103k
}
2245
2246
/**
2247
 * exsltDateWeekInMonth:
2248
 * @dateTime: a date/time string
2249
 *
2250
 * Implements the EXSLT - Dates and Times week-in-month() function
2251
 *    number date:week-in-month (string?)
2252
 * The date:week-in-month function returns the week in a month of a
2253
 * date as a number. If no argument is given, then the current local
2254
 * date/time, as returned by date:date-time is used the default
2255
 * argument. For the purposes of numbering, the first day of the month
2256
 * is in week 1 and new weeks begin on a Monday (so the first and last
2257
 * weeks in a month will often have less than 7 days in them).
2258
 * The date/time string specified as the argument is a right-truncated
2259
 * string in the format defined as the lexical representation of
2260
 * xs:dateTime in one of the formats defined in [XML Schema Part 2:
2261
 * Datatypes].  The permitted formats are as follows:
2262
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2263
 *  - xs:date (CCYY-MM-DD)
2264
 * If the date/time string is not in one of these formats, then NaN is
2265
 * returned.
2266
 */
2267
static double
2268
exsltDateWeekInMonth (const xmlChar *dateTime)
2269
122k
{
2270
122k
    exsltDateValPtr dt;
2271
122k
    long fdiy, fdiw, ret;
2272
2273
122k
    if (dateTime == NULL) {
2274
556
  dt = exsltDateCurrent();
2275
556
  if (dt == NULL)
2276
0
      return xmlXPathNAN;
2277
122k
    } else {
2278
122k
  dt = exsltDateParse(dateTime);
2279
122k
  if (dt == NULL)
2280
68.1k
      return xmlXPathNAN;
2281
53.9k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2282
8.47k
      exsltDateFreeDate(dt);
2283
8.47k
      return xmlXPathNAN;
2284
8.47k
  }
2285
53.9k
    }
2286
2287
46.0k
    fdiy = DAY_IN_YEAR(1, dt->mon, dt->year);
2288
    /*
2289
     * Determine day-in-week (0=Sun, 1=Mon, etc.) then adjust so Monday
2290
     * is the first day-in-week
2291
     */
2292
46.0k
    fdiw = (_exsltDateDayInWeek(fdiy, dt->year) + 6) % 7;
2293
2294
46.0k
    ret = ((dt->day + fdiw - 1) / 7) + 1;
2295
2296
46.0k
    exsltDateFreeDate(dt);
2297
2298
46.0k
    return (double) ret;
2299
122k
}
2300
2301
/**
2302
 * exsltDateDayInYear:
2303
 * @dateTime: a date/time string
2304
 *
2305
 * Implements the EXSLT - Dates and Times day-in-year() function
2306
 *    number date:day-in-year (string?)
2307
 * Returns the day of a date in a year as a number.  If no argument is
2308
 * given, then the current local date/time, as returned by
2309
 * date:date-time is used the default argument.
2310
 * The date/time string specified as the argument is a right-truncated
2311
 * string in the format defined as the lexical representation of
2312
 * xs:dateTime in one of the formats defined in [XML Schema Part 2:
2313
 * Datatypes].  The permitted formats are as follows:
2314
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2315
 *  - xs:date (CCYY-MM-DD)
2316
 * If the date/time string is not in one of these formats, then NaN is
2317
 * returned.
2318
 */
2319
static double
2320
exsltDateDayInYear (const xmlChar *dateTime)
2321
20.0k
{
2322
20.0k
    exsltDateValPtr dt;
2323
20.0k
    long ret;
2324
2325
20.0k
    if (dateTime == NULL) {
2326
2.43k
  dt = exsltDateCurrent();
2327
2.43k
  if (dt == NULL)
2328
0
      return xmlXPathNAN;
2329
17.5k
    } else {
2330
17.5k
  dt = exsltDateParse(dateTime);
2331
17.5k
  if (dt == NULL)
2332
12.6k
      return xmlXPathNAN;
2333
4.93k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2334
2.19k
      exsltDateFreeDate(dt);
2335
2.19k
      return xmlXPathNAN;
2336
2.19k
  }
2337
4.93k
    }
2338
2339
5.16k
    ret = DAY_IN_YEAR(dt->day, dt->mon, dt->year);
2340
2341
5.16k
    exsltDateFreeDate(dt);
2342
2343
5.16k
    return (double) ret;
2344
20.0k
}
2345
2346
/**
2347
 * exsltDateDayInMonth:
2348
 * @dateTime: a date/time string
2349
 *
2350
 * Implements the EXSLT - Dates and Times day-in-month() function:
2351
 *    number date:day-in-month (string?)
2352
 * Returns the day of a date as a number.  If no argument is given,
2353
 * then the current local date/time, as returned by date:date-time is
2354
 * used the default argument.
2355
 * The date/time string specified as the argument is a left or
2356
 * right-truncated string in the format defined as the lexical
2357
 * representation of xs:dateTime in one of the formats defined in [XML
2358
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2359
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2360
 *  - xs:date (CCYY-MM-DD)
2361
 *  - xs:gMonthDay (--MM-DD)
2362
 *  - xs:gDay (---DD)
2363
 * If the date/time string is not in one of these formats, then NaN is
2364
 * returned.
2365
 */
2366
static double
2367
exsltDateDayInMonth (const xmlChar *dateTime)
2368
39.4k
{
2369
39.4k
    exsltDateValPtr dt;
2370
39.4k
    double ret;
2371
2372
39.4k
    if (dateTime == NULL) {
2373
127
  dt = exsltDateCurrent();
2374
127
  if (dt == NULL)
2375
0
      return xmlXPathNAN;
2376
39.2k
    } else {
2377
39.2k
  dt = exsltDateParse(dateTime);
2378
39.2k
  if (dt == NULL)
2379
34.7k
      return xmlXPathNAN;
2380
4.57k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
2381
4.57k
      (dt->type != XS_GMONTHDAY) && (dt->type != XS_GDAY)) {
2382
3.37k
      exsltDateFreeDate(dt);
2383
3.37k
      return xmlXPathNAN;
2384
3.37k
  }
2385
4.57k
    }
2386
2387
1.32k
    ret = (double) dt->day;
2388
1.32k
    exsltDateFreeDate(dt);
2389
2390
1.32k
    return ret;
2391
39.4k
}
2392
2393
/**
2394
 * exsltDateDayOfWeekInMonth:
2395
 * @dateTime: a date/time string
2396
 *
2397
 * Implements the EXSLT - Dates and Times day-of-week-in-month() function:
2398
 *    number date:day-of-week-in-month (string?)
2399
 * Returns the day-of-the-week in a month of a date as a number
2400
 * (e.g. 3 for the 3rd Tuesday in May).  If no argument is
2401
 * given, then the current local date/time, as returned by
2402
 * date:date-time is used the default argument.
2403
 * The date/time string specified as the argument is a right-truncated
2404
 * string in the format defined as the lexical representation of
2405
 * xs:dateTime in one of the formats defined in [XML Schema Part 2:
2406
 * Datatypes].  The permitted formats are as follows:
2407
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2408
 *  - xs:date (CCYY-MM-DD)
2409
 * If the date/time string is not in one of these formats, then NaN is
2410
 * returned.
2411
 */
2412
static double
2413
exsltDateDayOfWeekInMonth (const xmlChar *dateTime)
2414
11.7k
{
2415
11.7k
    exsltDateValPtr dt;
2416
11.7k
    long ret;
2417
2418
11.7k
    if (dateTime == NULL) {
2419
20
  dt = exsltDateCurrent();
2420
20
  if (dt == NULL)
2421
0
      return xmlXPathNAN;
2422
11.7k
    } else {
2423
11.7k
  dt = exsltDateParse(dateTime);
2424
11.7k
  if (dt == NULL)
2425
9.22k
      return xmlXPathNAN;
2426
2.47k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2427
2.33k
      exsltDateFreeDate(dt);
2428
2.33k
      return xmlXPathNAN;
2429
2.33k
  }
2430
2.47k
    }
2431
2432
157
    ret = ((dt->day -1) / 7) + 1;
2433
2434
157
    exsltDateFreeDate(dt);
2435
2436
157
    return (double) ret;
2437
11.7k
}
2438
2439
/**
2440
 * exsltDateDayInWeek:
2441
 * @dateTime: a date/time string
2442
 *
2443
 * Implements the EXSLT - Dates and Times day-in-week() function:
2444
 *    number date:day-in-week (string?)
2445
 * Returns the day of the week given in a date as a number.  If no
2446
 * argument is given, then the current local date/time, as returned by
2447
 * date:date-time is used the default argument.
2448
 * The date/time string specified as the argument is a left or
2449
 * right-truncated string in the format defined as the lexical
2450
 * representation of xs:dateTime in one of the formats defined in [XML
2451
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2452
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2453
 *  - xs:date (CCYY-MM-DD)
2454
 * If the date/time string is not in one of these formats, then NaN is
2455
 * returned.
2456
 * The numbering of days of the week starts at 1 for Sunday, 2 for
2457
 * Monday and so on up to 7 for Saturday.
2458
 */
2459
static double
2460
exsltDateDayInWeek (const xmlChar *dateTime)
2461
114k
{
2462
114k
    exsltDateValPtr dt;
2463
114k
    long diy, ret;
2464
2465
114k
    if (dateTime == NULL) {
2466
21.0k
  dt = exsltDateCurrent();
2467
21.0k
  if (dt == NULL)
2468
0
      return xmlXPathNAN;
2469
93.1k
    } else {
2470
93.1k
  dt = exsltDateParse(dateTime);
2471
93.1k
  if (dt == NULL)
2472
75.7k
      return xmlXPathNAN;
2473
17.3k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2474
12.8k
      exsltDateFreeDate(dt);
2475
12.8k
      return xmlXPathNAN;
2476
12.8k
  }
2477
17.3k
    }
2478
2479
25.6k
    diy = DAY_IN_YEAR(dt->day, dt->mon, dt->year);
2480
2481
25.6k
    ret = _exsltDateDayInWeek(diy, dt->year) + 1;
2482
2483
25.6k
    exsltDateFreeDate(dt);
2484
2485
25.6k
    return (double) ret;
2486
114k
}
2487
2488
/**
2489
 * exsltDateDayName:
2490
 * @dateTime: a date/time string
2491
 *
2492
 * Implements the EXSLT - Dates and Time day-name() function
2493
 *    string date:day-name (string?)
2494
 * Returns the full name of the day of the week of a date.  If no
2495
 * argument is given, then the current local date/time, as returned by
2496
 * date:date-time is used the default argument.
2497
 * The date/time string specified as the argument is a left or
2498
 * right-truncated string in the format defined as the lexical
2499
 * representation of xs:dateTime in one of the formats defined in [XML
2500
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2501
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2502
 *  - xs:date (CCYY-MM-DD)
2503
 * If the date/time string is not in one of these formats, then an
2504
 * empty string ('') is returned.
2505
 * The result is an English day name: one of 'Sunday', 'Monday',
2506
 * 'Tuesday', 'Wednesday', 'Thursday' or 'Friday'.
2507
 */
2508
static const xmlChar *
2509
exsltDateDayName (const xmlChar *dateTime)
2510
97.9k
{
2511
97.9k
    static const xmlChar dayNames[8][10] = {
2512
97.9k
        { 0 },
2513
97.9k
  { 'S', 'u', 'n', 'd', 'a', 'y', 0 },
2514
97.9k
  { 'M', 'o', 'n', 'd', 'a', 'y', 0 },
2515
97.9k
  { 'T', 'u', 'e', 's', 'd', 'a', 'y', 0 },
2516
97.9k
  { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', 0 },
2517
97.9k
  { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y', 0 },
2518
97.9k
  { 'F', 'r', 'i', 'd', 'a', 'y', 0 },
2519
97.9k
  { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y', 0 }
2520
97.9k
    };
2521
97.9k
    double day;
2522
97.9k
    int index = 0;
2523
97.9k
    day = exsltDateDayInWeek(dateTime);
2524
97.9k
    if(!xmlXPathIsNaN(day) && (day >= 1.0) && (day <= 7.0))
2525
21.2k
      index = (int) day;
2526
97.9k
    return dayNames[index];
2527
97.9k
}
2528
2529
/**
2530
 * exsltDateDayAbbreviation:
2531
 * @dateTime: a date/time string
2532
 *
2533
 * Implements the EXSLT - Dates and Time day-abbreviation() function
2534
 *    string date:day-abbreviation (string?)
2535
 * Returns the abbreviation of the day of the week of a date.  If no
2536
 * argument is given, then the current local date/time, as returned by
2537
 * date:date-time is used the default argument.
2538
 * The date/time string specified as the argument is a left or
2539
 * right-truncated string in the format defined as the lexical
2540
 * representation of xs:dateTime in one of the formats defined in [XML
2541
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2542
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2543
 *  - xs:date (CCYY-MM-DD)
2544
 * If the date/time string is not in one of these formats, then an
2545
 * empty string ('') is returned.
2546
 * The result is a three-letter English day abbreviation: one of
2547
 * 'Sun', 'Mon', 'Tue', 'Wed', 'Thu' or 'Fri'.
2548
 */
2549
static const xmlChar *
2550
exsltDateDayAbbreviation (const xmlChar *dateTime)
2551
3.52k
{
2552
3.52k
    static const xmlChar dayAbbreviations[8][4] = {
2553
3.52k
        { 0 },
2554
3.52k
  { 'S', 'u', 'n', 0 },
2555
3.52k
  { 'M', 'o', 'n', 0 },
2556
3.52k
  { 'T', 'u', 'e', 0 },
2557
3.52k
  { 'W', 'e', 'd', 0 },
2558
3.52k
  { 'T', 'h', 'u', 0 },
2559
3.52k
  { 'F', 'r', 'i', 0 },
2560
3.52k
  { 'S', 'a', 't', 0 }
2561
3.52k
    };
2562
3.52k
    double day;
2563
3.52k
    int index = 0;
2564
3.52k
    day = exsltDateDayInWeek(dateTime);
2565
3.52k
    if(!xmlXPathIsNaN(day) && (day >= 1.0) && (day <= 7.0))
2566
805
      index = (int) day;
2567
3.52k
    return dayAbbreviations[index];
2568
3.52k
}
2569
2570
/**
2571
 * exsltDateHourInDay:
2572
 * @dateTime: a date/time string
2573
 *
2574
 * Implements the EXSLT - Dates and Times day-in-month() function:
2575
 *    number date:day-in-month (string?)
2576
 * Returns the hour of the day as a number.  If no argument is given,
2577
 * then the current local date/time, as returned by date:date-time is
2578
 * used the default argument.
2579
 * The date/time string specified as the argument is a left or
2580
 * right-truncated string in the format defined as the lexical
2581
 * representation of xs:dateTime in one of the formats defined in [XML
2582
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2583
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2584
 *  - xs:time (hh:mm:ss)
2585
 * If the date/time string is not in one of these formats, then NaN is
2586
 * returned.
2587
 */
2588
static double
2589
exsltDateHourInDay (const xmlChar *dateTime)
2590
56.1k
{
2591
56.1k
    exsltDateValPtr dt;
2592
56.1k
    double ret;
2593
2594
56.1k
    if (dateTime == NULL) {
2595
93
  dt = exsltDateCurrent();
2596
93
  if (dt == NULL)
2597
0
      return xmlXPathNAN;
2598
56.0k
    } else {
2599
56.0k
  dt = exsltDateParse(dateTime);
2600
56.0k
  if (dt == NULL)
2601
47.9k
      return xmlXPathNAN;
2602
8.13k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
2603
2.30k
      exsltDateFreeDate(dt);
2604
2.30k
      return xmlXPathNAN;
2605
2.30k
  }
2606
8.13k
    }
2607
2608
5.92k
    ret = (double) dt->hour;
2609
5.92k
    exsltDateFreeDate(dt);
2610
2611
5.92k
    return ret;
2612
56.1k
}
2613
2614
/**
2615
 * exsltDateMinuteInHour:
2616
 * @dateTime: a date/time string
2617
 *
2618
 * Implements the EXSLT - Dates and Times day-in-month() function:
2619
 *    number date:day-in-month (string?)
2620
 * Returns the minute of the hour as a number.  If no argument is
2621
 * given, then the current local date/time, as returned by
2622
 * date:date-time is used the default argument.
2623
 * The date/time string specified as the argument is a left or
2624
 * right-truncated string in the format defined as the lexical
2625
 * representation of xs:dateTime in one of the formats defined in [XML
2626
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2627
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2628
 *  - xs:time (hh:mm:ss)
2629
 * If the date/time string is not in one of these formats, then NaN is
2630
 * returned.
2631
 */
2632
static double
2633
exsltDateMinuteInHour (const xmlChar *dateTime)
2634
3.49k
{
2635
3.49k
    exsltDateValPtr dt;
2636
3.49k
    double ret;
2637
2638
3.49k
    if (dateTime == NULL) {
2639
20
  dt = exsltDateCurrent();
2640
20
  if (dt == NULL)
2641
0
      return xmlXPathNAN;
2642
3.47k
    } else {
2643
3.47k
  dt = exsltDateParse(dateTime);
2644
3.47k
  if (dt == NULL)
2645
2.74k
      return xmlXPathNAN;
2646
728
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
2647
443
      exsltDateFreeDate(dt);
2648
443
      return xmlXPathNAN;
2649
443
  }
2650
728
    }
2651
2652
305
    ret = (double) dt->min;
2653
305
    exsltDateFreeDate(dt);
2654
2655
305
    return ret;
2656
3.49k
}
2657
2658
/**
2659
 * exsltDateSecondInMinute:
2660
 * @dateTime: a date/time string
2661
 *
2662
 * Implements the EXSLT - Dates and Times second-in-minute() function:
2663
 *    number date:day-in-month (string?)
2664
 * Returns the second of the minute as a number.  If no argument is
2665
 * given, then the current local date/time, as returned by
2666
 * date:date-time is used the default argument.
2667
 * The date/time string specified as the argument is a left or
2668
 * right-truncated string in the format defined as the lexical
2669
 * representation of xs:dateTime in one of the formats defined in [XML
2670
 * Schema Part 2: Datatypes].  The permitted formats are as follows:
2671
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2672
 *  - xs:time (hh:mm:ss)
2673
 * If the date/time string is not in one of these formats, then NaN is
2674
 * returned.
2675
 *
2676
 * Returns the second or NaN.
2677
 */
2678
static double
2679
exsltDateSecondInMinute (const xmlChar *dateTime)
2680
27.8k
{
2681
27.8k
    exsltDateValPtr dt;
2682
27.8k
    double ret;
2683
2684
27.8k
    if (dateTime == NULL) {
2685
1.90k
  dt = exsltDateCurrent();
2686
1.90k
  if (dt == NULL)
2687
0
      return xmlXPathNAN;
2688
25.9k
    } else {
2689
25.9k
  dt = exsltDateParse(dateTime);
2690
25.9k
  if (dt == NULL)
2691
19.2k
      return xmlXPathNAN;
2692
6.68k
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
2693
1.69k
      exsltDateFreeDate(dt);
2694
1.69k
      return xmlXPathNAN;
2695
1.69k
  }
2696
6.68k
    }
2697
2698
6.88k
    ret = dt->sec;
2699
6.88k
    exsltDateFreeDate(dt);
2700
2701
6.88k
    return ret;
2702
27.8k
}
2703
2704
/**
2705
 * exsltDateAdd:
2706
 * @xstr: date/time string
2707
 * @ystr: date/time string
2708
 *
2709
 * Implements the date:add (string,string) function which returns the
2710
 * date/time * resulting from adding a duration to a date/time.
2711
 * The first argument (@xstr) must be right-truncated date/time
2712
 * strings in one of the formats defined in [XML Schema Part 2:
2713
 * Datatypes]. The permitted formats are as follows:
2714
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2715
 *  - xs:date (CCYY-MM-DD)
2716
 *  - xs:gYearMonth (CCYY-MM)
2717
 *  - xs:gYear (CCYY)
2718
 * The second argument (@ystr) is a string in the format defined for
2719
 * xs:duration in [3.2.6 duration] of [XML Schema Part 2: Datatypes].
2720
 * The return value is a right-truncated date/time strings in one of
2721
 * the formats defined in [XML Schema Part 2: Datatypes] and listed
2722
 * above. This value is calculated using the algorithm described in
2723
 * [Appendix E Adding durations to dateTimes] of [XML Schema Part 2:
2724
 * Datatypes].
2725
2726
 * Returns date/time string or NULL.
2727
 */
2728
static xmlChar *
2729
exsltDateAdd (const xmlChar *xstr, const xmlChar *ystr)
2730
944k
{
2731
944k
    exsltDateValPtr dt, res;
2732
944k
    exsltDateDurValPtr dur;
2733
944k
    xmlChar     *ret;
2734
2735
944k
    if ((xstr == NULL) || (ystr == NULL))
2736
0
        return NULL;
2737
2738
944k
    dt = exsltDateParse(xstr);
2739
944k
    if (dt == NULL)
2740
401k
        return NULL;
2741
542k
    else if ((dt->type < XS_GYEAR) || (dt->type > XS_DATETIME)) {
2742
60.4k
        exsltDateFreeDate(dt);
2743
60.4k
        return NULL;
2744
60.4k
    }
2745
2746
481k
    dur = exsltDateParseDuration(ystr);
2747
481k
    if (dur == NULL) {
2748
214k
        exsltDateFreeDate(dt);
2749
214k
        return NULL;
2750
214k
    }
2751
2752
267k
    res = _exsltDateAdd(dt, dur);
2753
2754
267k
    exsltDateFreeDate(dt);
2755
267k
    exsltDateFreeDuration(dur);
2756
2757
267k
    if (res == NULL)
2758
0
        return NULL;
2759
2760
267k
    ret = exsltDateFormat(res);
2761
267k
    exsltDateFreeDate(res);
2762
2763
267k
    return ret;
2764
267k
}
2765
2766
/**
2767
 * exsltDateAddDuration:
2768
 * @xstr:      first duration string
2769
 * @ystr:      second duration string
2770
 *
2771
 * Implements the date:add-duration (string,string) function which returns
2772
 * the duration resulting from adding two durations together.
2773
 * Both arguments are strings in the format defined for xs:duration
2774
 * in [3.2.6 duration] of [XML Schema Part 2: Datatypes]. If either
2775
 * argument is not in this format, the function returns an empty string
2776
 * ('').
2777
 * The return value is a string in the format defined for xs:duration
2778
 * in [3.2.6 duration] of [XML Schema Part 2: Datatypes].
2779
 * The durations can usually be added by summing the numbers given for
2780
 * each of the components in the durations. However, if the durations
2781
 * are differently signed, then this sometimes results in durations
2782
 * that are impossible to express in this syntax (e.g. 'P1M' + '-P1D').
2783
 * In these cases, the function returns an empty string ('').
2784
 *
2785
 * Returns duration string or NULL.
2786
 */
2787
static xmlChar *
2788
exsltDateAddDuration (const xmlChar *xstr, const xmlChar *ystr)
2789
157k
{
2790
157k
    exsltDateDurValPtr x, y, res;
2791
157k
    xmlChar     *ret;
2792
2793
157k
    if ((xstr == NULL) || (ystr == NULL))
2794
0
        return NULL;
2795
2796
157k
    x = exsltDateParseDuration(xstr);
2797
157k
    if (x == NULL)
2798
24.0k
        return NULL;
2799
2800
133k
    y = exsltDateParseDuration(ystr);
2801
133k
    if (y == NULL) {
2802
57.2k
        exsltDateFreeDuration(x);
2803
57.2k
        return NULL;
2804
57.2k
    }
2805
2806
76.5k
    res = _exsltDateAddDuration(x, y);
2807
2808
76.5k
    exsltDateFreeDuration(x);
2809
76.5k
    exsltDateFreeDuration(y);
2810
2811
76.5k
    if (res == NULL)
2812
15.5k
        return NULL;
2813
2814
60.9k
    ret = exsltDateFormatDuration(res);
2815
60.9k
    exsltDateFreeDuration(res);
2816
2817
60.9k
    return ret;
2818
76.5k
}
2819
2820
/**
2821
 * exsltDateSumFunction:
2822
 * @ns:      a node set of duration strings
2823
 *
2824
 * The date:sum function adds a set of durations together.
2825
 * The string values of the nodes in the node set passed as an argument
2826
 * are interpreted as durations and added together as if using the
2827
 * date:add-duration function. (from exslt.org)
2828
 *
2829
 * The return value is a string in the format defined for xs:duration
2830
 * in [3.2.6 duration] of [XML Schema Part 2: Datatypes].
2831
 * The durations can usually be added by summing the numbers given for
2832
 * each of the components in the durations. However, if the durations
2833
 * are differently signed, then this sometimes results in durations
2834
 * that are impossible to express in this syntax (e.g. 'P1M' + '-P1D').
2835
 * In these cases, the function returns an empty string ('').
2836
 *
2837
 * Returns duration string or NULL.
2838
 */
2839
static void
2840
exsltDateSumFunction (xmlXPathParserContextPtr ctxt, int nargs)
2841
517k
{
2842
517k
    xmlNodeSetPtr ns;
2843
517k
    void *user = NULL;
2844
517k
    xmlChar *tmp;
2845
517k
    exsltDateDurValPtr x, total;
2846
517k
    xmlChar *ret;
2847
517k
    int i;
2848
2849
517k
    if (nargs != 1) {
2850
2.01k
  xmlXPathSetArityError (ctxt);
2851
2.01k
  return;
2852
2.01k
    }
2853
2854
    /* We need to delay the freeing of value->user */
2855
515k
    if ((ctxt->value != NULL) && ctxt->value->boolval != 0) {
2856
178
  user = ctxt->value->user;
2857
178
  ctxt->value->boolval = 0;
2858
178
  ctxt->value->user = NULL;
2859
178
    }
2860
2861
515k
    ns = xmlXPathPopNodeSet (ctxt);
2862
515k
    if (xmlXPathCheckError (ctxt))
2863
673
  return;
2864
2865
514k
    if ((ns == NULL) || (ns->nodeNr == 0)) {
2866
13.8k
  xmlXPathReturnEmptyString (ctxt);
2867
13.8k
  if (ns != NULL)
2868
6.64k
      xmlXPathFreeNodeSet (ns);
2869
13.8k
  return;
2870
13.8k
    }
2871
2872
500k
    total = exsltDateCreateDuration ();
2873
500k
    if (total == NULL) {
2874
0
        xmlXPathFreeNodeSet (ns);
2875
0
        return;
2876
0
    }
2877
2878
1.01M
    for (i = 0; i < ns->nodeNr; i++) {
2879
943k
  int result;
2880
943k
  tmp = xmlXPathCastNodeToString (ns->nodeTab[i]);
2881
943k
  if (tmp == NULL) {
2882
0
      xmlXPathFreeNodeSet (ns);
2883
0
      exsltDateFreeDuration (total);
2884
0
      return;
2885
0
  }
2886
2887
943k
  x = exsltDateParseDuration (tmp);
2888
943k
  if (x == NULL) {
2889
400k
      xmlFree (tmp);
2890
400k
      exsltDateFreeDuration (total);
2891
400k
      xmlXPathFreeNodeSet (ns);
2892
400k
      xmlXPathReturnEmptyString (ctxt);
2893
400k
      return;
2894
400k
  }
2895
2896
543k
  result = _exsltDateAddDurCalc(total, total, x);
2897
2898
543k
  exsltDateFreeDuration (x);
2899
543k
  xmlFree (tmp);
2900
543k
  if (!result) {
2901
30.4k
      exsltDateFreeDuration (total);
2902
30.4k
      xmlXPathFreeNodeSet (ns);
2903
30.4k
      xmlXPathReturnEmptyString (ctxt);
2904
30.4k
      return;
2905
30.4k
  }
2906
543k
    }
2907
2908
69.7k
    ret = exsltDateFormatDuration (total);
2909
69.7k
    exsltDateFreeDuration (total);
2910
2911
69.7k
    xmlXPathFreeNodeSet (ns);
2912
69.7k
    if (user != NULL)
2913
0
  xmlFreeNodeList ((xmlNodePtr) user);
2914
2915
69.7k
    if (ret == NULL)
2916
0
  xmlXPathReturnEmptyString (ctxt);
2917
69.7k
    else
2918
69.7k
  xmlXPathReturnString (ctxt, ret);
2919
69.7k
}
2920
2921
/**
2922
 * exsltDateSeconds:
2923
 * @dateTime: a date/time string
2924
 *
2925
 * Implements the EXSLT - Dates and Times seconds() function:
2926
 *    number date:seconds(string?)
2927
 * The date:seconds function returns the number of seconds specified
2928
 * by the argument string. If no argument is given, then the current
2929
 * local date/time, as returned by exsltDateCurrent() is used as the
2930
 * default argument. If the date/time string is a xs:duration, then the
2931
 * years and months must be zero (or not present). Parsing a duration
2932
 * converts the fields to seconds. If the date/time string is not a
2933
 * duration (and not null), then the legal formats are:
2934
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
2935
 *  - xs:date     (CCYY-MM-DD)
2936
 *  - xs:gYearMonth (CCYY-MM)
2937
 *  - xs:gYear      (CCYY)
2938
 * In these cases the difference between the @dateTime and
2939
 * 1970-01-01T00:00:00Z is calculated and converted to seconds.
2940
 *
2941
 * Note that there was some confusion over whether "difference" meant
2942
 * that a dateTime of 1970-01-01T00:00:01Z should be a positive one or
2943
 * a negative one.  After correspondence with exslt.org, it was determined
2944
 * that the intent of the specification was to have it positive.  The
2945
 * coding was modified in July 2003 to reflect this.
2946
 *
2947
 * Returns seconds or Nan.
2948
 */
2949
static double
2950
exsltDateSeconds (const xmlChar *dateTime)
2951
538k
{
2952
538k
    exsltDateValPtr dt;
2953
538k
    exsltDateDurValPtr dur = NULL;
2954
538k
    double ret = xmlXPathNAN;
2955
2956
538k
    if (dateTime == NULL) {
2957
110k
  dt = exsltDateCurrent();
2958
110k
  if (dt == NULL)
2959
0
      return xmlXPathNAN;
2960
427k
    } else {
2961
427k
        dt = exsltDateParse(dateTime);
2962
427k
        if (dt == NULL)
2963
299k
            dur = exsltDateParseDuration(dateTime);
2964
427k
    }
2965
2966
538k
    if ((dt != NULL) && (dt->type >= XS_GYEAR)) {
2967
234k
        exsltDateValPtr y;
2968
234k
        exsltDateDurValPtr diff;
2969
2970
        /*
2971
         * compute the difference between the given (or current) date
2972
         * and epoch date
2973
         */
2974
234k
        y = exsltDateCreateDate(XS_DATETIME);
2975
234k
        if (y != NULL) {
2976
234k
            y->year = 1970;
2977
234k
            y->mon  = 1;
2978
234k
            y->day  = 1;
2979
234k
            y->tz_flag = 1;
2980
2981
234k
            diff = _exsltDateDifference(y, dt, 1);
2982
234k
            if (diff != NULL) {
2983
230k
                ret = (double)diff->day * SECS_PER_DAY + diff->sec;
2984
230k
                exsltDateFreeDuration(diff);
2985
230k
            }
2986
234k
            exsltDateFreeDate(y);
2987
234k
        }
2988
2989
304k
    } else if ((dur != NULL) && (dur->mon == 0)) {
2990
1.38k
        ret = (double)dur->day * SECS_PER_DAY + dur->sec;
2991
1.38k
    }
2992
2993
538k
    if (dt != NULL)
2994
239k
        exsltDateFreeDate(dt);
2995
538k
    if (dur != NULL)
2996
3.23k
        exsltDateFreeDuration(dur);
2997
2998
538k
    return ret;
2999
538k
}
3000
3001
/**
3002
 * exsltDateDifference:
3003
 * @xstr: date/time string
3004
 * @ystr: date/time string
3005
 *
3006
 * Implements the date:difference (string,string) function which returns
3007
 * the duration between the first date and the second date. If the first
3008
 * date occurs before the second date, then the result is a positive
3009
 * duration; if it occurs after the second date, the result is a
3010
 * negative duration.  The two dates must both be right-truncated
3011
 * date/time strings in one of the formats defined in [XML Schema Part
3012
 * 2: Datatypes]. The date/time with the most specific format (i.e. the
3013
 * least truncation) is converted into the same format as the date with
3014
 * the least specific format (i.e. the most truncation). The permitted
3015
 * formats are as follows, from most specific to least specific:
3016
 *  - xs:dateTime (CCYY-MM-DDThh:mm:ss)
3017
 *  - xs:date (CCYY-MM-DD)
3018
 *  - xs:gYearMonth (CCYY-MM)
3019
 *  - xs:gYear (CCYY)
3020
 * If either of the arguments is not in one of these formats,
3021
 * date:difference returns the empty string ('').
3022
 * The difference between the date/times is returned as a string in the
3023
 * format defined for xs:duration in [3.2.6 duration] of [XML Schema
3024
 * Part 2: Datatypes].
3025
 * If the date/time string with the least specific format is in either
3026
 * xs:gYearMonth or xs:gYear format, then the number of days, hours,
3027
 * minutes and seconds in the duration string must be equal to zero.
3028
 * (The format of the string will be PnYnM.) The number of months
3029
 * specified in the duration must be less than 12.
3030
 * Otherwise, the number of years and months in the duration string
3031
 * must be equal to zero. (The format of the string will be
3032
 * PnDTnHnMnS.) The number of seconds specified in the duration string
3033
 * must be less than 60; the number of minutes must be less than 60;
3034
 * the number of hours must be less than 24.
3035
 *
3036
 * Returns duration string or NULL.
3037
 */
3038
static xmlChar *
3039
exsltDateDifference (const xmlChar *xstr, const xmlChar *ystr)
3040
205k
{
3041
205k
    exsltDateValPtr x, y;
3042
205k
    exsltDateDurValPtr dur;
3043
205k
    xmlChar *ret = NULL;
3044
3045
205k
    if ((xstr == NULL) || (ystr == NULL))
3046
0
        return NULL;
3047
3048
205k
    x = exsltDateParse(xstr);
3049
205k
    if (x == NULL)
3050
55.3k
        return NULL;
3051
3052
149k
    y = exsltDateParse(ystr);
3053
149k
    if (y == NULL) {
3054
45.1k
        exsltDateFreeDate(x);
3055
45.1k
        return NULL;
3056
45.1k
    }
3057
3058
104k
    if (((x->type < XS_GYEAR) || (x->type > XS_DATETIME)) ||
3059
104k
        ((y->type < XS_GYEAR) || (y->type > XS_DATETIME)))  {
3060
200
  exsltDateFreeDate(x);
3061
200
  exsltDateFreeDate(y);
3062
200
        return NULL;
3063
200
    }
3064
3065
104k
    dur = _exsltDateDifference(x, y, 0);
3066
3067
104k
    exsltDateFreeDate(x);
3068
104k
    exsltDateFreeDate(y);
3069
3070
104k
    if (dur == NULL)
3071
5.45k
        return NULL;
3072
3073
99.0k
    ret = exsltDateFormatDuration(dur);
3074
99.0k
    exsltDateFreeDuration(dur);
3075
3076
99.0k
    return ret;
3077
104k
}
3078
3079
/**
3080
 * exsltDateDuration:
3081
 * @number: a xmlChar string
3082
 *
3083
 * Implements the The date:duration function returns a duration string
3084
 * representing the number of seconds specified by the argument string.
3085
 * If no argument is given, then the result of calling date:seconds
3086
 * without any arguments is used as a default argument.
3087
 * The duration is returned as a string in the format defined for
3088
 * xs:duration in [3.2.6 duration] of [XML Schema Part 2: Datatypes].
3089
 * The number of years and months in the duration string must be equal
3090
 * to zero. (The format of the string will be PnDTnHnMnS.) The number
3091
 * of seconds specified in the duration string must be less than 60;
3092
 * the number of minutes must be less than 60; the number of hours must
3093
 * be less than 24.
3094
 * If the argument is Infinity, -Infinity or NaN, then date:duration
3095
 * returns an empty string ('').
3096
 *
3097
 * Returns duration string or NULL.
3098
 */
3099
static xmlChar *
3100
exsltDateDuration (const xmlChar *number)
3101
490k
{
3102
490k
    exsltDateDurValPtr dur;
3103
490k
    double       secs, days;
3104
490k
    xmlChar     *ret;
3105
3106
490k
    if (number == NULL)
3107
10.5k
        secs = exsltDateSeconds(number);
3108
480k
    else
3109
480k
        secs = xmlXPathCastStringToNumber(number);
3110
3111
490k
    if (xmlXPathIsNaN(secs))
3112
60.7k
        return NULL;
3113
3114
430k
    days = floor(secs / SECS_PER_DAY);
3115
430k
    if ((days <= (double)LONG_MIN) || (days >= (double)LONG_MAX))
3116
4.21k
        return NULL;
3117
3118
425k
    dur = exsltDateCreateDuration();
3119
425k
    if (dur == NULL)
3120
0
        return NULL;
3121
3122
425k
    dur->day = (long)days;
3123
425k
    dur->sec = secs - days * SECS_PER_DAY;
3124
3125
425k
    ret = exsltDateFormatDuration(dur);
3126
425k
    exsltDateFreeDuration(dur);
3127
3128
425k
    return ret;
3129
425k
}
3130
3131
/****************************************************************
3132
 *                *
3133
 *    Wrappers for use by the XPath engine    *
3134
 *                *
3135
 ****************************************************************/
3136
3137
/**
3138
 * exsltDateDateTimeFunction:
3139
 * @ctxt: an XPath parser context
3140
 * @nargs : the number of arguments
3141
 *
3142
 * Wraps exsltDateDateTime() for use by the XPath engine.
3143
 */
3144
static void
3145
exsltDateDateTimeFunction (xmlXPathParserContextPtr ctxt, int nargs)
3146
8.09k
{
3147
8.09k
    xmlChar *ret;
3148
3149
8.09k
    if (nargs != 0) {
3150
180
  xmlXPathSetArityError(ctxt);
3151
180
  return;
3152
180
    }
3153
3154
7.91k
    ret = exsltDateDateTime();
3155
7.91k
    if (ret == NULL)
3156
0
        xmlXPathReturnEmptyString(ctxt);
3157
7.91k
    else
3158
7.91k
        xmlXPathReturnString(ctxt, ret);
3159
7.91k
}
3160
3161
/**
3162
 * exsltDateDateFunction:
3163
 * @ctxt: an XPath parser context
3164
 * @nargs : the number of arguments
3165
 *
3166
 * Wraps exsltDateDate() for use by the XPath engine.
3167
 */
3168
static void
3169
exsltDateDateFunction (xmlXPathParserContextPtr ctxt, int nargs)
3170
925k
{
3171
925k
    xmlChar *ret, *dt = NULL;
3172
3173
925k
    if ((nargs < 0) || (nargs > 1)) {
3174
202
  xmlXPathSetArityError(ctxt);
3175
202
  return;
3176
202
    }
3177
924k
    if (nargs == 1) {
3178
915k
  dt = xmlXPathPopString(ctxt);
3179
915k
  if (xmlXPathCheckError(ctxt)) {
3180
0
      xmlXPathSetTypeError(ctxt);
3181
0
      return;
3182
0
  }
3183
915k
    }
3184
3185
924k
    ret = exsltDateDate(dt);
3186
3187
924k
    if (ret == NULL) {
3188
855k
  xsltGenericDebug(xsltGenericDebugContext,
3189
855k
       "{http://exslt.org/dates-and-times}date: "
3190
855k
       "invalid date or format %s\n", dt);
3191
855k
  xmlXPathReturnEmptyString(ctxt);
3192
855k
    } else {
3193
69.2k
  xmlXPathReturnString(ctxt, ret);
3194
69.2k
    }
3195
3196
924k
    if (dt != NULL)
3197
915k
  xmlFree(dt);
3198
924k
}
3199
3200
/**
3201
 * exsltDateTimeFunction:
3202
 * @ctxt: an XPath parser context
3203
 * @nargs : the number of arguments
3204
 *
3205
 * Wraps exsltDateTime() for use by the XPath engine.
3206
 */
3207
static void
3208
exsltDateTimeFunction (xmlXPathParserContextPtr ctxt, int nargs)
3209
77.7k
{
3210
77.7k
    xmlChar *ret, *dt = NULL;
3211
3212
77.7k
    if ((nargs < 0) || (nargs > 1)) {
3213
77
  xmlXPathSetArityError(ctxt);
3214
77
  return;
3215
77
    }
3216
77.6k
    if (nargs == 1) {
3217
70.0k
  dt = xmlXPathPopString(ctxt);
3218
70.0k
  if (xmlXPathCheckError(ctxt)) {
3219
0
      xmlXPathSetTypeError(ctxt);
3220
0
      return;
3221
0
  }
3222
70.0k
    }
3223
3224
77.6k
    ret = exsltDateTime(dt);
3225
3226
77.6k
    if (ret == NULL) {
3227
67.5k
  xsltGenericDebug(xsltGenericDebugContext,
3228
67.5k
       "{http://exslt.org/dates-and-times}time: "
3229
67.5k
       "invalid date or format %s\n", dt);
3230
67.5k
  xmlXPathReturnEmptyString(ctxt);
3231
67.5k
    } else {
3232
10.1k
  xmlXPathReturnString(ctxt, ret);
3233
10.1k
    }
3234
3235
77.6k
    if (dt != NULL)
3236
70.0k
  xmlFree(dt);
3237
77.6k
}
3238
3239
/**
3240
 * exsltDateYearFunction:
3241
 * @ctxt: an XPath parser context
3242
 * @nargs : the number of arguments
3243
 *
3244
 * Wraps exsltDateYear() for use by the XPath engine.
3245
 */
3246
static void
3247
exsltDateYearFunction (xmlXPathParserContextPtr ctxt, int nargs)
3248
365k
{
3249
365k
    xmlChar *dt = NULL;
3250
365k
    double ret;
3251
3252
365k
    if ((nargs < 0) || (nargs > 1)) {
3253
111
  xmlXPathSetArityError(ctxt);
3254
111
  return;
3255
111
    }
3256
3257
365k
    if (nargs == 1) {
3258
357k
  dt = xmlXPathPopString(ctxt);
3259
357k
  if (xmlXPathCheckError(ctxt)) {
3260
0
      xmlXPathSetTypeError(ctxt);
3261
0
      return;
3262
0
  }
3263
357k
    }
3264
3265
365k
    ret = exsltDateYear(dt);
3266
3267
365k
    if (dt != NULL)
3268
357k
  xmlFree(dt);
3269
3270
365k
    xmlXPathReturnNumber(ctxt, ret);
3271
365k
}
3272
3273
/**
3274
 * exsltDateLeapYearFunction:
3275
 * @ctxt: an XPath parser context
3276
 * @nargs : the number of arguments
3277
 *
3278
 * Wraps exsltDateLeapYear() for use by the XPath engine.
3279
 */
3280
static void
3281
exsltDateLeapYearFunction (xmlXPathParserContextPtr ctxt, int nargs)
3282
112k
{
3283
112k
    xmlChar *dt = NULL;
3284
112k
    xmlXPathObjectPtr ret;
3285
3286
112k
    if ((nargs < 0) || (nargs > 1)) {
3287
21
  xmlXPathSetArityError(ctxt);
3288
21
  return;
3289
21
    }
3290
3291
112k
    if (nargs == 1) {
3292
112k
  dt = xmlXPathPopString(ctxt);
3293
112k
  if (xmlXPathCheckError(ctxt)) {
3294
0
      xmlXPathSetTypeError(ctxt);
3295
0
      return;
3296
0
  }
3297
112k
    }
3298
3299
112k
    ret = exsltDateLeapYear(dt);
3300
3301
112k
    if (dt != NULL)
3302
112k
  xmlFree(dt);
3303
3304
112k
    valuePush(ctxt, ret);
3305
112k
}
3306
3307
#define X_IN_Y(x, y)            \
3308
static void             \
3309
exsltDate##x##In##y##Function (xmlXPathParserContextPtr ctxt, \
3310
703k
            int nargs) {     \
3311
703k
    xmlChar *dt = NULL;           \
3312
703k
    double ret;             \
3313
703k
                \
3314
703k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
224
  xmlXPathSetArityError(ctxt);       \
3316
224
  return;             \
3317
224
    }                \
3318
703k
                \
3319
703k
    if (nargs == 1) {           \
3320
696k
  dt = xmlXPathPopString(ctxt);       \
3321
696k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
696k
    }                \
3326
703k
                \
3327
703k
    ret = exsltDate##x##In##y(dt);        \
3328
703k
                \
3329
703k
    if (dt != NULL)           \
3330
703k
  xmlFree(dt);           \
3331
703k
                \
3332
703k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
703k
}
date.c:exsltDateDayInMonthFunction
Line
Count
Source
3310
39.4k
            int nargs) {     \
3311
39.4k
    xmlChar *dt = NULL;           \
3312
39.4k
    double ret;             \
3313
39.4k
                \
3314
39.4k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
21
  xmlXPathSetArityError(ctxt);       \
3316
21
  return;             \
3317
21
    }                \
3318
39.4k
                \
3319
39.4k
    if (nargs == 1) {           \
3320
39.2k
  dt = xmlXPathPopString(ctxt);       \
3321
39.2k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
39.2k
    }                \
3326
39.4k
                \
3327
39.4k
    ret = exsltDate##x##In##y(dt);        \
3328
39.4k
                \
3329
39.4k
    if (dt != NULL)           \
3330
39.4k
  xmlFree(dt);           \
3331
39.4k
                \
3332
39.4k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
39.4k
}
date.c:exsltDateDayInWeekFunction
Line
Count
Source
3310
12.7k
            int nargs) {     \
3311
12.7k
    xmlChar *dt = NULL;           \
3312
12.7k
    double ret;             \
3313
12.7k
                \
3314
12.7k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
22
  xmlXPathSetArityError(ctxt);       \
3316
22
  return;             \
3317
22
    }                \
3318
12.7k
                \
3319
12.7k
    if (nargs == 1) {           \
3320
11.9k
  dt = xmlXPathPopString(ctxt);       \
3321
11.9k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
11.9k
    }                \
3326
12.7k
                \
3327
12.7k
    ret = exsltDate##x##In##y(dt);        \
3328
12.7k
                \
3329
12.7k
    if (dt != NULL)           \
3330
12.7k
  xmlFree(dt);           \
3331
12.7k
                \
3332
12.7k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
12.7k
}
date.c:exsltDateDayInYearFunction
Line
Count
Source
3310
20.0k
            int nargs) {     \
3311
20.0k
    xmlChar *dt = NULL;           \
3312
20.0k
    double ret;             \
3313
20.0k
                \
3314
20.0k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
22
  xmlXPathSetArityError(ctxt);       \
3316
22
  return;             \
3317
22
    }                \
3318
20.0k
                \
3319
20.0k
    if (nargs == 1) {           \
3320
17.5k
  dt = xmlXPathPopString(ctxt);       \
3321
17.5k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
17.5k
    }                \
3326
20.0k
                \
3327
20.0k
    ret = exsltDate##x##In##y(dt);        \
3328
20.0k
                \
3329
20.0k
    if (dt != NULL)           \
3330
20.0k
  xmlFree(dt);           \
3331
20.0k
                \
3332
20.0k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
20.0k
}
date.c:exsltDateDayOfWeekInMonthFunction
Line
Count
Source
3310
11.7k
            int nargs) {     \
3311
11.7k
    xmlChar *dt = NULL;           \
3312
11.7k
    double ret;             \
3313
11.7k
                \
3314
11.7k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
20
  xmlXPathSetArityError(ctxt);       \
3316
20
  return;             \
3317
20
    }                \
3318
11.7k
                \
3319
11.7k
    if (nargs == 1) {           \
3320
11.7k
  dt = xmlXPathPopString(ctxt);       \
3321
11.7k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
11.7k
    }                \
3326
11.7k
                \
3327
11.7k
    ret = exsltDate##x##In##y(dt);        \
3328
11.7k
                \
3329
11.7k
    if (dt != NULL)           \
3330
11.7k
  xmlFree(dt);           \
3331
11.7k
                \
3332
11.7k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
11.7k
}
date.c:exsltDateHourInDayFunction
Line
Count
Source
3310
56.1k
            int nargs) {     \
3311
56.1k
    xmlChar *dt = NULL;           \
3312
56.1k
    double ret;             \
3313
56.1k
                \
3314
56.1k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
22
  xmlXPathSetArityError(ctxt);       \
3316
22
  return;             \
3317
22
    }                \
3318
56.1k
                \
3319
56.1k
    if (nargs == 1) {           \
3320
56.0k
  dt = xmlXPathPopString(ctxt);       \
3321
56.0k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
56.0k
    }                \
3326
56.1k
                \
3327
56.1k
    ret = exsltDate##x##In##y(dt);        \
3328
56.1k
                \
3329
56.1k
    if (dt != NULL)           \
3330
56.1k
  xmlFree(dt);           \
3331
56.1k
                \
3332
56.1k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
56.1k
}
date.c:exsltDateMinuteInHourFunction
Line
Count
Source
3310
3.51k
            int nargs) {     \
3311
3.51k
    xmlChar *dt = NULL;           \
3312
3.51k
    double ret;             \
3313
3.51k
                \
3314
3.51k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
20
  xmlXPathSetArityError(ctxt);       \
3316
20
  return;             \
3317
20
    }                \
3318
3.51k
                \
3319
3.51k
    if (nargs == 1) {           \
3320
3.47k
  dt = xmlXPathPopString(ctxt);       \
3321
3.47k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
3.47k
    }                \
3326
3.49k
                \
3327
3.49k
    ret = exsltDate##x##In##y(dt);        \
3328
3.49k
                \
3329
3.49k
    if (dt != NULL)           \
3330
3.49k
  xmlFree(dt);           \
3331
3.49k
                \
3332
3.49k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
3.49k
}
date.c:exsltDateMonthInYearFunction
Line
Count
Source
3310
305k
            int nargs) {     \
3311
305k
    xmlChar *dt = NULL;           \
3312
305k
    double ret;             \
3313
305k
                \
3314
305k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
20
  xmlXPathSetArityError(ctxt);       \
3316
20
  return;             \
3317
20
    }                \
3318
305k
                \
3319
305k
    if (nargs == 1) {           \
3320
305k
  dt = xmlXPathPopString(ctxt);       \
3321
305k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
305k
    }                \
3326
305k
                \
3327
305k
    ret = exsltDate##x##In##y(dt);        \
3328
305k
                \
3329
305k
    if (dt != NULL)           \
3330
305k
  xmlFree(dt);           \
3331
305k
                \
3332
305k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
305k
}
date.c:exsltDateSecondInMinuteFunction
Line
Count
Source
3310
27.8k
            int nargs) {     \
3311
27.8k
    xmlChar *dt = NULL;           \
3312
27.8k
    double ret;             \
3313
27.8k
                \
3314
27.8k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
23
  xmlXPathSetArityError(ctxt);       \
3316
23
  return;             \
3317
23
    }                \
3318
27.8k
                \
3319
27.8k
    if (nargs == 1) {           \
3320
25.9k
  dt = xmlXPathPopString(ctxt);       \
3321
25.9k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
25.9k
    }                \
3326
27.8k
                \
3327
27.8k
    ret = exsltDate##x##In##y(dt);        \
3328
27.8k
                \
3329
27.8k
    if (dt != NULL)           \
3330
27.8k
  xmlFree(dt);           \
3331
27.8k
                \
3332
27.8k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
27.8k
}
date.c:exsltDateWeekInMonthFunction
Line
Count
Source
3310
122k
            int nargs) {     \
3311
122k
    xmlChar *dt = NULL;           \
3312
122k
    double ret;             \
3313
122k
                \
3314
122k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
31
  xmlXPathSetArityError(ctxt);       \
3316
31
  return;             \
3317
31
    }                \
3318
122k
                \
3319
122k
    if (nargs == 1) {           \
3320
122k
  dt = xmlXPathPopString(ctxt);       \
3321
122k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
122k
    }                \
3326
122k
                \
3327
122k
    ret = exsltDate##x##In##y(dt);        \
3328
122k
                \
3329
122k
    if (dt != NULL)           \
3330
122k
  xmlFree(dt);           \
3331
122k
                \
3332
122k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
122k
}
date.c:exsltDateWeekInYearFunction
Line
Count
Source
3310
103k
            int nargs) {     \
3311
103k
    xmlChar *dt = NULL;           \
3312
103k
    double ret;             \
3313
103k
                \
3314
103k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
23
  xmlXPathSetArityError(ctxt);       \
3316
23
  return;             \
3317
23
    }                \
3318
103k
                \
3319
103k
    if (nargs == 1) {           \
3320
103k
  dt = xmlXPathPopString(ctxt);       \
3321
103k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
103k
    }                \
3326
103k
                \
3327
103k
    ret = exsltDate##x##In##y(dt);        \
3328
103k
                \
3329
103k
    if (dt != NULL)           \
3330
103k
  xmlFree(dt);           \
3331
103k
                \
3332
103k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
103k
}
3334
3335
/**
3336
 * exsltDateMonthInYearFunction:
3337
 * @ctxt: an XPath parser context
3338
 * @nargs : the number of arguments
3339
 *
3340
 * Wraps exsltDateMonthInYear() for use by the XPath engine.
3341
 */
3342
X_IN_Y(Month,Year)
3343
3344
/**
3345
 * exsltDateMonthNameFunction:
3346
 * @ctxt: an XPath parser context
3347
 * @nargs : the number of arguments
3348
 *
3349
 * Wraps exsltDateMonthName() for use by the XPath engine.
3350
 */
3351
static void
3352
exsltDateMonthNameFunction (xmlXPathParserContextPtr ctxt, int nargs)
3353
61.9k
{
3354
61.9k
    xmlChar *dt = NULL;
3355
61.9k
    const xmlChar *ret;
3356
3357
61.9k
    if ((nargs < 0) || (nargs > 1)) {
3358
21
  xmlXPathSetArityError(ctxt);
3359
21
  return;
3360
21
    }
3361
3362
61.9k
    if (nargs == 1) {
3363
59.7k
  dt = xmlXPathPopString(ctxt);
3364
59.7k
  if (xmlXPathCheckError(ctxt)) {
3365
0
      xmlXPathSetTypeError(ctxt);
3366
0
      return;
3367
0
  }
3368
59.7k
    }
3369
3370
61.9k
    ret = exsltDateMonthName(dt);
3371
3372
61.9k
    if (dt != NULL)
3373
59.7k
  xmlFree(dt);
3374
3375
61.9k
    if (ret == NULL)
3376
0
  xmlXPathReturnEmptyString(ctxt);
3377
61.9k
    else
3378
61.9k
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3379
61.9k
}
3380
3381
/**
3382
 * exsltDateMonthAbbreviationFunction:
3383
 * @ctxt: an XPath parser context
3384
 * @nargs : the number of arguments
3385
 *
3386
 * Wraps exsltDateMonthAbbreviation() for use by the XPath engine.
3387
 */
3388
static void
3389
exsltDateMonthAbbreviationFunction (xmlXPathParserContextPtr ctxt, int nargs)
3390
4.53k
{
3391
4.53k
    xmlChar *dt = NULL;
3392
4.53k
    const xmlChar *ret;
3393
3394
4.53k
    if ((nargs < 0) || (nargs > 1)) {
3395
20
  xmlXPathSetArityError(ctxt);
3396
20
  return;
3397
20
    }
3398
3399
4.51k
    if (nargs == 1) {
3400
4.49k
  dt = xmlXPathPopString(ctxt);
3401
4.49k
  if (xmlXPathCheckError(ctxt)) {
3402
0
      xmlXPathSetTypeError(ctxt);
3403
0
      return;
3404
0
  }
3405
4.49k
    }
3406
3407
4.51k
    ret = exsltDateMonthAbbreviation(dt);
3408
3409
4.51k
    if (dt != NULL)
3410
4.49k
  xmlFree(dt);
3411
3412
4.51k
    if (ret == NULL)
3413
0
  xmlXPathReturnEmptyString(ctxt);
3414
4.51k
    else
3415
4.51k
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3416
4.51k
}
3417
3418
/**
3419
 * exsltDateWeekInYearFunction:
3420
 * @ctxt: an XPath parser context
3421
 * @nargs : the number of arguments
3422
 *
3423
 * Wraps exsltDateWeekInYear() for use by the XPath engine.
3424
 */
3425
X_IN_Y(Week,Year)
3426
3427
/**
3428
 * exsltDateWeekInMonthFunction:
3429
 * @ctxt: an XPath parser context
3430
 * @nargs : the number of arguments
3431
 *
3432
 * Wraps exsltDateWeekInMonthYear() for use by the XPath engine.
3433
 */
3434
X_IN_Y(Week,Month)
3435
3436
/**
3437
 * exsltDateDayInYearFunction:
3438
 * @ctxt: an XPath parser context
3439
 * @nargs : the number of arguments
3440
 *
3441
 * Wraps exsltDateDayInYear() for use by the XPath engine.
3442
 */
3443
X_IN_Y(Day,Year)
3444
3445
/**
3446
 * exsltDateDayInMonthFunction:
3447
 * @ctxt: an XPath parser context
3448
 * @nargs : the number of arguments
3449
 *
3450
 * Wraps exsltDateDayInMonth() for use by the XPath engine.
3451
 */
3452
X_IN_Y(Day,Month)
3453
3454
/**
3455
 * exsltDateDayOfWeekInMonthFunction:
3456
 * @ctxt: an XPath parser context
3457
 * @nargs : the number of arguments
3458
 *
3459
 * Wraps exsltDayOfWeekInMonth() for use by the XPath engine.
3460
 */
3461
X_IN_Y(DayOfWeek,Month)
3462
3463
/**
3464
 * exsltDateDayInWeekFunction:
3465
 * @ctxt: an XPath parser context
3466
 * @nargs : the number of arguments
3467
 *
3468
 * Wraps exsltDateDayInWeek() for use by the XPath engine.
3469
 */
3470
X_IN_Y(Day,Week)
3471
3472
/**
3473
 * exsltDateDayNameFunction:
3474
 * @ctxt: an XPath parser context
3475
 * @nargs : the number of arguments
3476
 *
3477
 * Wraps exsltDateDayName() for use by the XPath engine.
3478
 */
3479
static void
3480
exsltDateDayNameFunction (xmlXPathParserContextPtr ctxt, int nargs)
3481
97.9k
{
3482
97.9k
    xmlChar *dt = NULL;
3483
97.9k
    const xmlChar *ret;
3484
3485
97.9k
    if ((nargs < 0) || (nargs > 1)) {
3486
22
  xmlXPathSetArityError(ctxt);
3487
22
  return;
3488
22
    }
3489
3490
97.9k
    if (nargs == 1) {
3491
77.8k
  dt = xmlXPathPopString(ctxt);
3492
77.8k
  if (xmlXPathCheckError(ctxt)) {
3493
0
      xmlXPathSetTypeError(ctxt);
3494
0
      return;
3495
0
  }
3496
77.8k
    }
3497
3498
97.9k
    ret = exsltDateDayName(dt);
3499
3500
97.9k
    if (dt != NULL)
3501
77.8k
  xmlFree(dt);
3502
3503
97.9k
    if (ret == NULL)
3504
0
  xmlXPathReturnEmptyString(ctxt);
3505
97.9k
    else
3506
97.9k
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3507
97.9k
}
3508
3509
/**
3510
 * exsltDateMonthDayFunction:
3511
 * @ctxt: an XPath parser context
3512
 * @nargs : the number of arguments
3513
 *
3514
 * Wraps exsltDateDayAbbreviation() for use by the XPath engine.
3515
 */
3516
static void
3517
exsltDateDayAbbreviationFunction (xmlXPathParserContextPtr ctxt, int nargs)
3518
3.54k
{
3519
3.54k
    xmlChar *dt = NULL;
3520
3.54k
    const xmlChar *ret;
3521
3522
3.54k
    if ((nargs < 0) || (nargs > 1)) {
3523
20
  xmlXPathSetArityError(ctxt);
3524
20
  return;
3525
20
    }
3526
3527
3.52k
    if (nargs == 1) {
3528
3.30k
  dt = xmlXPathPopString(ctxt);
3529
3.30k
  if (xmlXPathCheckError(ctxt)) {
3530
0
      xmlXPathSetTypeError(ctxt);
3531
0
      return;
3532
0
  }
3533
3.30k
    }
3534
3535
3.52k
    ret = exsltDateDayAbbreviation(dt);
3536
3537
3.52k
    if (dt != NULL)
3538
3.30k
  xmlFree(dt);
3539
3540
3.52k
    if (ret == NULL)
3541
0
  xmlXPathReturnEmptyString(ctxt);
3542
3.52k
    else
3543
3.52k
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3544
3.52k
}
3545
3546
3547
/**
3548
 * exsltDateHourInDayFunction:
3549
 * @ctxt: an XPath parser context
3550
 * @nargs : the number of arguments
3551
 *
3552
 * Wraps exsltDateHourInDay() for use by the XPath engine.
3553
 */
3554
X_IN_Y(Hour,Day)
3555
3556
/**
3557
 * exsltDateMinuteInHourFunction:
3558
 * @ctxt: an XPath parser context
3559
 * @nargs : the number of arguments
3560
 *
3561
 * Wraps exsltDateMinuteInHour() for use by the XPath engine.
3562
 */
3563
X_IN_Y(Minute,Hour)
3564
3565
/**
3566
 * exsltDateSecondInMinuteFunction:
3567
 * @ctxt: an XPath parser context
3568
 * @nargs : the number of arguments
3569
 *
3570
 * Wraps exsltDateSecondInMinute() for use by the XPath engine.
3571
 */
3572
X_IN_Y(Second,Minute)
3573
3574
/**
3575
 * exsltDateSecondsFunction:
3576
 * @ctxt: an XPath parser context
3577
 * @nargs : the number of arguments
3578
 *
3579
 * Wraps exsltDateSeconds() for use by the XPath engine.
3580
 */
3581
static void
3582
exsltDateSecondsFunction (xmlXPathParserContextPtr ctxt, int nargs)
3583
527k
{
3584
527k
    xmlChar *str = NULL;
3585
527k
    double   ret;
3586
3587
527k
    if (nargs > 1) {
3588
24
  xmlXPathSetArityError(ctxt);
3589
24
  return;
3590
24
    }
3591
3592
527k
    if (nargs == 1) {
3593
427k
  str = xmlXPathPopString(ctxt);
3594
427k
  if (xmlXPathCheckError(ctxt)) {
3595
0
      xmlXPathSetTypeError(ctxt);
3596
0
      return;
3597
0
  }
3598
427k
    }
3599
3600
527k
    ret = exsltDateSeconds(str);
3601
527k
    if (str != NULL)
3602
427k
  xmlFree(str);
3603
3604
527k
    xmlXPathReturnNumber(ctxt, ret);
3605
527k
}
3606
3607
/**
3608
 * exsltDateAddFunction:
3609
 * @ctxt:  an XPath parser context
3610
 * @nargs:  the number of arguments
3611
 *
3612
 * Wraps exsltDateAdd() for use by the XPath processor.
3613
 */
3614
static void
3615
exsltDateAddFunction (xmlXPathParserContextPtr ctxt, int nargs)
3616
944k
{
3617
944k
    xmlChar *ret, *xstr, *ystr;
3618
3619
944k
    if (nargs != 2) {
3620
266
  xmlXPathSetArityError(ctxt);
3621
266
  return;
3622
266
    }
3623
944k
    ystr = xmlXPathPopString(ctxt);
3624
944k
    if (xmlXPathCheckError(ctxt))
3625
0
  return;
3626
3627
944k
    xstr = xmlXPathPopString(ctxt);
3628
944k
    if (xmlXPathCheckError(ctxt)) {
3629
0
        xmlFree(ystr);
3630
0
  return;
3631
0
    }
3632
3633
944k
    ret = exsltDateAdd(xstr, ystr);
3634
3635
944k
    xmlFree(ystr);
3636
944k
    xmlFree(xstr);
3637
3638
944k
    if (ret == NULL)
3639
677k
        xmlXPathReturnEmptyString(ctxt);
3640
267k
    else
3641
267k
  xmlXPathReturnString(ctxt, ret);
3642
944k
}
3643
3644
/**
3645
 * exsltDateAddDurationFunction:
3646
 * @ctxt:  an XPath parser context
3647
 * @nargs:  the number of arguments
3648
 *
3649
 * Wraps exsltDateAddDuration() for use by the XPath processor.
3650
 */
3651
static void
3652
exsltDateAddDurationFunction (xmlXPathParserContextPtr ctxt, int nargs)
3653
157k
{
3654
157k
    xmlChar *ret, *xstr, *ystr;
3655
3656
157k
    if (nargs != 2) {
3657
53
  xmlXPathSetArityError(ctxt);
3658
53
  return;
3659
53
    }
3660
157k
    ystr = xmlXPathPopString(ctxt);
3661
157k
    if (xmlXPathCheckError(ctxt))
3662
0
  return;
3663
3664
157k
    xstr = xmlXPathPopString(ctxt);
3665
157k
    if (xmlXPathCheckError(ctxt)) {
3666
0
        xmlFree(ystr);
3667
0
  return;
3668
0
    }
3669
3670
157k
    ret = exsltDateAddDuration(xstr, ystr);
3671
3672
157k
    xmlFree(ystr);
3673
157k
    xmlFree(xstr);
3674
3675
157k
    if (ret == NULL)
3676
96.9k
        xmlXPathReturnEmptyString(ctxt);
3677
60.9k
    else
3678
60.9k
  xmlXPathReturnString(ctxt, ret);
3679
157k
}
3680
3681
/**
3682
 * exsltDateDifferenceFunction:
3683
 * @ctxt:  an XPath parser context
3684
 * @nargs:  the number of arguments
3685
 *
3686
 * Wraps exsltDateDifference() for use by the XPath processor.
3687
 */
3688
static void
3689
exsltDateDifferenceFunction (xmlXPathParserContextPtr ctxt, int nargs)
3690
205k
{
3691
205k
    xmlChar *ret, *xstr, *ystr;
3692
3693
205k
    if (nargs != 2) {
3694
75
  xmlXPathSetArityError(ctxt);
3695
75
  return;
3696
75
    }
3697
205k
    ystr = xmlXPathPopString(ctxt);
3698
205k
    if (xmlXPathCheckError(ctxt))
3699
0
  return;
3700
3701
205k
    xstr = xmlXPathPopString(ctxt);
3702
205k
    if (xmlXPathCheckError(ctxt)) {
3703
0
        xmlFree(ystr);
3704
0
  return;
3705
0
    }
3706
3707
205k
    ret = exsltDateDifference(xstr, ystr);
3708
3709
205k
    xmlFree(ystr);
3710
205k
    xmlFree(xstr);
3711
3712
205k
    if (ret == NULL)
3713
106k
        xmlXPathReturnEmptyString(ctxt);
3714
99.0k
    else
3715
99.0k
  xmlXPathReturnString(ctxt, ret);
3716
205k
}
3717
3718
/**
3719
 * exsltDateDurationFunction:
3720
 * @ctxt: an XPath parser context
3721
 * @nargs : the number of arguments
3722
 *
3723
 * Wraps exsltDateDuration() for use by the XPath engine
3724
 */
3725
static void
3726
exsltDateDurationFunction (xmlXPathParserContextPtr ctxt, int nargs)
3727
490k
{
3728
490k
    xmlChar *ret;
3729
490k
    xmlChar *number = NULL;
3730
3731
490k
    if ((nargs < 0) || (nargs > 1)) {
3732
24
  xmlXPathSetArityError(ctxt);
3733
24
  return;
3734
24
    }
3735
3736
490k
    if (nargs == 1) {
3737
480k
  number = xmlXPathPopString(ctxt);
3738
480k
  if (xmlXPathCheckError(ctxt)) {
3739
0
      xmlXPathSetTypeError(ctxt);
3740
0
      return;
3741
0
  }
3742
480k
    }
3743
3744
490k
    ret = exsltDateDuration(number);
3745
3746
490k
    if (number != NULL)
3747
480k
  xmlFree(number);
3748
3749
490k
    if (ret == NULL)
3750
64.9k
  xmlXPathReturnEmptyString(ctxt);
3751
425k
    else
3752
425k
  xmlXPathReturnString(ctxt, ret);
3753
490k
}
3754
3755
/**
3756
 * exsltDateRegister:
3757
 *
3758
 * Registers the EXSLT - Dates and Times module
3759
 */
3760
void
3761
exsltDateRegister (void)
3762
3.71k
{
3763
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "add",
3764
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3765
3.71k
           exsltDateAddFunction);
3766
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "add-duration",
3767
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3768
3.71k
           exsltDateAddDurationFunction);
3769
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "date",
3770
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3771
3.71k
           exsltDateDateFunction);
3772
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "date-time",
3773
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3774
3.71k
           exsltDateDateTimeFunction);
3775
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-abbreviation",
3776
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3777
3.71k
           exsltDateDayAbbreviationFunction);
3778
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-in-month",
3779
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3780
3.71k
           exsltDateDayInMonthFunction);
3781
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-in-week",
3782
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3783
3.71k
           exsltDateDayInWeekFunction);
3784
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-in-year",
3785
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3786
3.71k
           exsltDateDayInYearFunction);
3787
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-name",
3788
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3789
3.71k
           exsltDateDayNameFunction);
3790
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-of-week-in-month",
3791
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3792
3.71k
           exsltDateDayOfWeekInMonthFunction);
3793
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "difference",
3794
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3795
3.71k
           exsltDateDifferenceFunction);
3796
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "duration",
3797
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3798
3.71k
           exsltDateDurationFunction);
3799
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "hour-in-day",
3800
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3801
3.71k
           exsltDateHourInDayFunction);
3802
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "leap-year",
3803
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3804
3.71k
           exsltDateLeapYearFunction);
3805
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "minute-in-hour",
3806
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3807
3.71k
           exsltDateMinuteInHourFunction);
3808
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "month-abbreviation",
3809
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3810
3.71k
           exsltDateMonthAbbreviationFunction);
3811
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "month-in-year",
3812
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3813
3.71k
           exsltDateMonthInYearFunction);
3814
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "month-name",
3815
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3816
3.71k
           exsltDateMonthNameFunction);
3817
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "second-in-minute",
3818
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3819
3.71k
           exsltDateSecondInMinuteFunction);
3820
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "seconds",
3821
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3822
3.71k
           exsltDateSecondsFunction);
3823
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "sum",
3824
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3825
3.71k
           exsltDateSumFunction);
3826
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "time",
3827
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3828
3.71k
           exsltDateTimeFunction);
3829
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "week-in-month",
3830
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3831
3.71k
           exsltDateWeekInMonthFunction);
3832
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "week-in-year",
3833
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3834
3.71k
           exsltDateWeekInYearFunction);
3835
3.71k
    xsltRegisterExtModuleFunction ((const xmlChar *) "year",
3836
3.71k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3837
3.71k
           exsltDateYearFunction);
3838
3.71k
}
3839
3840
/**
3841
 * exsltDateXpathCtxtRegister:
3842
 *
3843
 * Registers the EXSLT - Dates and Times module for use outside XSLT
3844
 */
3845
int
3846
exsltDateXpathCtxtRegister (xmlXPathContextPtr ctxt, const xmlChar *prefix)
3847
0
{
3848
0
    if (ctxt
3849
0
        && prefix
3850
0
        && !xmlXPathRegisterNs(ctxt,
3851
0
                               prefix,
3852
0
                               (const xmlChar *) EXSLT_DATE_NAMESPACE)
3853
0
        && !xmlXPathRegisterFuncNS(ctxt,
3854
0
                                   (const xmlChar *) "add",
3855
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3856
0
                                   exsltDateAddFunction)
3857
0
        && !xmlXPathRegisterFuncNS(ctxt,
3858
0
                                   (const xmlChar *) "add-duration",
3859
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3860
0
                                   exsltDateAddDurationFunction)
3861
0
        && !xmlXPathRegisterFuncNS(ctxt,
3862
0
                                   (const xmlChar *) "date",
3863
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3864
0
                                   exsltDateDateFunction)
3865
0
        && !xmlXPathRegisterFuncNS(ctxt,
3866
0
                                   (const xmlChar *) "date-time",
3867
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3868
0
                                   exsltDateDateTimeFunction)
3869
0
        && !xmlXPathRegisterFuncNS(ctxt,
3870
0
                                   (const xmlChar *) "day-abbreviation",
3871
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3872
0
                                   exsltDateDayAbbreviationFunction)
3873
0
        && !xmlXPathRegisterFuncNS(ctxt,
3874
0
                                   (const xmlChar *) "day-in-month",
3875
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3876
0
                                   exsltDateDayInMonthFunction)
3877
0
        && !xmlXPathRegisterFuncNS(ctxt,
3878
0
                                   (const xmlChar *) "day-in-week",
3879
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3880
0
                                   exsltDateDayInWeekFunction)
3881
0
        && !xmlXPathRegisterFuncNS(ctxt,
3882
0
                                   (const xmlChar *) "day-in-year",
3883
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3884
0
                                   exsltDateDayInYearFunction)
3885
0
        && !xmlXPathRegisterFuncNS(ctxt,
3886
0
                                   (const xmlChar *) "day-name",
3887
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3888
0
                                   exsltDateDayNameFunction)
3889
0
        && !xmlXPathRegisterFuncNS(ctxt,
3890
0
                                   (const xmlChar *) "day-of-week-in-month",
3891
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3892
0
                                   exsltDateDayOfWeekInMonthFunction)
3893
0
        && !xmlXPathRegisterFuncNS(ctxt,
3894
0
                                   (const xmlChar *) "difference",
3895
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3896
0
                                   exsltDateDifferenceFunction)
3897
0
        && !xmlXPathRegisterFuncNS(ctxt,
3898
0
                                   (const xmlChar *) "duration",
3899
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3900
0
                                   exsltDateDurationFunction)
3901
0
        && !xmlXPathRegisterFuncNS(ctxt,
3902
0
                                   (const xmlChar *) "hour-in-day",
3903
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3904
0
                                   exsltDateHourInDayFunction)
3905
0
        && !xmlXPathRegisterFuncNS(ctxt,
3906
0
                                   (const xmlChar *) "leap-year",
3907
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3908
0
                                   exsltDateLeapYearFunction)
3909
0
        && !xmlXPathRegisterFuncNS(ctxt,
3910
0
                                   (const xmlChar *) "minute-in-hour",
3911
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3912
0
                                   exsltDateMinuteInHourFunction)
3913
0
        && !xmlXPathRegisterFuncNS(ctxt,
3914
0
                                   (const xmlChar *) "month-abbreviation",
3915
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3916
0
                                   exsltDateMonthAbbreviationFunction)
3917
0
        && !xmlXPathRegisterFuncNS(ctxt,
3918
0
                                   (const xmlChar *) "month-in-year",
3919
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3920
0
                                   exsltDateMonthInYearFunction)
3921
0
        && !xmlXPathRegisterFuncNS(ctxt,
3922
0
                                   (const xmlChar *) "month-name",
3923
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3924
0
                                   exsltDateMonthNameFunction)
3925
0
        && !xmlXPathRegisterFuncNS(ctxt,
3926
0
                                   (const xmlChar *) "second-in-minute",
3927
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3928
0
                                   exsltDateSecondInMinuteFunction)
3929
0
        && !xmlXPathRegisterFuncNS(ctxt,
3930
0
                                   (const xmlChar *) "seconds",
3931
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3932
0
                                   exsltDateSecondsFunction)
3933
0
        && !xmlXPathRegisterFuncNS(ctxt,
3934
0
                                   (const xmlChar *) "sum",
3935
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3936
0
                                   exsltDateSumFunction)
3937
0
        && !xmlXPathRegisterFuncNS(ctxt,
3938
0
                                   (const xmlChar *) "time",
3939
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3940
0
                                   exsltDateTimeFunction)
3941
0
        && !xmlXPathRegisterFuncNS(ctxt,
3942
0
                                   (const xmlChar *) "week-in-month",
3943
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3944
0
                                   exsltDateWeekInMonthFunction)
3945
0
        && !xmlXPathRegisterFuncNS(ctxt,
3946
0
                                   (const xmlChar *) "week-in-year",
3947
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3948
0
                                   exsltDateWeekInYearFunction)
3949
0
        && !xmlXPathRegisterFuncNS(ctxt,
3950
0
                                   (const xmlChar *) "year",
3951
0
                                   (const xmlChar *) EXSLT_DATE_NAMESPACE,
3952
0
                                   exsltDateYearFunction)) {
3953
0
        return 0;
3954
0
    }
3955
0
    return -1;
3956
0
}