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 (Integer) (defaults to: 1)

    year

  • month (Integer) (defaults to: 1)

    month

  • day (Integer) (defaults to: 1)

    day of month

  • hour (Integer) (defaults to: 0)

    hour

  • min (Integer) (defaults to: 0)

    minute

  • sec (Integer) (defaults to: 0)

    second



115
116
117
118
119
120
121
122
123
124
125
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
# File 'ext/oci8/oradate.c', line 115

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



538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'ext/oci8/oradate.c', line 538

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:



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/oci8/oradate.c', line 190

static VALUE ora_date_s_now(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)


472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'ext/oci8/oradate.c', line 472

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:



521
522
523
524
525
526
# File 'ext/oci8/oradate.c', line 521

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 */
}

#dayInteger

Returns the day of month field of self.

Returns:

  • (Integer)


325
326
327
328
329
330
# File 'ext/oci8/oradate.c', line 325

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 (Integer)

    number between 1 and 31



339
340
341
342
343
344
345
346
347
# File 'ext/oci8/oradate.c', line 339

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

#hourInteger

Returns the hour field of self.

Returns:

  • (Integer)


356
357
358
359
360
361
# File 'ext/oci8/oradate.c', line 356

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 (Integer)

    number between 0 and 23



370
371
372
373
374
375
376
377
378
# File 'ext/oci8/oradate.c', line 370

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

#minuteInteger

Returns the minute field of self.

Returns:

  • (Integer)


387
388
389
390
391
392
# File 'ext/oci8/oradate.c', line 387

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 (Integer)

    number between 0 and 59



401
402
403
404
405
406
407
408
409
# File 'ext/oci8/oradate.c', line 401

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

#monthInteger

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

Returns:

  • (Integer)


293
294
295
296
297
298
# File 'ext/oci8/oradate.c', line 293

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 (Integer)

    number between 1 and 12



308
309
310
311
312
313
314
315
316
# File 'ext/oci8/oradate.c', line 308

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

#secondInteger

Returns the second field of self.

Returns:

  • (Integer)


418
419
420
421
422
423
# File 'ext/oci8/oradate.c', line 418

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 (Integer)

    number between 0 and 59



432
433
434
435
436
437
438
439
440
441
# File 'ext/oci8/oradate.c', line 432

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)


240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/oci8/oradate.c', line 240

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.



658
659
660
# File 'lib/oci8/oci8.rb', line 658

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.



670
671
672
# File 'lib/oci8/oci8.rb', line 670

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:



223
224
225
226
227
228
229
230
231
# File 'ext/oci8/oradate.c', line 223

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.



648
649
650
651
652
653
654
655
# File 'lib/oci8/oci8.rb', line 648

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:



454
455
456
457
458
459
460
461
462
# File 'ext/oci8/oradate.c', line 454

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

#yearInteger

Returns the year field of self.

Returns:

  • (Integer)


261
262
263
264
265
266
# File 'ext/oci8/oradate.c', line 261

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 (Integer)

    number between -4712 and 9999



275
276
277
278
279
280
281
282
283
# File 'ext/oci8/oradate.c', line 275

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