Rails get Date part of DateTime with Time Zone - ruby-on-rails

I'm trying to execute a query in rails 3 with the following syntax: CourseOrder.where("DATE(ordered_at) = ?", date).
My problem is that rails saves all times in UTC, so in my Timezone (+2) at 0:33 its the 4th but in UTC its the 3th of the month. Is there a way to query the date part of a DateTime with Timezone?

Don't use date, do it like this instead:
time = DateTime.civil_from_format(:local, 2011, 4, 9)
CourseOrder.where("ordered_at > ? AND ordered_at < ?", time - 1.day, time)
You're storing a datetime, so query that instead of trying to use date, which doesn't account for Timezones.

solved the problem with a change in which values I compare.
I have a Date value to, and now I'm comparing a date value with a date value which is better than comparing date value with datetime value which have to fail.
thanks for leading me to the right direction

Related

changing or adding hours to a datetime in ruby

im having a problem with datetime in rails. Im reading a json and saving dates in a model. that has a column t.date:datetime
The thing is there is 2 dates that i get from this json, and i want only to save it as 1.
I get date for day : 2022-04-16 00:00:00 and also i get a hour date : 01:00:00 is there a way to add that hour date to my day date ? Like getting 2022-04-16 01:00:00 ?
Im new to rails so sorry if im not explaining correctly.
An hour is 1/24th of a day. DateTime happily let's you add that to a date.
t1 = DateTime.today
t2 = t1 + 1/24r # a rational number. 1.0/24 is fine too.
You can convert your time to second and then add it to your date
date = "2022-04-16 00:00:00".to_datetime
time = Time.parse("01:00:00").seconds_since_midnight.seconds
date + time
You don't need to do to_datetimeif your date is already a datetime object

Data retrieving from sqlite DB between two dates - using objective c

I am using the below query with date filtering, but I am getting wrong result.
SELECT * FROM TRANSACTIONSHISTORY
WHERE DATE > "29-01-2015 12:00:00"
AND DATE < "30-01-2015 00:00:00" AND USERID=abc
I am getting result with date column with value of 29-Jan-2016 records, what am I missing here, can any one help me to get out of this value.
The date format in your SQL will not work because SQLite doesn't have a native datetime type, so it's generally stored either as a string, in YYYY-MM-DD HH:MM:SS.SSS format, or as an numeric value representing the number of seconds since 1970-01-01 00:00:00 UTC. See date and time types on SQLite.org. Note that if you're using the string representation that the sequence is year, month, day (which, when sorting/querying this string field, the this alphanumeric string will sort correctly by year first, then month, and then day, which is critical when doing queries like yours).
If you really stored dates in the database as a string in the DD-MM-YYYY HH:MM:SS format, you should consider changing the format in which you saved the values into one of the approved date formats. It will make the date interactions with the database much, much easier, allowing queries like the one you asked for (though, obviously, with DD-MM-YYYY replaced with YYYY-MM-DD format).
You have cast your string to Date
SELECT * FROM TRANSACTIONSHISTORY WHERE DATE between Datetime('29-01-2015 12:00:00') and Datetime('30-01-2015 00:00:00') AND USERID=abc
The first answer is exactly what you need. What you did in your code would be comparing strings using ASCII values.
I would recommend you to use the linux time stamps like: 1453818208, which is easier to save and compare. In addition, it can always be translated to human-readable dates like: 29-01-2015 12:00:00.
SELECT * FROM TRANSACTIONSHISTORY
WHERE DATE > "29-01-2015 12:00:00"
AND DATE < "30-01-2015 00:00:00" AND USERID=abc
I hope this helps you :)
Try this first try without Time,after that try date and time both , Hope i will work for you
SELECT TRANSACTIONSHISTORY
FROM SHIPMENT
WHERE DATE
BETWEEN '11-15-2010'
AND '30-01-2015'
// you can try this one also
SELECT * FROM TRANSACTIONSHISTORY WHERE DATE BETWEEN "2011-01-11" AND "2011-8-11"

Comparing datetime and date in a .where() on Rails

