Class: OraDate

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

Overview

ruby class compatible with Oracle DATE data type. Date and time between 4712 B.C. and 9999 A.D.

Constant Summary collapse

@@tz_offset =

get timezone offset.

Time.now.utc_offset.to_r/86400

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



87
88
89
90
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
# File 'ext/oci8/oradate.c', line 87

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(string) ⇒ Object

Unmarshals a dumped OraDate object.



504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'ext/oci8/oradate.c', line 504

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

.nowObject

Returns an OraDate object initialized to the current local time.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/oci8/oradate.c', line 160

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

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

Comparison—Compares oradate1 with oradate2. Other comparison operators are available because Comparable module is included.

Returns:

  • (-1, 0, +1)


445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'ext/oci8/oradate.c', line 445

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

#_dumpString

Dumps oradate for marshaling.

Returns:



491
492
493
494
495
496
# File 'ext/oci8/oradate.c', line 491

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 the month (1..31) for oradate.

Returns:

  • (Fixnum)


295
296
297
298
299
300
301
# File 'ext/oci8/oradate.c', line 295

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=(fixnum) ⇒ Object

Sets the day of the month (1..31) for oradate.



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

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

#hashObject

:nodoc:



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'ext/oci8/oradate.c', line 469

static VALUE ora_date_hash(VALUE self)
{
    ora_date_t *od;
    unsigned int v;

    Data_Get_Struct(self, ora_date_t, od);
    v = (od->century <<  8)
        + (od->year    << 15)
        + (od->month   << 22)
        + (od->day     << 26)
        + (od->hour    << 12)
        + (od->minute  <<  6)
        + (od->second  <<  0);
    return INT2FIX(v);
}

#hourFixnum

Returns the hour of the day (0..23) for oradate.

Returns:

  • (Fixnum)


327
328
329
330
331
332
333
# File 'ext/oci8/oradate.c', line 327

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=(fixnum) ⇒ Object

Sets the hour of the day (0..23) for oradate.



341
342
343
344
345
346
347
348
349
350
351
# File 'ext/oci8/oradate.c', line 341

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

#initialize_copy(rhs) ⇒ Object

:nodoc:



142
143
144
145
146
147
148
149
150
151
# File 'ext/oci8/oradate.c', line 142

static VALUE ora_date_initialize_copy(VALUE lhs, VALUE rhs)
{
    ora_date_t *l, *r;

    rb_call_super(1, &rhs);
    Data_Get_Struct(lhs, ora_date_t, l);
    Data_Get_Struct(rhs, ora_date_t, r);
    memcpy(l, r, sizeof(ora_date_t));
    return lhs;
}

#minuteFixnum

Returns the minute of the hour (0..59) for oradate.

Returns:

  • (Fixnum)


359
360
361
362
363
364
365
# File 'ext/oci8/oradate.c', line 359

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=(fixnum) ⇒ Object

Sets the minute of the hour (0..59) for oradate.



373
374
375
376
377
378
379
380
381
382
383
# File 'ext/oci8/oradate.c', line 373

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 of the year (1..12) for oradate.

Returns:

  • (Fixnum)


263
264
265
266
267
268
269
# File 'ext/oci8/oradate.c', line 263

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=(fixnum) ⇒ Object

Sets the month of the year (1..12) for oradate.



277
278
279
280
281
282
283
284
285
286
287
# File 'ext/oci8/oradate.c', line 277

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 of the minute (0..59) for oradate.

Returns:

  • (Fixnum)


391
392
393
394
395
396
397
# File 'ext/oci8/oradate.c', line 391

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=(fixnum) ⇒ Object

Sets the second of the minute (0..59) for oradate.



405
406
407
408
409
410
411
412
413
414
415
# File 'ext/oci8/oradate.c', line 405

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 values for oradate: month, day, hour, minute, second].

Returns:

  • (Array)


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

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



651
652
653
# File 'lib/oci8/oci8.rb', line 651

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

#to_datetimeObject



660
661
662
# File 'lib/oci8/oci8.rb', line 660

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

#to_json(options = nil) ⇒ Object

:nodoc:



675
676
677
# File 'lib/oci8/oci8.rb', line 675

def to_json(options=nil) # :nodoc:
  to_datetime.to_json(options)
end

#to_sString

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

Returns:



192
193
194
195
196
197
198
199
200
201
# File 'ext/oci8/oradate.c', line 192

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



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

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

#to_yaml(opts = {}) ⇒ Object

:nodoc:



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

def to_yaml(opts = {}) # :nodoc:
  YAML.quick_emit(object_id, opts) do |out|
    out.scalar(taguri, self.to_s, :plain)
  end
end

#truncObject

Truncates hour, minute and second to zero for oradate.

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


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

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

#yaml_initialize(type, val) ⇒ Object

:nodoc:



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

def yaml_initialize(type, val) # :nodoc:
  initialize(*val.split(/[ -\/:]+/).collect do |i| i.to_i end)
end

#yearFixnum

Returns the year (-4712..9999) for oradate.

Returns:

  • (Fixnum)


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

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=(fixnum) ⇒ Object

Sets the year (-4712..9999) for oradate.



245
246
247
248
249
250
251
252
253
254
255
# File 'ext/oci8/oradate.c', line 245

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