【问题标题】:next payment date save as datetime rails下一个付款日期另存为日期时间轨道
【发布时间】:2016-01-02 06:24:50
【问题描述】:

我正在尝试设置一种方法来创建比较这些列的每日计划:

我正在存储 paypal IPN next_payment_date,它的格式与默认的 rails 格式不同

示例

贝宝 ipn "next_payment_date"=>"03:00:00 Oct 06, 2015 PDT"

导轨 created_at: "2015-10-05 14:24:17"

这些是不同的格式,我不太确定 rails 是否可以比较它们。

例如

我尝试像这样将next_payment_date 存储为datetime

subscription = Subscription.find_by(paypal_recurring_profile_token: params[:recurring_payment_id])
if params[:next_payment_date]
  subscription.update_attributes(paid_until: params[:next_payment_date])
end

这是贝宝日期:

"next_payment_date"=>"03:00:00 Oct 06, 2015 PDT"

这就是 rails 存储的内容

paid_until: "2015-10-05 03:00:06"

似乎 rails 确实知道一点,但它不会转换月份和日期,只是时间。

存储这些参数的正确方法是什么?

编辑

架构:

create_table "payment_notifications", force: :cascade do |t|
  t.text     "params"
  t.integer  "user_role_id"
  t.string   "status"
  t.string   "transaction_id"
  t.datetime "created_at",                   null: false
  t.datetime "updated_at",                   null: false
  t.integer  "user_id"
  t.string   "txn_type"
  t.string   "recurring_payment_profile_id"
end

create_table "subscriptions", force: :cascade do |t|
  t.integer  "user_role_id"
  t.integer  "user_id"
  t.string   "paypal_customer_token"
  t.string   "paypal_recurring_profile_token"
  t.datetime "created_at",                                     null: false
  t.datetime "updated_at",                                     null: false
  t.datetime "paid_until"
  t.boolean  "canceled",                       default: false
end

编辑 2

试图转换字符串时间但似乎乱序:

irb(main):006:0> PaymentNotification.last.params[:next_payment_date]
  PaymentNotification Load (1.9ms)  SELECT  "payment_notifications".* FROM "payment_notifications"  ORDER BY "payment_notifications"."id" DESC LIMIT 1
=> "03:00:00 Oct 07, 2015 PDT"
irb(main):007:0> PaymentNotification.last.params[:next_payment_date].to_time
  PaymentNotification Load (4.7ms)  SELECT  "payment_notifications".* FROM "payment_notifications"  ORDER BY "payment_notifications"."id" DESC LIMIT 1
=> 2015-10-06 03:00:07 +0000

【问题讨论】:

    标签: ruby-on-rails datetime paypal paypal-ipn


    【解决方案1】:

    有几个不同的 ruby​​/rails 类可以处理日期和时间。检查您正在使用的不同对象的类。示例(在 rails 控制台中):

    2.2.2 :011 > Date.current.class
     => Date
    2.2.2 :012 > Time.now.class
     => Time
    2.2.2 :013 > DateTime.now.class
     => DateTime
    2.2.2 :014 > Time.zone.now.class
     => ActiveSupport::TimeWithZone
    

    关于处理这些类的体面参考:http://stevenyue.com/2013/03/23/date-time-datetime-in-ruby-and-rails/

    【讨论】:

      【解决方案2】:

      Created_at 是ActiveSupport::TimeWithZone 类。

      我猜 next_payment_date 是 DateDateTime 类 - 但再次运行 .class 来解决这个问题。

      让您感到困惑的是,您不知道为什么 rails 会以不同的方式显示它们——这是因为没有相同的对象——它们每个都是不同的类。

      所以: 1)检查他们是哪个类

      2) 决定要转换的类。

      3) 比较它们。

      示例:(假设paid_until 是UTC)

      > next_payment_date_timezone_pdt = ActiveSupport::TimeZone["Pacific Time (US & Canada)"].parse("03:00:00 Oct 06, 2015 PDT")
      => Tue, 06 Oct 2015 03:00:06 PDT -07:00
      > next_payment_date_timezone_utc = next_payment_date_timezone.in_time_zone('UTC')
      => Tue, 06 Oct 2015 10:00:06 UTC +00:00
      
      > paid_until_timezone_utc = ActiveSupport::TimeZone['UTC'].parse('2015-10-05 03:00:06')
      => Mon, 05 Oct 2015 03:00:06 UTC +00:00
      

      现在您可以比较它们。 这是主要思想 - 您应该根据自己的需要进行调整。

      【讨论】:

      • 当我执行PaymentNotification.find(5).params[:next_payment_date].class 时,它会以字符串形式返回,在这种情况下我该怎么办?
      • 您能否将架构文件添加到问题中?
      • 我将架构添加到问题中
      • 嗨,我在问题末尾添加了更多关于我尝试做的事情的信息,我需要一种格式化时间的方法,以便比较 next_payment_dateTime.currentTime.now(不确定哪个更适合)。
      猜你喜欢
      • 2013-06-24
      • 2014-04-28
      • 2013-12-18
      • 2013-03-22
      • 2015-10-18
      • 1970-01-01
      • 2020-02-07
      • 2016-11-22
      • 2019-06-19
      相关资源
      最近更新 更多