Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6916031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:33:02+00:00 2026-05-27T09:33:02+00:00

In my app I have a Person model. Each Person has an attribute time_zone

  • 0

In my app I have a Person model. Each Person has an attribute time_zone that specifies their default time zone. I also have an Event model. Each Event has a start_time and end_time timestamp, saved in a Postgres database in UTC time.

I need to create a query that finds events for a particular person that fall between midnight of one day and midnight of the next. The @todays_events controller variable hold the results of the query.

Part of the reason that I’m taking this approach is that I may have people from other time zones looking at the list of events for a person. I want them to see the day as the person would see the day and not based on the time zone they are in as an observer.

For whatever reason, I’m still getting some events from the previous day in my result set for @todays_events. My guess is that I’m comparing a UTC timestamp with a non-UTC parameter, or something along those lines. Generally, only events that begin or end in the evening of the previous day show up on the query result list for today.

Right now, I’m setting up:

@today    = Time.now.in_time_zone(@person.time_zone).midnight.to_date
@tomorrow = (@today + 1.day ).to_datetime
@today    = @today.to_datetime

My query looks like:

@todays_activities = @person.marks.where("(start_time >= ? AND start_time < ?) OR (end_time >= ? AND end_time < ?);", @today, @tomorrow, @today, @tomorrow ).order("start_time DESC")

How should I change this so that I’m guaranteed only to receive results from today (per the @person.time_zone in the @todays_activities query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:33:03+00:00Added an answer on May 27, 2026 at 9:33 am

    You’re losing track of your timezones when you call to_date so don’t do that:

    @today    = Time.now.in_time_zone(@person.time_zone).midnight.utc
    @tomorrow = @today + 1.day
    

    When you some_date.to_datetime, you get a DateTime instance that is in UTC so the result of something like this:

    Time.now.in_time_zone(@person.time_zone).midnight.to_date.to_datetime
    

    will have a time-of-day of 00:00:00 and a time zone of UTC; the 00:00:00 is the correct time-of-day in @person.time_zone but not right for UTC (unless, of course, @person is in in the +0 time zone).

    And you could simplify your query with overlaps:

    where(
        '(start_time, end_time) overlaps (timestamp :today, timestamp :tomorrow)',
        :today => @today, :tomorrow => @tomorrow
    )
    

    Note that overlaps works with half-open intervals:

    Each time period is considered to represent the half-open interval start <= time < end, unless start and end are equal in which case it represents that single time instant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little app that have by default youtube as homepage. You can
I've created a simple rails app that has a people and a notes model.
I have an ActiveAdmin app with a simple model that I want to persist
Say I have the following ember-data model: App.Person = DS.Model.extend({ firstName: DS.attr('string'), lastName: DS.attr('string'),
I have model Person or say Profile and this class Person has a genericRelationship
I've got a simple Django photography competition app that has three simple model definitions:
Let's say I have an app that matches two People against each other, kind
I have app in which i have recorded sound files i want that i
I have an app that have several views. In one view I would like
I have to develop web2py app and have to use Facebook sdk in that.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.