Class: Strftime

Inherits:
Object
  • Object
show all
Defined in:
ext/strptime/strftime.c,
ext/strptime/strftime.c

Overview

Strftime is a faster way to format time string like strftime(3).

Examples:

generator = Strftime.new('%Y-%m-%dT%H:%M:%S%z')
generator.source #=> "%Y-%m-%dT%H:%M:%S%z"
generator.exec(Time.now) #=> 2017-12-25T12:34:56+09:00

Instance Method Summary collapse

Constructor Details

#new(format) ⇒ Object

returns generator object

Parameters:

  • format (String)

    strftime(3) style format string.



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'ext/strptime/strftime.c', line 388

static VALUE
strftime_init(VALUE self, VALUE fmt)
{
    struct strftime_object *tobj;
    void **isns;
    size_t rlen;
    StringValueCStr(fmt);
    TypedData_Get_Struct(self, struct strftime_object, &strftime_data_type,
			 tobj);
    isns = strftime_compile(RSTRING_PTR(fmt), RSTRING_LEN(fmt), &rlen);
    tobj->isns = isns;
    tobj->fmt = rb_str_new_frozen(fmt);
    tobj->result_length = rlen;
    return self;
}

Instance Method Details

#exec(str) ⇒ Time

Return a formatted datetime string

Parameters:

  • str (String)

    string to parse

Returns:

  • (Time)

    the time object given string means



428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'ext/strptime/strftime.c', line 428

static VALUE
strftime_exec(VALUE self, VALUE time)
{
    struct strftime_object *sobj;
    struct timespec ts = rb_time_timespec(time);
#ifdef HAVE_RB_TIME_UTC_OFFSET
    int gmtoff = FIX2INT(rb_time_utc_offset(time));
#else
    int gmtoff = NUM2INT(rb_funcall(time, id_gmtoff, 0));
#endif
    GetStrftimeval(self, sobj);

    return strftime_exec0(sobj->isns, sobj->fmt, &ts, gmtoff, sobj->result_length);
}

#execi(epoch) ⇒ String

Return a formatted datetime string

Parameters:

  • epoch (Integer)

    Unix epoch

Returns:

  • (String)

    the formatted datetime string



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'ext/strptime/strftime.c', line 451

static VALUE
strftime_execi(VALUE self, VALUE epoch)
{
    struct strftime_object *tobj;
    struct timespec ts;
    GetStrftimeval(self, tobj);

    if (RB_INTEGER_TYPE_P(epoch)) {
	ts.tv_sec = NUM2TIMET(epoch);
	ts.tv_nsec = 0;
    } else if (RB_FLOAT_TYPE_P(epoch)) {
	double d = NUM2DBL(epoch);
	ts.tv_sec = (time_t)d;
	ts.tv_nsec = (int)((int64_t)(d * 1000000000) % 1000000000);
    } else if (RB_TYPE_P(epoch, T_RATIONAL)) {
	ts.tv_sec = NUM2TIMET(epoch);
	ts.tv_nsec = NUM2INT(rb_funcall(rb_funcall(epoch, '*', 1, INT2FIX(1000000000)), '%', 1, INT2FIX(1000000000)));
    }

    return strftime_exec0(tobj->isns, tobj->fmt, &ts, 0, tobj->result_length);
}

#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.



407
408
409
410
411
412
413
414
415
416
417
418
# File 'ext/strptime/strftime.c', line 407

static VALUE
strftime_init_copy(VALUE copy, VALUE self)
{
    struct strftime_object *tobj, *tcopy;

    if (!OBJ_INIT_COPY(copy, self)) return copy;
    GetStrftimeval(self, tobj);
    GetNewStrftimeval(copy, tcopy);
    MEMCPY(tcopy, tobj, struct strftime_object, 1);

    return copy;
}

#sourceString

Returns source format string.

Returns:

  • (String)

    source format string



477
478
479
480
481
482
483
484
# File 'ext/strptime/strftime.c', line 477

static VALUE
strftime_source(VALUE self)
{
    struct strftime_object *tobj;
    GetStrftimeval(self, tobj);

    return tobj->fmt;
}