Class: Gravitext::DateSupport::JDate

Inherits:
Object
  • Object
show all
Defined in:
lib/gravitext-util/date_support.rb

Overview

Extensions to java.util.Date for some basic math operations and and conversions.

Constant Summary collapse

JSystem =

:nodoc:

Java::java.lang.System

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object

Return a new JDate representing the sum of self and other TimeDelta or Numeric (seconds).



193
194
195
196
197
198
199
200
201
202
# File 'lib/gravitext-util/date_support.rb', line 193

def +( other )
  case( other )
  when TimeDelta
    JDate.new( self.time + other.msecs )
  when Numeric
    JDate.new( self.time + ( other * 1000 ).to_i )
  else
    raise TypeError, "Can't add #{other.class} to JDate"
  end
end

#-(other) ⇒ Object

Return the difference between self and other. If other is a JDate or ruby core Time, returns a TimeDelta. If other is a TimeDelta or Numeric (seconds), return a new JDate.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/gravitext-util/date_support.rb', line 175

def -( other )
  case( other )
  when JDate
    TimeDelta.new( self.time - other.time )
  when TimeDelta
    JDate.new( self.time - other.msecs )
  when Numeric
    JDate.new( self.time - ( other * 1000 ).to_i )
  when Time
    TimeDelta.new( self.time -
                   ( ( other.to_i * 1000 ) + other.usec / 1000 ) )
  else
    raise TypeError, "Can't subract #{other.class} from JDate"
  end
end

#ageObject

Return age of self as a TimeDelta, by difference between the current time and self (with millisecond resolution.) Will be positive if self is “in the past”.



207
208
209
# File 'lib/gravitext-util/date_support.rb', line 207

def age
  TimeDelta.new( JSystem.current_time_millis - self.time )
end

#to_rubyObject

Return conversion to ruby core Time (with millisecond precision).



167
168
169
170
# File 'lib/gravitext-util/date_support.rb', line 167

def to_ruby
  ems = self.time
  Time.at( ems/1000, ( ems % 1000 ) * 1000 )
end