Sha256: aa5168f82c3b5b6f862cf0950a87e42382da3f5f2c06ceceeb1309beb571bf51

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

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.2.1 lib/flux_capacitor/capacitor.rb
flux_capacitor-0.2.0 lib/flux_capacitor/capacitor.rb
flux_capacitor-0.1.4 lib/flux_capacitor/capacitor.rb