activegraph icon indicating copy to clipboard operation
activegraph copied to clipboard

Timestamps perhaps should default persist as floats (vs integer) to get msec precision?

Open brucek opened this issue 7 years ago • 1 comments

It's very helpful to have float (ie msec) precision in timestamps, especially when you want to use the created_at time as the sort value for ActiveRel's.

I'm manually doing something like this (inspired by this post, where you might still be able to see my prior broken code without SpecialDateTime):

class MyRel
  property :created_at, type: SpecialDateTime, typecaster: DatetimeFloatConverter
  type 'our_type'
end

# This just prevents us from jacking normal timestamps
# Without it, existing timestamps are reloaded with our DatetimeFloatConverter
# (which is bad)
class SpecialDateTime < DateTime
end

# As per http://neo4jrb.readthedocs.io/en/7.1.x/api/Neo4j/Shared/Typecaster.html
class DatetimeFloatConverter
  class << self
    def primitive_type
      Float
    end

    def convert_type
      SpecialDateTime
    end

    # multiply by 1000 so we can use Neo4j's internal timestamp() to compare
    def to_db(value)
      value.to_time.to_f * 1000
    end

    def to_ruby(value)
      value = (value / 1000) if value.is_a?(Float)
      Time.at(value).to_datetime
    end
    alias_method :call, :to_ruby
  end

  include Neo4j::Shared::Typecaster
end

Just throwing this out there to see if I am a lone voice in the wilderness or if there is any interest in this for other folks...

brucek avatar Jan 18 '17 07:01 brucek

It has been a few years, but I want to +1 @brucek and @jorroll 's requests for millisecond timestamp precision being an option. The solution provided in #1406 is probably what I will go with for now, but bumping this years old thread to see if anyone else is still tracking this issue.

ahampton-seczetta avatar Mar 24 '22 18:03 ahampton-seczetta