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:

  • strftime(3) style format string.



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'ext/strptime/strftime.c', line 375

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:

  • string to parse

Returns:

  • the time object given string means



415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'ext/strptime/strftime.c', line 415

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:

  • Unix epoch

Returns:

  • the formatted datetime string



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'ext/strptime/strftime.c', line 438

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.

API:

  • private



394
395
396
397
398
399
400
401
402
403
404
405
# File 'ext/strptime/strftime.c', line 394

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:

  • source format string



464
465
466
467
468
469
470
471
# File 'ext/strptime/strftime.c', line 464

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

    return tobj->fmt;
}