Class: Mingle::MingleTimestamp

Inherits:
MingleValue
  • Object
show all
Extended by:
BitGirder::Core::BitGirderMethods, Forwardable
Includes:
Comparable
Defined in:
lib/mingle.rb

Defined Under Namespace

Classes: Rfc3339FormatError

Constant Summary

Constants included from BitGirder::Core::BitGirderMethods

BitGirder::Core::BitGirderMethods::PARAM_TYPE_ARG, BitGirder::Core::BitGirderMethods::PARAM_TYPE_ENVVAR, BitGirder::Core::BitGirderMethods::PARAM_TYPE_KEY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BitGirder::Core::BitGirderMethods

argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn

Constructor Details

#initialize(time, make_copy = true) ⇒ MingleTimestamp

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



303
304
305
306
307
# 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

Instance Attribute Details

#timeObject (readonly)

Returns the value of attribute time.



298
299
300
# File 'lib/mingle.rb', line 298

def time
  @time
end

Class Method Details

.from_millis(ms) ⇒ Object

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”



350
351
352
353
354
355
356
357
358
# 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) ⇒ Object



329
330
331
332
333
# File 'lib/mingle.rb', line 329

def self.from_seconds( secs )
    
    not_nil( secs, :secs )
    new( Time.at( secs ), false )
end

.nowObject



309
310
311
# File 'lib/mingle.rb', line 309

def self.now
    new( Time.now, false )
end

.rfc3339(str) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
# 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

Instance Method Details

#<=>(other) ⇒ Object



386
387
388
389
390
391
392
393
# 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) ⇒ Object Also known as: eql?



378
379
380
# File 'lib/mingle.rb', line 378

def ==( other )
    other.is_a?( MingleTimestamp ) && other.rfc3339 == rfc3339
end

#rfc3339Object Also known as: to_s



361
362
363
# File 'lib/mingle.rb', line 361

def rfc3339
    @time.iso8601( 9 )
end

#to_fObject



373
374
375
# File 'lib/mingle.rb', line 373

def to_f
    @time.to_f
end

#to_iObject



368
369
370
# File 'lib/mingle.rb', line 368

def to_i
    @time.to_i
end