timex_ecto, 使用 Timex DateTimes和Ecto的适配器

分享于 

8分钟阅读

GitHub

  繁體 雙語
An adapter for using Timex DateTimes with Ecto
  • 源代码名称:timex_ecto
  • 源代码网址:http://www.github.com/bitwalker/timex_ecto
  • timex_ecto源代码文档
  • timex_ecto源代码下载
  • Git URL:
    git://www.github.com/bitwalker/timex_ecto.git
    Git Clone代码到本地:
    git clone http://www.github.com/bitwalker/timex_ecto
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/bitwalker/timex_ecto
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    

    Ecto的插件插件

    MasterHex.pm Version

    正在启动

    了解如何将 timex_ecto 添加到Elixir项目中并开始使用它。

    注释: 你必须使用Timex或者更高版本的3。

    向你的项目添加 timex_ecto

    要在项目中使用 timex_ecto,请编辑 mix.exs 文件并将它的作为依赖项添加:

    defapplicationdo [ applications: [:timex_ecto, ...], ...]enddefpdepsdo [{:timex, "~> 3.0"},
     {:timex_ecto, "~> 3.0"}]end

    将Timex类型添加到Ecto模型中

    defmoduleUserdouseEcto.Schema schema "users"do field :name, :string# Stored as an ISO date (year-month-day), reified as Date field :a_date, Timex.Ecto.Date# Timex version of :date# Stored as an ISO time (hour:minute:second.fractional), reified as Timex.Duration field :a_time, Timex.Ecto.Time# Timex version of :time# Stored as an ISO 8601 datetime in UTC (year-month-day hour:minute:second.fractional),# reified as DateTime in UTC field :a_datetime, Timex.Ecto.DateTime# Timex version of :datetime# DateTimeWithTimezone is a special case, please see the `Using DateTimeWithTimezone` section!# Stored as a tuple of ISO 8601 datetime and timezone name ((year-month-day hour:minute:second.fractional, timezone)),# reified as DateTime in stored timezone field :a_datetimetz, Timex.Ecto.DateTimeWithTimezone# A custom datatype (:datetimetz) implemented by Timex# Stored as an ISO 8601 datetime in UTC (year-month-day hour:minute:second.fractional),# reified as DateTime in local timezone field :a_timestamptz, Timex.Ecto.TimestampWithTimezone# Timex version of :timestamptzendend

    使用 DateTimeWithTimezone

    请参见这里的文档

    更改ecto宏的timestamps的默认时间戳类型

    根据 /timestamps/1的文档,通过设置 @timestamps_opts,很容易将默认的时间戳类型更改为其他的。 例如可以将 Timex.Ecto.TimestampWithTimezone 与下列选项一起使用。

    defmoduleUserdouseEcto.Schema@timestamps_opts [type:Timex.Ecto.TimestampWithTimezone,
     autogenerate: {Timex.Ecto.TimestampWithTimezone, :autogenerate, []}]
     schema "users"do field :name, :string timestamps()
     endend

    使用ecto宏的timestamps

    超级简单你的时间戳现在将是 Timex.Ecto.DateTime 结构而不是 Ecto.DateTime 结构。

    defmoduleUserdouseEcto.Schema@timestamps_opts [type:Timex.Ecto.DateTime]
     schema "users"do field :name, :string timestamps()
     endend

    与Phoenix一起使用

    Phoenix允许你通过更改 model 函数,通过 web/web.ex 将默认值全局应用到Ecto模型,如下所示:

    defmodeldoquotedouseEcto.Schema@timestamps_opts [type:Timex.Ecto.DateTime]
     endend

    这样做,你可以将Timex时间戳放入所有模型的范围中。

    精度

    默认情况下,Timex将生成最近的时间戳。 如果你想生成更精确的时间戳,你可以将 usec: true 选项传递给宏。 这将配置Timex以生成精确到微秒级精度的时间戳。

    @timestamps_opts [type:Timex.Ecto.DateTime,
     autogenerate: {Timex.Ecto.DateTime, :autogenerate, [:usec]}]

    示例用法

    下面是我为审核这个插件而构建的一个简单的测试应用程序:

    defmoduleEctoTest.RepodouseEcto.Repo, otp_app::timex_ecto_testenddefmoduleEctoTest.UserdouseEcto.Schema@timestamps_opts [type:Timex.Ecto.DateTime,
     autogenerate: {Timex.Ecto.DateTime, :autogenerate, []}]
     schema "users"do field :name, :string field :date_test, Timex.Ecto.Date field :time_test, Timex.Ecto.Time field :datetime_test, Timex.Ecto.DateTime field :datetimetz_test, Timex.Ecto.DateTimeWithTimezone field :timestamptz_test, Timex.Ecto.TimestampWithTimezone timestamps()
     endenddefmoduleEctoTestdoimportEcto.QueryuseTimexaliasEctoTest.UseraliasEctoTest.Repodefseeddo time =Duration.now
     date =Timex.today
     datetime =Timex.now
     datetimetz =Timezone.convert(datetime, "Europe/Copenhagen")
     timestamptz =Timex.local
     u = %User{name:"Paul", date_test: date, time_test: time, datetime_test: datetime, datetimetz_test: datetimetz, timestamptz_test: timestamptz}
     Repo.insert!(u)
     enddefalldo query = from u inUser,
     select: u
     Repo.all(query)
     endenddefmoduleEctoTest.AppdouseApplicationdefstart(_type, _args) doimportSupervisor.Spec tree = [worker(EctoTest.Repo, [])]
     opts = [name:EctoTest.Sup, strategy::one_for_one]
     Supervisor.start_link(tree, opts)
     endend

    结果是:

    iex(1)>EctoTest.seed14:45:43.461 [debug] INSERTINTO"users" ("date_test", "datetime_test", "datetimetz_test", "name", "time_test") VALUES ($1, $2, $3, $4, $5) RETURNING"id" [{2015, 6, 25}, {{2015, 6, 25}, {19, 45, 43, 457000}}, {{{2015, 6, 25}, {21, 45, 43, 457000}}, "Europe/Copenhagen"}, "Paul", {19, 45, 43, 457000}] OK query=3.9ms
    %EctoTest.User{__meta__: %Ecto.Schema.Metadata{source:"users",
     state::loaded},
     date_test:~D[2015-06-25],
     datetime_test:#<DateTime(2015-06-25T21:45:43.457Z Etc/UTC)>,datetimetz_test:#<DateTime(2015-06-25T21:45:43.457+02:00 Europe/Copenhagen)>,timestamptz_test:#<DateTime(2015-06-25T13:45:43.457-06:00 America/Chicago)>,name:"Paul", time_test:#<Duration(P45Y6M6DT19H45M43.456856S)>}iex(2)>EctoTest.all14:45:46.721 [debug] SELECT u0."id", u0."name", u0."date_test", u0."time_test", u0."datetime_test", u0."datetimetz_test"FROM"users"AS u0 [] OK query=0.7ms
    [%EctoTest.User{__meta__: %Ecto.Schema.Metadata{source:"users",
     state::loaded},
     date_test:~D[2015-06-25],
     datetime_test:#<DateTime(2015-06-25T21:45:43.457Z Etc/UTC)>,datetimetz_test:#<DateTime(2015-06-25T21:45:43.457+02:00 Europe/Copenhagen)>,timestamptz_test:#<DateTime(2015-06-25T13:45:43.457-06:00 America/Chicago)>,name:"Paul", time_test:#<Duration(PT19H45M43S)>}]iex(3)>

    附加文档

    Timex和timex_ecto的文档在这里是 ,并且在hexdocs上。

    许可证

    这个项目是麻省理工学院授权的。 查看这里 repo 中的许可证文件。


    相关文章