下一个付款日期保存为datetime rails

时间:2015-10-05 14:37:52

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

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

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

示例

paypal 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

这是paypal日期:

"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

2 个答案:

答案 0 :(得分:2)

有一些不同的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/

答案 1 :(得分:1)

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

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

相关问题