great084 / twitter_tool

ツイッターツールの開発
0 stars 0 forks source link

[改善] APIリクエスト時間をUTCから日本時間に修正 #30

Closed domi10momo closed 3 years ago

domi10momo commented 3 years ago

概要

  1. APIへのリクエスト時間を日本時間にする (現在、9時間の時差 UTC)

  2. (1の実装完了後) tweet.rb 内 set_period_paramsdate_toago(0.years)に修正 (現在、ago(9.hours)にて指定

実装内容

1年前から現在までのツイートを取得しようとした場合、下記"sent"の時間が9時間時差のためAPIの返り値がエラーとなる。 (日本時間では、"toDate"の18時17分に実行しています)

エラー [1] pry(#)> response => {"error"=> {"message"=> "There were errors processing your request: Invalid 'toDate':'202012041817'. 'toDate' must be a minimum of 10 seconds prior to the request time.", "sent"=>"2020-12-04T09:17:45+00:00", "transactionId"=>"002a2da700477f27"}}

現象確認方法

tweet.rbtwitter_search_data responseがAPIからの戻り値のため、response取得後にbinding.pryを挿入

その他情報

Railsタイムゾーンまとめ

上記サイトを元に、config/application.rb追記しましたが、テーブル内の時刻が日本時間になったのみ、sentは変わらず。

    config.time_zone = 'Tokyo'
    config.active_record.default_timezone = :local
great084 commented 3 years ago

@domi10momo

以下のように、GMT基準の現在時刻を取得後に、型変換すれば良いと思いますがどうでしょうか?

pry(main)> Time.now.strftime("%Y%m%d%H%M")  #日本時間
=> "202012051951"
pry(main)> Time.now.gmtime.strftime("%Y%m%d%H%M")  #GMT基準時
=> "202012051051"