Sha256: 2048433ccb4ab3c81afa8266dc2bfcd9665e70988539fd7a7f0d1cbf6d5a0ff7

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

require 'flux_capacitor/version'
require 'date'
require 'murmurhash3'

module Flux

  class Capacitor
    attr_reader :pivot, :time_dilation
    def initialize (start_time, completion_target, oldest_target = Capacitor.oldest_string_time(start_time))
      @pivot = start_time
      time_to_complete = seconds_between(completion_target, pivot)
      dilation = seconds_between(pivot, oldest_target).to_f / time_to_complete.to_f
      @time_dilation = dilation
    end

    def limit
      diff = seconds_between(DateTime.now, pivot)
      datetime_minus_seconds(pivot, (diff * time_dilation).to_i)
    end

    def travel_to?(destination)
      destination = destination.is_a?(DateTime) ? destination : string_to_time(destination)
      limit < destination
    end

    def string_to_time(str)
      Capacitor.string_to_time(pivot, str)
    end

    def self.string_to_time(pivot, str)
      timestamp  = pivot.strftime("%s").to_i - (MurmurHash3::V32.str_hexdigest(str).to_i(16)/8)
      DateTime.strptime(timestamp.to_s, "%s")
    end

    def self.oldest_string_time(pivot)
      timestamp  = pivot.strftime("%s").to_i - ("ffffffff".to_i(16)/8)
      DateTime.strptime(timestamp.to_s, "%s")
    end

    private

    def seconds_between(date_time_1, date_time_2)
      date_time_1.strftime("%s").to_i - date_time_2.strftime("%s").to_i
    end

    def datetime_minus_seconds(datetime, seconds)
      ts = (datetime.strftime("%s").to_i - seconds).to_s
      DateTime.strptime(ts, "%s")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flux_capacitor-0.1.3 lib/flux_capacitor.rb
flux_capacitor-0.1.2 lib/flux_capacitor.rb
flux_capacitor-0.1.1 lib/flux_capacitor.rb