I'm working on an event system where admins can select events to feature and add promotional text. As they need to add additional text the featured events are in their own table and reference the event details.
I'm now trying to output featured events for each day in the next week. Simplified example:
day = DateTime.now.to_date
featured_events = #featured_events.where('event.start_datetime = ?', day)
The problem is that start_datetime is in datetime format and day is in date format so it always outputs nothing. Is it possible to compare these two values with .where()?
Thanks!
May be you're looking for this:
#featured_events.where('event.start_datetime > ? and event.start_datetime < ?', DateTime.now, 1.day.from_now.beginning_of_day)
You don't say what database you're using.
In PostgreSQL we'd truncate the datetime inside the DB query so the DBM can do the compare. On MySQL we'd use a function to convert the datetime to a date.

rails group by utc date

I have a time field in table "timestamp without time zone". When record is saved to database, the utc time might be a different day compared to the local time. However, I need to group the records by date. Hence, I am doing something like this:
result = transmissions.joins(:report).where('reports.time::timestamp::date = ?', record.time.to_date)
The problem is if the utc date is on a different date than local time, then that record is not included in result. Any ideas how to get the right result?
And apparently I cannot change the "without time zone" either:
Rails database-specific data type
It says:
"concluded that the default ActiveRecord datetime and timestamp column types in schema migrations cannot be modified to force PostgreSQL to use timestamp with time zone."
So I have no idea how to group by date, as obviously something like this is wrong:
Unit.where(id: 1100).first.reports.order("DATE(time)").group("DATE(time)").count
=> {"2013-12-14"=>19, "2013-12-15"=>5}
That return value is completely wrong. All 25 records should be on 2013-12-14 and 0 records on 2013-12-15.
Assuming your records are timestamped with a particular UTC offset, you can try passing in the start and end times of the date in question in UTC format to your query:
result = transmissions.joins(:report).where('reports.time >= ? AND reports.time < ?', record.time.midnight.utc, (record.time.midnight + 1.day).utc)
Explanation:
midnight is a Rails method on an instance of Time that returns the Time object that represents midnight on the date of the original Time object. Similarly, record.time.midnight + 1.day returns the Time object representing midnight of the following day. Then, converting both Time objects – which are presumably timestamped in a standard UTC offset – to UTC creates a time period representing midnight-to-midnight for the system timezone in UTC format (not midnight in UTC time), which is precisely what you're seeking to query.
How about something like result = transmissions.joins(:report).where('reports.time >= ? AND reports.time <= ?', record.time.beginning_of_day.utc, record.time.end_of_day.utc)
The .utc part may not be necessary.

How do you filter records by the time of day in Rails?

I have a bunch of records that have a DateTime stored on them that is only used to pull out the time of day (i.e. 6:00 p.m.) and I'd like to filter them by the time of day. For example, I'd like to get all the records where the time of day is between 10:00 a.m. and 2:00 p.m., regardless of what day that timestamp is for.
Is there a way to do this in Rails? I realize that DateTime might not be the easiest/best way to store this data, so I'm open to changing that. Also, I'm using Postgres.
With Postgres you can use typecasting to convert your datetime/timestamp fields to a time, for example:
select * from posts where created_at::time BETWEEN '10:00:00' and '14:00:00';
So, in Rails, you should be able to do:
Post.where('start_at::time BETWEEN ? AND ?', '10:00:00', '14:00:00')
And Postgres is smart enough to understand AM/PM format also:
Post.where('start_at::time BETWEEN ? AND ?', '10:00:00 AM', '02:00:00 PM')
If you're using the standard timestamps that are generated as part of the Rails model generator, then you're using DateTime for those timestamps.
Assuming that this is the created_at model attribute (it doesn't have to be, change it the value that you're using), here's how to do it:
In raw SQL, the query you're looking for is SELECT * FROM TABLE WHERE created_at BETWEEN FIRST_DATE AND SECOND_DATE; which can be represented in Rails as:
#first_date = DateTime.new(now.year, now.month, now.day, 10, 0, 0, 0)
#second_date = DateTime.new(now.year, now.month, now.day, 14, 0, 0, 0)
Model.where(created_at: #first_date..#second_date)
The .. (which is a sequence operator) tells Rails to build the BETWEEN query using #first_date and #second_date as the FIRST_DATE and SECOND_DATE values in the raw SQL query.

Resources