Sha256: d78e0585fd1350763115f56d0f094989af4b825821a4fe0d20ed9c7667ac1df6

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

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

module Flux

  class Capacitor
    attr_reader :pivot, :pivot_ts, :time_dilation
    def initialize (start_time, completion_target, oldest_target = Capacitor.oldest_string_time(start_time))
      @pivot = start_time
      @pivot_ts = pivot.strftime("%s").to_i
      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

1 entries across 1 versions & 1 rubygems

Version Path
flux_capacitor-0.1.0 lib/flux_capacitor.rb