Class: OraDate

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
ext/oci8/oradate.c,
lib/oci8/oci8.rb,
ext/oci8/oradate.c

Overview

OraDate is the ruby class compatible with Oracle DATE data type. The range is between 4712 B.C. and 9999 A.D.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year = 1, month = 1, day = 1, hour = 0, min = 0, sec = 0) ⇒ Object

Returns an OraDate object initialized to the specified date and time.

Examples:

OraDate.new              # => 0001-01-01 00:00:00
OraDate.new(2012)        # => 2012-01-01 00:00:00
OraDate.new(2012, 3, 4)  # => 2012-03-04 00:00:00

Parameters:

  • year (Fixnum) (defaults to: 1)

    year

  • month (Fixnum) (defaults to: 1)

    month

  • day (Fixnum) (defaults to: 1)

    day of month

  • hour (Fixnum) (defaults to: 0)

    hour

  • min (Fixnum) (defaults to: 0)

    minute

  • sec (Fixnum) (defaults to: 0)

    second



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'ext/oci8/oradate.c', line 126

static VALUE ora_date_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE vyear, vmonth, vday, vhour, vmin, vsec;
    ora_date_t *od = check_oradate(self);
    int year, month, day, hour, min, sec;

    rb_scan_args(argc, argv, "06", &vyear, &vmonth, &vday, &vhour, &vmin, &vsec);
    /* set year */
    if (argc > 0) {
        year = NUM2INT(vyear);
        Check_year(year);
    } else {
        year = 1;
    }
    /* set month */
    if (argc > 1) {
        month = NUM2INT(vmonth);
        Check_month(month);
    } else {
        month = 1;
    }
    /* set day */
    if (argc > 2) {
        day = NUM2INT(vday);
        Check_day(day);
    } else {
        day = 1;
    }
    /* set hour */
    if (argc > 3) {
        hour = NUM2INT(vhour);
        Check_hour(hour);
    } else {
        hour = 0;
    }
    /* set minute */
    if (argc > 4) {
        min = NUM2INT(vmin);
        Check_minute(min);
    } else {
        min = 0;
    }
    /* set second */
    if (argc > 5) {
        sec = NUM2INT(vsec);
        Check_second(sec);
    } else {
        sec = 0;
    }

    oci8_set_ora_date(od, year, month, day, hour, min, sec);
    return Qnil;
}

Class Method Details

._load(bytes) ⇒ OraDate

Restores a byte stream serialized by #_dump. This method is called by Marshal.load() to deserialize a byte stream created by Marshal.dump().

Parameters:

  • bytes (String)

    a byte stream

Returns:

  • (OraDate)

    an deserialized object



549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/oci8/oradate.c', line 549

static VALUE ora_date_s_load(VALUE klass, VALUE str)
{
    ora_date_t *od;
    VALUE obj;

    Check_Type(str, T_STRING);
    if (RSTRING_LEN(str) != sizeof(ora_date_t)) {
        rb_raise(rb_eTypeError, "marshaled OraDate format differ");
    }
    obj = TypedData_Make_Struct(cOraDate, ora_date_t, &odate_data_type, od);
    memcpy(od, RSTRING_PTR(str), sizeof(ora_date_t));
    return obj;
}

.nowOraDate

Returns an OraDate object initialized to the current local time.

Returns:



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'ext/oci8/oradate.c', line 201

static VALUE ora_date_s_now(int argc, VALUE *argv, VALUE klass)
{
    VALUE obj = ora_date_s_allocate(klass);
    ora_date_t *od = check_oradate(obj);
    time_t tm = time(0);
    int year, month, day, hour, min, sec;
#ifdef HAVE_LOCALTIME_R
    struct tm t;
    localtime_r(&tm, &t);
#define tp (&t)
#else
    struct tm *tp;
    tp = localtime(&tm);
#endif
    year = tp->tm_year + 1900;
    month = tp->tm_mon + 1;
    day = tp->tm_mday;
    hour = tp->tm_hour;
    min = tp->tm_min;
    sec = tp->tm_sec;

    oci8_set_ora_date(od, year, month, day, hour, min, sec);
    return obj;
}

Instance Method Details

#<=>(other) ⇒ -1, ...

Returns -1, 0, or +1 depending on whether self is less than, equal to, or greater than other.

Returns:

  • (-1, 0, +1)


483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'ext/oci8/oradate.c', line 483

