Coverage Report

Created: 2024-05-15 07:11

/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
7.67k
  ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
105
106
1.12k
#define VALID_ALWAYS(num) (num >= 0)
107
8.09k
#define VALID_MONTH(mon)        ((mon >= 1) && (mon <= 12))
108
/* VALID_DAY should only be used when month is unknown */
109
1.95k
#define VALID_DAY(day)          ((day >= 1) && (day <= 31))
110
9.57k
#define VALID_HOUR(hr)          ((hr >= 0) && (hr <= 23))
111
1.43k
#define VALID_MIN(min)          ((min >= 0) && (min <= 59))
112
4.43k
#define VALID_SEC(sec)          ((sec >= 0) && (sec < 60))
113
2.31k
#define VALID_TZO(tzo)          ((tzo > -1440) && (tzo < 1440))
114
#define IS_LEAP(y)            \
115
67.2k
  (((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
63.7k
        (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
124
125
#define VALID_MDAY(dt)            \
126
2.87k
  (IS_LEAP(dt->year) ?                \
127
2.87k
      (dt->day <= daysInMonthLeap[dt->mon - 1]) :          \
128
2.87k
      (dt->day <= daysInMonth[dt->mon - 1]))
129
130
#define VALID_DATE(dt)            \
131
3.89k
  (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
2.21k
  ((dt->hour <=23 ) && (dt->min <= 59) &&     \
139
2.21k
   VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
140
141
#define VALID_DATETIME(dt)          \
142
1.02k
  (VALID_DATE(dt) && VALID_TIME(dt))
143
144
22.9k
#define SECS_PER_MIN            60
145
22.9k
#define MINS_PER_HOUR           60
146
23.1k
#define HOURS_PER_DAY           24
147
19.2k
#define SECS_PER_HOUR           (MINS_PER_HOUR * SECS_PER_MIN)
148
15.4k
#define SECS_PER_DAY            (HOURS_PER_DAY * SECS_PER_HOUR)
149
3.69k
#define MINS_PER_DAY            (HOURS_PER_DAY * MINS_PER_HOUR)
150
220
#define DAYS_PER_EPOCH          (400 * 365 + 100 - 4 + 1)
151
110
#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
557
        ((IS_LEAP(year) ?          \
160
557
                dayInLeapYearByMonth[month - 1] :    \
161
557
                dayInYearByMonth[month - 1]) + day)
162
163
28.4k
#define YEAR_MAX LONG_MAX
164
5.40k
#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
4.29k
{
185
4.29k
    const xmlChar *cur = *str, *firstChar;
186
4.29k
    int isneg = 0, digcnt = 0;
187
188
4.29k
    if (((*cur < '0') || (*cur > '9')) &&
189
4.29k
  (*cur != '-') && (*cur != '+'))
190
292
  return -1;
191
192
4.00k
    if (*cur == '-') {
193
130
  isneg = 1;
194
130
  cur++;
195
130
    }
196
197
4.00k
    firstChar = cur;
198
199
32.4k
    while ((*cur >= '0') && (*cur <= '9')) {
200
28.4k
        if (dt->year >= YEAR_MAX / 10) /* Not really exact */
201
29
            return -1;
202
28.4k
  dt->year = dt->year * 10 + (*cur - '0');
203
28.4k
  cur++;
204
28.4k
  digcnt++;
205
28.4k
    }
206
207
    /* year must be at least 4 digits (CCYY); over 4
208
     * digits cannot have a leading zero. */
209
3.97k
    if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
210
580
  return 1;
211
212
3.39k
    if (dt->year == 0)
213
18
  return 2;
214
215
    /* The internal representation of negative years is continuous. */
216
3.37k
    if (isneg)
217
51
  dt->year = -dt->year + 1;
218
219
3.37k
    *str = cur;
220
221
#ifdef DEBUG_EXSLT_DATE
222
    xsltGenericDebug(xsltGenericDebugContext,
223
         "Parsed year %04ld\n", dt->year);
224
#endif
225
226
3.37k
    return 0;
227
3.39k
}
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
183
  if (yr <= 0) {                 \
239
16
      *cur = '-';           \
240
16
      cur++;            \
241
16
  }              \
242
183
  {             \
243
183
      long year = (yr <= 0) ? -yr + 1 : yr;               \
244
183
      xmlChar tmp_buf[100], *tmp = tmp_buf;   \
245
183
      /* result is in reverse-order */      \
246
1.11k
      while (year > 0) {         \
247
934
    *tmp = '0' + (xmlChar)(year % 10);    \
248
934
    year /= 10;         \
249
934
    tmp++;            \
250
934
      }              \
251
183
      /* virtually adds leading zeros */      \
252
203
      while ((tmp - tmp_buf) < 4)       \
253
183
    *tmp++ = '0';         \
254
183
      /* restore the correct order */     \
255
1.13k
      while (tmp > tmp_buf) {       \
256
954
    tmp--;            \
257
954
    *cur = *tmp;          \
258
954
    cur++;            \
259
954
      }              \
260
183
  }
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
18.2k
  if ((cur[0] < '0') || (cur[0] > '9') ||     \
276
18.2k
      (cur[1] < '0') || (cur[1] > '9'))     \
277
18.2k
      invalid = 1;         \
278
18.2k
  else {             \
279
16.4k
      int val;            \
280
16.4k
      val = (cur[0] - '0') * 10 + (cur[1] - '0');   \
281
16.4k
      if (!func(val))          \
282
16.4k
          invalid = 2;         \
283
16.4k
      else            \
284
16.4k
          num = val;         \
285
16.4k
  }              \
286
18.2k
  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
361
  *cur = '0' + ((num / 10) % 10);       \
298
361
  cur++;              \
299
361
  *cur = '0' + (num % 10);        \
300
361
  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
1.21k
  PARSE_2_DIGITS(num, cur, VALID_ALWAYS, invalid); \
316
1.21k
  if (!invalid && (*cur == '.')) {     \
317
136
      double mult = 1;                \
318
136
      cur++;            \
319
136
      if ((*cur < '0') || (*cur > '9'))     \
320
136
    invalid = 1;         \
321
3.86k
      while ((*cur >= '0') && (*cur <= '9')) {   \
322
3.72k
    mult /= 10;         \
323
3.72k
    num += (*cur - '0') * mult;     \
324
3.72k
    cur++;            \
325
3.72k
      }              \
326
136
  }
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.75k
{
342
2.75k
    const xmlChar *cur = *str;
343
2.75k
    int ret = 0;
344
345
2.75k
    PARSE_2_DIGITS(dt->mon, cur, VALID_MONTH, ret);
346
2.75k
    if (ret != 0)
347
476
  return ret;
348
349
2.28k
    *str = cur;
350
351
#ifdef DEBUG_EXSLT_DATE
352
    xsltGenericDebug(xsltGenericDebugContext,
353
         "Parsed month %02i\n", dt->mon);
354
#endif
355
356
2.28k
    return 0;
357
2.75k
}
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
181
  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
2.22k
{
384
2.22k
    const xmlChar *cur = *str;
385
2.22k
    int ret = 0;
386
387
2.22k
    PARSE_2_DIGITS(dt->day, cur, VALID_DAY, ret);
388
2.22k
    if (ret != 0)
389
293
  return ret;
390
391
1.92k
    *str = cur;
392
393
#ifdef DEBUG_EXSLT_DATE
394
    xsltGenericDebug(xsltGenericDebugContext,
395
         "Parsed day %02i\n", dt->day);
396
#endif
397
398
1.92k
    return 0;
399
2.22k
}
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
180
  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
180
  FORMAT_GYEAR(dt->year, cur);        \
422
180
  *cur = '-';           \
423
180
  cur++;              \
424
180
  FORMAT_GMONTH(dt->mon, cur);       \
425
180
  *cur = '-';           \
426
180
  cur++;              \
427
180
  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
5.60k
{
444
5.60k
    const xmlChar *cur = *str;
445
5.60k
    unsigned int hour = 0; /* use temp var in case str is not xs:time */
446
5.60k
    int ret = 0;
447
448
5.60k
    PARSE_2_DIGITS(hour, cur, VALID_HOUR, ret);
449
5.60k
    if (ret != 0)
450
487
  return ret;
451
452
5.11k
    if (*cur != ':')
453
3.66k
  return 1;
454
1.45k
    cur++;
455
456
    /* the ':' insures this string is xs:time */
457
1.45k
    dt->hour = hour;
458
459
1.45k
    PARSE_2_DIGITS(dt->min, cur, VALID_MIN, ret);
460
1.45k
    if (ret != 0)
461
191
  return ret;
462
463
1.26k
    if (*cur != ':')
464
51
  return 1;
465
1.21k
    cur++;
466
467
1.21k
    PARSE_FLOAT(dt->sec, cur, ret);
468
1.21k
    if (ret != 0)
469
111
  return ret;
470
471
1.10k
    if (!VALID_TIME(dt))
472
27
  return 2;
473
474
1.07k
    *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
1.07k
    return 0;
483
1.10k
}
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
6.35k
{
499
6.35k
    const xmlChar *cur;
500
6.35k
    int ret = 0;
501
502
6.35k
    if (str == NULL)
503
0
  return -1;
504
6.35k
    cur = *str;
505
6.35k
    switch (*cur) {
506
1.36k
    case 0:
507
1.36k
  dt->tz_flag = 0;
508
1.36k
  dt->tzo = 0;
509
1.36k
  break;
510
511
57
    case 'Z':
512
57
  dt->tz_flag = 1;
513
57
  dt->tzo = 0;
514
57
  cur++;
515
57
  break;
516
517
70
    case '+':
518
4.84k
    case '-': {
519
4.84k
  int isneg = 0, tmp = 0;
520
4.84k
  isneg = (*cur == '-');
521
522
4.84k
  cur++;
523
524
4.84k
  PARSE_2_DIGITS(tmp, cur, VALID_HOUR, ret);
525
4.84k
  if (ret != 0)
526
531
      return ret;
527
528
4.31k
  if (*cur != ':')
529
4.13k
      return 1;
530
183
  cur++;
531
532
183
  dt->tzo = tmp * 60;
533
534
183
  PARSE_2_DIGITS(tmp, cur, VALID_MIN, ret);
535
183
  if (ret != 0)
536
57
      return ret;
537
538
126
  dt->tzo += tmp;
539
126
  if (isneg)
540
120
      dt->tzo = - dt->tzo;
541
542
126
  if (!VALID_TZO(dt->tzo))
543
0
      return 2;
544
545
126
  break;
546
126
      }
547
126
    default:
548
89
  return 1;
549
6.35k
    }
550
551
1.54k
    *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.54k
    return 0;
560
6.35k
}
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
126
  if (tzo == 0) {                 \
572
126
      *cur = 'Z';           \
573
126
      cur++;            \
574
126
  } else {           \
575
0
      int aTzo = (tzo < 0) ? - tzo : tzo;                 \
576
0
      int tzHh = aTzo / 60, tzMm = aTzo % 60;   \
577
0
      *cur = (tzo < 0) ? '-' : '+' ;      \
578
0
      cur++;            \
579
0
      FORMAT_2_DIGITS(tzHh, cur);        \
580
0
      *cur = ':';           \
581
0
      cur++;            \
582
0
      FORMAT_2_DIGITS(tzMm, cur);        \
583
0
  }
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
5.33k
{
602
5.33k
    exsltDateValPtr ret;
603
604
5.33k
    ret = (exsltDateValPtr) xmlMalloc(sizeof(exsltDateVal));
605
5.33k
    if (ret == NULL) {
606
0
  xsltGenericError(xsltGenericErrorContext,
607
0
       "exsltDateCreateDate: out of memory\n");
608
0
  return (NULL);
609
0
    }
610
5.33k
    memset (ret, 0, sizeof(exsltDateVal));
611
612
5.33k
    ret->mon = 1;
613
5.33k
    ret->day = 1;
614
615
5.33k
    if (type != EXSLT_UNKNOWN)
616
592
        ret->type = type;
617
618
5.33k
    return ret;
619
5.33k
}
620
621
/**
622
 * exsltDateFreeDate:
623
 * @date: an #exsltDateValPtr
624
 *
625
 * Frees up the @date
626
 */
627
static void
628
5.33k
exsltDateFreeDate (exsltDateValPtr date) {
629
5.33k
    if (date == NULL)
630
0
  return;
631
632
5.33k
    xmlFree(date);
633
5.33k
}
634
635
/**
636
 * exsltDateCreateDuration:
637
 *
638
 * Creates a new #exsltDateDurVal, uninitialized.
639
 *
640
 * Returns the #exsltDateDurValPtr
641
 */
642
static exsltDateDurValPtr
643
exsltDateCreateDuration (void)
644
4.82k
{
645
4.82k
    exsltDateDurValPtr ret;
646
647
4.82k
    ret = (exsltDateDurValPtr) xmlMalloc(sizeof(exsltDateDurVal));
648
4.82k
    if (ret == NULL) {
649
0
  xsltGenericError(xsltGenericErrorContext,
650
0
       "exsltDateCreateDuration: out of memory\n");
651
0
  return (NULL);
652
0
    }
653
4.82k
    memset (ret, 0, sizeof(exsltDateDurVal));
654
655
4.82k
    return ret;
656
4.82k
}
657
658
/**
659
 * exsltDateFreeDuration:
660
 * @date: an #exsltDateDurValPtr
661
 *
662
 * Frees up the @duration
663
 */
664
static void
665
4.82k
exsltDateFreeDuration (exsltDateDurValPtr duration) {
666
4.82k
    if (duration == NULL)
667
0
  return;
668
669
4.82k
    xmlFree(duration);
670
4.82k
}
671
672
/**
673
 * exsltDateCurrent:
674
 *
675
 * Returns the current date and time.
676
 */
677
static exsltDateValPtr
678
exsltDateCurrent (void)
679
381
{
680
381
    struct tm localTm, gmTm;
681
#if !defined(HAVE_GMTIME_R) && !defined(_WIN32)
682
    struct tm *tb = NULL;
683
#endif
684
381
    time_t secs;
685
381
    int local_s, gm_s;
686
381
    exsltDateValPtr ret;
687
381
    char *source_date_epoch;
688
381
    int override = 0;
689
690
381
    ret = exsltDateCreateDate(XS_DATETIME);
691
381
    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
381
    source_date_epoch = getenv("SOURCE_DATE_EPOCH");
699
381
    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
381
    if (override == 0) {
721
    /* get current time */
722
381
  secs    = time(NULL);
723
724
#ifdef _WIN32
725
  localtime_s(&localTm, &secs);
726
#elif HAVE_LOCALTIME_R
727
381
  localtime_r(&secs, &localTm);
728
#else
729
  localTm = *localtime(&secs);
730
#endif
731
381
    }
732
733
    /* get real year, not years since 1900 */
734
381
    ret->year = localTm.tm_year + 1900;
735
736
381
    ret->mon  = localTm.tm_mon + 1;
737
381
    ret->day  = localTm.tm_mday;
738
381
    ret->hour = localTm.tm_hour;
739
381
    ret->min  = localTm.tm_min;
740
741
    /* floating point seconds */
742
381
    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
381
    gmtime_r(&secs, &gmTm);
749
#else
750
    tb = gmtime(&secs);
751
    if (tb == NULL)
752
        return NULL;
753
    gmTm = *tb;
754
#endif
755
381
    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
381
    local_s = localTm.tm_hour * SECS_PER_HOUR +
764
381
        localTm.tm_min * SECS_PER_MIN +
765
381
        localTm.tm_sec;
766
767
381
    gm_s = gmTm.tm_hour * SECS_PER_HOUR +
768
381
        gmTm.tm_min * SECS_PER_MIN +
769
381
        gmTm.tm_sec;
770
771
381
    if (localTm.tm_year < gmTm.tm_year) {
772
0
  ret->tzo = -((SECS_PER_DAY - local_s) + gm_s)/60;
773
381
    } else if (localTm.tm_year > gmTm.tm_year) {
774
0
  ret->tzo = ((SECS_PER_DAY - gm_s) + local_s)/60;
775
381
    } else if (localTm.tm_mon < gmTm.tm_mon) {
776
0
  ret->tzo = -((SECS_PER_DAY - local_s) + gm_s)/60;
777
381
    } else if (localTm.tm_mon > gmTm.tm_mon) {
778
0
  ret->tzo = ((SECS_PER_DAY - gm_s) + local_s)/60;
779
381
    } else if (localTm.tm_mday < gmTm.tm_mday) {
780
0
  ret->tzo = -((SECS_PER_DAY - local_s) + gm_s)/60;
781
381
    } else if (localTm.tm_mday > gmTm.tm_mday) {
782
0
  ret->tzo = ((SECS_PER_DAY - gm_s) + local_s)/60;
783
381
    } else  {
784
381
  ret->tzo = (local_s - gm_s)/60;
785
381
    }
786
787
381
    return ret;
788
381
}
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.74k
{
801
4.74k
    exsltDateValPtr dt;
802
4.74k
    int ret;
803
4.74k
    const xmlChar *cur = dateTime;
804
805
4.74k
#define RETURN_TYPE_IF_VALID(t)         \
806
7.67k
    if (IS_TZO_CHAR(*cur)) {         \
807
5.39k
  ret = _exsltDateParseTimeZone(dt, &cur);    \
808
5.39k
  if (ret == 0) {           \
809
690
      if (*cur != 0)         \
810
690
    goto error;         \
811
690
      dt->type = t;         \
812
602
      return dt;           \
813
690
  }              \
814
5.39k
    }
815
816
4.74k
    if (dateTime == NULL)
817
0
  return NULL;
818
819
4.74k
    if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
820
0
  return NULL;
821
822
4.74k
    dt = exsltDateCreateDate(EXSLT_UNKNOWN);
823
4.74k
    if (dt == NULL)
824
0
  return NULL;
825
826
4.74k
    if ((cur[0] == '-') && (cur[1] == '-')) {
827
  /*
828
   * It's an incomplete date (xs:gMonthDay, xs:gMonth or
829
   * xs:gDay)
830
   */
831
378
  cur += 2;
832
833
  /* is it an xs:gDay? */
834
378
  if (*cur == '-') {
835
195
    ++cur;
836
195
      ret = _exsltDateParseGDay(dt, &cur);
837
195
      if (ret != 0)
838
132
    goto error;
839
840
63
      RETURN_TYPE_IF_VALID(XS_GDAY);
841
842
55
      goto error;
843
63
  }
844
845
  /*
846
   * it should be an xs:gMonthDay or xs:gMonth
847
   */
848
183
  ret = _exsltDateParseGMonth(dt, &cur);
849
183
  if (ret != 0)
850
144
      goto error;
851
852
39
  if (*cur != '-')
853
4
      goto error;
854
35
  cur++;
855
856
  /* is it an xs:gMonth? */
857
35
  if (*cur == '-') {
858
14
      cur++;
859
14
      RETURN_TYPE_IF_VALID(XS_GMONTH);
860
14
      goto error;
861
14
  }
862
863
  /* it should be an xs:gMonthDay */
864
21
  ret = _exsltDateParseGDay(dt, &cur);
865
21
  if (ret != 0)
866
9
      goto error;
867
868
12
  RETURN_TYPE_IF_VALID(XS_GMONTHDAY);
869
870
9
  goto error;
871
12
    }
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
4.36k
    if ((*cur >= '0') && (*cur <= '9')) {
878
3.92k
  ret = _exsltDateParseTime(dt, &cur);
879
3.92k
  if (ret == 0) {
880
      /* it's an xs:time */
881
118
      RETURN_TYPE_IF_VALID(XS_TIME);
882
42
  }
883
3.92k
    }
884
885
    /* fallback on date parsing */
886
4.29k
    cur = dateTime;
887
888
4.29k
    ret = _exsltDateParseGYear(dt, &cur);
889
4.29k
    if (ret != 0)
890
919
  goto error;
891
892
    /* is it an xs:gYear? */
893
3.37k
    RETURN_TYPE_IF_VALID(XS_GYEAR);
894
895
2.92k
    if (*cur != '-')
896
349
  goto error;
897
2.57k
    cur++;
898
899
2.57k
    ret = _exsltDateParseGMonth(dt, &cur);
900
2.57k
    if (ret != 0)
901
332
  goto error;
902
903
    /* is it an xs:gYearMonth? */
904
2.24k
    RETURN_TYPE_IF_VALID(XS_GYEARMONTH);
905
906
2.14k
    if (*cur != '-')
907
139
  goto error;
908
2.00k
    cur++;
909
910
2.00k
    ret = _exsltDateParseGDay(dt, &cur);
911
2.00k
    if ((ret != 0) || !VALID_DATE(dt))
912
156
  goto error;
913
914
    /* is it an xs:date? */
915
1.84k
    RETURN_TYPE_IF_VALID(XS_DATE);
916
917
1.79k
    if (*cur != 'T')
918
112
  goto error;
919
1.68k
    cur++;
920
921
    /* it should be an xs:dateTime */
922
1.68k
    ret = _exsltDateParseTime(dt, &cur);
923
1.68k
    if (ret != 0)
924
722
  goto error;
925
926
960
    ret = _exsltDateParseTimeZone(dt, &cur);
927
960
    if ((ret != 0) || (*cur != 0) || !VALID_DATETIME(dt))
928
117
  goto error;
929
930
843
    dt->type = XS_DATETIME;
931
932
843
    return dt;
933
934
3.30k
error:
935
3.30k
    if (dt != NULL)
936
3.30k
  exsltDateFreeDate(dt);
937
3.30k
    return NULL;
938
960
}
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
3.88k
{
951
3.88k
    const xmlChar  *cur = duration;
952
3.88k
    exsltDateDurValPtr dur;
953
3.88k
    int isneg = 0;
954
3.88k
    unsigned int seq = 0;
955
3.88k
    long days, secs = 0;
956
3.88k
    double sec_frac = 0.0;
957
958
3.88k
    if (duration == NULL)
959
0
  return NULL;
960
961
3.88k
    if (*cur == '-') {
962
2.55k
        isneg = 1;
963
2.55k
        cur++;
964
2.55k
    }
965
966
    /* duration must start with 'P' (after sign) */
967
3.88k
    if (*cur++ != 'P')
968
282
  return NULL;
969
970
3.60k
    if (*cur == 0)
971
17
  return NULL;
972
973
3.58k
    dur = exsltDateCreateDuration();
974
3.58k
    if (dur == NULL)
975
0
  return NULL;
976
977
18.2k
    while (*cur != 0) {
978
15.6k
        long           num = 0;
979
15.6k
        size_t         has_digits = 0;
980
15.6k
        int            has_frac = 0;
981
15.6k
        const xmlChar  desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
982
983
        /* input string should be empty or invalid date/time item */
984
15.6k
        if (seq >= sizeof(desig))
985
57
            goto error;
986
987
        /* T designator must be present for time items */
988
15.5k
        if (*cur == 'T') {
989
2.67k
            if (seq > 3)
990
0
                goto error;
991
2.67k
            cur++;
992
2.67k
            seq = 3;
993
12.9k
        } else if (seq == 3)
994
47
            goto error;
995
996
        /* Parse integral part. */
997
55.4k
        while (*cur >= '0' && *cur <= '9') {
998
39.9k
            long digit = *cur - '0';
999
1000
39.9k
            if (num > LONG_MAX / 10)
1001
24
                goto error;
1002
39.8k
            num *= 10;
1003
39.8k
            if (num > LONG_MAX - digit)
1004
0
                goto error;
1005
39.8k
            num += digit;
1006
1007
39.8k
            has_digits = 1;
1008
39.8k
            cur++;
1009
39.8k
        }
1010
1011
15.5k
        if (*cur == '.') {
1012
            /* Parse fractional part. */
1013
329
            double mult = 1.0;
1014
329
            cur++;
1015
329
            has_frac = 1;
1016
63.6k
            while (*cur >= '0' && *cur <= '9') {
1017
63.3k
                mult /= 10.0;
1018
63.3k
                sec_frac += (*cur - '0') * mult;
1019
63.3k
                has_digits = 1;
1020
63.3k
                cur++;
1021
63.3k
            }
1022
329
        }
1023
1024
18.1k
        while (*cur != desig[seq]) {
1025
3.39k
            seq++;
1026
            /* No T designator or invalid char. */
1027
3.39k
            if (seq == 3 || seq == sizeof(desig))
1028
764
                goto error;
1029
3.39k
        }
1030
14.7k
        cur++;
1031
1032
14.7k
        if (!has_digits || (has_frac && (seq != 5)))
1033
46
            goto error;
1034
1035
14.6k
        switch (seq) {
1036
2.93k
            case 0:
1037
                /* Year */
1038
2.93k
                if (num > LONG_MAX / 12)
1039
6
                    goto error;
1040
2.92k
                dur->mon = num * 12;
1041
2.92k
                break;
1042
2.83k
            case 1:
1043
                /* Month */
1044
2.83k
                if (dur->mon > LONG_MAX - num)
1045
0
                    goto error;
1046
2.83k
                dur->mon += num;
1047
2.83k
                break;
1048
2.75k
            case 2:
1049
                /* Day */
1050
2.75k
                dur->day = num;
1051
2.75k
                break;
1052
2.01k
            case 3:
1053
                /* Hour */
1054
2.01k
                days = num / HOURS_PER_DAY;
1055
2.01k
                if (dur->day > LONG_MAX - days)
1056
0
                    goto error;
1057
2.01k
                dur->day += days;
1058
2.01k
                secs = (num % HOURS_PER_DAY) * SECS_PER_HOUR;
1059
2.01k
                break;
1060
1.84k
            case 4:
1061
                /* Minute */
1062
1.84k
                days = num / MINS_PER_DAY;
1063
1.84k
                if (dur->day > LONG_MAX - days)
1064
0
                    goto error;
1065
1.84k
                dur->day += days;
1066
1.84k
                secs += (num % MINS_PER_DAY) * SECS_PER_MIN;
1067
1.84k
                break;
1068
2.32k
            case 5:
1069
                /* Second */
1070
2.32k
                days = num / SECS_PER_DAY;
1071
2.32k
                if (dur->day > LONG_MAX - days)
1072
0
                    goto error;
1073
2.32k
                dur->day += days;
1074
2.32k
                secs += num % SECS_PER_DAY;
1075
2.32k
                break;
1076
14.6k
        }
1077
1078
14.6k
        seq++;
1079
14.6k
    }
1080
1081
2.64k
    days = secs / SECS_PER_DAY;
1082
2.64k
    if (dur->day > LONG_MAX - days)
1083
0
        goto error;
1084
2.64k
    dur->day += days;
1085
2.64k
    dur->sec = (secs % SECS_PER_DAY) + sec_frac;
1086
1087
2.64k
    if (isneg) {
1088
1.71k
        dur->mon = -dur->mon;
1089
1.71k
        dur->day = -dur->day;
1090
1.71k
        if (dur->sec != 0.0) {
1091
1.50k
            dur->sec = SECS_PER_DAY - dur->sec;
1092
1.50k
            dur->day -= 1;
1093
1.50k
        }
1094
1.71k
    }
1095
1096
#ifdef DEBUG_EXSLT_DATE
1097
    xsltGenericDebug(xsltGenericDebugContext,
1098
         "Parsed duration %f\n", dur->sec);
1099
#endif
1100
1101
2.64k
    return dur;
1102
1103
944
error:
1104
944
    if (dur != NULL)
1105
944
  exsltDateFreeDuration(dur);
1106
944
    return NULL;
1107
2.64k
}
1108
1109
static void
1110
1.29k
exsltFormatLong(xmlChar **cur, xmlChar *end, long num) {
1111
1.29k
    xmlChar buf[20];
1112
1.29k
    int i = 0;
1113
1114
2.75k
    while (i < 20) {
1115
2.75k
        buf[i++] = '0' + num % 10;
1116
2.75k
        num /= 10;
1117
2.75k
        if (num == 0)
1118
1.29k
            break;
1119
2.75k
    }
1120
1121
4.05k
    while (i > 0) {
1122
2.75k
        if (*cur < end)
1123
2.75k
            *(*cur)++ = buf[--i];
1124
2.75k
    }
1125
1.29k
}
1126
1127
static void
1128
474
exsltFormatNanoseconds(xmlChar **cur, xmlChar *end, long nsecs) {
1129
474
    long p10, digit;
1130
1131
474
    if (nsecs > 0) {
1132
41
        if (*cur < end)
1133
41
            *(*cur)++ = '.';
1134
41
        p10 = 100000000;
1135
234
        while (nsecs > 0) {
1136
193
            digit = nsecs / p10;
1137
193
            if (*cur < end)
1138
193
                *(*cur)++ = '0' + digit;
1139
193
            nsecs -= digit * p10;
1140
193
            p10 /= 10;
1141
193
        }
1142
41
    }
1143
474
}
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
314
{
1156
314
    xmlChar buf[100], *cur = buf, *end = buf + 99;
1157
314
    double secs, tmp;
1158
314
    long days, months, intSecs, nsecs;
1159
1160
314
    if (dur == NULL)
1161
0
  return NULL;
1162
1163
    /* quick and dirty check */
1164
314
    if ((dur->sec == 0.0) && (dur->day == 0) && (dur->mon == 0))
1165
7
        return xmlStrdup((xmlChar*)"P0D");
1166
1167
307
    secs   = dur->sec;
1168
307
    days   = dur->day;
1169
307
    months = dur->mon;
1170
1171
307
    *cur = '\0';
1172
307
    if (days < 0) {
1173
136
        if (secs != 0.0) {
1174
135
            secs = SECS_PER_DAY - secs;
1175
135
            days += 1;
1176
135
        }
1177
136
        days = -days;
1178
136
        *cur = '-';
1179
136
    }
1180
307
    if (months < 0) {
1181
136
        months = -months;
1182
136
        *cur = '-';
1183
136
    }
1184
307
    if (*cur == '-')
1185
146
  cur++;
1186
1187
307
    *cur++ = 'P';
1188
1189
307
    if (months >= 12) {
1190
172
        long years = months / 12;
1191
1192
172
        months -= years * 12;
1193
172
        exsltFormatLong(&cur, end, years);
1194
172
        if (cur < end)
1195
172
            *cur++ = 'Y';
1196
172
    }
1197
1198
307
    if (months != 0) {
1199
148
        exsltFormatLong(&cur, end, months);
1200
148
        if (cur < end)
1201
148
            *cur++ = 'M';
1202
148
    }
1203
1204
307
    if (days != 0) {
1205
259
        exsltFormatLong(&cur, end, days);
1206
259
        if (cur < end)
1207
259
            *cur++ = 'D';
1208
259
    }
1209
1210
307
    tmp = floor(secs);
1211
307
    intSecs = (long) tmp;
1212
    /* Round to nearest to avoid issues with floating point precision */
1213
307
    nsecs = (long) floor((secs - tmp) * 1000000000 + 0.5);
1214
307
    if (nsecs >= 1000000000) {
1215
0
        nsecs -= 1000000000;
1216
0
        intSecs += 1;
1217
0
    }
1218
1219
307
    if ((intSecs > 0) || (nsecs > 0)) {
1220
269
        if (cur < end)
1221
269
            *cur++ = 'T';
1222
1223
269
        if (intSecs >= SECS_PER_HOUR) {
1224
237
            long hours = intSecs / SECS_PER_HOUR;
1225
1226
237
            intSecs -= hours * SECS_PER_HOUR;
1227
237
            exsltFormatLong(&cur, end, hours);
1228
237
            if (cur < end)
1229
237
                *cur++ = 'H';
1230
237
        }
1231
1232
269
        if (intSecs >= SECS_PER_MIN) {
1233
221
            long mins = intSecs / SECS_PER_MIN;
1234
1235
221
            intSecs -= mins * SECS_PER_MIN;
1236
221
            exsltFormatLong(&cur, end, mins);
1237
221
            if (cur < end)
1238
221
                *cur++ = 'M';
1239
221
        }
1240
1241
269
        if ((intSecs > 0) || (nsecs > 0)) {
1242
257
            exsltFormatLong(&cur, end, intSecs);
1243
257
            exsltFormatNanoseconds(&cur, end, nsecs);
1244
257
            if (cur < end)
1245
257
                *cur++ = 'S';
1246
257
        }
1247
269
    }
1248
1249
307
    *cur = 0;
1250
1251
307
    return xmlStrdup(buf);
1252
314
}
1253
1254
static void
1255
651
exsltFormatTwoDigits(xmlChar **cur, xmlChar *end, int num) {
1256
651
    if (num < 0 || num >= 100)
1257
0
        return;
1258
651
    if (*cur < end)
1259
651
        *(*cur)++ = '0' + num / 10;
1260
651
    if (*cur < end)
1261
651
        *(*cur)++ = '0' + num % 10;
1262
651
}
1263
1264
static void
1265
217
exsltFormatTime(xmlChar **cur, xmlChar *end, exsltDateValPtr dt) {
1266
217
    double tmp;
1267
217
    long intSecs, nsecs;
1268
1269
217
    exsltFormatTwoDigits(cur, end, dt->hour);
1270
217
    if (*cur < end)
1271
217
        *(*cur)++ = ':';
1272
1273
217
    exsltFormatTwoDigits(cur, end, dt->min);
1274
217
    if (*cur < end)
1275
217
        *(*cur)++ = ':';
1276
1277
217
    tmp = floor(dt->sec);
1278
217
    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
217
    nsecs = (long) floor((dt->sec - tmp) * 1000000000 + 0.5);
1284
217
    if (nsecs > 999999999)
1285
0
        nsecs = 999999999;
1286
217
    exsltFormatTwoDigits(cur, end, intSecs);
1287
217
    exsltFormatNanoseconds(cur, end, nsecs);
1288
217
}
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
126
{
1301
126
    xmlChar buf[100], *cur = buf, *end = buf + 99;
1302
1303
126
    if ((dt == NULL) || !VALID_DATETIME(dt))
1304
0
  return NULL;
1305
1306
126
    FORMAT_DATE(dt, cur);
1307
126
    *cur = 'T';
1308
126
    cur++;
1309
126
    exsltFormatTime(&cur, end, dt);
1310
126
    FORMAT_TZ(dt->tzo, cur);
1311
126
    *cur = 0;
1312
1313
126
    return xmlStrdup(buf);
1314
126
}
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
54
{
1327
54
    xmlChar buf[100], *cur = buf;
1328
1329
54
    if ((dt == NULL) || !VALID_DATETIME(dt))
1330
0
  return NULL;
1331
1332
54
    FORMAT_DATE(dt, cur);
1333
54
    if (dt->tz_flag || (dt->tzo != 0)) {
1334
0
  FORMAT_TZ(dt->tzo, cur);
1335
0
    }
1336
54
    *cur = 0;
1337
1338
54
    return xmlStrdup(buf);
1339
54
}
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
91
{
1352
91
    xmlChar buf[100], *cur = buf, *end = buf + 99;
1353
1354
91
    if ((dt == NULL) || !VALID_TIME(dt))
1355
0
  return NULL;
1356
1357
91
    exsltFormatTime(&cur, end, dt);
1358
91
    if (dt->tz_flag || (dt->tzo != 0)) {
1359
0
  FORMAT_TZ(dt->tzo, cur);
1360
0
    }
1361
91
    *cur = 0;
1362
1363
91
    return xmlStrdup(buf);
1364
91
}
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
110
{
1379
1380
110
    if (dt == NULL)
1381
0
  return NULL;
1382
1383
110
    switch (dt->type) {
1384
105
    case XS_DATETIME:
1385
105
        return exsltDateFormatDateTime(dt);
1386
2
    case XS_DATE:
1387
2
        return exsltDateFormatDate(dt);
1388
0
    case XS_TIME:
1389
0
        return exsltDateFormatTime(dt);
1390
3
    default:
1391
3
        break;
1392
110
    }
1393
1394
3
    if (dt->type & XS_GYEAR) {
1395
3
        xmlChar buf[100], *cur = buf;
1396
1397
3
        FORMAT_GYEAR(dt->year, cur);
1398
3
        if (dt->type == XS_GYEARMONTH) {
1399
1
      *cur = '-';
1400
1
      cur++;
1401
1
      FORMAT_GMONTH(dt->mon, cur);
1402
1
        }
1403
1404
3
        if (dt->tz_flag || (dt->tzo != 0)) {
1405
0
      FORMAT_TZ(dt->tzo, cur);
1406
0
        }
1407
3
        *cur = 0;
1408
3
        return xmlStrdup(buf);
1409
3
    }
1410
1411
0
    return NULL;
1412
3
}
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
258
{
1428
258
    long ret;
1429
1430
258
    if (dt->year <= 0)
1431
1
        ret = ((dt->year-1) * 365) +
1432
1
              (((dt->year)/4)-((dt->year)/100)+
1433
1
               ((dt->year)/400)) +
1434
1
              DAY_IN_YEAR(0, dt->mon, dt->year) - 1;
1435
257
    else
1436
257
        ret = ((dt->year-1) * 365) +
1437
257
              (((dt->year-1)/4)-((dt->year-1)/100)+
1438
257
               ((dt->year-1)/400)) +
1439
257
              DAY_IN_YEAR(0, dt->mon, dt->year);
1440
1441
258
    return ret;
1442
258
}
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
258
    ((double)((dt->hour * SECS_PER_HOUR) +   \
1454
258
              (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
47
{
1468
47
    if (dt == NULL)
1469
0
        return 1;
1470
1471
47
    if ((type & XS_TIME) != XS_TIME) {
1472
47
        dt->hour = 0;
1473
47
        dt->min  = 0;
1474
47
        dt->sec  = 0.0;
1475
47
    }
1476
1477
47
    if ((type & XS_GDAY) != XS_GDAY)
1478
46
        dt->day = 1;
1479
1480
47
    if ((type & XS_GMONTH) != XS_GMONTH)
1481
38
        dt->mon = 1;
1482
1483
47
    if ((type & XS_GYEAR) != XS_GYEAR)
1484
0
        dt->year = 0;
1485
1486
47
    dt->type = type;
1487
1488
47
    return 0;
1489
47
}
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
208
{
1507
208
    long ret;
1508
1509
208
    if (yr <= 0) {
1510
        /* Compute modulus twice to avoid integer overflow */
1511
1
        ret = ((yr%7-2 + ((yr/4)-(yr/100)+(yr/400)) + yday) % 7);
1512
1
        if (ret < 0)
1513
1
            ret += 7;
1514
1
    } else
1515
207
        ret = (((yr%7-1) + (((yr-1)/4)-((yr-1)/100)+((yr-1)/400)) + yday) % 7);
1516
1517
208
    return ret;
1518
208
}
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
110
{
1533
110
    exsltDateValPtr ret;
1534
110
    long carry, temp;
1535
110
    double sum;
1536
1537
110
    if ((dt == NULL) || (dur == NULL))
1538
0
        return NULL;
1539
1540
110
    ret = exsltDateCreateDate(dt->type);
1541
110
    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
110
    temp  = dt->mon + dur->mon % 12;
1551
110
    carry = dur->mon / 12;
1552
110
    if (temp < 1) {
1553
66
        temp  += 12;
1554
66
        carry -= 1;
1555
66
    }
1556
44
    else if (temp > 12) {
1557
0
        temp  -= 12;
1558
0
        carry += 1;
1559
0
    }
1560
110
    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
110
    carry += (dur->day / DAYS_PER_EPOCH) * YEARS_PER_EPOCH;
1569
110
    if ((carry > 0 && dt->year > YEAR_MAX - carry) ||
1570
110
        (carry < 0 && dt->year < YEAR_MIN - carry)) {
1571
        /* Overflow */
1572
0
        exsltDateFreeDate(ret);
1573
0
        return NULL;
1574
0
    }
1575
110
    ret->year = dt->year + carry;
1576
1577
    /* time zone */
1578
110
    ret->tzo     = dt->tzo;
1579
110
    ret->tz_flag = dt->tz_flag;
1580
1581
    /* seconds */
1582
110
    sum    = dt->sec + dur->sec;
1583
110
    ret->sec = fmod(sum, 60.0);
1584
110
    carry  = (long)(sum / 60.0);
1585
1586
    /* minute */
1587
110
    temp  = dt->min + carry % 60;
1588
110
    carry = carry / 60;
1589
110
    if (temp >= 60) {
1590
2
        temp  -= 60;
1591
2
        carry += 1;
1592
2
    }
1593
110
    ret->min = temp;
1594
1595
    /* hours */
1596
110
    temp  = dt->hour + carry % 24;
1597
110
    carry = carry / 24;
1598
110
    if (temp >= 24) {
1599
48
        temp  -= 24;
1600
48
        carry += 1;
1601
48
    }
1602
110
    ret->hour = temp;
1603
1604
    /* days */
1605
110
    if (dt->day > MAX_DAYINMONTH(ret->year, ret->mon))
1606
3
        temp = MAX_DAYINMONTH(ret->year, ret->mon);
1607
107
    else if (dt->day < 1)
1608
0
        temp = 1;
1609
107
    else
1610
107
        temp = dt->day;
1611
1612
110
    temp += dur->day % DAYS_PER_EPOCH + carry;
1613
1614
63.6k
    while (1) {
1615
63.6k
        if (temp < 1) {
1616
63.5k
            if (ret->mon > 1) {
1617
58.2k
                ret->mon -= 1;
1618
58.2k
            }
1619
5.30k
            else {
1620
5.30k
                if (ret->year == YEAR_MIN) {
1621
0
                    exsltDateFreeDate(ret);
1622
0
                    return NULL;
1623
0
                }
1624
5.30k
                ret->mon   = 12;
1625
5.30k
                ret->year -= 1;
1626
5.30k
            }
1627
63.5k
            temp += MAX_DAYINMONTH(ret->year, ret->mon);
1628
63.5k
        } else if (temp > (long)MAX_DAYINMONTH(ret->year, ret->mon)) {
1629
0
            temp -= MAX_DAYINMONTH(ret->year, ret->mon);
1630
0
            if (ret->mon < 12) {
1631
0
                ret->mon += 1;
1632
0
            }
1633
0
            else {
1634
0
                if (ret->year == YEAR_MAX) {
1635
0
                    exsltDateFreeDate(ret);
1636
0
                    return NULL;
1637
0
                }
1638
0
                ret->mon   = 1;
1639
0
                ret->year += 1;
1640
0
            }
1641
0
        } else
1642
110
            break;
1643
63.6k
    }
1644
1645
110
    ret->day = temp;
1646
1647
    /*
1648
     * adjust the date/time type to the date values
1649
     */
1650
110
    if (ret->type != XS_DATETIME) {
1651
39
        if ((ret->hour) || (ret->min) || (ret->sec))
1652
34
            ret->type = XS_DATETIME;
1653
5
        else if (ret->type != XS_DATE) {
1654
5
            if (ret->day != 1)
1655
2
                ret->type = XS_DATE;
1656
3
            else if ((ret->type != XS_GYEARMONTH) && (ret->mon != 1))
1657
1
                ret->type = XS_GYEARMONTH;
1658
5
        }
1659
39
    }
1660
1661
110
    return ret;
1662
110
}
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
157
{
1679
157
    exsltDateDurValPtr ret;
1680
1681
157
    if ((x == NULL) || (y == NULL))
1682
0
        return NULL;
1683
1684
157
    if (((x->type < XS_GYEAR) || (x->type > XS_DATETIME)) ||
1685
157
        ((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
157
    if (x->type != y->type) {
1693
47
        if (x->type < y->type) {
1694
12
            _exsltDateTruncateDate(y, x->type);
1695
35
        } else {
1696
35
            _exsltDateTruncateDate(x, y->type);
1697
35
        }
1698
47
    }
1699
1700
157
    ret = exsltDateCreateDuration();
1701
157
    if (ret == NULL)
1702
0
        return NULL;
1703
1704
157
    if (((x->type == XS_GYEAR) || (x->type == XS_GYEARMONTH)) && (!flag)) {
1705
        /* compute the difference in months */
1706
27
        if ((x->year >= LONG_MAX / 24) || (x->year <= LONG_MIN / 24) ||
1707
27
            (y->year >= LONG_MAX / 24) || (y->year <= LONG_MIN / 24)) {
1708
            /* Possible overflow. */
1709
2
            exsltDateFreeDuration(ret);
1710
2
            return NULL;
1711
2
        }
1712
25
        ret->mon = (y->year - x->year) * 12 + (y->mon - x->mon);
1713
130
    } else {
1714
130
        long carry;
1715
1716
130
        if ((x->year > LONG_MAX / 731) || (x->year < LONG_MIN / 731) ||
1717
130
            (y->year > LONG_MAX / 731) || (y->year < LONG_MIN / 731)) {
1718
            /* Possible overflow. */
1719
1
            exsltDateFreeDuration(ret);
1720
1
            return NULL;
1721
1
        }
1722
1723
129
        ret->sec  = TIME_TO_NUMBER(y) - TIME_TO_NUMBER(x);
1724
129
        ret->sec += (x->tzo - y->tzo) * SECS_PER_MIN;
1725
129
        carry    = (long)floor(ret->sec / SECS_PER_DAY);
1726
129
        ret->sec  = ret->sec - carry * SECS_PER_DAY;
1727
1728
129
        ret->day  = _exsltDateCastYMToDays(y) - _exsltDateCastYMToDays(x);
1729
129
        ret->day += y->day - x->day;
1730
129
        ret->day += carry;
1731
129
    }
1732
1733
154
    return ret;
1734
157
}
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
2.43k
{
1751
    /* months */
1752
2.43k
    if ((x->mon > 0 && y->mon > LONG_MAX - x->mon) ||
1753
2.43k
        (x->mon < 0 && y->mon < LONG_MIN - x->mon)) {
1754
        /* Overflow */
1755
0
        return 0;
1756
0
    }
1757
2.43k
    ret->mon = x->mon + y->mon;
1758
1759
    /* days */
1760
2.43k
    if ((x->day > 0 && y->day > LONG_MAX - x->day) ||
1761
2.43k
        (x->day < 0 && y->day < LONG_MIN - x->day)) {
1762
        /* Overflow */
1763
0
        return 0;
1764
0
    }
1765
2.43k
    ret->day = x->day + y->day;
1766
1767
    /* seconds */
1768
2.43k
    ret->sec = x->sec + y->sec;
1769
2.43k
    if (ret->sec >= SECS_PER_DAY) {
1770
858
        if (ret->day == LONG_MAX) {
1771
            /* Overflow */
1772
0
            return 0;
1773
0
        }
1774
858
        ret->sec -= SECS_PER_DAY;
1775
858
        ret->day += 1;
1776
858
    }
1777
1778
    /*
1779
     * are the results indeterminate? i.e. how do you subtract days from
1780
     * months or years?
1781
     */
1782
2.43k
    if (ret->day >= 0) {
1783
1.01k
        if (((ret->day > 0) || (ret->sec > 0)) && (ret->mon < 0))
1784
36
            return 0;
1785
1.01k
    }
1786
1.42k
    else {
1787
1.42k
        if (ret->mon > 0)
1788
26
            return 0;
1789
1.42k
    }
1790
2.37k
    return 1;
1791
2.43k
}
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
47
{
1805
47
    exsltDateDurValPtr ret;
1806
1807
47
    if ((x == NULL) || (y == NULL))
1808
0
        return NULL;
1809
1810
47
    ret = exsltDateCreateDuration();
1811
47
    if (ret == NULL)
1812
0
        return NULL;
1813
1814
47
    if (_exsltDateAddDurCalc(ret, x, y))
1815
46
        return ret;
1816
1817
1
    exsltDateFreeDuration(ret);
1818
1
    return NULL;
1819
47
}
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
21
{
1838
21
    xmlChar *ret = NULL;
1839
21
    exsltDateValPtr cur;
1840
1841
21
    cur = exsltDateCurrent();
1842
21
    if (cur != NULL) {
1843
21
  ret = exsltDateFormatDateTime(cur);
1844
21
  exsltDateFreeDate(cur);
1845
21
    }
1846
1847
21
    return ret;
1848
21
}
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
147
{
1869
147
    exsltDateValPtr dt = NULL;
1870
147
    xmlChar *ret = NULL;
1871
1872
147
    if (dateTime == NULL) {
1873
17
  dt = exsltDateCurrent();
1874
17
  if (dt == NULL)
1875
0
      return NULL;
1876
130
    } else {
1877
130
  dt = exsltDateParse(dateTime);
1878
130
  if (dt == NULL)
1879
77
      return NULL;
1880
53
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
1881
18
      exsltDateFreeDate(dt);
1882
18
      return NULL;
1883
18
  }
1884
53
    }
1885
1886
52
    ret = exsltDateFormatDate(dt);
1887
52
    exsltDateFreeDate(dt);
1888
1889
52
    return ret;
1890
147
}
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
878
{
1911
878
    exsltDateValPtr dt = NULL;
1912
878
    xmlChar *ret = NULL;
1913
1914
878
    if (dateTime == NULL) {
1915
18
  dt = exsltDateCurrent();
1916
18
  if (dt == NULL)
1917
0
      return NULL;
1918
860
    } else {
1919
860
  dt = exsltDateParse(dateTime);
1920
860
  if (dt == NULL)
1921
764
      return NULL;
1922
96
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
1923
23
      exsltDateFreeDate(dt);
1924
23
      return NULL;
1925
23
  }
1926
96
    }
1927
1928
91
    ret = exsltDateFormatTime(dt);
1929
91
    exsltDateFreeDate(dt);
1930
1931
91
    return ret;
1932
878
}
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
687
{
1957
687
    exsltDateValPtr dt;
1958
687
    long year;
1959
687
    double ret;
1960
1961
687
    if (dateTime == NULL) {
1962
17
  dt = exsltDateCurrent();
1963
17
  if (dt == NULL)
1964
0
      return xmlXPathNAN;
1965
670
    } else {
1966
670
  dt = exsltDateParse(dateTime);
1967
670
  if (dt == NULL)
1968
609
      return xmlXPathNAN;
1969
61
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
1970
61
      (dt->type != XS_GYEARMONTH) && (dt->type != XS_GYEAR)) {
1971
9
      exsltDateFreeDate(dt);
1972
9
      return xmlXPathNAN;
1973
9
  }
1974
61
    }
1975
1976
69
    year = dt->year;
1977
69
    if (year <= 0) year -= 1; /* Adjust for missing year 0. */
1978
69
    ret = (double) year;
1979
69
    exsltDateFreeDate(dt);
1980
1981
69
    return ret;
1982
687
}
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
304
{
2007
304
    exsltDateValPtr dt = NULL;
2008
304
    xmlXPathObjectPtr ret;
2009
2010
304
    if (dateTime == NULL) {
2011
17
  dt = exsltDateCurrent();
2012
287
    } else {
2013
287
  dt = exsltDateParse(dateTime);
2014
287
  if ((dt != NULL) &&
2015
287
            (dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
2016
287
      (dt->type != XS_GYEARMONTH) && (dt->type != XS_GYEAR)) {
2017
4
      exsltDateFreeDate(dt);
2018
4
      dt = NULL;
2019
4
  }
2020
287
    }
2021
2022
304
    if (dt == NULL) {
2023
231
        ret = xmlXPathNewFloat(xmlXPathNAN);
2024
231
    }
2025
73
    else {
2026
73
        ret = xmlXPathNewBoolean(IS_LEAP(dt->year));
2027
73
        exsltDateFreeDate(dt);
2028
73
    }
2029
2030
304
    return ret;
2031
304
}
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
306
{
2057
306
    exsltDateValPtr dt;
2058
306
    double ret;
2059
2060
306
    if (dateTime == NULL) {
2061
55
  dt = exsltDateCurrent();
2062
55
  if (dt == NULL)
2063
0
      return xmlXPathNAN;
2064
251
    } else {
2065
251
  dt = exsltDateParse(dateTime);
2066
251
  if (dt == NULL)
2067
153
      return xmlXPathNAN;
2068
98
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
2069
98
      (dt->type != XS_GYEARMONTH) && (dt->type != XS_GMONTH) &&
2070
98
      (dt->type != XS_GMONTHDAY)) {
2071
23
      exsltDateFreeDate(dt);
2072
23
      return xmlXPathNAN;
2073
23
  }
2074
98
    }
2075
2076
130
    ret = (double) dt->mon;
2077
130
    exsltDateFreeDate(dt);
2078
2079
130
    return ret;
2080
306
}
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
100
{
2108
100
    static const xmlChar monthNames[13][10] = {
2109
100
        { 0 },
2110
100
  { 'J', 'a', 'n', 'u', 'a', 'r', 'y', 0 },
2111
100
  { 'F', 'e', 'b', 'r', 'u', 'a', 'r', 'y', 0 },
2112
100
  { 'M', 'a', 'r', 'c', 'h', 0 },
2113
100
  { 'A', 'p', 'r', 'i', 'l', 0 },
2114
100
  { 'M', 'a', 'y', 0 },
2115
100
  { 'J', 'u', 'n', 'e', 0 },
2116
100
  { 'J', 'u', 'l', 'y', 0 },
2117
100
  { 'A', 'u', 'g', 'u', 's', 't', 0 },
2118
100
  { 'S', 'e', 'p', 't', 'e', 'm', 'b', 'e', 'r', 0 },
2119
100
  { 'O', 'c', 't', 'o', 'b', 'e', 'r', 0 },
2120
100
  { 'N', 'o', 'v', 'e', 'm', 'b', 'e', 'r', 0 },
2121
100
  { 'D', 'e', 'c', 'e', 'm', 'b', 'e', 'r', 0 }
2122
100
    };
2123
100
    double month;
2124
100
    int index = 0;
2125
100
    month = exsltDateMonthInYear(dateTime);
2126
100
    if (!xmlXPathIsNaN(month) && (month >= 1.0) && (month <= 12.0))
2127
45
      index = (int) month;
2128
100
    return monthNames[index];
2129
100
}
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
124
{
2157
124
    static const xmlChar monthAbbreviations[13][4] = {
2158
124
        { 0 },
2159
124
  { 'J', 'a', 'n', 0 },
2160
124
  { 'F', 'e', 'b', 0 },
2161
124
  { 'M', 'a', 'r', 0 },
2162
124
  { 'A', 'p', 'r', 0 },
2163
124
  { 'M', 'a', 'y', 0 },
2164
124
  { 'J', 'u', 'n', 0 },
2165
124
  { 'J', 'u', 'l', 0 },
2166
124
  { 'A', 'u', 'g', 0 },
2167
124
  { 'S', 'e', 'p', 0 },
2168
124
  { 'O', 'c', 't', 0 },
2169
124
  { 'N', 'o', 'v', 0 },
2170
124
  { 'D', 'e', 'c', 0 }
2171
124
    };
2172
124
    double month;
2173
124
    int index = 0;
2174
124
    month = exsltDateMonthInYear(dateTime);
2175
124
    if (!xmlXPathIsNaN(month) && (month >= 1.0) && (month <= 12.0))
2176
42
      index = (int) month;
2177
124
    return monthAbbreviations[index];
2178
124
}
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
100
{
2204
100
    exsltDateValPtr dt;
2205
100
    long diy, diw, year, ret;
2206
2207
100
    if (dateTime == NULL) {
2208
18
  dt = exsltDateCurrent();
2209
18
  if (dt == NULL)
2210
0
      return xmlXPathNAN;
2211
82
    } else {
2212
82
  dt = exsltDateParse(dateTime);
2213
82
  if (dt == NULL)
2214
40
      return xmlXPathNAN;
2215
42
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2216
13
      exsltDateFreeDate(dt);
2217
13
      return xmlXPathNAN;
2218
13
  }
2219
42
    }
2220
2221
47
    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
47
    diw = (_exsltDateDayInWeek(diy, dt->year) + 6) % 7;
2228
2229
    /* ISO 8601 adjustment, 3 is Thu */
2230
47
    diy += (3 - diw);
2231
47
    if(diy < 1) {
2232
23
  year = dt->year - 1;
2233
23
  if(year == 0) year--;
2234
23
  diy = DAY_IN_YEAR(31, 12, year) + diy;
2235
24
    } else if (diy > (long)DAY_IN_YEAR(31, 12, dt->year)) {
2236
0
  diy -= DAY_IN_YEAR(31, 12, dt->year);
2237
0
    }
2238
2239
47
    ret = ((diy - 1) / 7) + 1;
2240
2241
47
    exsltDateFreeDate(dt);
2242
2243
47
    return (double) ret;
2244
100
}
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
105
{
2270
105
    exsltDateValPtr dt;
2271
105
    long fdiy, fdiw, ret;
2272
2273
105
    if (dateTime == NULL) {
2274
15
  dt = exsltDateCurrent();
2275
15
  if (dt == NULL)
2276
0
      return xmlXPathNAN;
2277
90
    } else {
2278
90
  dt = exsltDateParse(dateTime);
2279
90
  if (dt == NULL)
2280
55
      return xmlXPathNAN;
2281
35
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2282
11
      exsltDateFreeDate(dt);
2283
11
      return xmlXPathNAN;
2284
11
  }
2285
35
    }
2286
2287
39
    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
39
    fdiw = (_exsltDateDayInWeek(fdiy, dt->year) + 6) % 7;
2293
2294
39
    ret = ((dt->day + fdiw - 1) / 7) + 1;
2295
2296
39
    exsltDateFreeDate(dt);
2297
2298
39
    return (double) ret;
2299
105
}
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
148
{
2322
148
    exsltDateValPtr dt;
2323
148
    long ret;
2324
2325
148
    if (dateTime == NULL) {
2326
17
  dt = exsltDateCurrent();
2327
17
  if (dt == NULL)
2328
0
      return xmlXPathNAN;
2329
131
    } else {
2330
131
  dt = exsltDateParse(dateTime);
2331
131
  if (dt == NULL)
2332
85
      return xmlXPathNAN;
2333
46
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2334
19
      exsltDateFreeDate(dt);
2335
19
      return xmlXPathNAN;
2336
19
  }
2337
46
    }
2338
2339
44
    ret = DAY_IN_YEAR(dt->day, dt->mon, dt->year);
2340
2341
44
    exsltDateFreeDate(dt);
2342
2343
44
    return (double) ret;
2344
148
}
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
141
{
2369
141
    exsltDateValPtr dt;
2370
141
    double ret;
2371
2372
141
    if (dateTime == NULL) {
2373
20
  dt = exsltDateCurrent();
2374
20
  if (dt == NULL)
2375
0
      return xmlXPathNAN;
2376
121
    } else {
2377
121
  dt = exsltDateParse(dateTime);
2378
121
  if (dt == NULL)
2379
87
      return xmlXPathNAN;
2380
34
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE) &&
2381
34
      (dt->type != XS_GMONTHDAY) && (dt->type != XS_GDAY)) {
2382
12
      exsltDateFreeDate(dt);
2383
12
      return xmlXPathNAN;
2384
12
  }
2385
34
    }
2386
2387
42
    ret = (double) dt->day;
2388
42
    exsltDateFreeDate(dt);
2389
2390
42
    return ret;
2391
141
}
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
110
{
2415
110
    exsltDateValPtr dt;
2416
110
    long ret;
2417
2418
110
    if (dateTime == NULL) {
2419
16
  dt = exsltDateCurrent();
2420
16
  if (dt == NULL)
2421
0
      return xmlXPathNAN;
2422
94
    } else {
2423
94
  dt = exsltDateParse(dateTime);
2424
94
  if (dt == NULL)
2425
64
      return xmlXPathNAN;
2426
30
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2427
10
      exsltDateFreeDate(dt);
2428
10
      return xmlXPathNAN;
2429
10
  }
2430
30
    }
2431
2432
36
    ret = ((dt->day -1) / 7) + 1;
2433
2434
36
    exsltDateFreeDate(dt);
2435
2436
36
    return (double) ret;
2437
110
}
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
392
{
2462
392
    exsltDateValPtr dt;
2463
392
    long diy, ret;
2464
2465
392
    if (dateTime == NULL) {
2466
51
  dt = exsltDateCurrent();
2467
51
  if (dt == NULL)
2468
0
      return xmlXPathNAN;
2469
341
    } else {
2470
341
  dt = exsltDateParse(dateTime);
2471
341
  if (dt == NULL)
2472
255
      return xmlXPathNAN;
2473
86
  if ((dt->type != XS_DATETIME) && (dt->type != XS_DATE)) {
2474
15
      exsltDateFreeDate(dt);
2475
15
      return xmlXPathNAN;
2476
15
  }
2477
86
    }
2478
2479
122
    diy = DAY_IN_YEAR(dt->day, dt->mon, dt->year);
2480
2481
122
    ret = _exsltDateDayInWeek(diy, dt->year) + 1;
2482
2483
122
    exsltDateFreeDate(dt);
2484
2485
122
    return (double) ret;
2486
392
}
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
175
{
2511
175
    static const xmlChar dayNames[8][10] = {
2512
175
        { 0 },
2513
175
  { 'S', 'u', 'n', 'd', 'a', 'y', 0 },
2514
175
  { 'M', 'o', 'n', 'd', 'a', 'y', 0 },
2515
175
  { 'T', 'u', 'e', 's', 'd', 'a', 'y', 0 },
2516
175
  { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y', 0 },
2517
175
  { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y', 0 },
2518
175
  { 'F', 'r', 'i', 'd', 'a', 'y', 0 },
2519
175
  { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y', 0 }
2520
175
    };
2521
175
    double day;
2522
175
    int index = 0;
2523
175
    day = exsltDateDayInWeek(dateTime);
2524
175
    if(!xmlXPathIsNaN(day) && (day >= 1.0) && (day <= 7.0))
2525
43
      index = (int) day;
2526
175
    return dayNames[index];
2527
175
}
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
158
{
2552
158
    static const xmlChar dayAbbreviations[8][4] = {
2553
158
        { 0 },
2554
158
  { 'S', 'u', 'n', 0 },
2555
158
  { 'M', 'o', 'n', 0 },
2556
158
  { 'T', 'u', 'e', 0 },
2557
158
  { 'W', 'e', 'd', 0 },
2558
158
  { 'T', 'h', 'u', 0 },
2559
158
  { 'F', 'r', 'i', 0 },
2560
158
  { 'S', 'a', 't', 0 }
2561
158
    };
2562
158
    double day;
2563
158
    int index = 0;
2564
158
    day = exsltDateDayInWeek(dateTime);
2565
158
    if(!xmlXPathIsNaN(day) && (day >= 1.0) && (day <= 7.0))
2566
40
      index = (int) day;
2567
158
    return dayAbbreviations[index];
2568
158
}
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
465
{
2591
465
    exsltDateValPtr dt;
2592
465
    double ret;
2593
2594
465
    if (dateTime == NULL) {
2595
16
  dt = exsltDateCurrent();
2596
16
  if (dt == NULL)
2597
0
      return xmlXPathNAN;
2598
449
    } else {
2599
449
  dt = exsltDateParse(dateTime);
2600
449
  if (dt == NULL)
2601
271
      return xmlXPathNAN;
2602
178
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
2603
150
      exsltDateFreeDate(dt);
2604
150
      return xmlXPathNAN;
2605
150
  }
2606
178
    }
2607
2608
44
    ret = (double) dt->hour;
2609
44
    exsltDateFreeDate(dt);
2610
2611
44
    return ret;
2612
465
}
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
116
{
2635
116
    exsltDateValPtr dt;
2636
116
    double ret;
2637
2638
116
    if (dateTime == NULL) {
2639
15
  dt = exsltDateCurrent();
2640
15
  if (dt == NULL)
2641
0
      return xmlXPathNAN;
2642
101
    } else {
2643
101
  dt = exsltDateParse(dateTime);
2644
101
  if (dt == NULL)
2645
60
      return xmlXPathNAN;
2646
41
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
2647
16
      exsltDateFreeDate(dt);
2648
16
      return xmlXPathNAN;
2649
16
  }
2650
41
    }
2651
2652
40
    ret = (double) dt->min;
2653
40
    exsltDateFreeDate(dt);
2654
2655
40
    return ret;
2656
116
}
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
109
{
2681
109
    exsltDateValPtr dt;
2682
109
    double ret;
2683
2684
109
    if (dateTime == NULL) {
2685
15
  dt = exsltDateCurrent();
2686
15
  if (dt == NULL)
2687
0
      return xmlXPathNAN;
2688
94
    } else {
2689
94
  dt = exsltDateParse(dateTime);
2690
94
  if (dt == NULL)
2691
57
      return xmlXPathNAN;
2692
37
  if ((dt->type != XS_DATETIME) && (dt->type != XS_TIME)) {
2693
14
      exsltDateFreeDate(dt);
2694
14
      return xmlXPathNAN;
2695
14
  }
2696
37
    }
2697
2698
38
    ret = dt->sec;
2699
38
    exsltDateFreeDate(dt);
2700
2701
38
    return ret;
2702
109
}
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
396
{
2731
396
    exsltDateValPtr dt, res;
2732
396
    exsltDateDurValPtr dur;
2733
396
    xmlChar     *ret;
2734
2735
396
    if ((xstr == NULL) || (ystr == NULL))
2736
0
        return NULL;
2737
2738
396
    dt = exsltDateParse(xstr);
2739
396
    if (dt == NULL)
2740
145
        return NULL;
2741
251
    else if ((dt->type < XS_GYEAR) || (dt->type > XS_DATETIME)) {
2742
10
        exsltDateFreeDate(dt);
2743
10
        return NULL;
2744
10
    }
2745
2746
241
    dur = exsltDateParseDuration(ystr);
2747
241
    if (dur == NULL) {
2748
131
        exsltDateFreeDate(dt);
2749
131
        return NULL;
2750
131
    }
2751
2752
110
    res = _exsltDateAdd(dt, dur);
2753
2754
110
    exsltDateFreeDate(dt);
2755
110
    exsltDateFreeDuration(dur);
2756
2757
110
    if (res == NULL)
2758
0
        return NULL;
2759
2760
110
    ret = exsltDateFormat(res);
2761
110
    exsltDateFreeDate(res);
2762
2763
110
    return ret;
2764
110
}
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
330
{
2790
330
    exsltDateDurValPtr x, y, res;
2791
330
    xmlChar     *ret;
2792
2793
330
    if ((xstr == NULL) || (ystr == NULL))
2794
0
        return NULL;
2795
2796
330
    x = exsltDateParseDuration(xstr);
2797
330
    if (x == NULL)
2798
239
        return NULL;
2799
2800
91
    y = exsltDateParseDuration(ystr);
2801
91
    if (y == NULL) {
2802
44
        exsltDateFreeDuration(x);
2803
44
        return NULL;
2804
44
    }
2805
2806
47
    res = _exsltDateAddDuration(x, y);
2807
2808
47
    exsltDateFreeDuration(x);
2809
47
    exsltDateFreeDuration(y);
2810
2811
47
    if (res == NULL)
2812
1
        return NULL;
2813
2814
46
    ret = exsltDateFormatDuration(res);
2815
46
    exsltDateFreeDuration(res);
2816
2817
46
    return ret;
2818
47
}
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
1.08k
{
2842
1.08k
    xmlNodeSetPtr ns;
2843
1.08k
    void *user = NULL;
2844
1.08k
    xmlChar *tmp;
2845
1.08k
    exsltDateDurValPtr x, total;
2846
1.08k
    xmlChar *ret;
2847
1.08k
    int i;
2848
2849
1.08k
    if (nargs != 1) {
2850
24
  xmlXPathSetArityError (ctxt);
2851
24
  return;
2852
24
    }
2853
2854
    /* We need to delay the freeing of value->user */
2855
1.06k
    if ((ctxt->value != NULL) && ctxt->value->boolval != 0) {
2856
21
  user = ctxt->value->user;
2857
21
  ctxt->value->boolval = 0;
2858
21
  ctxt->value->user = NULL;
2859
21
    }
2860
2861
1.06k
    ns = xmlXPathPopNodeSet (ctxt);
2862
1.06k
    if (xmlXPathCheckError (ctxt))
2863
55
  return;
2864
2865
1.00k
    if ((ns == NULL) || (ns->nodeNr == 0)) {
2866
78
  xmlXPathReturnEmptyString (ctxt);
2867
78
  if (ns != NULL)
2868
57
      xmlXPathFreeNodeSet (ns);
2869
78
  return;
2870
78
    }
2871
2872
931
    total = exsltDateCreateDuration ();
2873
931
    if (total == NULL) {
2874
0
        xmlXPathFreeNodeSet (ns);
2875
0
        return;
2876
0
    }
2877
2878
3.26k
    for (i = 0; i < ns->nodeNr; i++) {
2879
3.15k
  int result;
2880
3.15k
  tmp = xmlXPathCastNodeToString (ns->nodeTab[i]);
2881
3.15k
  if (tmp == NULL) {
2882
0
      xmlXPathFreeNodeSet (ns);
2883
0
      exsltDateFreeDuration (total);
2884
0
      return;
2885
0
  }
2886
2887
3.15k
  x = exsltDateParseDuration (tmp);
2888
3.15k
  if (x == NULL) {
2889
763
      xmlFree (tmp);
2890
763
      exsltDateFreeDuration (total);
2891
763
      xmlXPathFreeNodeSet (ns);
2892
763
      xmlXPathReturnEmptyString (ctxt);
2893
763
      return;
2894
763
  }
2895
2896
2.39k
  result = _exsltDateAddDurCalc(total, total, x);
2897
2898
2.39k
  exsltDateFreeDuration (x);
2899
2.39k
  xmlFree (tmp);
2900
2.39k
  if (!result) {
2901
61
      exsltDateFreeDuration (total);
2902
61
      xmlXPathFreeNodeSet (ns);
2903
61
      xmlXPathReturnEmptyString (ctxt);
2904
61
      return;
2905
61
  }
2906
2.39k
    }
2907
2908
107
    ret = exsltDateFormatDuration (total);
2909
107
    exsltDateFreeDuration (total);
2910
2911
107
    xmlXPathFreeNodeSet (ns);
2912
107
    if (user != NULL)
2913
0
  xmlFreeNodeList ((xmlNodePtr) user);
2914
2915
107
    if (ret == NULL)
2916
0
  xmlXPathReturnEmptyString (ctxt);
2917
107
    else
2918
107
  xmlXPathReturnString (ctxt, ret);
2919
107
}
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
169
{
2952
169
    exsltDateValPtr dt;
2953
169
    exsltDateDurValPtr dur = NULL;
2954
169
    double ret = xmlXPathNAN;
2955
2956
169
    if (dateTime == NULL) {
2957
53
  dt = exsltDateCurrent();
2958
53
  if (dt == NULL)
2959
0
      return xmlXPathNAN;
2960
116
    } else {
2961
116
        dt = exsltDateParse(dateTime);
2962
116
        if (dt == NULL)
2963
67
            dur = exsltDateParseDuration(dateTime);
2964
116
    }
2965
2966
169
    if ((dt != NULL) && (dt->type >= XS_GYEAR)) {
2967
101
        exsltDateValPtr y;
2968
101
        exsltDateDurValPtr diff;
2969
2970
        /*
2971
         * compute the difference between the given (or current) date
2972
         * and epoch date
2973
         */
2974
101
        y = exsltDateCreateDate(XS_DATETIME);
2975
101
        if (y != NULL) {
2976
101
            y->year = 1970;
2977
101
            y->mon  = 1;
2978
101
            y->day  = 1;
2979
101
            y->tz_flag = 1;
2980
2981
101
            diff = _exsltDateDifference(y, dt, 1);
2982
101
            if (diff != NULL) {
2983
100
                ret = (double)diff->day * SECS_PER_DAY + diff->sec;
2984
100
                exsltDateFreeDuration(diff);
2985
100
            }
2986
101
            exsltDateFreeDate(y);
2987
101
        }
2988
2989
101
    } else if ((dur != NULL) && (dur->mon == 0)) {
2990
0
        ret = (double)dur->day * SECS_PER_DAY + dur->sec;
2991
0
    }
2992
2993
169
    if (dt != NULL)
2994
102
        exsltDateFreeDate(dt);
2995
169
    if (dur != NULL)
2996
1
        exsltDateFreeDuration(dur);
2997
2998
169
    return ret;
2999
169
}
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
345
{
3041
345
    exsltDateValPtr x, y;
3042
345
    exsltDateDurValPtr dur;
3043
345
    xmlChar *ret = NULL;
3044
3045
345
    if ((xstr == NULL) || (ystr == NULL))
3046
0
        return NULL;
3047
3048
345
    x = exsltDateParse(xstr);
3049
345
    if (x == NULL)
3050
157
        return NULL;
3051
3052
188
    y = exsltDateParse(ystr);
3053
188
    if (y == NULL) {
3054
128
        exsltDateFreeDate(x);
3055
128
        return NULL;
3056
128
    }
3057
3058
60
    if (((x->type < XS_GYEAR) || (x->type > XS_DATETIME)) ||
3059
60
        ((y->type < XS_GYEAR) || (y->type > XS_DATETIME)))  {
3060
4
  exsltDateFreeDate(x);
3061
4
  exsltDateFreeDate(y);
3062
4
        return NULL;
3063
4
    }
3064
3065
56
    dur = _exsltDateDifference(x, y, 0);
3066
3067
56
    exsltDateFreeDate(x);
3068
56
    exsltDateFreeDate(y);
3069
3070
56
    if (dur == NULL)
3071
2
        return NULL;
3072
3073
54
    ret = exsltDateFormatDuration(dur);
3074
54
    exsltDateFreeDuration(dur);
3075
3076
54
    return ret;
3077
56
}
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
180
{
3102
180
    exsltDateDurValPtr dur;
3103
180
    double       secs, days;
3104
180
    xmlChar     *ret;
3105
3106
180
    if (number == NULL)
3107
38
        secs = exsltDateSeconds(number);
3108
142
    else
3109
142
        secs = xmlXPathCastStringToNumber(number);
3110
3111
180
    if (xmlXPathIsNaN(secs))
3112
59
        return NULL;
3113
3114
121
    days = floor(secs / SECS_PER_DAY);
3115
121
    if ((days <= (double)LONG_MIN) || (days >= (double)LONG_MAX))
3116
14
        return NULL;
3117
3118
107
    dur = exsltDateCreateDuration();
3119
107
    if (dur == NULL)
3120
0
        return NULL;
3121
3122
107
    dur->day = (long)days;
3123
107
    dur->sec = secs - days * SECS_PER_DAY;
3124
3125
107
    ret = exsltDateFormatDuration(dur);
3126
107
    exsltDateFreeDuration(dur);
3127
3128
107
    return ret;
3129
107
}
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
41
{
3147
41
    xmlChar *ret;
3148
3149
41
    if (nargs != 0) {
3150
20
  xmlXPathSetArityError(ctxt);
3151
20
  return;
3152
20
    }
3153
3154
21
    ret = exsltDateDateTime();
3155
21
    if (ret == NULL)
3156
0
        xmlXPathReturnEmptyString(ctxt);
3157
21
    else
3158
21
        xmlXPathReturnString(ctxt, ret);
3159
21
}
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
168
{
3171
168
    xmlChar *ret, *dt = NULL;
3172
3173
168
    if ((nargs < 0) || (nargs > 1)) {
3174
21
  xmlXPathSetArityError(ctxt);
3175
21
  return;
3176
21
    }
3177
147
    if (nargs == 1) {
3178
130
  dt = xmlXPathPopString(ctxt);
3179
130
  if (xmlXPathCheckError(ctxt)) {
3180
0
      xmlXPathSetTypeError(ctxt);
3181
0
      return;
3182
0
  }
3183
130
    }
3184
3185
147
    ret = exsltDateDate(dt);
3186
3187
147
    if (ret == NULL) {
3188
95
  xsltGenericDebug(xsltGenericDebugContext,
3189
95
       "{http://exslt.org/dates-and-times}date: "
3190
95
       "invalid date or format %s\n", dt);
3191
95
  xmlXPathReturnEmptyString(ctxt);
3192
95
    } else {
3193
52
  xmlXPathReturnString(ctxt, ret);
3194
52
    }
3195
3196
147
    if (dt != NULL)
3197
130
  xmlFree(dt);
3198
147
}
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
896
{
3210
896
    xmlChar *ret, *dt = NULL;
3211
3212
896
    if ((nargs < 0) || (nargs > 1)) {
3213
18
  xmlXPathSetArityError(ctxt);
3214
18
  return;
3215
18
    }
3216
878
    if (nargs == 1) {
3217
860
  dt = xmlXPathPopString(ctxt);
3218
860
  if (xmlXPathCheckError(ctxt)) {
3219
0
      xmlXPathSetTypeError(ctxt);
3220
0
      return;
3221
0
  }
3222
860
    }
3223
3224
878
    ret = exsltDateTime(dt);
3225
3226
878
    if (ret == NULL) {
3227
787
  xsltGenericDebug(xsltGenericDebugContext,
3228
787
       "{http://exslt.org/dates-and-times}time: "
3229
787
       "invalid date or format %s\n", dt);
3230
787
  xmlXPathReturnEmptyString(ctxt);
3231
787
    } else {
3232
91
  xmlXPathReturnString(ctxt, ret);
3233
91
    }
3234
3235
878
    if (dt != NULL)
3236
860
  xmlFree(dt);
3237
878
}
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
706
{
3249
706
    xmlChar *dt = NULL;
3250
706
    double ret;
3251
3252
706
    if ((nargs < 0) || (nargs > 1)) {
3253
19
  xmlXPathSetArityError(ctxt);
3254
19
  return;
3255
19
    }
3256
3257
687
    if (nargs == 1) {
3258
670
  dt = xmlXPathPopString(ctxt);
3259
670
  if (xmlXPathCheckError(ctxt)) {
3260
0
      xmlXPathSetTypeError(ctxt);
3261
0
      return;
3262
0
  }
3263
670
    }
3264
3265
687
    ret = exsltDateYear(dt);
3266
3267
687
    if (dt != NULL)
3268
670
  xmlFree(dt);
3269
3270
687
    xmlXPathReturnNumber(ctxt, ret);
3271
687
}
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
324
{
3283
324
    xmlChar *dt = NULL;
3284
324
    xmlXPathObjectPtr ret;
3285
3286
324
    if ((nargs < 0) || (nargs > 1)) {
3287
20
  xmlXPathSetArityError(ctxt);
3288
20
  return;
3289
20
    }
3290
3291
304
    if (nargs == 1) {
3292
287
  dt = xmlXPathPopString(ctxt);
3293
287
  if (xmlXPathCheckError(ctxt)) {
3294
0
      xmlXPathSetTypeError(ctxt);
3295
0
      return;
3296
0
  }
3297
287
    }
3298
3299
304
    ret = exsltDateLeapYear(dt);
3300
3301
304
    if (dt != NULL)
3302
287
  xmlFree(dt);
3303
3304
304
    valuePush(ctxt, ret);
3305
304
}
3306
3307
#define X_IN_Y(x, y)            \
3308
static void             \
3309
exsltDate##x##In##y##Function (xmlXPathParserContextPtr ctxt, \
3310
1.59k
            int nargs) {     \
3311
1.59k
    xmlChar *dt = NULL;           \
3312
1.59k
    double ret;             \
3313
1.59k
                \
3314
1.59k
    if ((nargs < 0) || (nargs > 1)) {       \
3315
159
  xmlXPathSetArityError(ctxt);       \
3316
159
  return;             \
3317
159
    }                \
3318
1.59k
                \
3319
1.59k
    if (nargs == 1) {           \
3320
1.26k
  dt = xmlXPathPopString(ctxt);       \
3321
1.26k
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
1.26k
    }                \
3326
1.43k
                \
3327
1.43k
    ret = exsltDate##x##In##y(dt);        \
3328
1.43k
                \
3329
1.43k
    if (dt != NULL)           \
3330
1.43k
  xmlFree(dt);           \
3331
1.43k
                \
3332
1.43k
    xmlXPathReturnNumber(ctxt, ret);        \
3333
1.43k
}
date.c:exsltDateDayInMonthFunction
Line
Count
Source
3310
161
            int nargs) {     \
3311
161
    xmlChar *dt = NULL;           \
3312
161
    double ret;             \
3313
161
                \
3314
161
    if ((nargs < 0) || (nargs > 1)) {       \
3315
20
  xmlXPathSetArityError(ctxt);       \
3316
20
  return;             \
3317
20
    }                \
3318
161
                \
3319
161
    if (nargs == 1) {           \
3320
121
  dt = xmlXPathPopString(ctxt);       \
3321
121
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
121
    }                \
3326
141
                \
3327
141
    ret = exsltDate##x##In##y(dt);        \
3328
141
                \
3329
141
    if (dt != NULL)           \
3330
141
  xmlFree(dt);           \
3331
141
                \
3332
141
    xmlXPathReturnNumber(ctxt, ret);        \
3333
141
}
date.c:exsltDateDayInWeekFunction
Line
Count
Source
3310
72
            int nargs) {     \
3311
72
    xmlChar *dt = NULL;           \
3312
72
    double ret;             \
3313
72
                \
3314
72
    if ((nargs < 0) || (nargs > 1)) {       \
3315
13
  xmlXPathSetArityError(ctxt);       \
3316
13
  return;             \
3317
13
    }                \
3318
72
                \
3319
72
    if (nargs == 1) {           \
3320
41
  dt = xmlXPathPopString(ctxt);       \
3321
41
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
41
    }                \
3326
59
                \
3327
59
    ret = exsltDate##x##In##y(dt);        \
3328
59
                \
3329
59
    if (dt != NULL)           \
3330
59
  xmlFree(dt);           \
3331
59
                \
3332
59
    xmlXPathReturnNumber(ctxt, ret);        \
3333
59
}
date.c:exsltDateDayInYearFunction
Line
Count
Source
3310
174
            int nargs) {     \
3311
174
    xmlChar *dt = NULL;           \
3312
174
    double ret;             \
3313
174
                \
3314
174
    if ((nargs < 0) || (nargs > 1)) {       \
3315
26
  xmlXPathSetArityError(ctxt);       \
3316
26
  return;             \
3317
26
    }                \
3318
174
                \
3319
174
    if (nargs == 1) {           \
3320
131
  dt = xmlXPathPopString(ctxt);       \
3321
131
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
131
    }                \
3326
148
                \
3327
148
    ret = exsltDate##x##In##y(dt);        \
3328
148
                \
3329
148
    if (dt != NULL)           \
3330
148
  xmlFree(dt);           \
3331
148
                \
3332
148
    xmlXPathReturnNumber(ctxt, ret);        \
3333
148
}
date.c:exsltDateDayOfWeekInMonthFunction
Line
Count
Source
3310
113
            int nargs) {     \
3311
113
    xmlChar *dt = NULL;           \
3312
113
    double ret;             \
3313
113
                \
3314
113
    if ((nargs < 0) || (nargs > 1)) {       \
3315
3
  xmlXPathSetArityError(ctxt);       \
3316
3
  return;             \
3317
3
    }                \
3318
113
                \
3319
113
    if (nargs == 1) {           \
3320
94
  dt = xmlXPathPopString(ctxt);       \
3321
94
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
94
    }                \
3326
110
                \
3327
110
    ret = exsltDate##x##In##y(dt);        \
3328
110
                \
3329
110
    if (dt != NULL)           \
3330
110
  xmlFree(dt);           \
3331
110
                \
3332
110
    xmlXPathReturnNumber(ctxt, ret);        \
3333
110
}
date.c:exsltDateHourInDayFunction
Line
Count
Source
3310
482
            int nargs) {     \
3311
482
    xmlChar *dt = NULL;           \
3312
482
    double ret;             \
3313
482
                \
3314
482
    if ((nargs < 0) || (nargs > 1)) {       \
3315
17
  xmlXPathSetArityError(ctxt);       \
3316
17
  return;             \
3317
17
    }                \
3318
482
                \
3319
482
    if (nargs == 1) {           \
3320
449
  dt = xmlXPathPopString(ctxt);       \
3321
449
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
449
    }                \
3326
465
                \
3327
465
    ret = exsltDate##x##In##y(dt);        \
3328
465
                \
3329
465
    if (dt != NULL)           \
3330
465
  xmlFree(dt);           \
3331
465
                \
3332
465
    xmlXPathReturnNumber(ctxt, ret);        \
3333
465
}
date.c:exsltDateMinuteInHourFunction
Line
Count
Source
3310
132
            int nargs) {     \
3311
132
    xmlChar *dt = NULL;           \
3312
132
    double ret;             \
3313
132
                \
3314
132
    if ((nargs < 0) || (nargs > 1)) {       \
3315
16
  xmlXPathSetArityError(ctxt);       \
3316
16
  return;             \
3317
16
    }                \
3318
132
                \
3319
132
    if (nargs == 1) {           \
3320
101
  dt = xmlXPathPopString(ctxt);       \
3321
101
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
101
    }                \
3326
116
                \
3327
116
    ret = exsltDate##x##In##y(dt);        \
3328
116
                \
3329
116
    if (dt != NULL)           \
3330
116
  xmlFree(dt);           \
3331
116
                \
3332
116
    xmlXPathReturnNumber(ctxt, ret);        \
3333
116
}
date.c:exsltDateMonthInYearFunction
Line
Count
Source
3310
104
            int nargs) {     \
3311
104
    xmlChar *dt = NULL;           \
3312
104
    double ret;             \
3313
104
                \
3314
104
    if ((nargs < 0) || (nargs > 1)) {       \
3315
22
  xmlXPathSetArityError(ctxt);       \
3316
22
  return;             \
3317
22
    }                \
3318
104
                \
3319
104
    if (nargs == 1) {           \
3320
63
  dt = xmlXPathPopString(ctxt);       \
3321
63
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
63
    }                \
3326
82
                \
3327
82
    ret = exsltDate##x##In##y(dt);        \
3328
82
                \
3329
82
    if (dt != NULL)           \
3330
82
  xmlFree(dt);           \
3331
82
                \
3332
82
    xmlXPathReturnNumber(ctxt, ret);        \
3333
82
}
date.c:exsltDateSecondInMinuteFunction
Line
Count
Source
3310
109
            int nargs) {     \
3311
109
    xmlChar *dt = NULL;           \
3312
109
    double ret;             \
3313
109
                \
3314
109
    if ((nargs < 0) || (nargs > 1)) {       \
3315
0
  xmlXPathSetArityError(ctxt);       \
3316
0
  return;             \
3317
0
    }                \
3318
109
                \
3319
109
    if (nargs == 1) {           \
3320
94
  dt = xmlXPathPopString(ctxt);       \
3321
94
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
94
    }                \
3326
109
                \
3327
109
    ret = exsltDate##x##In##y(dt);        \
3328
109
                \
3329
109
    if (dt != NULL)           \
3330
109
  xmlFree(dt);           \
3331
109
                \
3332
109
    xmlXPathReturnNumber(ctxt, ret);        \
3333
109
}
date.c:exsltDateWeekInMonthFunction
Line
Count
Source
3310
130
            int nargs) {     \
3311
130
    xmlChar *dt = NULL;           \
3312
130
    double ret;             \
3313
130
                \
3314
130
    if ((nargs < 0) || (nargs > 1)) {       \
3315
25
  xmlXPathSetArityError(ctxt);       \
3316
25
  return;             \
3317
25
    }                \
3318
130
                \
3319
130
    if (nargs == 1) {           \
3320
90
  dt = xmlXPathPopString(ctxt);       \
3321
90
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
90
    }                \
3326
105
                \
3327
105
    ret = exsltDate##x##In##y(dt);        \
3328
105
                \
3329
105
    if (dt != NULL)           \
3330
105
  xmlFree(dt);           \
3331
105
                \
3332
105
    xmlXPathReturnNumber(ctxt, ret);        \
3333
105
}
date.c:exsltDateWeekInYearFunction
Line
Count
Source
3310
117
            int nargs) {     \
3311
117
    xmlChar *dt = NULL;           \
3312
117
    double ret;             \
3313
117
                \
3314
117
    if ((nargs < 0) || (nargs > 1)) {       \
3315
17
  xmlXPathSetArityError(ctxt);       \
3316
17
  return;             \
3317
17
    }                \
3318
117
                \
3319
117
    if (nargs == 1) {           \
3320
82
  dt = xmlXPathPopString(ctxt);       \
3321
82
  if (xmlXPathCheckError(ctxt)) {       \
3322
0
      xmlXPathSetTypeError(ctxt);       \
3323
0
      return;           \
3324
0
  }              \
3325
82
    }                \
3326
100
                \
3327
100
    ret = exsltDate##x##In##y(dt);        \
3328
100
                \
3329
100
    if (dt != NULL)           \
3330
100
  xmlFree(dt);           \
3331
100
                \
3332
100
    xmlXPathReturnNumber(ctxt, ret);        \
3333
100
}
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
139
{
3354
139
    xmlChar *dt = NULL;
3355
139
    const xmlChar *ret;
3356
3357
139
    if ((nargs < 0) || (nargs > 1)) {
3358
39
  xmlXPathSetArityError(ctxt);
3359
39
  return;
3360
39
    }
3361
3362
100
    if (nargs == 1) {
3363
80
  dt = xmlXPathPopString(ctxt);
3364
80
  if (xmlXPathCheckError(ctxt)) {
3365
0
      xmlXPathSetTypeError(ctxt);
3366
0
      return;
3367
0
  }
3368
80
    }
3369
3370
100
    ret = exsltDateMonthName(dt);
3371
3372
100
    if (dt != NULL)
3373
80
  xmlFree(dt);
3374
3375
100
    if (ret == NULL)
3376
0
  xmlXPathReturnEmptyString(ctxt);
3377
100
    else
3378
100
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3379
100
}
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
140
{
3391
140
    xmlChar *dt = NULL;
3392
140
    const xmlChar *ret;
3393
3394
140
    if ((nargs < 0) || (nargs > 1)) {
3395
16
  xmlXPathSetArityError(ctxt);
3396
16
  return;
3397
16
    }
3398
3399
124
    if (nargs == 1) {
3400
108
  dt = xmlXPathPopString(ctxt);
3401
108
  if (xmlXPathCheckError(ctxt)) {
3402
0
      xmlXPathSetTypeError(ctxt);
3403
0
      return;
3404
0
  }
3405
108
    }
3406
3407
124
    ret = exsltDateMonthAbbreviation(dt);
3408
3409
124
    if (dt != NULL)
3410
108
  xmlFree(dt);
3411
3412
124
    if (ret == NULL)
3413
0
  xmlXPathReturnEmptyString(ctxt);
3414
124
    else
3415
124
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3416
124
}
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
194
{
3482
194
    xmlChar *dt = NULL;
3483
194
    const xmlChar *ret;
3484
3485
194
    if ((nargs < 0) || (nargs > 1)) {
3486
19
  xmlXPathSetArityError(ctxt);
3487
19
  return;
3488
19
    }
3489
3490
175
    if (nargs == 1) {
3491
160
  dt = xmlXPathPopString(ctxt);
3492
160
  if (xmlXPathCheckError(ctxt)) {
3493
0
      xmlXPathSetTypeError(ctxt);
3494
0
      return;
3495
0
  }
3496
160
    }
3497
3498
175
    ret = exsltDateDayName(dt);
3499
3500
175
    if (dt != NULL)
3501
160
  xmlFree(dt);
3502
3503
175
    if (ret == NULL)
3504
0
  xmlXPathReturnEmptyString(ctxt);
3505
175
    else
3506
175
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3507
175
}
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
178
{
3519
178
    xmlChar *dt = NULL;
3520
178
    const xmlChar *ret;
3521
3522
178
    if ((nargs < 0) || (nargs > 1)) {
3523
20
  xmlXPathSetArityError(ctxt);
3524
20
  return;
3525
20
    }
3526
3527
158
    if (nargs == 1) {
3528
140
  dt = xmlXPathPopString(ctxt);
3529
140
  if (xmlXPathCheckError(ctxt)) {
3530
0
      xmlXPathSetTypeError(ctxt);
3531
0
      return;
3532
0
  }
3533
140
    }
3534
3535
158
    ret = exsltDateDayAbbreviation(dt);
3536
3537
158
    if (dt != NULL)
3538
140
  xmlFree(dt);
3539
3540
158
    if (ret == NULL)
3541
0
  xmlXPathReturnEmptyString(ctxt);
3542
158
    else
3543
158
  xmlXPathReturnString(ctxt, xmlStrdup(ret));
3544
158
}
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
153
{
3584
153
    xmlChar *str = NULL;
3585
153
    double   ret;
3586
3587
153
    if (nargs > 1) {
3588
22
  xmlXPathSetArityError(ctxt);
3589
22
  return;
3590
22
    }
3591
3592
131
    if (nargs == 1) {
3593
116
  str = xmlXPathPopString(ctxt);
3594
116
  if (xmlXPathCheckError(ctxt)) {
3595
0
      xmlXPathSetTypeError(ctxt);
3596
0
      return;
3597
0
  }
3598
116
    }
3599
3600
131
    ret = exsltDateSeconds(str);
3601
131
    if (str != NULL)
3602
116
  xmlFree(str);
3603
3604
131
    xmlXPathReturnNumber(ctxt, ret);
3605
131
}
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
430
{
3617
430
    xmlChar *ret, *xstr, *ystr;
3618
3619
430
    if (nargs != 2) {
3620
34
  xmlXPathSetArityError(ctxt);
3621
34
  return;
3622
34
    }
3623
396
    ystr = xmlXPathPopString(ctxt);
3624
396
    if (xmlXPathCheckError(ctxt))
3625
0
  return;
3626
3627
396
    xstr = xmlXPathPopString(ctxt);
3628
396
    if (xmlXPathCheckError(ctxt)) {
3629
0
        xmlFree(ystr);
3630
0
  return;
3631
0
    }
3632
3633
396
    ret = exsltDateAdd(xstr, ystr);
3634
3635
396
    xmlFree(ystr);
3636
396
    xmlFree(xstr);
3637
3638
396
    if (ret == NULL)
3639
286
        xmlXPathReturnEmptyString(ctxt);
3640
110
    else
3641
110
  xmlXPathReturnString(ctxt, ret);
3642
396
}
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
360
{
3654
360
    xmlChar *ret, *xstr, *ystr;
3655
3656
360
    if (nargs != 2) {
3657
30
  xmlXPathSetArityError(ctxt);
3658
30
  return;
3659
30
    }
3660
330
    ystr = xmlXPathPopString(ctxt);
3661
330
    if (xmlXPathCheckError(ctxt))
3662
0
  return;
3663
3664
330
    xstr = xmlXPathPopString(ctxt);
3665
330
    if (xmlXPathCheckError(ctxt)) {
3666
0
        xmlFree(ystr);
3667
0
  return;
3668
0
    }
3669
3670
330
    ret = exsltDateAddDuration(xstr, ystr);
3671
3672
330
    xmlFree(ystr);
3673
330
    xmlFree(xstr);
3674
3675
330
    if (ret == NULL)
3676
284
        xmlXPathReturnEmptyString(ctxt);
3677
46
    else
3678
46
  xmlXPathReturnString(ctxt, ret);
3679
330
}
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
383
{
3691
383
    xmlChar *ret, *xstr, *ystr;
3692
3693
383
    if (nargs != 2) {
3694
38
  xmlXPathSetArityError(ctxt);
3695
38
  return;
3696
38
    }
3697
345
    ystr = xmlXPathPopString(ctxt);
3698
345
    if (xmlXPathCheckError(ctxt))
3699
0
  return;
3700
3701
345
    xstr = xmlXPathPopString(ctxt);
3702
345
    if (xmlXPathCheckError(ctxt)) {
3703
0
        xmlFree(ystr);
3704
0
  return;
3705
0
    }
3706
3707
345
    ret = exsltDateDifference(xstr, ystr);
3708
3709
345
    xmlFree(ystr);
3710
345
    xmlFree(xstr);
3711
3712
345
    if (ret == NULL)
3713
291
        xmlXPathReturnEmptyString(ctxt);
3714
54
    else
3715
54
  xmlXPathReturnString(ctxt, ret);
3716
345
}
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
201
{
3728
201
    xmlChar *ret;
3729
201
    xmlChar *number = NULL;
3730
3731
201
    if ((nargs < 0) || (nargs > 1)) {
3732
21
  xmlXPathSetArityError(ctxt);
3733
21
  return;
3734
21
    }
3735
3736
180
    if (nargs == 1) {
3737
142
  number = xmlXPathPopString(ctxt);
3738
142
  if (xmlXPathCheckError(ctxt)) {
3739
0
      xmlXPathSetTypeError(ctxt);
3740
0
      return;
3741
0
  }
3742
142
    }
3743
3744
180
    ret = exsltDateDuration(number);
3745
3746
180
    if (number != NULL)
3747
142
  xmlFree(number);
3748
3749
180
    if (ret == NULL)
3750
73
  xmlXPathReturnEmptyString(ctxt);
3751
107
    else
3752
107
  xmlXPathReturnString(ctxt, ret);
3753
180
}
3754
3755
/**
3756
 * exsltDateRegister:
3757
 *
3758
 * Registers the EXSLT - Dates and Times module
3759
 */
