find created_at in local time

76 views
Skip to first unread message

Sven Koesling

unread,
Sep 2, 2010, 2:13:15 AM9/2/10
to rubyonra...@googlegroups.com
Hello and thanks for taking the time to read this,

last night I came across two problems from which the first one - I am
sure - is easy to solve. But as I am a beginner I am unable to find the
solution.

As I learned through the night rails stores created_at dates in utc.
Sure enough my local time zone is not utc :-)
When I have an item and use the method created_at rails translates the
time to my local time zone.

So when I add an item at 0:15 am local time and want to list all the
items for today with

@items = Item.find(:all, :conditions => [ "created_at LIKE ?",
"#{Time.now.to_date}%" ])

this item will never be found as its created_at date is yesterday. My
first idea was to add 7200 two created_at in the find clause, but that
would be an ugly workaround (I even did not get that to work...).

Can You tell me what would be the correct solution to translate?

And now the second question: Is there (already) a best practice to query
this in rails 3? I didn't find a solution to do a "LIKE" query without a
plugin, so how can one get all items created today out of the database?

@orders = Order.where(:created_at.to_date => Time.now.to_date) does not
work as I cannot add the method to_date to a symbol.

Thank You all
greetings
Sven
--
Posted via http://www.ruby-forum.com/.

Chris Mear

unread,
Sep 2, 2010, 4:19:33 AM9/2/10
to rubyonra...@googlegroups.com
On 2 September 2010 07:13, Sven Koesling <li...@ruby-forum.com> wrote:
> Hello and thanks for taking the time to read this,
>
> last night I came across two problems from which the first one - I am
> sure - is easy to solve. But as I am a beginner I am unable to find the
> solution.
>
> As I learned through the night rails stores created_at dates in utc.
> Sure enough my local time zone is not utc :-)
> When I have an item and use the method created_at rails translates the
> time to my local time zone.
>
> So when I add an item at 0:15 am local time and want to list all the
> items for today with
>
> @items = Item.find(:all, :conditions => [ "created_at LIKE ?",
> "#{Time.now.to_date}%" ])
>
> this item will never be found as its created_at date is yesterday. My
> first idea was to add 7200 two created_at in the find clause, but that
> would be an ugly workaround (I even did not get that to work...).
>
> Can You tell me what would be the correct solution to translate?

You can get the current UTC time using:

Time.now.utc

> And now the second question: Is there (already) a best practice to query
> this in rails 3? I didn't find a solution to do a "LIKE" query without a
> plugin, so how can one get all items created today out of the database?
>
> @orders = Order.where(:created_at.to_date => Time.now.to_date) does not
> work as I cannot add the method to_date to a symbol.

Yeah, you're right, using LIKE for this isn't great. If you're looking
for times that occur during today (local time) then what you're really
looking for is times that occur between midnight today and midnight
tomorrow. So I would probably do conditions like:

midnight = Time.now.midnight.utc # find last midnight in local time,
then convert to UTC

@items = Item.find(:all,
:conditions => ["created_at >= ? AND created_at < ?",
midnight,
midnight.advance(:days => 1)
]
)

Chris

Bill Walton

unread,
Sep 2, 2010, 10:34:52 AM9/2/10
to rubyonra...@googlegroups.com
Hi Sven,


On Thu, Sep 2, 2010 at 1:13 AM, Sven Koesling <li...@ruby-forum.com> wrote:
>
> Can You tell me what would be the correct solution to translate?
>

In addition to Chris' recommendations, you should check out the Rails
ActiveSupport::CoreExtensions helpers like .beginning_of_day and
.end_of_day methods which, IMHO, are more readable than using
:advance. (http://as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Calculations.html)

I'd also recommend the TZInfo library (http://tzinfo.rubyforge.org/)
if you're going to need to do reporting of this sort for clients in
more than one timezone.

HTH,
Bill

Sven Koesling

unread,
Sep 2, 2010, 2:39:15 PM9/2/10
to rubyonra...@googlegroups.com
Thank You all for Your help!

That was exactly what I needed.

Greetings

Robert Walker

unread,
Sep 2, 2010, 4:49:30 PM9/2/10
to rubyonra...@googlegroups.com
Bill Walton wrote:
> In addition to Chris' recommendations, you should check out the Rails
> ActiveSupport::CoreExtensions helpers like .beginning_of_day and
> .end_of_day methods which, IMHO, are more readable than using
> :advance.

It's also possible to improve readability using more Ruby like idioms:

@items = Item.find(:all, :conditions => {:created_at =>
Date.today...Date.today + 1.day } )

SELECT * FROM "items" WHERE ("items"."created_at" >= '2010-09-02' AND
'items"."created_at" < '2010-09-03')

Colin Law

unread,
Sep 2, 2010, 4:59:35 PM9/2/10
to rubyonra...@googlegroups.com

But going back to the OP's original point, that will give all items
created on that day UTC whereas I think he wanted items created today
local time.

Colin

Sven Koesling

unread,
Sep 2, 2010, 6:34:55 PM9/2/10
to rubyonra...@googlegroups.com

That's it. Normally nobody will use this app in the night, but I am
sure, if I leave this "hole" of two hours someone will test it at 1:00
am :-)

So thanks again to everybody for your input!

Sven

Reply all
Reply to author
Forward
0 new messages