Class: OraDate

Inherits:
Object
  • 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.



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
140
141
142
# File 'ext/oci8/oradate.c', line 90

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.



507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'ext/oci8/oradate.c', line 507

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.



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

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)


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

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:



494
495
496
497
498
499
# File 'ext/oci8/oradate.c', line 494

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)


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

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.



312
313
314
315
316
317
318
319
320
321
322
# File 'ext/oci8/oradate.c', line 312

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:



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

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)


330
331
332
333
334
335
336
# File 'ext/oci8/oradate.c', line 330

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.



344
345
346
347
348
349
350
351
352
353
354
# File 'ext/oci8/oradate.c', line 344

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:



145
146
147
148
149
150
151
152
153
154
# File 'ext/oci8/oradate.c', line 145

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

    rb_obj_init_copy(lhs, 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)


362
363
364
365
366
367
368
# File 'ext/oci8/oradate.c', line 362

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.



376
377
378
379
380
381
382
383
384
385
386
# File 'ext/oci8/oradate.c', line 376

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)


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

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



280
281
282
283
284
285
286
287
288
289
290
# File 'ext/oci8/oradate.c', line 280

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)


394
395
396
397
398
399
400
# File 'ext/oci8/oradate.c', line 394

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.



408
409
410
411
412
413
414
415
416
417
418
# File 'ext/oci8/oradate.c', line 408

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)


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

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



602
603
604
# File 'lib/oci8/oci8.rb', line 602

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

#to_datetimeObject



611
612
613
# File 'lib/oci8/oci8.rb', line 611

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

#to_json(options = nil) ⇒ Object

:nodoc:



626
627
628
# File 'lib/oci8/oci8.rb', line 626

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:



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

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



593
594
595
596
597
598
599
600
# File 'lib/oci8/oci8.rb', line 593

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:



620
621
622
623
624
# File 'lib/oci8/oci8.rb', line 620

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


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

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:



616
617
618
# File 'lib/oci8/oci8.rb', line 616

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)


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

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.



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