Class: Pacer::Orient::BinaryDateEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/pacer-orient/encoder.rb

Overview

This is an alternate encoder that uses a binary representation of Dates and DateTimes that allows the database to return the correct type and has much better handling of timezones than the standard approach which turns all dates into basic java.util.Date objects.

Constant Summary collapse

JavaDate =
java.util.Date

Class Method Summary collapse

Class Method Details

.decode_property(value) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pacer-orient/encoder.rb', line 121

def self.decode_property(value)
  case value
  when ArrayJavaProxy
    str = String.from_java_bytes(value)
    flag = str[0]
    case flag
    when 'D'
      data = str.unpack("ANnc")
      i = data[1]
      r = data[2]
      c = data[3]
      if r
        t = Time.at(i + (r / 10000.0))
        case c
        when 1
          t.utc.to_datetime
        when 2
          t.utc
        else
          t
        end
      else
        Time.at(i).utc.to_date
      end
    else
      Encoder.decode_property value
    end
  else
    Encoder.decode_property value
  end
end

.encode_property(value) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pacer-orient/encoder.rb', line 96

def self.encode_property(value)
  case value
  when DateTime, Time, JavaDate
    f = if value.is_a? JavaDate
          value.getTime / 1000.0
        else
          value.to_time.to_f
        end
    i = f.truncate
    r = (f.remainder(1) * 10000).round
    if value.is_a? DateTime
      c = 1
    elsif value.is_a? Time and value.utc?
      c = 2
    else
      c = 3
    end
    ["D", i, r, c].pack("ANnc").to_java_bytes
  when Date
    ["D", value.to_time.to_i].pack("AN").to_java_bytes
  else
    Encoder.encode_property(value)
  end
end