NoBrainerORM / nobrainer

Ruby ORM for RethinkDB
http://nobrainer.io/
Other
387 stars 49 forks source link

Non-UTC timezone issue #247

Open zedtux opened 6 years ago

zedtux commented 6 years ago

Time records attributes of my application are saved in UTC in RethinkDB, which is fine by me, but now I'd like to show them in the current timezone.

I have updated my Rails app config/application.rb file in order to set the time_zone:

...

module AppName
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.1

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Don't generate system test files.
    config.generators.system_tests = nil

    config.time_zone = 'Paris'
  end
end

And I have updated the NoBrainer initializer:

  ...

  # user_timezone can be configured with :utc, :local, or :unchanged.
  # When reading a Time attribute from a model, the timezone of that attribute
  # is set according to the following setting.
  config.user_timezone = :local

  ...

Restarted everything but my dates and times are still displayed in UTC:

2018-01-11T10:31:56.733+00:00

(This should be with an hour more 11:31:56)

Can you please tell me what am I doing wrong?

nviennot commented 6 years ago

What's your model code? How did you declare your attributes?

zedtux commented 6 years ago

Sorry to not have provided my code 😞. Here it is:

class Request
  include NoBrainer::Document

  before_create :populates_created_at

  field :url, type: String
  field :created_at, type: Time, index: true

  private

  def populates_created_at
    self.created_at = Time.zone.now
  end
end
nviennot commented 6 years ago

Thank you. Internally, NoBrainer uses Time#getlocal.

Example:

~/brainy (development) > Time.now.utc
=> 2018-01-11 13:29:35 UTC
~/brainy (development) > Time.now.utc.getlocal
=> 2018-01-11 08:29:37 -0500

What do you get?

The settings of the Rails time_zone is not taken in account. Maybe we can have another setting that uses the Rails configuration instead? The place to add this code is here: https://github.com/nviennot/nobrainer/blob/master/lib/no_brainer/document/types/time.rb#L35 Feel free to make a pull request :) :)

zedtux commented 6 years ago

Thank you @nviennot for your comment 👍.

Right now, I'm working on having AASM compatible with NoBrainer, but the I can try to implement this too. Thank you for having pointed me to the right place :)

zedtux commented 6 years ago

I'm done with AASM, if you want to have a look and give feedbacks: https://github.com/aasm/aasm/pull/522.

I will now check for this timestamp issue.

nviennot commented 6 years ago

Great work!

I put a few comments in the pull request

zedtux commented 6 years ago

Thank you @nviennot. Where are your comments? I can't see them.

nviennot commented 6 years ago

Forgot to press "submit review". Should be in