Module: RDO::Util

Defined in:
lib/rdo/util.rb

Overview

This file contains methods useful for drivers to convert complex types.

Performing these operations in C would not be any cheaper, since the data must first be converted into Ruby types anyway.

Constant Summary collapse

DEV_NULL =

A suitable NULL device for writing nothing

if defined? IO::NULL
  IO::NULL
else
  ENV["OS"] =~ /Windows/ ? "NUL" : "/dev/null"
end

Class Method Summary collapse

Class Method Details

.date(s) ⇒ Date

Convert a date string into a Date.

This method understands AD and BC.

Parameters:

  • s (String)

    a string representing a date, possibly BC

Returns:

  • (Date)

    a Date for this input



91
92
93
# File 'lib/rdo/util.rb', line 91

def date(s)
  Date.parse(s)
end

.date_time_with_zone(s) ⇒ DateTime

Convert a date & time string, with a time zone, into a DateTime.

Parameters:

  • s (String)

    a date & time string, including a time zone

Returns:

  • (DateTime)

    a DateTime for this input



78
79
80
# File 'lib/rdo/util.rb', line 78

def date_time_with_zone(s)
  DateTime.parse(s)
end

.date_time_without_zone(s) ⇒ DateTime

Convert a date & time string, without a time zone, into a DateTime.

This method will parse the DateTime using the system time zone.

Parameters:

  • s (String)

    a date & time string

Returns:

  • (DateTime)

    a DateTime in the system time zone



67
68
69
# File 'lib/rdo/util.rb', line 67

def date_time_without_zone(s)
  date_time_with_zone(s + system_time_zone)
end

.decimal(s) ⇒ BigDecimal

Convert a String to a BigDecimal.

Parameters:

  • s (String)

    a String that is formatted as a decimal, or NaN

Returns:

  • (BigDecimal)

    the BigDecimal representation of this number



54
55
56
# File 'lib/rdo/util.rb', line 54

def decimal(s)
  BigDecimal(s)
end

.float(s) ⇒ Float

Convert a String to a Float, taking into account Infinity and NaN.

Parameters:

  • s (String)

    a String that is formatted for a Float; or Infinity, -Infinity or NaN.

Returns:

  • (Float)

    a Float that is the same as the input



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rdo/util.rb', line 34

def float(s)
  case s
  when "Infinity"
    Float::INFINITY
  when "-Infinity"
    -Float::INFINITY
  when "NaN"
    Float::NAN
  else
    Float(s)
  end
end

.system_time_zoneString

Get the time zone of the local system.

This is useful—in fact crucial—for ensuring times are represented correctly.

Driver developers should use this, where possible, to notify the DBMS of the client’s time zone.

Returns:

  • (String)

    a string of the form ‘+10:00’, or ‘-09:30’



105
106
107
# File 'lib/rdo/util.rb', line 105

def system_time_zone
  DateTime.now.zone
end