Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Force conversion to/from a specific timezone #47

Open
phillipp opened this issue Aug 24, 2016 · 6 comments
Open

RFC: Force conversion to/from a specific timezone #47

phillipp opened this issue Aug 24, 2016 · 6 comments

Comments

@phillipp
Copy link

Hi,
I'm maintaining a legacy app where the time zones are stored in the local time zone instead of UTC. I want to access the database via ecto. The problem is that it messes up the time by converting it to/from UTC and so everything breaks (there are literally hundreds of other clients that use the same database).

What is the best way to turn off the time zone conversion?

@phillipp
Copy link
Author

phillipp commented Aug 25, 2016

I have implemented such behaviour in my fork (hard-coded for the time being: phillipp@475d0be). Would you like to integrate that? In that case, I'm not so sure what the best implementation would be. I thought about an extra type (like Timex.Ecto.LocalDateTime or sth.) but 99% of the module would be exactly the same code than the DateTime type. Can you think of a better way? I'm new to elixir and haven't fully grasped all the patterns that are possible and when to use what.

@bitwalker
Copy link
Owner

Probably the best way is to implement a Timex.Ecto.NaiveDateTime type which performs no conversions. Inputs will be converted to NaiveDateTime, and outputs will always be NaiveDateTime.

@bitwalker
Copy link
Owner

It's implementation can be very similar to Timex.Ecto.DateTime, just without the conversions to/from UTC.

@bitwalker
Copy link
Owner

I will put this on my TODO list, but it may be a week or two before I can get to it, if you'd like to take a stab at it and open a PR to collaborate on, I'd be glad to help with any questions.

@phillipp
Copy link
Author

phillipp commented Sep 1, 2016

I have now tested two implementations:

a) one that converts the time from/to the local timezone, and that worked as I expected
b) one "naive" that does not convert before/after storing. This works for my own timestamps from the application that I converted explicitly to the local timezone before storing but not for the inserted_at and updated_at timestamps as they seem to be always generated in the UTC time zone.

At least for my use case I would need the first implementation. I'm not sure if that is a extremely specific case, but coming from Rails I wanted to match this behaviour:

    config.time_zone = 'Berlin'

    config.active_record.default_timezone = :local
    config.active_record.time_zone_aware_attributes = false

What do you think, is that too application-specific and should be part of my app or might there be a use case that should be supported by the lib? I think both might be possible...

@bitwalker
Copy link
Owner

Sorry it took me so long to get back to this - I am open to support such configuration, but I would suggest proposing it to the Ecto team first - it would be far better if it was an upstream config option, but if it's something they are not prepared to do in Ecto, I would be open to a PR for this project :)

@bitwalker bitwalker changed the title Turn off conversion before storing/loading RFC: Force conversion to/from a specific timezone Jun 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants