是否可以将Rails默认时间戳更改为Y-m-d H:i:s(而不是Y-m-d H:i:s.u)或让laravel忽略Y-m-d H:i:s.u的小数部分? [英] Is there a way to change Rails default timestamps to Y-m-d H:i:s (instead of Y-m-d H:i:s.u) or have laravel ignore decimal portion of Y-m-d H:i:s.u?

查看:362
本文介绍了是否可以将Rails默认时间戳更改为Y-m-d H:i:s(而不是Y-m-d H:i:s.u)或让laravel忽略Y-m-d H:i:s.u的小数部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个使用相同postgres DB的应用程序. laravel应用程序和Rails应用程序. DB中的当前数据采用Y-m-d H:i:s格式,但是只要rails添加一条记录,该格式就是Y-m-d H:i:s.u(包括毫秒).

I have two applications using the same postgres DB. A laravel application and a rails application. The current data in the DB is in Y-m-d H:i:s format but whenever rails adds a records, the format is Y-m-d H:i:s.u which includes milliseconds.

如果在laravel中引用了created_at日期,则会在laravel端引起以下错误InvalidArgumentException Trailing data ...

This causes the following error on the laravel side if the created_at date is ever references in laravel InvalidArgumentException Trailing data...

Laravel模型可以更改其日期格式,因此我可以包括使其符合Y-m-d H:i:s.u,然后我需要更新所有记录,以使created_at日期(以及任何其他时间戳记)具有毫秒数.不幸的是,当我为laravel模型指定格式为Y-m-d H:i:s时,它不会忽略小数.这就是为什么我现在正在寻找默认导轨以Y-m-d H:i:s格式保存的方法,而不是包括毫秒数的原因.然后,Rails和Laravel应用程序将使用相同的格式,并且不会发生任何冲突.

Laravel models can mutate their date format, so I can include make it conform to Y-m-d H:i:s.u then I would need to update all the records to have the miliseconds in the created_at date ( and any other timestamps). Unfortunatly when I specify the format to be Y-m-d H:i:s for the laravel model, it will not ignore the decimals. This is why I'm now looking for ways to default rails to save in the Y-m-d H:i:s format, instead of including the miliseconds. Then both the Rails and Laravel applications would be using the same format and there wouldn't be any conflict.

我知道我可以进入DB并将列类型更改为timestamp(0),该类型将截断小数位,但是我愿意更改框架保存的格式,而不是更改DB接受的格式.

I know I can go into the DB and change the column type to timestamp(0) which truncates the decimals, but I would perfer to change the format that the frameworks are saving, rather then change what the DB will accept.

Y-m-d H:i:sY-m-d H:i:s.u都是有效的时间戳...如果我可以让Rails使用Y-m-d H:i:s格式或让Laravel在查看时间戳以防止尾随数据错误时忽略.u,我会清除.

Both Y-m-d H:i:s and Y-m-d H:i:s.u are valid timestamps... If I could get Rails to use the Y-m-d H:i:s format or have Laravel ignore the .u when looking at timestamps to prevent the trailing data error, I would be in the clear.

是否可以更改rails将时间戳保存到数据库的默认格式?

Is there a way to change the default format that rails saves timestamps to the DB?

是否可以让Larave Carbon忽略Y-m-d H:i:s.u格式的时间戳的小数部分?

Is a way to have Larave Carbon ignore the decimal portion of a timestamp in the Y-m-d H:i:s.u format?

谢谢

推荐答案

Rails方面的解决方案

在Rails(5.2)中使用的ActiveRecord似乎自动将小数秒增加至1毫秒,以保存created_atupdated_at或数据库中接受亚秒级的DB中任何其他时间戳记列,如文件

Solution in the Rails side

It seems ActiveRecord used in Rails (5.2) automatically adds decimal seconds down to 1 msec in saving created_at and updated_at or any other Timestamp columns in the DB that accept sub-seconds, as defined in the file active_record/connection_adapters/abstract/quoting.rb

这是一个解决方法.在访问模型时,Rails总是会读取的任何文件(例如ApplicationRecord模型文件)中的最顶层添加此行.

A work around is this. Add this line at a top level in any of the files which would be always read by Rails when accessing a model (such as, ApplicationRecord model file).

Time::DATE_FORMATS[:db] = '%Y-%m-%d %H:%M:%S.000000000'

module ActiveRecord::ConnectionAdapters::Quoting
  alias_method :quoted_date_orig, :quoted_date if ! self.method_defined?(:quoted_date_orig)

  def quoted_date(*rest, **kwd)
    quoted = quoted_date_orig(*rest, **kwd)
    quoted.sub(/(\.\d*)\.\d{6}$/, '\1')
  end
end

创建新记录后,您可以从Rails控制台进行确认,

You can confirm it from Rails console, after creating a new record,

MyModel.last.created_at.nsec  # => 0

或直接访问数据库以查看它.

or simply access the DB directly to see it.

此更改不仅会影响created_atupdated_at,还会影响数据库中的所有其他时间戳记列.我认为您仍然可以通过将String而不是Time实例设置为Model实例(如my_model.col_msec_desired = "2018-01-02 03:04:05.678")来将msec(或nsec)精度的值保存到此类列.那么在保存记录时将不会引用Time::DATE_FORMATS[:db].

This change affects not only created_at and updated_at but also all the other timestamp columns in the DB. I think you can still save a value to msec (or nsec) precision to such a column by setting a String as opposed to a Time instance to your Model instance like my_model.col_msec_desired = "2018-01-02 03:04:05.678"; then Time::DATE_FORMATS[:db] would not be referenced in saving the record.

在撰写本文时(2018-10-18)这可能很棘手,但是根据

It may be tricky at the time of writing (2018-10-18), but a work seems to be in progress, according to a very recent Laracast post by cmbertsch01

(注意:在评论之后的原始帖子的第二天进行了重大更新.)

(Note: a major update made a day after from the original post, following the comment.)

这篇关于是否可以将Rails默认时间戳更改为Y-m-d H:i:s(而不是Y-m-d H:i:s.u)或让laravel忽略Y-m-d H:i:s.u的小数部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