3760
void
3761
exsltDateRegister (void)
3762
3.72k
{
3763
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "add",
3764
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3765
3.72k
           exsltDateAddFunction);
3766
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "add-duration",
3767
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3768
3.72k
           exsltDateAddDurationFunction);
3769
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "date",
3770
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3771
3.72k
           exsltDateDateFunction);
3772
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "date-time",
3773
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3774
3.72k
           exsltDateDateTimeFunction);
3775
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-abbreviation",
3776
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3777
3.72k
           exsltDateDayAbbreviationFunction);
3778
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-in-month",
3779
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3780
3.72k
           exsltDateDayInMonthFunction);
3781
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-in-week",
3782
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3783
3.72k
           exsltDateDayInWeekFunction);
3784
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-in-year",
3785
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3786
3.72k
           exsltDateDayInYearFunction);
3787
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-name",
3788
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3789
3.72k
           exsltDateDayNameFunction);
3790
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "day-of-week-in-month",
3791
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3792
3.72k
           exsltDateDayOfWeekInMonthFunction);
3793
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "difference",
3794
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3795
3.72k
           exsltDateDifferenceFunction);
3796
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "duration",
3797
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3798
3.72k
           exsltDateDurationFunction);
3799
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "hour-in-day",
3800
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3801
3.72k
           exsltDateHourInDayFunction);
