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

Make it easier to work with Time, Date and DateTime Ecto types #94

Closed
paulcsmith opened this issue Feb 3, 2016 · 2 comments
Closed
Assignees

Comments

@paulcsmith
Copy link
Contributor

See #54 for why this would be nice.

I think it would be good to add to_date_time, to_date, to_time and corresponding sigil_t sigil_d and sigil_(not_sure_yet) sigils that can be imported and would convert a regular Erlang timestamp to the appropriate Ecto type. I imagine you could do something like this to make it easy to use:

defmodule MyApp.Factory do
  # This would import the functions for use inside the factory
  use ExMachina.Ecto, repo: MyRepo

  # I think this is nice because importing the sigils is optional so there are no accidental collisions
  defmacro __using__(_opts) do
    quote do
      import MyApp.Factory
      # Imports the sigils so you can them in your tests
      import ExMachina.TimeHelpers
    end
  end
end

# In a test

use MyApp.Factory # import the factory and the time helpers
create(:article, inserted_at: ~t(GoodTimes.a_day_ago)) # GoodTimes generates erlang time stamps
@paulcsmith paulcsmith added this to the Release 1.0 milestone Feb 5, 2016
@paulcsmith paulcsmith self-assigned this Feb 5, 2016
@paulcsmith
Copy link
Contributor Author

I believe I misunderstood the capabilities of sigils. For example

date_time = GoodTimes.a_day_ago
~d(date_time)

Gets passed to sigil_d as {:<<>>, [line: 21], ["date_time"]} without any other information that would make it possible to figure out what date_time refers to. It could work with tuples and strings, but not variables like in that example.

So I think the sigil route is too limited to be that useful. We could make it work with strings, but I don't think that's super useful. I think it might be useful to have some shortcut functions like dt, t, and d cast to Ecto.DateTime, Ecto.Time and Ecto.Date respectively

@paulcsmith paulcsmith removed this from the Release 1.0 milestone Apr 12, 2016
@paulcsmith
Copy link
Contributor Author

I may delay this and wait for better date time support to become standard in Ecto now that Elixir will have native calendar types: elixir-lang/elixir#4383

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant