2011-04-23 08:31:15 -04:00
|
|
|
/*
|
2012-01-18 17:13:10 -05:00
|
|
|
date_parse.c: Coded by Tadayoshi Funaba 2011,2012
|
2011-04-23 08:31:15 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "ruby/encoding.h"
|
|
|
|
#include "ruby/re.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2016-10-01 04:52:42 -04:00
|
|
|
RUBY_EXTERN VALUE rb_int_positive_pow(long x, unsigned long y);
|
|
|
|
RUBY_EXTERN unsigned long ruby_scan_digits(const char *str, ssize_t len, int base, size_t *retlen, int *overflow);
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
/* #define TIGHT_PARSER */
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
#define sizeof_array(o) (sizeof o / sizeof o[0])
|
|
|
|
|
|
|
|
#define f_negate(x) rb_funcall(x, rb_intern("-@"), 0)
|
|
|
|
#define f_add(x,y) rb_funcall(x, '+', 1, y)
|
|
|
|
#define f_sub(x,y) rb_funcall(x, '-', 1, y)
|
|
|
|
#define f_mul(x,y) rb_funcall(x, '*', 1, y)
|
|
|
|
#define f_div(x,y) rb_funcall(x, '/', 1, y)
|
2011-05-21 08:25:03 -04:00
|
|
|
#define f_idiv(x,y) rb_funcall(x, rb_intern("div"), 1, y)
|
2011-04-23 08:31:15 -04:00
|
|
|
#define f_mod(x,y) rb_funcall(x, '%', 1, y)
|
|
|
|
#define f_expt(x,y) rb_funcall(x, rb_intern("**"), 1, y)
|
|
|
|
|
|
|
|
#define f_lt_p(x,y) rb_funcall(x, '<', 1, y)
|
|
|
|
#define f_gt_p(x,y) rb_funcall(x, '>', 1, y)
|
|
|
|
#define f_le_p(x,y) rb_funcall(x, rb_intern("<="), 1, y)
|
|
|
|
#define f_ge_p(x,y) rb_funcall(x, rb_intern(">="), 1, y)
|
|
|
|
|
|
|
|
#define f_to_s(x) rb_funcall(x, rb_intern("to_s"), 0)
|
|
|
|
|
|
|
|
#define f_match(r,s) rb_funcall(r, rb_intern("match"), 1, s)
|
|
|
|
#define f_aref(o,i) rb_funcall(o, rb_intern("[]"), 1, i)
|
|
|
|
#define f_aref2(o,i,j) rb_funcall(o, rb_intern("[]"), 2, i, j)
|
|
|
|
#define f_begin(o,i) rb_funcall(o, rb_intern("begin"), 1, i)
|
|
|
|
#define f_end(o,i) rb_funcall(o, rb_intern("end"), 1, i)
|
|
|
|
#define f_aset(o,i,v) rb_funcall(o, rb_intern("[]="), 2, i, v)
|
|
|
|
#define f_aset2(o,i,j,v) rb_funcall(o, rb_intern("[]="), 3, i, j, v)
|
|
|
|
#define f_sub_bang(s,r,x) rb_funcall(s, rb_intern("sub!"), 2, r, x)
|
|
|
|
#define f_gsub_bang(s,r,x) rb_funcall(s, rb_intern("gsub!"), 2, r, x)
|
|
|
|
|
2018-02-25 08:17:35 -05:00
|
|
|
#define set_hash(k,v) rb_hash_aset(hash, ID2SYM(rb_intern(k"")), v)
|
|
|
|
#define ref_hash(k) rb_hash_aref(hash, ID2SYM(rb_intern(k"")))
|
|
|
|
#define del_hash(k) rb_hash_delete(hash, ID2SYM(rb_intern(k"")))
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
#define cstr2num(s) rb_cstr_to_inum(s, 10, 0)
|
|
|
|
#define str2num(s) rb_str_to_inum(s, 10, 0)
|
|
|
|
|
2016-06-07 21:09:21 -04:00
|
|
|
static const char abbr_days[][4] = {
|
2011-04-23 08:31:15 -04:00
|
|
|
"sun", "mon", "tue", "wed",
|
|
|
|
"thu", "fri", "sat"
|
|
|
|
};
|
|
|
|
|
2016-06-07 21:09:21 -04:00
|
|
|
static const char abbr_months[][4] = {
|
2011-04-23 08:31:15 -04:00
|
|
|
"jan", "feb", "mar", "apr", "may", "jun",
|
|
|
|
"jul", "aug", "sep", "oct", "nov", "dec"
|
|
|
|
};
|
|
|
|
|
2011-05-08 05:41:38 -04:00
|
|
|
#define issign(c) ((c) == '-' || (c) == '+')
|
|
|
|
#define asp_string() rb_str_new(" ", 1)
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-10 04:21:41 -05:00
|
|
|
#define asuba_string() rb_str_new("\001", 1)
|
|
|
|
#define asubb_string() rb_str_new("\002", 1)
|
2012-10-28 05:53:08 -04:00
|
|
|
#define asubw_string() rb_str_new("\027", 1)
|
|
|
|
#define asubt_string() rb_str_new("\024", 1)
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
2011-05-08 05:41:38 -04:00
|
|
|
|
2018-09-01 02:19:05 -04:00
|
|
|
static size_t
|
|
|
|
digit_span(const char *s, const char *e)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
while (s + i < e && isdigit(s[i])) i++;
|
|
|
|
return i;
|
|
|
|
}
|
2012-11-11 04:45:44 -05:00
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static void
|
|
|
|
s3e(VALUE hash, VALUE y, VALUE m, VALUE d, int bc)
|
|
|
|
{
|
2015-06-16 05:46:06 -04:00
|
|
|
VALUE vbuf = 0;
|
2011-04-23 08:31:15 -04:00
|
|
|
VALUE c = Qnil;
|
|
|
|
|
2014-08-02 21:56:01 -04:00
|
|
|
if (!RB_TYPE_P(m, T_STRING))
|
2011-04-23 08:31:15 -04:00
|
|
|
m = f_to_s(m);
|
|
|
|
|
|
|
|
if (!NIL_P(y) && !NIL_P(m) && NIL_P(d)) {
|
|
|
|
VALUE oy = y;
|
|
|
|
VALUE om = m;
|
|
|
|
VALUE od = d;
|
|
|
|
|
|
|
|
y = od;
|
|
|
|
m = oy;
|
|
|
|
d = om;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NIL_P(y)) {
|
|
|
|
if (!NIL_P(d) && RSTRING_LEN(d) > 2) {
|
|
|
|
y = d;
|
|
|
|
d = Qnil;
|
|
|
|
}
|
2018-09-01 02:19:05 -04:00
|
|
|
if (!NIL_P(d) && RSTRING_LEN(d) > 0 && *RSTRING_PTR(d) == '\'') {
|
2011-05-09 08:35:49 -04:00
|
|
|
y = d;
|
|
|
|
d = Qnil;
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(y)) {
|
|
|
|
const char *s, *bp, *ep;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
s = RSTRING_PTR(y);
|
2018-09-01 02:19:05 -04:00
|
|
|
ep = RSTRING_END(y);
|
|
|
|
while (s < ep && !issign(*s) && !isdigit(*s))
|
2011-04-23 08:31:15 -04:00
|
|
|
s++;
|
2018-09-01 02:19:05 -04:00
|
|
|
if (s >= ep) goto no_date;
|
2011-04-23 08:31:15 -04:00
|
|
|
bp = s;
|
2012-03-02 22:14:27 -05:00
|
|
|
if (issign((unsigned char)*s))
|
2011-04-23 08:31:15 -04:00
|
|
|
s++;
|
2018-09-01 02:19:05 -04:00
|
|
|
l = digit_span(s, ep);
|
2011-04-23 08:31:15 -04:00
|
|
|
ep = s + l;
|
|
|
|
if (*ep) {
|
|
|
|
y = d;
|
|
|
|
d = rb_str_new(bp, ep - bp);
|
|
|
|
}
|
2018-09-01 02:19:05 -04:00
|
|
|
no_date:;
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(m)) {
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
s = RSTRING_PTR(m);
|
|
|
|
if (*s == '\'' || RSTRING_LEN(m) > 2) {
|
|
|
|
/* us -> be */
|
|
|
|
VALUE oy = y;
|
|
|
|
VALUE om = m;
|
|
|
|
VALUE od = d;
|
|
|
|
|
|
|
|
y = om;
|
|
|
|
m = od;
|
|
|
|
d = oy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(d)) {
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
s = RSTRING_PTR(d);
|
|
|
|
if (*s == '\'' || RSTRING_LEN(d) > 2) {
|
|
|
|
VALUE oy = y;
|
|
|
|
VALUE od = d;
|
|
|
|
|
|
|
|
y = od;
|
|
|
|
d = oy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(y)) {
|
|
|
|
const char *s, *bp, *ep;
|
|
|
|
int sign = 0;
|
|
|
|
size_t l;
|
|
|
|
VALUE iy;
|
|
|
|
|
|
|
|
s = RSTRING_PTR(y);
|
2018-09-01 02:19:05 -04:00
|
|
|
ep = RSTRING_END(y);
|
|
|
|
while (s < ep && !issign(*s) && !isdigit(*s))
|
2011-04-23 08:31:15 -04:00
|
|
|
s++;
|
2018-09-01 02:19:05 -04:00
|
|
|
if (s >= ep) goto no_year;
|
2011-04-23 08:31:15 -04:00
|
|
|
bp = s;
|
2011-05-08 05:41:38 -04:00
|
|
|
if (issign(*s)) {
|
2011-04-23 08:31:15 -04:00
|
|
|
s++;
|
|
|
|
sign = 1;
|
|
|
|
}
|
|
|
|
if (sign)
|
|
|
|
c = Qfalse;
|
2018-09-01 02:19:05 -04:00
|
|
|
l = digit_span(s, ep);
|
2011-04-23 08:31:15 -04:00
|
|
|
ep = s + l;
|
|
|
|
if (l > 2)
|
|
|
|
c = Qfalse;
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
2015-06-16 05:46:06 -04:00
|
|
|
buf = ALLOCV_N(char, vbuf, ep - bp + 1);
|
2011-04-23 08:31:15 -04:00
|
|
|
memcpy(buf, bp, ep - bp);
|
|
|
|
buf[ep - bp] = '\0';
|
|
|
|
iy = cstr2num(buf);
|
2015-06-16 05:46:06 -04:00
|
|
|
ALLOCV_END(vbuf);
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
set_hash("year", iy);
|
2018-09-01 02:19:05 -04:00
|
|
|
no_year:;
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
2012-11-10 04:21:41 -05:00
|
|
|
if (bc)
|
|
|
|
set_hash("_bc", Qtrue);
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
if (!NIL_P(m)) {
|
|
|
|
const char *s, *bp, *ep;
|
|
|
|
size_t l;
|
|
|
|
VALUE im;
|
|
|
|
|
|
|
|
s = RSTRING_PTR(m);
|
2018-09-01 02:19:05 -04:00
|
|
|
ep = RSTRING_END(m);
|
|
|
|
while (s < ep && !isdigit(*s))
|
2011-04-23 08:31:15 -04:00
|
|
|
s++;
|
2018-09-01 02:19:05 -04:00
|
|
|
if (s >= ep) goto no_month;
|
2011-04-23 08:31:15 -04:00
|
|
|
bp = s;
|
2018-09-01 02:19:05 -04:00
|
|
|
l = digit_span(s, ep);
|
2011-04-23 08:31:15 -04:00
|
|
|
ep = s + l;
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
2015-06-16 05:46:06 -04:00
|
|
|
buf = ALLOCV_N(char, vbuf, ep - bp + 1);
|
2011-04-23 08:31:15 -04:00
|
|
|
memcpy(buf, bp, ep - bp);
|
|
|
|
buf[ep - bp] = '\0';
|
|
|
|
im = cstr2num(buf);
|
2015-06-16 05:46:06 -04:00
|
|
|
ALLOCV_END(vbuf);
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
set_hash("mon", im);
|
2018-09-01 02:19:05 -04:00
|
|
|
no_month:;
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(d)) {
|
|
|
|
const char *s, *bp, *ep;
|
|
|
|
size_t l;
|
|
|
|
VALUE id;
|
|
|
|
|
|
|
|
s = RSTRING_PTR(d);
|
2018-09-01 02:19:05 -04:00
|
|
|
ep = RSTRING_END(d);
|
|
|
|
while (s < ep && !isdigit(*s))
|
2011-04-23 08:31:15 -04:00
|
|
|
s++;
|
2018-09-01 02:19:05 -04:00
|
|
|
if (s >= ep) goto no_mday;
|
2011-04-23 08:31:15 -04:00
|
|
|
bp = s;
|
2018-09-01 02:19:05 -04:00
|
|
|
l = digit_span(s, ep);
|
2011-04-23 08:31:15 -04:00
|
|
|
ep = s + l;
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
2015-06-16 05:46:06 -04:00
|
|
|
buf = ALLOCV_N(char, vbuf, ep - bp + 1);
|
2011-04-23 08:31:15 -04:00
|
|
|
memcpy(buf, bp, ep - bp);
|
|
|
|
buf[ep - bp] = '\0';
|
|
|
|
id = cstr2num(buf);
|
2015-06-16 05:46:06 -04:00
|
|
|
ALLOCV_END(vbuf);
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
set_hash("mday", id);
|
2018-09-01 02:19:05 -04:00
|
|
|
no_mday:;
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(c))
|
|
|
|
set_hash("_comp", c);
|
|
|
|
}
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
#define DAYS "sunday|monday|tuesday|wednesday|thursday|friday|saturday"
|
|
|
|
#define MONTHS "january|february|march|april|may|june|july|august|september|october|november|december"
|
2011-04-23 08:31:15 -04:00
|
|
|
#define ABBR_DAYS "sun|mon|tue|wed|thu|fri|sat"
|
|
|
|
#define ABBR_MONTHS "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec"
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-03 01:47:32 -04:00
|
|
|
#define VALID_DAYS "(?:" DAYS ")" "|(?:tues|wednes|thurs|thur|" ABBR_DAYS ")\\.?"
|
|
|
|
#define VALID_MONTHS "(?:" MONTHS ")" "|(?:sept|" ABBR_MONTHS ")\\.?"
|
|
|
|
#define DOTLESS_VALID_MONTHS "(?:" MONTHS ")" "|(?:sept|" ABBR_MONTHS ")"
|
2012-10-21 05:41:25 -04:00
|
|
|
#define BOS "\\A\\s*"
|
2012-11-10 04:21:41 -05:00
|
|
|
#define FPA "\\001"
|
|
|
|
#define FPB "\\002"
|
2012-10-28 05:53:08 -04:00
|
|
|
#define FPW "\\027"
|
|
|
|
#define FPT "\\024"
|
|
|
|
#define FPW_COM "\\s*(?:" FPW "\\s*,?)?\\s*"
|
|
|
|
#define FPT_COM "\\s*(?:" FPT "\\s*,?)?\\s*"
|
|
|
|
#define COM_FPW "\\s*(?:,?\\s*" FPW ")?\\s*"
|
|
|
|
#define COM_FPT "\\s*(?:,?\\s*(?:@|\\b[aA][tT]\\b)?\\s*" FPT ")?\\s*"
|
|
|
|
#define TEE_FPT "\\s*(?:[tT]?" FPT ")?"
|
2012-10-21 05:41:25 -04:00
|
|
|
#define EOS "\\s*\\z"
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE
|
|
|
|
regcomp(const char *source, long len, int opt)
|
|
|
|
{
|
|
|
|
VALUE pat;
|
|
|
|
|
|
|
|
pat = rb_reg_new(source, len, opt);
|
|
|
|
rb_gc_register_mark_object(pat);
|
|
|
|
return pat;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define REGCOMP(pat,opt) \
|
2018-09-01 00:09:02 -04:00
|
|
|
do { \
|
2011-04-23 08:31:15 -04:00
|
|
|
if (NIL_P(pat)) \
|
|
|
|
pat = regcomp(pat##_source, sizeof pat##_source - 1, opt); \
|
2018-09-01 00:09:02 -04:00
|
|
|
} while (0)
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
#define REGCOMP_0(pat) REGCOMP(pat, 0)
|
|
|
|
#define REGCOMP_I(pat) REGCOMP(pat, ONIG_OPTION_IGNORECASE)
|
|
|
|
|
2012-01-18 17:13:10 -05:00
|
|
|
#define MATCH(s,p,c) \
|
2018-09-01 00:09:02 -04:00
|
|
|
do { \
|
2012-01-18 17:13:10 -05:00
|
|
|
return match(s, p, hash, c); \
|
2018-09-01 00:09:02 -04:00
|
|
|
} while (0)
|
2012-01-18 17:13:10 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
match(VALUE str, VALUE pat, VALUE hash, int (*cb)(VALUE, VALUE))
|
|
|
|
{
|
|
|
|
VALUE m;
|
|
|
|
|
|
|
|
m = f_match(pat, str);
|
|
|
|
|
|
|
|
if (NIL_P(m))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
(*cb)(m, hash);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static int
|
2012-10-28 05:53:08 -04:00
|
|
|
subx(VALUE str, VALUE rep, VALUE pat, VALUE hash, int (*cb)(VALUE, VALUE))
|
2011-04-23 08:31:15 -04:00
|
|
|
{
|
|
|
|
VALUE m;
|
|
|
|
|
|
|
|
m = f_match(pat, str);
|
|
|
|
|
|
|
|
if (NIL_P(m))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
VALUE be, en;
|
|
|
|
|
|
|
|
be = f_begin(m, INT2FIX(0));
|
|
|
|
en = f_end(m, INT2FIX(0));
|
2012-10-28 05:53:08 -04:00
|
|
|
f_aset2(str, be, LONG2NUM(NUM2LONG(en) - NUM2LONG(be)), rep);
|
2011-04-23 08:31:15 -04:00
|
|
|
(*cb)(m, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#define SUBS(s,p,c) \
|
2018-09-01 00:09:02 -04:00
|
|
|
do { \
|
2012-10-28 05:53:08 -04:00
|
|
|
return subx(s, asp_string(), p, hash, c); \
|
2018-09-01 00:09:02 -04:00
|
|
|
} while (0)
|
2012-10-21 05:41:25 -04:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-10 04:21:41 -05:00
|
|
|
#define SUBA(s,p,c) \
|
2018-09-01 00:09:02 -04:00
|
|
|
do { \
|
2012-11-10 04:21:41 -05:00
|
|
|
return subx(s, asuba_string(), p, hash, c); \
|
2018-09-01 00:09:02 -04:00
|
|
|
} while (0)
|
2012-11-10 04:21:41 -05:00
|
|
|
|
|
|
|
#define SUBB(s,p,c) \
|
2018-09-01 00:09:02 -04:00
|
|
|
do { \
|
2012-11-10 04:21:41 -05:00
|
|
|
return subx(s, asubb_string(), p, hash, c); \
|
2018-09-01 00:09:02 -04:00
|
|
|
} while (0)
|
2012-11-10 04:21:41 -05:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#define SUBW(s,p,c) \
|
2018-09-01 00:09:02 -04:00
|
|
|
do { \
|
2012-10-28 05:53:08 -04:00
|
|
|
return subx(s, asubw_string(), p, hash, c); \
|
2018-09-01 00:09:02 -04:00
|
|
|
} while (0)
|
2012-10-21 05:41:25 -04:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#define SUBT(s,p,c) \
|
2018-09-01 00:09:02 -04:00
|
|
|
do { \
|
2012-10-28 05:53:08 -04:00
|
|
|
return subx(s, asubt_string(), p, hash, c); \
|
2018-09-01 00:09:02 -04:00
|
|
|
} while (0)
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
|
|
|
|
2016-06-07 21:09:23 -04:00
|
|
|
#include "zonetab.h"
|
2011-05-21 08:25:03 -04:00
|
|
|
|
2016-10-01 05:00:05 -04:00
|
|
|
static int
|
|
|
|
str_end_with(const char *s, long l, const char *w)
|
|
|
|
{
|
|
|
|
int n = (int)strlen(w);
|
|
|
|
return (l >= n && strncmp(s - n, w, n) == 0);
|
|
|
|
}
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
VALUE
|
|
|
|
date_zone_to_diff(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE offset = Qnil;
|
2015-06-16 05:46:06 -04:00
|
|
|
VALUE vbuf = 0;
|
2011-05-21 08:25:03 -04:00
|
|
|
|
|
|
|
long l, i;
|
|
|
|
char *s, *dest, *d;
|
|
|
|
int sp = 1;
|
|
|
|
|
|
|
|
l = RSTRING_LEN(str);
|
|
|
|
s = RSTRING_PTR(str);
|
|
|
|
|
2015-06-16 05:46:06 -04:00
|
|
|
dest = d = ALLOCV_N(char, vbuf, l + 1);
|
2011-05-21 08:25:03 -04:00
|
|
|
|
|
|
|
for (i = 0; i < l; i++) {
|
2012-03-02 22:14:27 -05:00
|
|
|
if (isspace((unsigned char)s[i]) || s[i] == '\0') {
|
2011-05-21 08:25:03 -04:00
|
|
|
if (!sp)
|
|
|
|
*d++ = ' ';
|
|
|
|
sp = 1;
|
|
|
|
}
|
|
|
|
else {
|
2012-03-02 22:14:27 -05:00
|
|
|
if (isalpha((unsigned char)s[i]))
|
|
|
|
*d++ = tolower((unsigned char)s[i]);
|
2011-05-21 08:25:03 -04:00
|
|
|
else
|
|
|
|
*d++ = s[i];
|
|
|
|
sp = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (d > dest) {
|
|
|
|
if (*(d - 1) == ' ')
|
|
|
|
--d;
|
|
|
|
*d = '\0';
|
|
|
|
}
|
2016-10-01 04:52:42 -04:00
|
|
|
l = d - dest;
|
|
|
|
s = dest;
|
2011-05-21 08:25:03 -04:00
|
|
|
{
|
2016-10-01 04:52:42 -04:00
|
|
|
static const char STD[] = " standard time";
|
|
|
|
static const char DST1[] = " daylight time";
|
|
|
|
static const char DST2[] = " dst";
|
2011-05-21 08:25:03 -04:00
|
|
|
int dst = 0;
|
|
|
|
|
2016-10-01 05:00:05 -04:00
|
|
|
if (str_end_with(d, l, STD)) {
|
2016-10-01 04:52:42 -04:00
|
|
|
l -= sizeof(STD) - 1;
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
2016-10-01 05:00:05 -04:00
|
|
|
else if (str_end_with(d, l, DST1)) {
|
2016-10-01 04:52:42 -04:00
|
|
|
l -= sizeof(DST1) - 1;
|
2011-05-21 08:25:03 -04:00
|
|
|
dst = 1;
|
|
|
|
}
|
2016-10-01 05:00:05 -04:00
|
|
|
else if (str_end_with(d, l, DST2)) {
|
2016-10-01 04:52:42 -04:00
|
|
|
l -= sizeof(DST2) - 1;
|
|
|
|
dst = 1;
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
{
|
2016-10-01 04:52:42 -04:00
|
|
|
const struct zone *z = zonetab(s, (unsigned int)l);
|
2016-06-07 21:09:23 -04:00
|
|
|
if (z) {
|
|
|
|
int d = z->offset;
|
2011-05-21 08:25:03 -04:00
|
|
|
if (dst)
|
2016-06-07 21:09:23 -04:00
|
|
|
d += 3600;
|
|
|
|
offset = INT2FIX(d);
|
2011-05-21 08:25:03 -04:00
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2016-10-01 04:52:42 -04:00
|
|
|
char *p;
|
|
|
|
int sign = 0;
|
2016-10-01 10:12:34 -04:00
|
|
|
long hour = 0, min = 0, sec = 0;
|
2011-05-21 08:25:03 -04:00
|
|
|
|
2016-10-01 04:52:42 -04:00
|
|
|
if (l > 3 &&
|
|
|
|
(strncmp(s, "gmt", 3) == 0 ||
|
|
|
|
strncmp(s, "utc", 3) == 0)) {
|
2011-05-21 08:25:03 -04:00
|
|
|
s += 3;
|
2016-10-01 04:52:42 -04:00
|
|
|
l -= 3;
|
|
|
|
}
|
2011-05-21 08:25:03 -04:00
|
|
|
if (issign(*s)) {
|
2016-10-01 04:52:42 -04:00
|
|
|
sign = *s == '-';
|
2011-05-21 08:25:03 -04:00
|
|
|
s++;
|
2016-10-01 04:52:42 -04:00
|
|
|
l--;
|
2011-05-21 08:25:03 -04:00
|
|
|
|
2016-10-01 04:52:42 -04:00
|
|
|
hour = STRTOUL(s, &p, 10);
|
|
|
|
if (*p == ':') {
|
2011-05-21 08:25:03 -04:00
|
|
|
s = ++p;
|
2016-10-01 04:52:42 -04:00
|
|
|
min = STRTOUL(s, &p, 10);
|
|
|
|
if (*p == ':') {
|
2011-05-21 08:25:03 -04:00
|
|
|
s = ++p;
|
2016-10-01 04:52:42 -04:00
|
|
|
sec = STRTOUL(s, &p, 10);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
goto num;
|
|
|
|
}
|
2016-10-01 04:52:42 -04:00
|
|
|
if (*p == ',' || *p == '.') {
|
|
|
|
char *e = 0;
|
|
|
|
p++;
|
|
|
|
min = STRTOUL(p, &e, 10) * 3600;
|
|
|
|
if (sign) {
|
|
|
|
hour = -hour;
|
|
|
|
min = -min;
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
2016-10-01 04:52:42 -04:00
|
|
|
offset = rb_rational_new(INT2FIX(min),
|
|
|
|
rb_int_positive_pow(10, (int)(e - p)));
|
|
|
|
offset = f_add(INT2FIX(hour * 3600), offset);
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
else if (l > 2) {
|
|
|
|
size_t n;
|
|
|
|
int ov;
|
|
|
|
|
|
|
|
if (l >= 1)
|
|
|
|
hour = ruby_scan_digits(&s[0], 2 - l % 2, 10, &n, &ov);
|
|
|
|
if (l >= 3)
|
|
|
|
min = ruby_scan_digits(&s[2 - l % 2], 2, 10, &n, &ov);
|
|
|
|
if (l >= 5)
|
|
|
|
sec = ruby_scan_digits(&s[4 - l % 2], 2, 10, &n, &ov);
|
2011-05-21 08:25:03 -04:00
|
|
|
goto num;
|
|
|
|
}
|
|
|
|
num:
|
2016-10-01 04:52:42 -04:00
|
|
|
sec += min * 60 + hour * 3600;
|
|
|
|
if (sign) sec = -sec;
|
|
|
|
offset = INT2FIX(sec);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-09 10:00:49 -04:00
|
|
|
RB_GC_GUARD(str);
|
2011-05-21 08:25:03 -04:00
|
|
|
ok:
|
2015-06-16 05:46:06 -04:00
|
|
|
ALLOCV_END(vbuf);
|
2011-05-21 08:25:03 -04:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static int
|
|
|
|
day_num(VALUE s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < (int)sizeof_array(abbr_days); i++)
|
|
|
|
if (strncasecmp(abbr_days[i], RSTRING_PTR(s), 3) == 0)
|
|
|
|
break;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mon_num(VALUE s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < (int)sizeof_array(abbr_months); i++)
|
|
|
|
if (strncasecmp(abbr_months[i], RSTRING_PTR(s), 3) == 0)
|
|
|
|
break;
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_day_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
s = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
set_hash("wday", INT2FIX(day_num(s)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_day(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"\\b(" ABBR_DAYS ")[^-/\\d\\s]*"
|
|
|
|
#else
|
2012-10-28 05:53:08 -04:00
|
|
|
"(" VALID_DAYS ")"
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
SUBS(str, pat, parse_day_cb);
|
2012-10-28 05:53:08 -04:00
|
|
|
#else
|
|
|
|
SUBW(str, pat, parse_day_cb);
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_time2_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE h, min, s, f, p;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
h = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
h = str2num(h);
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
min = rb_reg_nth_match(2, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
if (!NIL_P(min))
|
|
|
|
min = str2num(min);
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
s = rb_reg_nth_match(3, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
if (!NIL_P(s))
|
|
|
|
s = str2num(s);
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
f = rb_reg_nth_match(4, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
if (!NIL_P(f))
|
|
|
|
f = rb_rational_new2(str2num(f),
|
|
|
|
f_expt(INT2FIX(10), LONG2NUM(RSTRING_LEN(f))));
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
p = rb_reg_nth_match(5, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
if (!NIL_P(p)) {
|
|
|
|
int ih = NUM2INT(h);
|
|
|
|
ih %= 12;
|
|
|
|
if (*RSTRING_PTR(p) == 'P' || *RSTRING_PTR(p) == 'p')
|
|
|
|
ih += 12;
|
|
|
|
h = INT2FIX(ih);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("hour", h);
|
|
|
|
if (!NIL_P(min))
|
|
|
|
set_hash("min", min);
|
|
|
|
if (!NIL_P(s))
|
|
|
|
set_hash("sec", s);
|
|
|
|
if (!NIL_P(f))
|
|
|
|
set_hash("sec_fraction", f);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_time_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2011-05-31 09:28:48 -04:00
|
|
|
"\\A(\\d+)h?"
|
2011-04-23 08:31:15 -04:00
|
|
|
"(?:\\s*:?\\s*(\\d+)m?"
|
|
|
|
"(?:"
|
|
|
|
"\\s*:?\\s*(\\d+)(?:[,.](\\d+))?s?"
|
|
|
|
")?"
|
|
|
|
")?"
|
|
|
|
"(?:\\s*([ap])(?:m\\b|\\.m\\.))?";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
VALUE s1, s2;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
s1 = rb_reg_nth_match(1, m);
|
|
|
|
s2 = rb_reg_nth_match(2, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
if (!NIL_P(s2))
|
|
|
|
set_hash("zone", s2);
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
|
|
|
|
{
|
|
|
|
VALUE m = f_match(pat, s1);
|
|
|
|
|
|
|
|
if (NIL_P(m))
|
|
|
|
return 0;
|
|
|
|
parse_time2_cb(m, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_time(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"("
|
|
|
|
"(?:"
|
|
|
|
"\\d+\\s*:\\s*\\d+"
|
|
|
|
"(?:"
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
"\\s*:\\s*\\d+(?:[,.]\\d*)?"
|
2012-10-28 05:53:08 -04:00
|
|
|
#else
|
|
|
|
"\\s*:\\s*\\d+(?:[,.]\\d+)?"
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
")?"
|
|
|
|
"|"
|
|
|
|
"\\d+\\s*h(?:\\s*\\d+m?(?:\\s*\\d+s?)?)?"
|
|
|
|
")"
|
|
|
|
"(?:"
|
|
|
|
"\\s*"
|
|
|
|
"[ap](?:m\\b|\\.m\\.)"
|
|
|
|
")?"
|
|
|
|
"|"
|
|
|
|
"\\d+\\s*[ap](?:m\\b|\\.m\\.)"
|
|
|
|
")"
|
|
|
|
"(?:"
|
|
|
|
"\\s*"
|
|
|
|
"("
|
|
|
|
"(?:gmt|utc?)?[-+]\\d+(?:[,.:]\\d+(?::\\d+)?)?"
|
|
|
|
"|"
|
2013-12-08 21:14:35 -05:00
|
|
|
"(?-i:[[:alpha:].\\s]+)(?:standard|daylight)\\stime\\b"
|
2011-04-23 08:31:15 -04:00
|
|
|
"|"
|
2013-12-08 21:14:35 -05:00
|
|
|
"(?-i:[[:alpha:]]+)(?:\\sdst)?\\b"
|
2011-04-23 08:31:15 -04:00
|
|
|
")"
|
|
|
|
")?";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
SUBS(str, pat, parse_time_cb);
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
2012-10-28 05:53:08 -04:00
|
|
|
SUBT(str, pat, parse_time_cb);
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
2012-11-10 04:21:41 -05:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
static int
|
|
|
|
parse_era1_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_era1(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"(a(?:d|\\.d\\.))";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBA(str, pat, parse_era1_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_era2_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE b;
|
|
|
|
|
|
|
|
b = rb_reg_nth_match(1, m);
|
|
|
|
if (*RSTRING_PTR(b) == 'B' ||
|
|
|
|
*RSTRING_PTR(b) == 'b')
|
|
|
|
set_hash("_bc", Qtrue);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_era2(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"(c(?:e|\\.e\\.)|b(?:ce|\\.c\\.e\\.)|b(?:c|\\.c\\.))";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBB(str, pat, parse_era2_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_era(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
if (parse_era1(str, hash)) /* pre */
|
|
|
|
goto ok;
|
|
|
|
if (parse_era2(str, hash)) /* post */
|
|
|
|
goto ok;
|
|
|
|
return 0;
|
|
|
|
ok:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-11 04:45:44 -05:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
static int
|
|
|
|
check_year_width(VALUE y)
|
|
|
|
{
|
2018-09-01 02:19:05 -04:00
|
|
|
const char *s;
|
|
|
|
long l;
|
2012-11-11 04:45:44 -05:00
|
|
|
|
2018-09-01 02:19:05 -04:00
|
|
|
l = RSTRING_LEN(y);
|
|
|
|
if (l < 2) return 0;
|
2012-11-11 04:45:44 -05:00
|
|
|
s = RSTRING_PTR(y);
|
2018-09-01 02:19:05 -04:00
|
|
|
if (!isdigit(s[1])) return 0;
|
|
|
|
return (l == 2 || !isdigit(s[2]));
|
2012-11-11 04:45:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_apost(VALUE a, VALUE b, VALUE c)
|
|
|
|
{
|
|
|
|
int f = 0;
|
|
|
|
|
|
|
|
if (!NIL_P(a) && *RSTRING_PTR(a) == '\'') {
|
|
|
|
if (!check_year_width(a))
|
|
|
|
return 0;
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
if (!NIL_P(b) && *RSTRING_PTR(b) == '\'') {
|
|
|
|
if (!check_year_width(b))
|
|
|
|
return 0;
|
|
|
|
if (!NIL_P(c))
|
|
|
|
return 0;
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
if (!NIL_P(c) && *RSTRING_PTR(c) == '\'') {
|
|
|
|
if (!check_year_width(c))
|
|
|
|
return 0;
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
if (f > 1)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static int
|
|
|
|
parse_eu_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
VALUE y, mon, d, b;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
d = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
b = rb_reg_nth_match(3, m);
|
|
|
|
y = rb_reg_nth_match(4, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, !NIL_P(b) &&
|
|
|
|
(*RSTRING_PTR(b) == 'B' ||
|
|
|
|
*RSTRING_PTR(b) == 'b'));
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
|
|
|
d = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
|
|
|
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(d, mon, y))
|
|
|
|
return 0;
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_eu(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
|
|
|
#ifndef TIGHT_PARSER
|
2012-11-03 01:59:08 -04:00
|
|
|
"('?\\d+)[^-\\d\\s]*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
2012-11-03 01:47:32 -04:00
|
|
|
"(\\d+)(?:(?:st|nd|rd|th)\\b)?"
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
"\\s*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
"(" ABBR_MONTHS ")[^-\\d\\s']*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
2012-10-28 05:53:08 -04:00
|
|
|
"(" VALID_MONTHS ")"
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
"(?:"
|
|
|
|
"\\s*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
"(c(?:e|\\.e\\.)|b(?:ce|\\.c\\.e\\.)|a(?:d|\\.d\\.)|b(?:c|\\.c\\.))?"
|
|
|
|
"\\s*"
|
|
|
|
"('?-?\\d+(?:(?:st|nd|rd|th)\\b)?)"
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
2012-11-10 04:21:41 -05:00
|
|
|
"(?:" FPA ")?"
|
|
|
|
"\\s*"
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-']?\\d+)"
|
2012-11-10 04:21:41 -05:00
|
|
|
"\\s*"
|
|
|
|
"(?:" FPA "|" FPB ")?"
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
|
|
|
")?"
|
|
|
|
#ifdef TIGHT_PARSER
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_eu_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_us_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
VALUE y, mon, d, b;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
mon = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
2012-10-21 05:41:25 -04:00
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
b = rb_reg_nth_match(3, m);
|
|
|
|
y = rb_reg_nth_match(4, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, !NIL_P(b) &&
|
|
|
|
(*RSTRING_PTR(b) == 'B' ||
|
|
|
|
*RSTRING_PTR(b) == 'b'));
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
|
|
|
mon = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
|
|
|
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(mon, d, y))
|
|
|
|
return 0;
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_us(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
"\\b(" ABBR_MONTHS ")[^-\\d\\s']*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
2012-10-28 05:53:08 -04:00
|
|
|
"\\b(" VALID_MONTHS ")"
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
"\\s*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
"('?\\d+)[^-\\d\\s']*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
2012-11-11 04:45:44 -05:00
|
|
|
"('?\\d+)(?:(?:st|nd|rd|th)\\b)?"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
"(?:"
|
2012-10-21 05:41:25 -04:00
|
|
|
"\\s*,?"
|
2011-04-23 08:31:15 -04:00
|
|
|
"\\s*"
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
"(c(?:e|\\.e\\.)|b(?:ce|\\.c\\.e\\.)|a(?:d|\\.d\\.)|b(?:c|\\.c\\.))?"
|
|
|
|
"\\s*"
|
|
|
|
"('?-?\\d+)"
|
2012-11-10 04:21:41 -05:00
|
|
|
#else
|
|
|
|
"(?:" FPA ")?"
|
|
|
|
"\\s*"
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-']?\\d+)"
|
2012-11-10 04:21:41 -05:00
|
|
|
"\\s*"
|
|
|
|
"(?:" FPA "|" FPB ")?"
|
|
|
|
#endif
|
2012-10-21 05:41:25 -04:00
|
|
|
")?"
|
|
|
|
#ifdef TIGHT_PARSER
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_us_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
y = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
d = rb_reg_nth_match(3, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(y, mon, d))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"('?[-+]?\\d+)-(\\d+)-('?-?\\d+)"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-+']?\\d+)-(\\d+)-([-']?\\d+)"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
SUBS(str, pat, parse_iso_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso21_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, w, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
y = rb_reg_nth_match(1, m);
|
|
|
|
w = rb_reg_nth_match(2, m);
|
|
|
|
d = rb_reg_nth_match(3, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
if (!NIL_P(y))
|
|
|
|
set_hash("cwyear", str2num(y));
|
|
|
|
set_hash("cweek", str2num(w));
|
|
|
|
if (!NIL_P(d))
|
|
|
|
set_hash("cwday", str2num(d));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso21(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"\\b(\\d{2}|\\d{4})?-?w(\\d{2})(?:-?(\\d))?\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"(\\d{2}|\\d{4})?-?w(\\d{2})(?:-?(\\d))?"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_iso21_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso22_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
d = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
set_hash("cwday", str2num(d));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso22(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"-w-(\\d)\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"-w-(\\d)"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_iso22_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso23_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE mon, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
mon = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
if (!NIL_P(mon))
|
|
|
|
set_hash("mon", str2num(mon));
|
|
|
|
set_hash("mday", str2num(d));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso23(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"--(\\d{2})?-(\\d{2})\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"--(\\d{2})?-(\\d{2})"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
SUBS(str, pat, parse_iso23_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso24_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE mon, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
mon = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
set_hash("mon", str2num(mon));
|
|
|
|
if (!NIL_P(d))
|
|
|
|
set_hash("mday", str2num(d));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso24(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"--(\\d{2})(\\d{2})?\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"--(\\d{2})(\\d{2})?"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
SUBS(str, pat, parse_iso24_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso25_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
y = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
set_hash("year", str2num(y));
|
|
|
|
set_hash("yday", str2num(d));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso25(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat0_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"[,.](\\d{2}|\\d{4})-\\d{3}\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"[,.](\\d{2}|\\d{4})-\\d{3}"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat0 = Qnil;
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"\\b(\\d{2}|\\d{4})-(\\d{3})\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"(\\d{2}|\\d{4})-(\\d{3})"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat0);
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
|
|
|
|
if (!NIL_P(f_match(pat0, str)))
|
|
|
|
return 0;
|
|
|
|
SUBS(str, pat, parse_iso25_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso26_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
d = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
set_hash("yday", str2num(d));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
static int
|
|
|
|
parse_iso26(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat0_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"\\d-\\d{3}\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"\\d-\\d{3}"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat0 = Qnil;
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"\\b-(\\d{3})\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"-(\\d{3})"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat0);
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
|
|
|
|
if (!NIL_P(f_match(pat0, str)))
|
|
|
|
return 0;
|
|
|
|
SUBS(str, pat, parse_iso26_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_iso2(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
if (parse_iso21(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_iso22(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_iso23(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_iso24(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_iso25(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_iso26(str, hash))
|
|
|
|
goto ok;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-08 21:27:36 -04:00
|
|
|
#define JISX0301_ERA_INITIALS "mtshr"
|
2019-04-02 00:36:34 -04:00
|
|
|
#define JISX0301_DEFAULT_ERA 'H' /* obsolete */
|
2019-04-01 01:21:36 -04:00
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
static int
|
|
|
|
gengo(int c)
|
|
|
|
{
|
|
|
|
int e;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'M': case 'm': e = 1867; break;
|
|
|
|
case 'T': case 't': e = 1911; break;
|
|
|
|
case 'S': case 's': e = 1925; break;
|
|
|
|
case 'H': case 'h': e = 1988; break;
|
2019-04-08 21:27:36 -04:00
|
|
|
case 'R': case 'r': e = 2018; break;
|
2011-05-21 08:25:03 -04:00
|
|
|
default: e = 0; break;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static int
|
|
|
|
parse_jis_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE e, y, mon, d;
|
|
|
|
int ep;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
e = rb_reg_nth_match(1, m);
|
|
|
|
y = rb_reg_nth_match(2, m);
|
|
|
|
mon = rb_reg_nth_match(3, m);
|
|
|
|
d = rb_reg_nth_match(4, m);
|
|
|
|
|
|
|
|
ep = gengo(*RSTRING_PTR(e));
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
set_hash("year", f_add(str2num(y), INT2FIX(ep)));
|
|
|
|
set_hash("mon", str2num(mon));
|
|
|
|
set_hash("mday", str2num(d));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_jis(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
2019-04-02 00:36:33 -04:00
|
|
|
"\\b([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)"
|
2012-10-21 05:41:25 -04:00
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2019-04-02 00:36:33 -04:00
|
|
|
"([" JISX0301_ERA_INITIALS "])(\\d+)\\.(\\d+)\\.(\\d+)"
|
2012-10-28 05:53:08 -04:00
|
|
|
TEE_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_jis_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_vms11_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
d = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(d, mon, y))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_vms11(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"('?-?\\d+)-(" ABBR_MONTHS ")[^-/.]*"
|
|
|
|
"-('?-?\\d+)"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-']?\\d+)-(" DOTLESS_VALID_MONTHS ")"
|
|
|
|
"-([-']?\\d+)"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_vms11_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_vms12_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
mon = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(mon, d, y))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_vms12(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"\\b(" ABBR_MONTHS ")[^-/.]*"
|
|
|
|
"-('?-?\\d+)(?:-('?-?\\d+))?"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
|
|
|
"(" DOTLESS_VALID_MONTHS ")"
|
2012-11-11 04:45:44 -05:00
|
|
|
"-([-']?\\d+)(?:-([-']?\\d+))?"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_vms12_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_vms(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
if (parse_vms11(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_vms12(str, hash))
|
|
|
|
goto ok;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_sla_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
y = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
d = rb_reg_nth_match(3, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(y, mon, d))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_sla(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"('?-?\\d+)/\\s*('?\\d+)(?:\\D\\s*('?-?\\d+))?"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-']?\\d+)/\\s*('?\\d+)(?:(?:[-/]|\\s+)\\s*([-']?\\d+))?"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_sla_cb);
|
|
|
|
}
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
static int
|
|
|
|
parse_sla2_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
|
|
|
d = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
|
|
|
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(d, mon, y))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_sla2(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-']?\\d+)/\\s*(" DOTLESS_VALID_MONTHS ")(?:(?:[-/]|\\s+)\\s*([-']?\\d+))?"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
;
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_sla2_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_sla3_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
|
|
|
mon = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
|
|
|
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(mon, d, y))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_sla3(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"(" DOTLESS_VALID_MONTHS ")/\\s*([-']?\\d+)(?:(?:[-/]|\\s+)\\s*([-']?\\d+))?"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
;
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_sla3_cb);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static int
|
|
|
|
parse_dot_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
y = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
d = rb_reg_nth_match(3, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(y, mon, d))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_dot(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"('?-?\\d+)\\.\\s*('?\\d+)\\.\\s*('?-?\\d+)"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-']?\\d+)\\.\\s*(\\d+)\\.\\s*([-']?\\d+)"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_dot_cb);
|
|
|
|
}
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
static int
|
|
|
|
parse_dot2_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
|
|
|
d = rb_reg_nth_match(1, m);
|
|
|
|
mon = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
|
|
|
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(d, mon, y))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_dot2(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"([-']?\\d+)\\.\\s*(" DOTLESS_VALID_MONTHS ")(?:(?:[./])\\s*([-']?\\d+))?"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
;
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_dot2_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_dot3_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y, mon, d;
|
|
|
|
|
|
|
|
mon = rb_reg_nth_match(1, m);
|
|
|
|
d = rb_reg_nth_match(2, m);
|
|
|
|
y = rb_reg_nth_match(3, m);
|
|
|
|
|
2012-11-11 04:45:44 -05:00
|
|
|
if (!check_apost(mon, d, y))
|
2012-10-28 05:53:08 -04:00
|
|
|
return 0;
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
mon = INT2FIX(mon_num(mon));
|
|
|
|
|
|
|
|
s3e(hash, y, mon, d, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_dot3(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-11-11 04:45:44 -05:00
|
|
|
"(" DOTLESS_VALID_MONTHS ")\\.\\s*([-']?\\d+)(?:(?:[./])\\s*([-']?\\d+))?"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
;
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_dot3_cb);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
static int
|
|
|
|
parse_year_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE y;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
y = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
set_hash("year", str2num(y));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_year(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"'(\\d+)\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"'(\\d+)"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
SUBS(str, pat, parse_year_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_mon_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE mon;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
mon = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
set_hash("mon", INT2FIX(mon_num(mon)));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_mon(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"\\b(" ABBR_MONTHS ")\\S*"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
|
|
|
"(" VALID_MONTHS ")"
|
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_mon_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_mday_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE d;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
d = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
set_hash("mday", str2num(d));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_mday(VALUE str, VALUE hash)
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"(\\d+)(st|nd|rd|th)\\b"
|
|
|
|
#else
|
|
|
|
BOS
|
2012-10-28 05:53:08 -04:00
|
|
|
FPW_COM FPT_COM
|
2012-10-21 05:41:25 -04:00
|
|
|
"(\\d+)(st|nd|rd|th)"
|
2012-10-28 05:53:08 -04:00
|
|
|
COM_FPT COM_FPW
|
2012-10-21 05:41:25 -04:00
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_mday_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-04-23 13:59:38 -04:00
|
|
|
n2i(const char *s, long f, long w)
|
2011-04-23 08:31:15 -04:00
|
|
|
{
|
2011-04-23 13:59:38 -04:00
|
|
|
long e, i;
|
|
|
|
int v;
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
e = f + w;
|
|
|
|
v = 0;
|
|
|
|
for (i = f; i < e; i++) {
|
|
|
|
v *= 10;
|
|
|
|
v += s[i] - '0';
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_ddd_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s1, s2, s3, s4, s5;
|
|
|
|
const char *cs2, *cs3, *cs5;
|
2011-04-23 13:51:42 -04:00
|
|
|
long l2, l3, l4, l5;
|
2011-04-23 08:31:15 -04:00
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
s1 = rb_reg_nth_match(1, m);
|
|
|
|
s2 = rb_reg_nth_match(2, m);
|
|
|
|
s3 = rb_reg_nth_match(3, m);
|
|
|
|
s4 = rb_reg_nth_match(4, m);
|
|
|
|
s5 = rb_reg_nth_match(5, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
cs2 = RSTRING_PTR(s2);
|
2011-04-23 13:51:42 -04:00
|
|
|
l2 = RSTRING_LEN(s2);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
switch (l2) {
|
|
|
|
case 2:
|
|
|
|
if (NIL_P(s3) && !NIL_P(s4))
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, l2-2, 2)));
|
|
|
|
else
|
|
|
|
set_hash("mday", INT2FIX(n2i(cs2, 0, 2)));
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
if (NIL_P(s3) && !NIL_P(s4)) {
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, l2-2, 2)));
|
|
|
|
set_hash("min", INT2FIX(n2i(cs2, l2-4, 2)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set_hash("mon", INT2FIX(n2i(cs2, 0, 2)));
|
|
|
|
set_hash("mday", INT2FIX(n2i(cs2, 2, 2)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
if (NIL_P(s3) && !NIL_P(s4)) {
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, l2-2, 2)));
|
|
|
|
set_hash("min", INT2FIX(n2i(cs2, l2-4, 2)));
|
|
|
|
set_hash("hour", INT2FIX(n2i(cs2, l2-6, 2)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int y = n2i(cs2, 0, 2);
|
|
|
|
if (!NIL_P(s1) && *RSTRING_PTR(s1) == '-')
|
|
|
|
y = -y;
|
|
|
|
set_hash("year", INT2FIX(y));
|
|
|
|
set_hash("mon", INT2FIX(n2i(cs2, 2, 2)));
|
|
|
|
set_hash("mday", INT2FIX(n2i(cs2, 4, 2)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
case 10:
|
|
|
|
case 12:
|
|
|
|
case 14:
|
|
|
|
if (NIL_P(s3) && !NIL_P(s4)) {
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, l2-2, 2)));
|
|
|
|
set_hash("min", INT2FIX(n2i(cs2, l2-4, 2)));
|
|
|
|
set_hash("hour", INT2FIX(n2i(cs2, l2-6, 2)));
|
|
|
|
set_hash("mday", INT2FIX(n2i(cs2, l2-8, 2)));
|
|
|
|
if (l2 >= 10)
|
|
|
|
set_hash("mon", INT2FIX(n2i(cs2, l2-10, 2)));
|
|
|
|
if (l2 == 12) {
|
|
|
|
int y = n2i(cs2, l2-12, 2);
|
|
|
|
if (!NIL_P(s1) && *RSTRING_PTR(s1) == '-')
|
|
|
|
y = -y;
|
|
|
|
set_hash("year", INT2FIX(y));
|
|
|
|
}
|
|
|
|
if (l2 == 14) {
|
|
|
|
int y = n2i(cs2, l2-14, 4);
|
|
|
|
if (!NIL_P(s1) && *RSTRING_PTR(s1) == '-')
|
|
|
|
y = -y;
|
|
|
|
set_hash("year", INT2FIX(y));
|
|
|
|
set_hash("_comp", Qfalse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int y = n2i(cs2, 0, 4);
|
|
|
|
if (!NIL_P(s1) && *RSTRING_PTR(s1) == '-')
|
|
|
|
y = -y;
|
|
|
|
set_hash("year", INT2FIX(y));
|
|
|
|
set_hash("mon", INT2FIX(n2i(cs2, 4, 2)));
|
|
|
|
set_hash("mday", INT2FIX(n2i(cs2, 6, 2)));
|
|
|
|
if (l2 >= 10)
|
|
|
|
set_hash("hour", INT2FIX(n2i(cs2, 8, 2)));
|
|
|
|
if (l2 >= 12)
|
|
|
|
set_hash("min", INT2FIX(n2i(cs2, 10, 2)));
|
|
|
|
if (l2 >= 14)
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, 12, 2)));
|
|
|
|
set_hash("_comp", Qfalse);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (NIL_P(s3) && !NIL_P(s4)) {
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, l2-2, 2)));
|
|
|
|
set_hash("min", INT2FIX(n2i(cs2, l2-3, 1)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
set_hash("yday", INT2FIX(n2i(cs2, 0, 3)));
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (NIL_P(s3) && !NIL_P(s4)) {
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, l2-2, 2)));
|
|
|
|
set_hash("min", INT2FIX(n2i(cs2, l2-4, 2)));
|
|
|
|
set_hash("hour", INT2FIX(n2i(cs2, l2-5, 1)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int y = n2i(cs2, 0, 2);
|
|
|
|
if (!NIL_P(s1) && *RSTRING_PTR(s1) == '-')
|
|
|
|
y = -y;
|
|
|
|
set_hash("year", INT2FIX(y));
|
|
|
|
set_hash("yday", INT2FIX(n2i(cs2, 2, 3)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
if (NIL_P(s3) && !NIL_P(s4)) {
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs2, l2-2, 2)));
|
|
|
|
set_hash("min", INT2FIX(n2i(cs2, l2-4, 2)));
|
|
|
|
set_hash("hour", INT2FIX(n2i(cs2, l2-6, 2)));
|
|
|
|
set_hash("mday", INT2FIX(n2i(cs2, l2-7, 1)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int y = n2i(cs2, 0, 4);
|
|
|
|
if (!NIL_P(s1) && *RSTRING_PTR(s1) == '-')
|
|
|
|
y = -y;
|
|
|
|
set_hash("year", INT2FIX(y));
|
|
|
|
set_hash("yday", INT2FIX(n2i(cs2, 4, 3)));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-08-12 20:40:06 -04:00
|
|
|
RB_GC_GUARD(s2);
|
2011-04-23 08:31:15 -04:00
|
|
|
if (!NIL_P(s3)) {
|
|
|
|
cs3 = RSTRING_PTR(s3);
|
2011-04-23 13:51:42 -04:00
|
|
|
l3 = RSTRING_LEN(s3);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
if (!NIL_P(s4)) {
|
|
|
|
switch (l3) {
|
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs3, l3-2, 2)));
|
|
|
|
if (l3 >= 4)
|
|
|
|
set_hash("min", INT2FIX(n2i(cs3, l3-4, 2)));
|
|
|
|
if (l3 >= 6)
|
|
|
|
set_hash("hour", INT2FIX(n2i(cs3, l3-6, 2)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (l3) {
|
|
|
|
case 2:
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
set_hash("hour", INT2FIX(n2i(cs3, 0, 2)));
|
|
|
|
if (l3 >= 4)
|
|
|
|
set_hash("min", INT2FIX(n2i(cs3, 2, 2)));
|
|
|
|
if (l3 >= 6)
|
|
|
|
set_hash("sec", INT2FIX(n2i(cs3, 4, 2)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-09 10:00:49 -04:00
|
|
|
RB_GC_GUARD(s3);
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
if (!NIL_P(s4)) {
|
2011-04-23 13:51:42 -04:00
|
|
|
l4 = RSTRING_LEN(s4);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
set_hash("sec_fraction",
|
|
|
|
rb_rational_new2(str2num(s4),
|
|
|
|
f_expt(INT2FIX(10), LONG2NUM(l4))));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s5)) {
|
|
|
|
cs5 = RSTRING_PTR(s5);
|
2011-04-23 13:51:42 -04:00
|
|
|
l5 = RSTRING_LEN(s5);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
set_hash("zone", s5);
|
|
|
|
|
|
|
|
if (*cs5 == '[') {
|
2019-04-03 06:56:37 -04:00
|
|
|
const char *s1, *s2;
|
2011-04-23 08:31:15 -04:00
|
|
|
VALUE zone;
|
|
|
|
|
2019-04-03 06:56:36 -04:00
|
|
|
l5 -= 2;
|
2019-04-03 06:56:37 -04:00
|
|
|
s1 = cs5 + 1;
|
|
|
|
s2 = memchr(s1, ':', l5);
|
2011-04-23 08:31:15 -04:00
|
|
|
if (s2) {
|
|
|
|
s2++;
|
2019-04-03 06:56:36 -04:00
|
|
|
zone = rb_str_subseq(s5, s2 - cs5, l5 - (s2 - s1));
|
|
|
|
s5 = rb_str_subseq(s5, 1, s2 - s1);
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
2019-04-03 06:56:37 -04:00
|
|
|
else {
|
|
|
|
zone = rb_str_subseq(s5, 1, l5);
|
2019-04-03 06:56:36 -04:00
|
|
|
if (isdigit((unsigned char)*s1))
|
|
|
|
s5 = rb_str_append(rb_str_new_cstr("+"), zone);
|
|
|
|
else
|
|
|
|
s5 = zone;
|
|
|
|
}
|
2011-04-23 08:31:15 -04:00
|
|
|
set_hash("zone", zone);
|
2019-04-03 06:56:37 -04:00
|
|
|
set_hash("offset", date_zone_to_diff(s5));
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
2011-08-09 10:00:49 -04:00
|
|
|
RB_GC_GUARD(s5);
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_ddd(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
BOS
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
"([-+]?)(\\d{2,14})"
|
|
|
|
"(?:"
|
|
|
|
"\\s*"
|
|
|
|
"t?"
|
|
|
|
"\\s*"
|
|
|
|
"(\\d{2,6})?(?:[,.](\\d*))?"
|
|
|
|
")?"
|
|
|
|
"(?:"
|
|
|
|
"\\s*"
|
|
|
|
"("
|
|
|
|
"z\\b"
|
|
|
|
"|"
|
|
|
|
"[-+]\\d{1,4}\\b"
|
|
|
|
"|"
|
|
|
|
"\\[[-+]?\\d[^\\]]*\\]"
|
|
|
|
")"
|
2012-10-21 05:41:25 -04:00
|
|
|
")?"
|
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
EOS
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_ddd_cb);
|
|
|
|
}
|
|
|
|
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
static int
|
|
|
|
parse_bc_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
2012-11-10 04:21:41 -05:00
|
|
|
set_hash("_bc", Qtrue);
|
2011-04-23 08:31:15 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_bc(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\b(bc\\b|bce\\b|b\\.c\\.|b\\.c\\.e\\.)";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_bc_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_frag_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s, n;
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
s = rb_reg_nth_match(1, m);
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
if (!NIL_P(ref_hash("hour")) && NIL_P(ref_hash("mday"))) {
|
|
|
|
n = str2num(s);
|
|
|
|
if (f_ge_p(n, INT2FIX(1)) &&
|
|
|
|
f_le_p(n, INT2FIX(31)))
|
|
|
|
set_hash("mday", n);
|
|
|
|
}
|
|
|
|
if (!NIL_P(ref_hash("mday")) && NIL_P(ref_hash("hour"))) {
|
|
|
|
n = str2num(s);
|
|
|
|
if (f_ge_p(n, INT2FIX(0)) &&
|
|
|
|
f_le_p(n, INT2FIX(24)))
|
|
|
|
set_hash("hour", n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_frag(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] = "\\A\\s*(\\d{1,2})\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
|
|
|
SUBS(str, pat, parse_frag_cb);
|
|
|
|
}
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
static int
|
2012-10-28 05:53:08 -04:00
|
|
|
parse_dummy_cb(VALUE m, VALUE hash)
|
2012-10-21 05:41:25 -04:00
|
|
|
{
|
2012-10-28 05:53:08 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_wday_only(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] = "\\A\\s*" FPW "\\s*\\z";
|
2012-10-21 05:41:25 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
2012-10-28 05:53:08 -04:00
|
|
|
SUBS(str, pat, parse_dummy_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_time_only(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] = "\\A\\s*" FPT "\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
SUBS(str, pat, parse_dummy_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_wday_and_time(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] = "\\A\\s*(" FPW "\\s+" FPT "|" FPT "\\s+" FPW ")\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
|
|
|
SUBS(str, pat, parse_dummy_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
have_invalid_char_p(VALUE s)
|
|
|
|
{
|
|
|
|
long i;
|
|
|
|
|
|
|
|
for (i = 0; i < RSTRING_LEN(s); i++)
|
|
|
|
if (iscntrl((unsigned char)RSTRING_PTR(s)[i]) &&
|
|
|
|
!isspace((unsigned char)RSTRING_PTR(s)[i]))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
2012-10-21 05:41:25 -04:00
|
|
|
}
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
|
|
|
|
#define HAVE_ALPHA (1<<0)
|
|
|
|
#define HAVE_DIGIT (1<<1)
|
|
|
|
#define HAVE_DASH (1<<2)
|
|
|
|
#define HAVE_DOT (1<<3)
|
|
|
|
#define HAVE_SLASH (1<<4)
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
check_class(VALUE s)
|
|
|
|
{
|
|
|
|
unsigned flags;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
for (i = 0; i < RSTRING_LEN(s); i++) {
|
2012-03-02 22:14:27 -05:00
|
|
|
if (isalpha((unsigned char)RSTRING_PTR(s)[i]))
|
2011-04-23 08:31:15 -04:00
|
|
|
flags |= HAVE_ALPHA;
|
2012-03-02 22:14:27 -05:00
|
|
|
if (isdigit((unsigned char)RSTRING_PTR(s)[i]))
|
2011-04-23 08:31:15 -04:00
|
|
|
flags |= HAVE_DIGIT;
|
|
|
|
if (RSTRING_PTR(s)[i] == '-')
|
|
|
|
flags |= HAVE_DASH;
|
|
|
|
if (RSTRING_PTR(s)[i] == '.')
|
|
|
|
flags |= HAVE_DOT;
|
|
|
|
if (RSTRING_PTR(s)[i] == '/')
|
|
|
|
flags |= HAVE_SLASH;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HAVE_ELEM_P(x) ((check_class(str) & (x)) == (x))
|
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
#define PARSER_ERROR return rb_hash_new()
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
VALUE
|
|
|
|
date__parse(VALUE str, VALUE comp)
|
|
|
|
{
|
|
|
|
VALUE backref, hash;
|
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
if (have_invalid_char_p(str))
|
|
|
|
PARSER_ERROR;
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
backref = rb_backref_get();
|
|
|
|
rb_match_busy(backref);
|
|
|
|
|
|
|
|
{
|
2012-10-21 05:41:25 -04:00
|
|
|
static const char pat_source[] =
|
|
|
|
#ifndef TIGHT_PARSER
|
|
|
|
"[^-+',./:@[:alnum:]\\[\\]]+"
|
|
|
|
#else
|
|
|
|
"[^[:graph:]]+"
|
|
|
|
#endif
|
|
|
|
;
|
2011-04-23 08:31:15 -04:00
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_0(pat);
|
2011-06-26 19:01:56 -04:00
|
|
|
str = rb_str_dup(str);
|
2011-05-08 05:41:38 -04:00
|
|
|
f_gsub_bang(str, pat, asp_string());
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
|
|
|
set_hash("_comp", comp);
|
|
|
|
|
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA))
|
|
|
|
parse_day(str, hash);
|
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT))
|
|
|
|
parse_time(str, hash);
|
|
|
|
|
2012-11-10 04:21:41 -05:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA))
|
|
|
|
parse_era(str, hash);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA|HAVE_DIGIT)) {
|
2011-04-23 08:31:15 -04:00
|
|
|
if (parse_eu(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_us(str, hash))
|
|
|
|
goto ok;
|
2012-11-10 04:21:41 -05:00
|
|
|
}
|
2011-04-23 08:31:15 -04:00
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT|HAVE_DASH))
|
|
|
|
if (parse_iso(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT|HAVE_DOT))
|
|
|
|
if (parse_jis(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA|HAVE_DIGIT|HAVE_DASH))
|
|
|
|
if (parse_vms(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT|HAVE_SLASH))
|
|
|
|
if (parse_sla(str, hash))
|
|
|
|
goto ok;
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA|HAVE_DIGIT|HAVE_SLASH)) {
|
|
|
|
if (parse_sla2(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_sla3(str, hash))
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT|HAVE_DOT))
|
|
|
|
if (parse_dot(str, hash))
|
|
|
|
goto ok;
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA|HAVE_DIGIT|HAVE_DOT)) {
|
|
|
|
if (parse_dot2(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_dot3(str, hash))
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-23 08:31:15 -04:00
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT))
|
|
|
|
if (parse_iso2(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT))
|
|
|
|
if (parse_year(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA))
|
|
|
|
if (parse_mon(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT))
|
|
|
|
if (parse_mday(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT))
|
|
|
|
if (parse_ddd(str, hash))
|
|
|
|
goto ok;
|
|
|
|
|
2012-10-28 05:53:08 -04:00
|
|
|
#ifdef TIGHT_PARSER
|
|
|
|
if (parse_wday_only(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_time_only(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (parse_wday_and_time(str, hash))
|
|
|
|
goto ok;
|
|
|
|
|
|
|
|
PARSER_ERROR; /* not found */
|
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
ok:
|
2012-10-21 05:41:25 -04:00
|
|
|
#ifndef TIGHT_PARSER
|
2011-04-23 08:31:15 -04:00
|
|
|
if (HAVE_ELEM_P(HAVE_ALPHA))
|
|
|
|
parse_bc(str, hash);
|
|
|
|
if (HAVE_ELEM_P(HAVE_DIGIT))
|
|
|
|
parse_frag(str, hash);
|
2012-10-21 05:41:25 -04:00
|
|
|
#endif
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
{
|
2019-04-01 23:49:00 -04:00
|
|
|
if (RTEST(del_hash("_bc"))) {
|
2012-11-10 04:21:41 -05:00
|
|
|
VALUE y;
|
|
|
|
|
|
|
|
y = ref_hash("cwyear");
|
|
|
|
if (!NIL_P(y)) {
|
|
|
|
y = f_add(f_negate(y), INT2FIX(1));
|
|
|
|
set_hash("cwyear", y);
|
|
|
|
}
|
|
|
|
y = ref_hash("year");
|
|
|
|
if (!NIL_P(y)) {
|
|
|
|
y = f_add(f_negate(y), INT2FIX(1));
|
|
|
|
set_hash("year", y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 23:49:00 -04:00
|
|
|
if (RTEST(del_hash("_comp"))) {
|
2011-04-23 08:31:15 -04:00
|
|
|
VALUE y;
|
|
|
|
|
|
|
|
y = ref_hash("cwyear");
|
|
|
|
if (!NIL_P(y))
|
|
|
|
if (f_ge_p(y, INT2FIX(0)) && f_le_p(y, INT2FIX(99))) {
|
|
|
|
if (f_ge_p(y, INT2FIX(69)))
|
|
|
|
set_hash("cwyear", f_add(y, INT2FIX(1900)));
|
|
|
|
else
|
|
|
|
set_hash("cwyear", f_add(y, INT2FIX(2000)));
|
|
|
|
}
|
|
|
|
y = ref_hash("year");
|
|
|
|
if (!NIL_P(y))
|
|
|
|
if (f_ge_p(y, INT2FIX(0)) && f_le_p(y, INT2FIX(99))) {
|
|
|
|
if (f_ge_p(y, INT2FIX(69)))
|
|
|
|
set_hash("year", f_add(y, INT2FIX(1900)));
|
|
|
|
else
|
|
|
|
set_hash("year", f_add(y, INT2FIX(2000)));
|
|
|
|
}
|
|
|
|
}
|
2012-11-10 04:21:41 -05:00
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
VALUE zone = ref_hash("zone");
|
|
|
|
if (!NIL_P(zone) && NIL_P(ref_hash("offset")))
|
2011-04-26 12:23:05 -04:00
|
|
|
set_hash("offset", date_zone_to_diff(zone));
|
2011-04-23 08:31:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rb_backref_set(backref);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2011-05-21 08:25:03 -04:00
|
|
|
static VALUE
|
|
|
|
comp_year69(VALUE y)
|
|
|
|
{
|
|
|
|
if (f_ge_p(y, INT2FIX(69)))
|
|
|
|
return f_add(y, INT2FIX(1900));
|
|
|
|
return f_add(y, INT2FIX(2000));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
comp_year50(VALUE y)
|
|
|
|
{
|
|
|
|
if (f_ge_p(y, INT2FIX(50)))
|
|
|
|
return f_add(y, INT2FIX(1900));
|
|
|
|
return f_add(y, INT2FIX(2000));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
sec_fraction(VALUE f)
|
|
|
|
{
|
|
|
|
return rb_rational_new2(str2num(f),
|
|
|
|
f_expt(INT2FIX(10),
|
|
|
|
LONG2NUM(RSTRING_LEN(f))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define SNUM 14
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601_ext_datetime_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1], y;
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(s[3])) {
|
|
|
|
set_hash("mday", str2num(s[3]));
|
|
|
|
if (strcmp(RSTRING_PTR(s[1]), "-") != 0) {
|
|
|
|
y = str2num(s[1]);
|
|
|
|
if (RSTRING_LEN(s[1]) < 4)
|
|
|
|
y = comp_year69(y);
|
|
|
|
set_hash("year", y);
|
|
|
|
}
|
|
|
|
if (NIL_P(s[2])) {
|
|
|
|
if (strcmp(RSTRING_PTR(s[1]), "-") != 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
set_hash("mon", str2num(s[2]));
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[5])) {
|
|
|
|
set_hash("yday", str2num(s[5]));
|
|
|
|
if (!NIL_P(s[4])) {
|
|
|
|
y = str2num(s[4]);
|
|
|
|
if (RSTRING_LEN(s[4]) < 4)
|
|
|
|
y = comp_year69(y);
|
|
|
|
set_hash("year", y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[8])) {
|
|
|
|
set_hash("cweek", str2num(s[7]));
|
|
|
|
set_hash("cwday", str2num(s[8]));
|
|
|
|
if (!NIL_P(s[6])) {
|
|
|
|
y = str2num(s[6]);
|
|
|
|
if (RSTRING_LEN(s[6]) < 4)
|
|
|
|
y = comp_year69(y);
|
|
|
|
set_hash("cwyear", y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[9])) {
|
|
|
|
set_hash("cwday", str2num(s[9]));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s[10])) {
|
|
|
|
set_hash("hour", str2num(s[10]));
|
|
|
|
set_hash("min", str2num(s[11]));
|
|
|
|
if (!NIL_P(s[12]))
|
|
|
|
set_hash("sec", str2num(s[12]));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s[13])) {
|
|
|
|
set_hash("sec_fraction", sec_fraction(s[13]));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s[14])) {
|
|
|
|
set_hash("zone", s[14]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[14]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601_ext_datetime(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(?:([-+]?\\d{2,}|-)-(\\d{2})?-(\\d{2})|"
|
|
|
|
"([-+]?\\d{2,})?-(\\d{3})|"
|
|
|
|
"(\\d{4}|\\d{2})?-w(\\d{2})-(\\d)|"
|
|
|
|
"-w-(\\d))"
|
|
|
|
"(?:t"
|
|
|
|
"(\\d{2}):(\\d{2})(?::(\\d{2})(?:[,.](\\d+))?)?"
|
|
|
|
"(z|[-+]\\d{2}(?::?\\d{2})?)?)?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, iso8601_ext_datetime_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 17
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601_bas_datetime_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1], y;
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(s[3])) {
|
|
|
|
set_hash("mday", str2num(s[3]));
|
|
|
|
if (strcmp(RSTRING_PTR(s[1]), "--") != 0) {
|
|
|
|
y = str2num(s[1]);
|
|
|
|
if (RSTRING_LEN(s[1]) < 4)
|
|
|
|
y = comp_year69(y);
|
|
|
|
set_hash("year", y);
|
|
|
|
}
|
|
|
|
if (*RSTRING_PTR(s[2]) == '-') {
|
|
|
|
if (strcmp(RSTRING_PTR(s[1]), "--") != 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
set_hash("mon", str2num(s[2]));
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[5])) {
|
|
|
|
set_hash("yday", str2num(s[5]));
|
|
|
|
y = str2num(s[4]);
|
|
|
|
if (RSTRING_LEN(s[4]) < 4)
|
|
|
|
y = comp_year69(y);
|
|
|
|
set_hash("year", y);
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[6])) {
|
|
|
|
set_hash("yday", str2num(s[6]));
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[9])) {
|
|
|
|
set_hash("cweek", str2num(s[8]));
|
|
|
|
set_hash("cwday", str2num(s[9]));
|
|
|
|
y = str2num(s[7]);
|
|
|
|
if (RSTRING_LEN(s[7]) < 4)
|
|
|
|
y = comp_year69(y);
|
|
|
|
set_hash("cwyear", y);
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[11])) {
|
|
|
|
set_hash("cweek", str2num(s[10]));
|
|
|
|
set_hash("cwday", str2num(s[11]));
|
|
|
|
}
|
|
|
|
else if (!NIL_P(s[12])) {
|
|
|
|
set_hash("cwday", str2num(s[12]));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s[13])) {
|
|
|
|
set_hash("hour", str2num(s[13]));
|
|
|
|
set_hash("min", str2num(s[14]));
|
|
|
|
if (!NIL_P(s[15]))
|
|
|
|
set_hash("sec", str2num(s[15]));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s[16])) {
|
|
|
|
set_hash("sec_fraction", sec_fraction(s[16]));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s[17])) {
|
|
|
|
set_hash("zone", s[17]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[17]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601_bas_datetime(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(?:([-+]?(?:\\d{4}|\\d{2})|--)(\\d{2}|-)(\\d{2})|"
|
|
|
|
"([-+]?(?:\\d{4}|\\d{2}))(\\d{3})|"
|
|
|
|
"-(\\d{3})|"
|
|
|
|
"(\\d{4}|\\d{2})w(\\d{2})(\\d)|"
|
|
|
|
"-w(\\d{2})(\\d)|"
|
|
|
|
"-w-(\\d))"
|
|
|
|
"(?:t?"
|
|
|
|
"(\\d{2})(\\d{2})(?:(\\d{2})(?:[,.](\\d+))?)?"
|
|
|
|
"(z|[-+]\\d{2}(?:\\d{2})?)?)?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, iso8601_bas_datetime_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 5
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601_ext_time_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("hour", str2num(s[1]));
|
|
|
|
set_hash("min", str2num(s[2]));
|
|
|
|
if (!NIL_P(s[3]))
|
|
|
|
set_hash("sec", str2num(s[3]));
|
|
|
|
if (!NIL_P(s[4]))
|
|
|
|
set_hash("sec_fraction", sec_fraction(s[4]));
|
|
|
|
if (!NIL_P(s[5])) {
|
|
|
|
set_hash("zone", s[5]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[5]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define iso8601_bas_time_cb iso8601_ext_time_cb
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601_ext_time(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-28 06:16:02 -04:00
|
|
|
"\\A\\s*(\\d{2}):(\\d{2})(?::(\\d{2})(?:[,.](\\d+))?"
|
2011-05-21 08:25:03 -04:00
|
|
|
"(z|[-+]\\d{2}(:?\\d{2})?)?)?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, iso8601_ext_time_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
iso8601_bas_time(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2012-10-28 06:16:02 -04:00
|
|
|
"\\A\\s*(\\d{2})(\\d{2})(?:(\\d{2})(?:[,.](\\d+))?"
|
2011-05-21 08:25:03 -04:00
|
|
|
"(z|[-+]\\d{2}(\\d{2})?)?)?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, iso8601_bas_time_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
date__iso8601(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE backref, hash;
|
|
|
|
|
|
|
|
backref = rb_backref_get();
|
|
|
|
rb_match_busy(backref);
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
|
|
|
|
|
|
|
if (iso8601_ext_datetime(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (iso8601_bas_datetime(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (iso8601_ext_time(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (iso8601_bas_time(str, hash))
|
|
|
|
goto ok;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
rb_backref_set(backref);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 8
|
|
|
|
|
|
|
|
static int
|
|
|
|
rfc3339_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("year", str2num(s[1]));
|
|
|
|
set_hash("mon", str2num(s[2]));
|
|
|
|
set_hash("mday", str2num(s[3]));
|
|
|
|
set_hash("hour", str2num(s[4]));
|
|
|
|
set_hash("min", str2num(s[5]));
|
|
|
|
set_hash("sec", str2num(s[6]));
|
|
|
|
set_hash("zone", s[8]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[8]));
|
|
|
|
if (!NIL_P(s[7]))
|
|
|
|
set_hash("sec_fraction", sec_fraction(s[7]));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rfc3339(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(-?\\d{4})-(\\d{2})-(\\d{2})"
|
|
|
|
"(?:t|\\s)"
|
|
|
|
"(\\d{2}):(\\d{2}):(\\d{2})(?:\\.(\\d+))?"
|
|
|
|
"(z|[-+]\\d{2}:\\d{2})\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, rfc3339_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
date__rfc3339(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE backref, hash;
|
|
|
|
|
|
|
|
backref = rb_backref_get();
|
|
|
|
rb_match_busy(backref);
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
|
|
|
rfc3339(str, hash);
|
|
|
|
rb_backref_set(backref);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 8
|
|
|
|
|
|
|
|
static int
|
|
|
|
xmlschema_datetime_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("year", str2num(s[1]));
|
|
|
|
if (!NIL_P(s[2]))
|
|
|
|
set_hash("mon", str2num(s[2]));
|
|
|
|
if (!NIL_P(s[3]))
|
|
|
|
set_hash("mday", str2num(s[3]));
|
|
|
|
if (!NIL_P(s[4]))
|
|
|
|
set_hash("hour", str2num(s[4]));
|
|
|
|
if (!NIL_P(s[5]))
|
|
|
|
set_hash("min", str2num(s[5]));
|
|
|
|
if (!NIL_P(s[6]))
|
|
|
|
set_hash("sec", str2num(s[6]));
|
|
|
|
if (!NIL_P(s[7]))
|
|
|
|
set_hash("sec_fraction", sec_fraction(s[7]));
|
|
|
|
if (!NIL_P(s[8])) {
|
|
|
|
set_hash("zone", s[8]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[8]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xmlschema_datetime(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(-?\\d{4,})(?:-(\\d{2})(?:-(\\d{2}))?)?"
|
|
|
|
"(?:t"
|
|
|
|
"(\\d{2}):(\\d{2}):(\\d{2})(?:\\.(\\d+))?)?"
|
|
|
|
"(z|[-+]\\d{2}:\\d{2})?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, xmlschema_datetime_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 5
|
|
|
|
|
|
|
|
static int
|
|
|
|
xmlschema_time_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("hour", str2num(s[1]));
|
|
|
|
set_hash("min", str2num(s[2]));
|
|
|
|
if (!NIL_P(s[3]))
|
|
|
|
set_hash("sec", str2num(s[3]));
|
|
|
|
if (!NIL_P(s[4]))
|
|
|
|
set_hash("sec_fraction", sec_fraction(s[4]));
|
|
|
|
if (!NIL_P(s[5])) {
|
|
|
|
set_hash("zone", s[5]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[5]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xmlschema_time(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(\\d{2}):(\\d{2}):(\\d{2})(?:\\.(\\d+))?"
|
|
|
|
"(z|[-+]\\d{2}:\\d{2})?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, xmlschema_time_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 4
|
|
|
|
|
|
|
|
static int
|
|
|
|
xmlschema_trunc_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NIL_P(s[1]))
|
|
|
|
set_hash("mon", str2num(s[1]));
|
|
|
|
if (!NIL_P(s[2]))
|
|
|
|
set_hash("mday", str2num(s[2]));
|
|
|
|
if (!NIL_P(s[3]))
|
|
|
|
set_hash("mday", str2num(s[3]));
|
|
|
|
if (!NIL_P(s[4])) {
|
|
|
|
set_hash("zone", s[4]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[4]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xmlschema_trunc(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(?:--(\\d{2})(?:-(\\d{2}))?|---(\\d{2}))"
|
|
|
|
"(z|[-+]\\d{2}:\\d{2})?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, xmlschema_trunc_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
date__xmlschema(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE backref, hash;
|
|
|
|
|
|
|
|
backref = rb_backref_get();
|
|
|
|
rb_match_busy(backref);
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
|
|
|
|
|
|
|
if (xmlschema_datetime(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (xmlschema_time(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (xmlschema_trunc(str, hash))
|
|
|
|
goto ok;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
rb_backref_set(backref);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 8
|
|
|
|
|
|
|
|
static int
|
|
|
|
rfc2822_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1], y;
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
2013-07-21 14:16:00 -04:00
|
|
|
if (!NIL_P(s[1])) {
|
|
|
|
set_hash("wday", INT2FIX(day_num(s[1])));
|
|
|
|
}
|
2011-05-21 08:25:03 -04:00
|
|
|
set_hash("mday", str2num(s[2]));
|
|
|
|
set_hash("mon", INT2FIX(mon_num(s[3])));
|
|
|
|
y = str2num(s[4]);
|
|
|
|
if (RSTRING_LEN(s[4]) < 4)
|
|
|
|
y = comp_year50(y);
|
|
|
|
set_hash("year", y);
|
|
|
|
set_hash("hour", str2num(s[5]));
|
|
|
|
set_hash("min", str2num(s[6]));
|
|
|
|
if (!NIL_P(s[7]))
|
|
|
|
set_hash("sec", str2num(s[7]));
|
|
|
|
set_hash("zone", s[8]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[8]));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rfc2822(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(?:(" ABBR_DAYS ")\\s*,\\s+)?"
|
|
|
|
"(\\d{1,2})\\s+"
|
|
|
|
"(" ABBR_MONTHS ")\\s+"
|
|
|
|
"(-?\\d{2,})\\s+"
|
|
|
|
"(\\d{2}):(\\d{2})(?::(\\d{2}))?\\s*"
|
|
|
|
"([-+]\\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, rfc2822_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
date__rfc2822(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE backref, hash;
|
|
|
|
|
|
|
|
backref = rb_backref_get();
|
|
|
|
rb_match_busy(backref);
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
|
|
|
rfc2822(str, hash);
|
|
|
|
rb_backref_set(backref);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 8
|
|
|
|
|
|
|
|
static int
|
|
|
|
httpdate_type1_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("wday", INT2FIX(day_num(s[1])));
|
|
|
|
set_hash("mday", str2num(s[2]));
|
|
|
|
set_hash("mon", INT2FIX(mon_num(s[3])));
|
|
|
|
set_hash("year", str2num(s[4]));
|
|
|
|
set_hash("hour", str2num(s[5]));
|
|
|
|
set_hash("min", str2num(s[6]));
|
|
|
|
set_hash("sec", str2num(s[7]));
|
|
|
|
set_hash("zone", s[8]);
|
|
|
|
set_hash("offset", INT2FIX(0));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
httpdate_type1(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(" ABBR_DAYS ")\\s*,\\s+"
|
|
|
|
"(\\d{2})\\s+"
|
|
|
|
"(" ABBR_MONTHS ")\\s+"
|
|
|
|
"(-?\\d{4})\\s+"
|
|
|
|
"(\\d{2}):(\\d{2}):(\\d{2})\\s+"
|
|
|
|
"(gmt)\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, httpdate_type1_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 8
|
|
|
|
|
|
|
|
static int
|
|
|
|
httpdate_type2_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1], y;
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("wday", INT2FIX(day_num(s[1])));
|
|
|
|
set_hash("mday", str2num(s[2]));
|
|
|
|
set_hash("mon", INT2FIX(mon_num(s[3])));
|
|
|
|
y = str2num(s[4]);
|
|
|
|
if (f_ge_p(y, INT2FIX(0)) && f_le_p(y, INT2FIX(99)))
|
|
|
|
y = comp_year69(y);
|
|
|
|
set_hash("year", y);
|
|
|
|
set_hash("hour", str2num(s[5]));
|
|
|
|
set_hash("min", str2num(s[6]));
|
|
|
|
set_hash("sec", str2num(s[7]));
|
|
|
|
set_hash("zone", s[8]);
|
|
|
|
set_hash("offset", INT2FIX(0));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
httpdate_type2(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(" DAYS ")\\s*,\\s+"
|
|
|
|
"(\\d{2})\\s*-\\s*"
|
|
|
|
"(" ABBR_MONTHS ")\\s*-\\s*"
|
|
|
|
"(\\d{2})\\s+"
|
|
|
|
"(\\d{2}):(\\d{2}):(\\d{2})\\s+"
|
|
|
|
"(gmt)\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, httpdate_type2_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 7
|
|
|
|
|
|
|
|
static int
|
|
|
|
httpdate_type3_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_hash("wday", INT2FIX(day_num(s[1])));
|
|
|
|
set_hash("mon", INT2FIX(mon_num(s[2])));
|
|
|
|
set_hash("mday", str2num(s[3]));
|
|
|
|
set_hash("hour", str2num(s[4]));
|
|
|
|
set_hash("min", str2num(s[5]));
|
|
|
|
set_hash("sec", str2num(s[6]));
|
|
|
|
set_hash("year", str2num(s[7]));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
httpdate_type3(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
|
|
|
"\\A\\s*(" ABBR_DAYS ")\\s+"
|
|
|
|
"(" ABBR_MONTHS ")\\s+"
|
|
|
|
"(\\d{1,2})\\s+"
|
|
|
|
"(\\d{2}):(\\d{2}):(\\d{2})\\s+"
|
|
|
|
"(\\d{4})\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, httpdate_type3_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
date__httpdate(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE backref, hash;
|
|
|
|
|
|
|
|
backref = rb_backref_get();
|
|
|
|
rb_match_busy(backref);
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
|
|
|
|
|
|
|
if (httpdate_type1(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (httpdate_type2(str, hash))
|
|
|
|
goto ok;
|
|
|
|
if (httpdate_type3(str, hash))
|
|
|
|
goto ok;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
rb_backref_set(backref);
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef SNUM
|
|
|
|
#define SNUM 9
|
|
|
|
|
|
|
|
static int
|
|
|
|
jisx0301_cb(VALUE m, VALUE hash)
|
|
|
|
{
|
|
|
|
VALUE s[SNUM + 1];
|
|
|
|
int ep;
|
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
s[0] = Qnil;
|
|
|
|
for (i = 1; i <= SNUM; i++)
|
|
|
|
s[i] = rb_reg_nth_match(i, m);
|
|
|
|
}
|
|
|
|
|
2019-04-02 00:36:34 -04:00
|
|
|
ep = gengo(NIL_P(s[1]) ? JISX0301_DEFAULT_ERA : *RSTRING_PTR(s[1]));
|
2011-05-21 08:25:03 -04:00
|
|
|
set_hash("year", f_add(str2num(s[2]), INT2FIX(ep)));
|
|
|
|
set_hash("mon", str2num(s[3]));
|
|
|
|
set_hash("mday", str2num(s[4]));
|
|
|
|
if (!NIL_P(s[5])) {
|
|
|
|
set_hash("hour", str2num(s[5]));
|
|
|
|
if (!NIL_P(s[6]))
|
|
|
|
set_hash("min", str2num(s[6]));
|
|
|
|
if (!NIL_P(s[7]))
|
|
|
|
set_hash("sec", str2num(s[7]));
|
|
|
|
}
|
|
|
|
if (!NIL_P(s[8]))
|
|
|
|
set_hash("sec_fraction", sec_fraction(s[8]));
|
|
|
|
if (!NIL_P(s[9])) {
|
|
|
|
set_hash("zone", s[9]);
|
|
|
|
set_hash("offset", date_zone_to_diff(s[9]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jisx0301(VALUE str, VALUE hash)
|
|
|
|
{
|
|
|
|
static const char pat_source[] =
|
2019-04-02 00:36:33 -04:00
|
|
|
"\\A\\s*([" JISX0301_ERA_INITIALS "])?(\\d{2})\\.(\\d{2})\\.(\\d{2})"
|
2011-05-21 08:25:03 -04:00
|
|
|
"(?:t"
|
|
|
|
"(?:(\\d{2}):(\\d{2})(?::(\\d{2})(?:[,.](\\d*))?)?"
|
|
|
|
"(z|[-+]\\d{2}(?::?\\d{2})?)?)?)?\\s*\\z";
|
|
|
|
static VALUE pat = Qnil;
|
|
|
|
|
|
|
|
REGCOMP_I(pat);
|
2012-01-18 17:13:10 -05:00
|
|
|
MATCH(str, pat, jisx0301_cb);
|
2011-05-21 08:25:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
date__jisx0301(VALUE str)
|
|
|
|
{
|
|
|
|
VALUE backref, hash;
|
|
|
|
|
|
|
|
backref = rb_backref_get();
|
|
|
|
rb_match_busy(backref);
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
|
|
|
if (jisx0301(str, hash))
|
|
|
|
goto ok;
|
|
|
|
hash = date__iso8601(str);
|
|
|
|
|
|
|
|
ok:
|
|
|
|
rb_backref_set(backref);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2011-04-23 08:31:15 -04:00
|
|
|
/*
|
|
|
|
Local variables:
|
|
|
|
c-file-style: "ruby"
|
|
|
|
End:
|
|
|
|
*/
|