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


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
# File 'ext/oci8/oradate.c', line 91

static VALUE ora_date_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE vyear, vmonth, vday, vhour, vmin, vsec;
    ora_date_t *od = DATA_PTR(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



521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'ext/oci8/oradate.c', line 521

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 = Data_Make_Struct(cOraDate, ora_date_t, NULL, xfree, od);
    memcpy(od, RSTRING_PTR(str), sizeof(ora_date_t));
    return obj;
}

.now(*args) ⇒ OraDate

Returns an OraDate object initialized to the current local time.

Returns:



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'ext/oci8/oradate.c', line 165

static VALUE ora_date_s_now(int argc, VALUE *argv, VALUE klass)
{
    VALUE obj = ora_date_s_allocate(klass);
    ora_date_t *od = DATA_PTR(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) ⇒ Object

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



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'ext/oci8/oradate.c', line 454

static VALUE ora_date_cmp(VALUE self, VALUE val)
{
    ora_date_t *od1, *od2;
    Data_Get_Struct(self, ora_date_t, od1);
    Check_Object(val, cOraDate);
    Data_Get_Struct(val, ora_date_t, od2);
    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);
}

#_dump(*args) ⇒ String

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

Returns:

See Also:



503
504
505
506
507
508
# File 'ext/oci8/oradate.c', line 503

static VALUE ora_date_dump(int argc, VALUE *argv, VALUE self)
{
    ora_date_t *od;
    Data_Get_Struct(self, ora_date_t, od);
    return rb_str_new((const char*)od, sizeof(ora_date_t)); /* ASCII-8BIT */
}

#dayFixnum

Returns the day of month field of self.

Returns:

  • (Fixnum)


300
301
302
303
304
305
306
# File 'ext/oci8/oradate.c', line 300

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

    Data_Get_Struct(self, ora_date_t, od);
    return INT2FIX(Get_day(od));
}

#day=(num) ⇒ Object

Assigns num to the day of month field of self.

Parameters:

  • number (Fixnum)

    between 1 and 31



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

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

    v = NUM2INT(val);
    Check_day(v);
    Data_Get_Struct(self, ora_date_t, od);
    Set_day(od, v);
    return self;
}

#hourFixnum

Returns the hour field of self.

Returns:

  • (Fixnum)


333
334
335
336
337
338
339
# File 'ext/oci8/oradate.c', line 333

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

    Data_Get_Struct(self, ora_date_t, od);
    return INT2FIX(Get_hour(od));
}

#hour=(num) ⇒ Object

Assigns num to the hour field of self.

Parameters:

  • number (Fixnum)

    between 0 and 23



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

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

    v = NUM2INT(val);
    Check_hour(v);
    Data_Get_Struct(self, ora_date_t, od);
    Set_hour(od, v);
    return self;
}

#minuteFixnum

Returns the minute field of self.

Returns:

  • (Fixnum)


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

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

    Data_Get_Struct(self, ora_date_t, od);
    return INT2FIX(Get_minute(od));
}

#minute=(num) ⇒ Object

Assigns num to the minute field of self.

Parameters:

  • number (Fixnum)

    between 0 and 59



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

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

    v = NUM2INT(val);
    Check_minute(v);
    Data_Get_Struct(self, ora_date_t, od);
    Set_minute(od, v);
    return self;
}

#monthFixnum

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

Returns:

  • (Fixnum)


266
267
268
269
270
271
272
# File 'ext/oci8/oradate.c', line 266

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

    Data_Get_Struct(self, ora_date_t, od);
    return INT2FIX(Get_month(od));
}

#month=(num) ⇒ Object

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

Parameters:

  • number (Fixnum)

    between 1 and 12



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

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

    v = NUM2INT(val);
    Check_month(v);
    Data_Get_Struct(self, ora_date_t, od);
    Set_month(od, v);
    return self;
}

#secondFixnum

Returns the second field of self.

Returns:

  • (Fixnum)


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

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

    Data_Get_Struct(self, ora_date_t, od);
    return INT2FIX(Get_second(od));
}

#second=(num) ⇒ Object

Assigns num to the second field of self.

Parameters:

  • number (Fixnum)

    between 0 and 59



415
416
417
418
419
420
421
422
423
424
425
# File 'ext/oci8/oradate.c', line 415

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

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

#to_aArray

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

Returns:

  • (Array)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/oci8/oradate.c', line 212

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

    Data_Get_Struct(self, ora_date_t, od);
    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.



459
460
461
# File 'lib/oci8/oci8.rb', line 459

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.



473
474
475
# File 'lib/oci8/oci8.rb', line 473

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:



196
197
198
199
200
201
202
203
204
205
# File 'ext/oci8/oradate.c', line 196

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

    Data_Get_Struct(self, ora_date_t, od);
    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.



449
450
451
452
453
454
455
456
# File 'lib/oci8/oci8.rb', line 449

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:



436
437
438
439
440
441
442
443
444
445
# File 'ext/oci8/oradate.c', line 436

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

    Data_Get_Struct(self, ora_date_t, od);
    od->hour = 1;
    od->minute = 1;
    od->second = 1;
    return self;
}

#yearFixnum

Returns the year field of self.

Returns:

  • (Fixnum)


232
233
234
235
236
237
238
# File 'ext/oci8/oradate.c', line 232

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

    Data_Get_Struct(self, ora_date_t, od);
    return INT2FIX(Get_year(od));
}

#year=(num) ⇒ Object

Assigns num to the year field of self.

Parameters:

  • number (Fixnum)

    between -4712 and 9999



248
249
250
251
252
253
254
255
256
257
258
# File 'ext/oci8/oradate.c', line 248

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

    v = NUM2INT(val);
    Check_year(v);
    Data_Get_Struct(self, ora_date_t, od);
    Set_year(od, v);
    return self;
}