Class: OraDate
- Inherits:
-
Object
- Object
- OraDate
- Includes:
- Comparable
- Defined in:
- lib/oci8/oci8.rb,
ext/oci8/oradate.c,
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
-
._load(bytes) ⇒ OraDate
Restores a byte stream serialized by #_dump.
-
.now(*args) ⇒ OraDate
Returns an
OraDate
object initialized to the current local time.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Returns -1, 0, or +1 depending on whether self is less than, equal to, or greater than other.
-
#_dump(*args) ⇒ String
Serializes self.
-
#day ⇒ Fixnum
Returns the day of month field of self.
-
#day=(num) ⇒ Object
Assigns num to the day of month field of self.
-
#hour ⇒ Fixnum
Returns the hour field of self.
-
#hour=(num) ⇒ Object
Assigns num to the hour field of self.
-
#initialize(year = 1, month = 1, day = 1, hour = 0, min = 0, sec = 0) ⇒ Object
constructor
Returns an
OraDate
object initialized to the specified date and time. -
#minute ⇒ Fixnum
Returns the minute field of self.
-
#minute=(num) ⇒ Object
Assigns num to the minute field of self.
-
#month ⇒ Fixnum
Returns the month field of self.
-
#month=(num) ⇒ Object
Assigns num to the month field of self.
-
#second ⇒ Fixnum
Returns the second field of self.
-
#second=(num) ⇒ Object
Assigns num to the second field of self.
-
#to_a ⇒ Array
Returns a 6-element array of year, month, day, hour, minute and second.
-
#to_date ⇒ Object
Returns a Date object which denotes self.
-
#to_datetime ⇒ Object
Returns a DateTime object which denotes self.
-
#to_s ⇒ OraDate
Returns a string representing self.
-
#to_time ⇒ Object
Returns a Time object which denotes self.
-
#trunc ⇒ OraDate
Truncates hour, minute and second fields to zero.
-
#year ⇒ Fixnum
Returns the year field of self.
-
#year=(num) ⇒ Object
Assigns num to the year field of self.
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.
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().
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.
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().
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 */
}
|
#day ⇒ Fixnum
Returns the day of month field of self.
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.
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;
}
|
#hour ⇒ Fixnum
Returns the hour field of self.
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.
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;
}
|
#minute ⇒ Fixnum
Returns the minute field of self.
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.
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;
}
|
#month ⇒ Fixnum
Returns the month field of self. The month starts with one.
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.
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;
}
|
#second ⇒ Fixnum
Returns the second field of self.
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.
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_a ⇒ Array
Returns a 6-element array of year, month, day, hour, minute and second.
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_date ⇒ Object
Returns a Date object which denotes self.
706 707 708 |
# File 'lib/oci8/oci8.rb', line 706 def to_date Date.new(year, month, day) end |
#to_datetime ⇒ Object
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.
720 721 722 |
# File 'lib/oci8/oci8.rb', line 720 def to_datetime DateTime.new(year, month, day, hour, minute, second, @@tz_offset) end |
#to_s ⇒ OraDate
Returns a string representing self. The string format is ‘yyyy/mm/dd hh:mi:ss’.
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_time ⇒ Object
Returns a Time object which denotes self.
696 697 698 699 700 701 702 703 |
# File 'lib/oci8/oci8.rb', line 696 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 |
#trunc ⇒ OraDate
Truncates hour, minute and second fields to zero.
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;
}
|
#year ⇒ Fixnum
Returns the year field of self.
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.
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;
}
|