class Mingle::MingleTimestamp

Attributes

time[R]

Public Class Methods

from_millis( ms ) click to toggle source

Impl :note => simply calling Time.at( ms / 1000.0 ) doesn’t work as we might want, since it ends up passing a Float to Time.at() which apparently performs more calculations or otherwise leads to a time which is close to but not precisely the result of the division. To illustrate:

irb(main):013:0> Time.at( 1299534304123 / 1000.0 ).iso8601( 9 )
=> "2011-03-07T13:45:04.122999907-08:00"

while the algorithm we use, which uses integral values only, gives the 123 fractional value as expected:

irb(main):014:0> Time.at( 1299534304123 / 1000, ( 1299534304123 % 1000 ) * 1000 ).iso8601( 9 )

> “2011-03-07T13:45:04.123000000-08:00”

# File lib/mingle.rb, line 350
def self.from_millis( ms )

    not_nil( ms, :ms )

    secs = ms / 1000
    usec = ( ms % 1000 ) * 1000

    new( Time.at( secs, usec ), false )
end
from_seconds( secs ) click to toggle source
# File lib/mingle.rb, line 329
def self.from_seconds( secs )
    
    not_nil( secs, :secs )
    new( Time.at( secs ), false )
end
new( time, make_copy = true ) click to toggle source

Uses iso8601 serialize –> parse to make a copy of the supplied time unless make_copy is false, in which case time is used directly by this instance

# File lib/mingle.rb, line 303
def initialize( time, make_copy = true )
    
    not_nil( time, "time" )
    @time = ( make_copy ? Time.iso8601( time.iso8601( 9 ) ) : time ).utc
end
now() click to toggle source
# File lib/mingle.rb, line 309
def self.now
    new( Time.now, false )
end
rfc3339( str ) click to toggle source
# File lib/mingle.rb, line 315
def self.rfc3339( str )

    begin
        new( Time.iso8601( not_nil( str, :str ).to_s ), false )
    rescue ArgumentError => ae
        
        if ae.message =~ /^invalid date: /
            raise Rfc3339FormatError.new( ae.message )
        else
            raise ae
        end
    end
end

Public Instance Methods

<=>( other ) click to toggle source
# File lib/mingle.rb, line 386
def <=>( other )
    
    if other.is_a?( MingleTimestamp ) 
        @time <=> other.time
    else
        raise TypeError, other.class.to_s
    end
end
==( other ) click to toggle source
# File lib/mingle.rb, line 378
def ==( other )
    other.is_a?( MingleTimestamp ) && other.rfc3339 == rfc3339
end
Also aliased as: eql?
eql?( other )
Alias for: ==
rfc3339() click to toggle source
# File lib/mingle.rb, line 361
def rfc3339
    @time.iso8601( 9 )
end
Also aliased as: to_s
to_f() click to toggle source
# File lib/mingle.rb, line 373
def to_f
    @time.to_f
end
to_i() click to toggle source
# File lib/mingle.rb, line 368
def to_i
    @time.to_i
end
to_s()
Alias for: rfc3339