Method: DateTime.parse

Defined in:
ext/date/date_core.c

.parse(string = '-4712-01-01T00:00:00+00:00'[, comp=true[, start=Date::ITALY]], limit: 128) ⇒ Object

Parses the given representation of date and time, and creates a DateTime object.

This method *does not* function as a validator. If the input string does not match valid formats strictly, you may get a cryptic result. Should consider to use ‘DateTime.strptime` instead of this method as possible.

If the optional second argument is true and the detected year is in the range “00” to “99”, makes it full.

DateTime.parse('2001-02-03T04:05:06+07:00')

#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>

DateTime.parse('20010203T040506+0700')

#=> #<DateTime: 2001-02-03T04:05:06+07:00 …>

DateTime.parse('3rd Feb 2001 04:05:06 PM')

#=> #<DateTime: 2001-02-03T16:05:06+00:00 …>

Raise an ArgumentError when the string length is longer than limit. You can stop this check by passing ‘limit: nil`, but note that it may take a long time to parse.



8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
# File 'ext/date/date_core.c', line 8181

static VALUE
datetime_s_parse(int argc, VALUE *argv, VALUE klass)
{
    VALUE str, comp, sg, opt;

    rb_scan_args(argc, argv, "03:", &str, &comp, &sg, &opt);
    if (!NIL_P(opt)) argc--;

    switch (argc) {
      case 0:
  str = rb_str_new2("-4712-01-01T00:00:00+00:00");
      case 1:
  comp = Qtrue;
      case 2:
  sg = INT2FIX(DEFAULT_SG);
    }

    {
        int argc2 = 2;
        VALUE argv2[3];
        argv2[0] = str;
        argv2[1] = comp;
        argv2[2] = opt;
        if (!NIL_P(opt)) argc2++;
  VALUE hash = date_s__parse(argc2, argv2, klass);
  return dt_new_by_frags(klass, hash, sg);
    }
}