我正在尝试设置一种方法来创建一个每日日程,比较这些列:
我正在存储paypal IPN next_payment_date,它的格式与默认的rails格式不同
示例
贝宝ipn "next_payment_date"=>"03:00:00 Oct 06, 2015 PDT"
rails 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发布于 2015-10-05 15:21:05
有几个不同的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/
发布于 2015-10-05 15:18:40
Created_at是ActiveSupport::TimeWithZone类。
我猜想next_payment_date要么是Date类,要么是DateTime类--但是再次运行.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现在你可以比较它们了。这是主要的想法--你应该根据你的需要调整它。
https://stackoverflow.com/questions/32951271
复制相似问题