Class: Gravitext::DateSupport::TimeDelta

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

Overview

A Calendar-unaware, immutable, positive or negative time duration optimized for Java’s Integer milliseconds resolution and offering various convenience methods for conversion.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msecs) ⇒ TimeDelta

New TimeDelta given Numeric, positive or negative milliseconds. Optimized for Integer msecs.



67
68
69
# File 'lib/gravitext-util/date_support.rb', line 67

def initialize( msecs )
  @msecs = msecs
end

Instance Attribute Details

#msecsObject (readonly)

Return milliseconds as initialized



63
64
65
# File 'lib/gravitext-util/date_support.rb', line 63

def msecs
  @msecs
end

Instance Method Details

#+(other) ⇒ Object

Return a new TimeDelta representing the sum of this and other TimeDelta



83
84
85
# File 'lib/gravitext-util/date_support.rb', line 83

def +( other )
  TimeDelta.new( @msecs + other.msecs )
end

#-(other) ⇒ Object

Return a new TimeDelta representing the difference of this and other TimeDelta.



89
90
91
# File 'lib/gravitext-util/date_support.rb', line 89

def -( other )
  TimeDelta.new( @msecs - other.msecs )
end

#<=>(other) ⇒ Object

Compare this with other TimeDelta.



77
78
79
# File 'lib/gravitext-util/date_support.rb', line 77

def <=>( other )
  @msecs <=> other.msecs
end

#==(other) ⇒ Object

True if other is a TimeDelta with equal msecs.



72
73
74
# File 'lib/gravitext-util/date_support.rb', line 72

def ==( other )
  other.is_a?( TimeDelta ) && ( @msecs == other.msecs )
end

#daysObject

Return delta as Float days.



126
127
128
# File 'lib/gravitext-util/date_support.rb', line 126

def days
  @msecs / DAY_TO_MS_F
end

#hashObject

Return hash code.



94
95
96
# File 'lib/gravitext-util/date_support.rb', line 94

def hash
  @msecs.hash
end

#hoursObject

Return delta as Float hours.



121
122
123
# File 'lib/gravitext-util/date_support.rb', line 121

def hours
  @msecs / HOUR_TO_MS_F
end

#minsObject

Return delta as Float minutes.



116
117
118
# File 'lib/gravitext-util/date_support.rb', line 116

def mins
  @msecs / MIN_TO_MS_F
end

#secsObject Also known as: to_f

Return delta as Float seconds (with precision as initialized.)



104
105
106
# File 'lib/gravitext-util/date_support.rb', line 104

def secs
  @msecs / SEC_TO_MS_F
end

#to_iObject

Return delta as Integer seconds.



99
100
101
# File 'lib/gravitext-util/date_support.rb', line 99

def to_i
  ( @msecs / SEC_TO_MS ).to_i
end

#to_rObject

Return delta as Rational seconds.



111
112
113
# File 'lib/gravitext-util/date_support.rb', line 111

def to_r
  Rational( @msecs, SEC_TO_MS )
end

#to_sObject

Return String representation of this TimeDelta using “+/-(hh:)(mm:)(ss.uuu)” notation (where hour and minute components are only provided when non-zero.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gravitext-util/date_support.rb', line 133

def to_s
  ms, neg = @msecs < 0 ? [ -@msecs, true ] : [ @msecs, false ]
  secs   = ( ms % MIN_TO_MS ) / SEC_TO_MS_F
  mmins  = ( ms / MIN_TO_MS ).to_i
  hours, mins = mmins / 60, mmins % 60

  pad = ( secs < 10.0 ) ? '0' : '';

  if hours > 0
    hours = -hours if neg
    "%+d:%02d:%s%g" % [ hours, mins, pad, secs ]
  elsif mins > 0
    mins = -mins if neg
    "%+d:%s%g" % [ mins, pad, secs ]
  else
    secs = -secs if neg
    "%+g" % [ secs ]
  end
end