2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Time.zone.localでout of range

Last updated at Posted at 2017-09-05

Time.localでout of range

当たり前といえば当たり前なのだが、ハマったのでメモしておく

以下のような日付入力フォームを作り、 入力は任意にしようとしたら表題のエラーが発生した。

結論から言うと、月と日には0がないため、月か日が入力されなかった場合、エラーになってしまう。

スクリーンショット 2017-09-05 14.54.45.png
search.html.erb
<%= f.label '開始' %>
<div class='form-inline'>
    <%= f.datetime_select :start_at, {use_month_numbers: true, include_blank: true}, {class: "form-control date-select"} %>
</div>
<%= f.label '終了' %>
<div class='form-inline'>
    <%= f.datetime_select :end_at, {use_month_numbers: true, include_blank: true}, {class: "form-control date-select"} %>
</div>
controller.rb
event[:start_at] = Time.zone.local(params[:search]["start_at(1i)"].to_i, params[:search]["start_at(2i)"].to_i, params[:search]["start_at(3i)"].to_i, params[:search]["start_at(4i)"].to_i, params[:search]["start_at(5i)"].to_i)
event[:end_at] = Time.zone.local(params[:search]["end_at(1i)"].to_i, params[:search]["end_at(2i)"].to_i, params[:search]["end_at(3i)"].to_i, params[:search]["end_at(4i)"].to_i, params[:search]["end_at(5i)"].to_i)
>> Time.zone.local(2017, 5, 3, 1, 20)
=> Wed, 03 May 2017 01:20:00 UTC +00:00

>> Time.zone.local(0, 5, 3, 2, 1)
=> Wed, 03 May 0000 02:01:00 UTC +00:00

>> Time.zone.local(0, 5, 0, 0, 0)
ArgumentError: argument out of range

>> Time.zone.local(2017, 0, 0, 0, 0)
ArgumentError: argument out of range

期限まで日がないっていうのにこんなのに時間を・・・(うまい)

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?