0

I have a Rails 4 application that according to best practices is storing dates in my MySQL database in UTC. This is ok.

However, I have an scenario that is not working properly.

I have an event manager which is always running looking for new events (stored in database) to run. These events are stored in database according to user actions on the site. The events table in DB has an execution_date field that stores the event planned execution date. According to this, when a user does something that creates an event event execution_date is converted to UTC.

In Rails, I use Time.now --> 2014-10-04T01:02:10-07:00 (I´m 7 hours from UTC). In DB this time is converted to UTC, so I see 2014-10-04 08:02:10. I think this is right too.

But the problem is the event manager is running next query to get events pending of execution:

SELECT e.*
                               FROM events e, event_states es
                               WHERE e.execution_date <= NOW()
                               AND es.name = 'Pending'
                               AND es.id = e.event_state_id

When I run this query, NOW() returns my local time which is 2014-10-04 01:02:25. So, the event that should be executed now, will be executed in 7 hours.

So, as summary, execution_date should always be stored in server local time.

2
  • Could you configure your DB to use UTC time?
    – mccannf
    Oct 4, 2014 at 9:07
  • Thanks mccannf. I finally replaces my NOW() function in the query with UTC_TIMESTAMP(), which has same effect as your suggestion.
    – Rober
    Oct 4, 2014 at 9:35

1 Answer 1

1

According to @mccannf comment above, I didn´t update my DB to UTC time, but I replaced my query with

SELECT e.*
                               FROM events e, event_states es
                               WHERE e.execution_date <= UTC_TIMESTAMP()
                               AND es.name = 'Pending'
                               AND es.id = e.event_state_id

NOTE: Using UTC_TIMESTAMP() gets the UTC time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.