3802
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "leap-year",
3803
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3804
3.72k
           exsltDateLeapYearFunction);
3805
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "minute-in-hour",
3806
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3807
3.72k
           exsltDateMinuteInHourFunction);
3808
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "month-abbreviation",
3809
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3810
3.72k
           exsltDateMonthAbbreviationFunction);
3811
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "month-in-year",
3812
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3813
3.72k
           exsltDateMonthInYearFunction);
3814
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "month-name",
3815
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3816
3.72k
           exsltDateMonthNameFunction);
3817
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "second-in-minute",
3818
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3819
3.72k
           exsltDateSecondInMinuteFunction);
3820
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "seconds",
3821
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3822
3.72k
           exsltDateSecondsFunction);
3823
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "sum",
3824
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3825
3.72k
           exsltDateSumFunction);
3826
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "time",
3827
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3828
3.72k
           exsltDateTimeFunction);
3829
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "week-in-month",
3830
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3831
3.72k
           exsltDateWeekInMonthFunction);
3832
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "week-in-year",
3833
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3834
3.72k
           exsltDateWeekInYearFunction);
3835
3.72k
    xsltRegisterExtModuleFunction ((const xmlChar *) "year",
3836
3.72k
           (const xmlChar *) EXSLT_DATE_NAMESPACE,
3837
3.72k
           exsltDateYearFunction);
3838
3.72k
}
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
}