class Strptime

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

@example

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

Constants

VERSION

Public Class Methods

new(p1) click to toggle source

@overload new(format)

@param format [String] strptime(3) style format string.

returns parser object

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;
}

Public Instance Methods

exec(p1) click to toggle source

@overload exec(str)

@param str [String] string to parse

@return [Time] the time object given string means

Parse given string, and return Time object

static VALUE
strptime_exec(VALUE self, VALUE str)
{
    struct strptime_object *tobj;
    int r, gmtoff = INT_MAX;
    struct timespec ts;
    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 rb_time_timespec_new(&ts, gmtoff);
}
execi(p1) click to toggle source

@overload execi(str)

@param str [String] string to parse

@return [Integer] the Unix epoch given string means

Parse given string, and return epoch as integer

static VALUE
strptime_execi(VALUE self, VALUE str)
{
    struct strptime_object *tobj;
    struct timespec ts;
    int r, gmtoff = INT_MAX;
    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(p1) click to toggle source

@api private For Ruby VM internal.

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;
}
source() click to toggle source

@overload source @return [String] source format string

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

    return tobj->fmt;
}