static VALUE ora_date_cmp(VALUE self, VALUE val)
{
    ora_date_t *od1 = check_oradate(self);
    ora_date_t *od2 = check_oradate(val);

    if (od1->century < od2->century) return INT2FIX(-1);
    if (od1->century > od2->century) return INT2FIX(1);
    if (od1->year < od2->year) return INT2FIX(-1);
    if (od1->year > od2->year) return INT2FIX(1);
    if (od1->month < od2->month) return INT2FIX(-1);
    if (od1->month > od2->month) return INT2FIX(1);
    if (od1->day < od2->day) return INT2FIX(-1);
    if (od1->day > od2->day) return INT2FIX(1);
    if (od1->hour < od2->hour) return INT2FIX(-1);
    if (od1->hour > od2->hour) return INT2FIX(1);
    if (od1->minute < od2->minute) return INT2FIX(-1);
    if (od1->minute > od2->minute) return INT2FIX(1);
    if (od1->second < od2->second) return INT2FIX(-1);
    if (od1->second > od2->second) return INT2FIX(1);
    return INT2FIX(0);
}

#_dumpString

Serializes self. This method is called by Marshal.dump().

Returns:

See Also:



532
533
534
535
536
537
# File 'ext/oci8/oradate.c', line 532

static VALUE ora_date_dump(int argc, VALUE *argv, VALUE self)
{
    ora_date_t *od = check_oradate(self);

    return rb_str_new((const char*)od, sizeof(ora_date_t)); /* ASCII-8BIT */
}

#dayFixnum

Returns the day of month field of self.

Returns:

  • (Fixnum)


336
337
338
339
340
341
# File 'ext/oci8/oradate.c', line 336

static VALUE ora_date_day(VALUE self)
{
    ora_date_t *od = check_oradate(self);

    return INT2FIX(Get_day(od));
}

#day=Object

Assigns num to the day of month field of self.

Parameters:

  • num (Fixnum)

    number between 1 and 31



350
351
352
353
354
355
356
357
358
# File 'ext/oci8/oradate.c', line 350

static VALUE ora_date_set_day(VALUE self, VALUE val)
{
    ora_date_t *od = check_oradate(self);
    int v = NUM2INT(val);

    Check_day(v);
    Set_day(od, v);
    return self;
}

#hourFixnum

Returns the hour field of self.

Returns:

  • (Fixnum)


367
368
369
370
371
372
# File 'ext/oci8/oradate.c', line 367

static VALUE ora_date_hour(VALUE self)
{
    ora_date_t *od = check_oradate(self);

    return INT2FIX(Get_hour(od));
}

#hour=Object

Assigns num to the hour field of self.

Parameters:

  • num (Fixnum)

    number between 0 and 23



381
382
383
384
385
386
387
388
389
# File 'ext/oci8/oradate.c', line 381

static VALUE ora_date_set_hour(VALUE self, VALUE val)
{
    ora_date_t *od = check_oradate(self);
    int v = NUM2INT(val);

    Check_hour(v);
    Set_hour(od, v);
    return self;
}

#minuteFixnum

Returns the minute field of self.

Returns:

  • (Fixnum)


398
399
400
401
402
403
# File 'ext/oci8/oradate.c', line 398

static VALUE ora_date_minute(VALUE self)
{
    ora_date_t *od = check_oradate(self);

    return INT2FIX(Get_minute(od));
}

#minute=Object

Assigns num to the minute field of self.

Parameters:

  • num (Fixnum)

    number between 0 and 59



412
413
414
415
416
417
418
419
420
# File 'ext/oci8/oradate.c', line 412

static VALUE ora_date_set_minute(VALUE self, VALUE val)
{
    ora_date_t *od = check_oradate(self);
    int v = NUM2INT(val);

    Check_minute(v);
    Set_minute(od, v);
    return self;
}

#monthFixnum

Returns the month field of self. The month starts with one.

Returns:

  • (Fixnum)


304
305
306
307
308
309
# File 'ext/oci8/oradate.c', line 304

static VALUE ora_date_month(VALUE self)
{
    ora_date_t *od = check_oradate(self);

    return INT2FIX(Get_month(od));
}

#month=Object

Assigns num to the month field of self. The month starts with one.

Parameters:

  • num (Fixnum)

    number between 1 and 12



319
320
321
322
323
324
325
326
327
# File 'ext/oci8/oradate.c', line 319

