Class: Strptime

Inherits:
Object
  • Object
show all
Defined in:
ext/strptime/strptime.c,
lib/strptime.rb,
lib/strptime/version.rb,
ext/strptime/strptime.c

Overview

Strptime is a faster way to parse time strings like strptime(3).

Examples:

parser = Strptime.new('%Y-%m-%dT%H:%M:%S%z')
parser.source #=> "%Y-%m-%dT%H:%M:%S%z"
parser.exec('2015-12-25T12:34:56+09') #=> 2015-12-25 12:34:56 +09:00
parser.execi('2015-12-25T12:34:56+09') #=> 1451014496

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Constructor Details

#new(format) ⇒ Object

returns parser object

Parameters:

  • format (String)

    strptime(3) style format string.



621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'ext/strptime/strptime.c', line 621

static VALUE
strptime_init(VALUE self, VALUE fmt)
{
    struct strptime_object *tobj;
    void **isns;
    StringValueCStr(fmt);
    TypedData_Get_Struct(self, struct strptime_object, &strptime_data_type,
			 tobj);
    isns = strptime_compile(RSTRING_PTR(fmt), RSTRING_LEN(fmt));
    tobj->isns = isns;
    tobj->fmt = rb_str_new_frozen(fmt);
    return self;
}

Instance Method Details

#exec(str) ⇒ Time

Parse given string, and return Time object

Parameters:

  • str (String)

    string to parse

Returns:

  • (Time)

    the time object given string means



685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'ext/strptime/strptime.c', line 685

static VALUE
strptime_exec(VALUE self, VALUE str)
{
    struct strptime_object *tobj;
    int r, gmtoff = 0;
    StringValue(str);
    GetStrptimeval(self, tobj);
    struct timespec ts;

    r = strptime_exec0(tobj->isns, RSTRING_PTR(tobj->fmt), RSTRING_PTR(str),
		       RSTRING_LEN(str), &ts, &gmtoff);
    if (r) rb_raise(rb_eArgError, "string doesn't match");
    return rbtime_timespec_new(&ts, gmtoff);
}

#execi(str) ⇒ Integer

Parse given string, and return epoch as integer

Parameters:

  • str (String)

    string to parse

Returns:

  • (Integer)

    the Unix epoch given string means



707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'ext/strptime/strptime.c', line 707

static VALUE
strptime_execi(VALUE self, VALUE str)
{
    struct strptime_object *tobj;
    struct timespec ts;
    int r, gmtoff = 0;
    StringValue(str);
    GetStrptimeval(self, tobj);

    r = strptime_exec0(tobj->isns, RSTRING_PTR(tobj->fmt), RSTRING_PTR(str),
		       RSTRING_LEN(str), &ts, &gmtoff);
    if (r) rb_raise(rb_eArgError, "string doesn't match");
    return TIMET2NUM(ts.tv_sec);
}

#initialize_copy(self) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

For Ruby VM internal.



638
639
640
641
642
643
644
645
646
647
648
649
# File 'ext/strptime/strptime.c', line 638

static VALUE
strptime_init_copy(VALUE copy, VALUE self)
{
    struct strptime_object *tobj, *tcopy;

    if (!OBJ_INIT_COPY(copy, self)) return copy;
    GetStrptimeval(self, tobj);
    GetNewStrptimeval(copy, tcopy);
    MEMCPY(tcopy, tobj, struct strptime_object, 1);

    return copy;
}

#sourceString

Returns source format string.

Returns:

  • (String)

    source format string



726
727
728
729
730
731
732
733
# File 'ext/strptime/strptime.c', line 726

static VALUE
strptime_source(VALUE self)
{
    struct strptime_object *tobj;
    GetStrptimeval(self, tobj);

    return tobj->fmt;
}