Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: get #beginning_of_day in time zone

I have a default time zone setup for the rails application. And an instance of the Date object.

How can I get make Date#beginning_of_day to return the beginning of the day in the specified time zone, but not my local timezone.

Is there any other method to get beginning of the day time in the specified timezone for the given date?

date = Date.new(2014,10,29)  zone = ActiveSupport::TimeZone.new('CET') date.foo(zone) # should return "Wed, 29 Oct 2014 00:00:00 CET +01:00"  zone = ActiveSupport::TimeZone.new('UTC') date.foo(zone) # should return "Wed, 29 Oct 2014 00:00:00 UTC +00:00" 
like image 339
Bogdan Gusiev Avatar asked Mar 24 '11 12:03

Bogdan Gusiev


People also ask

How do I run Rails?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option.

How do I find routes in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.


2 Answers

DateTime.now.in_time_zone(Time.zone).beginning_of_day 
like image 134
Peder Avatar answered Sep 17 '22 13:09

Peder


time_zone = Time.zone # any time zone really time_zone.local(date.year, date.month, date.day) 

Problem is, Date.beginning_of_day does not honor Time.zone in ActiveSupport 2.3

Compare https://github.com/rails/rails/blob/v2.3.11/activesupport/lib/active_support/core_ext/date/calculations.rb#L64 (AS 2.3)

to https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb#L74 and https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/zones.rb#L7 (AS 3)

like image 22
Leonid Shevtsov Avatar answered Sep 18 '22 13:09

Leonid Shevtsov