static VALUE ora_date_set_month(VALUE self, VALUE val)
{
    ora_date_t *od = check_oradate(self);
    int v = NUM2INT(val);

    Check_month(v);
    Set_month(od, v);
    return self;
}

#secondFixnum

Returns the second field of self.

Returns:

  • (Fixnum)


429
430
431
432
433
434
# File 'ext/oci8/oradate.c', line 429

static VALUE ora_date_second(VALUE self)
{
    ora_date_t *od = check_oradate(self);

    return INT2FIX(Get_second(od));
}

#second=Object

Assigns num to the second field of self.

Parameters:

  • num (Fixnum)

    number between 0 and 59



443
444
445
446
447
448
449
450
451
452
# File 'ext/oci8/oradate.c', line 443

static VALUE ora_date_set_second(VALUE self, VALUE val)
{
    ora_date_t *od = check_oradate(self);
    int v;

    v = NUM2INT(val);
    Check_second(v);
    Set_second(od, v);
    return self;
}

#to_aArray

Returns a 6-element array of year, month, day, hour, minute and second.

Returns:

  • (Array)


251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/oci8/oradate.c', line 251

static VALUE ora_date_to_a(VALUE self)
{
    ora_date_t *od = check_oradate(self);
    VALUE ary[6];

    ary[0] = INT2FIX(Get_year(od));
    ary[1] = INT2FIX(Get_month(od));
    ary[2] = INT2FIX(Get_day(od));
    ary[3] = INT2FIX(Get_hour(od));
    ary[4] = INT2FIX(Get_minute(od));
    ary[5] = INT2FIX(Get_second(od));
    return rb_ary_new4(6, ary);
}

#to_dateObject

Returns a Date object which denotes self.



653
654
655
# File 'lib/oci8/oci8.rb', line 653

def to_date
  Date.new(year, month, day)
end

#to_datetimeObject

Returns a DateTime object which denotes self.

Note that this is not daylight saving time aware. The Time zone offset is that of the time the command started.



665
666
667
# File 'lib/oci8/oci8.rb', line 665

def to_datetime
  DateTime.new(year, month, day, hour, minute, second, @@tz_offset)
end

#to_sOraDate

Returns a string representing self. The string format is ‘yyyy/mm/dd hh:mi:ss’.

Returns:



234
235
236
237
238
239
240
241
242
# File 'ext/oci8/oradate.c', line 234

static VALUE ora_date_to_s(VALUE self)
{
    ora_date_t *od = check_oradate(self);
    char buf[30];

    sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d", Get_year(od), Get_month(od),
            Get_day(od), Get_hour(od), Get_minute(od), Get_second(od));
    return rb_usascii_str_new_cstr(buf);
}

#to_timeObject

Returns a Time object which denotes self.



643
644
645
646
647
648
649
650
# File 'lib/oci8/oci8.rb', line 643

def to_time
  begin
    Time.local(year, month, day, hour, minute, second)
  rescue ArgumentError
    msg = format("out of range of Time (expect between 1970-01-01 00:00:00 UTC and 2037-12-31 23:59:59, but %04d-%02d-%02d %02d:%02d:%02d %s)", year, month, day, hour, minute, second, Time.at(0).zone)
    raise RangeError.new(msg)
  end
end

#truncOraDate

Truncates hour, minute and second fields to zero.

Examples:

oradate = OraDate.now  # => 2008/07/17 11:07:30
oradate.trunc          # => 2008/07/17 00:00:00

Returns:



465
466
467
468
469
470
471
472
473
# File 'ext/oci8/oradate.c', line 465

static VALUE ora_date_trunc(VALUE self)
{
    ora_date_t *od = check_oradate(self);

    od->hour = 1;
    od->minute = 1;
    od->second = 1;
    return self;
}

#yearFixnum

Returns the year field of self.

Returns:

  • (Fixnum)


272
273
274
275
276
277
# File 'ext/oci8/oradate.c', line 272

static VALUE ora_date_year(VALUE self)
{
    ora_date_t *od = check_oradate(self);

    return INT2FIX(Get_year(od));
}

#year=Object

Assigns num to the year field of self.

Parameters:

  • num (Fixnum)

    number between -4712 and 9999



286
287
288
289
290
291
292
293
294
# File 'ext/oci8/oradate.c', line 286

static VALUE ora_date_set_year(VALUE self, VALUE val)
{
    ora_date_t *od = check_oradate(self);
    int v = NUM2INT(val);

    Check_year(v);
    Set_year(od, v);
    return self;
}