I need to get records created 30 minutes ago from Time.now. I'm using a cron and I want to fire the cron once ever minute.
I essentially need this except it should ignore the seconds so that if Time.now == Wed, 31 Jan 2018 18:00:31 +0000 then a record with created_at = "Wed, 31 Jan 2018 17:30:23 +0000 should match.
Here is the query I have so far which doesn't work because the time is improperly evaluated.
Cart.joins(:cart_addresses).group('carts.id').where(created_at: Time.now - 30.minutes).each do |i|
puts i.id
end
Please try below query:
# if current time is 3:00:03
# it will return carts created at 2:30:00 -> 2:30:59
from_mins_ago = 30.minutes.ago.change(sec: 0)
to_mins_ago = from_mins_ago.change(sec: 59)
Cart.joins(:cart_addresses).group('carts.id').where(created_at: from_mins_ago..to_min_ago).pluck('carts.id')
You could use a range.
Cart.joins(:cart_addresses).group('carts.id').where(created_at: 30.minutes.ago..DateTime.now)
There's a lot of solutions.
More: Rails ActiveRecord date between
Related
Im still kinda new.
I saved an date from json data, i saved it in my database as an string. Here by my quistion:
Does anybody have a good idea of how i can calculate how many minutes,hours or days AGO the date is?
"2019-12-09T11:06:37.000Z"
This is what normal dates look like in my database: 2019-12-10 18:28:38.249866
The easiest way if you want to avoid doing a bunch of math is by using ActiveSupport::Duration.
irb(main):001:0> time = Time.zone.parse("2019-12-09T11:06:37.000Z")
=> Mon, 09 Dec 2019 11:06:37 UTC +00:00
irb(main):002:0> duration = ActiveSupport::Duration.build(Time.zone.now - time)
=> 1 day, 11 hours, 10 minutes, and 31.248518 seconds
irb(main):003:0> duration.parts
=> {:days=>1, :hours=>11, :minutes=>10, :seconds=>31.248518}
The last developer is using time in our app like this.
:timestamp_requested => Time.now.utc
I want to check if :timestamp_requested is between 4:15PM and 6:00PM CST.
I saw another post that uses in_time_zone but not sure how to check for between two times?
t = foo.start_time
#⇒ 2000-01-01 14:20:00 UTC
t.zone
#⇒ "UTC"
t.in_time_zone("America/Chicago")
#⇒ Sat, 01 Jan 2000 09:20:00 EST -05:00
You might want to try the use_zone method
Time.use_zone("America/Chicago") { (16..18).cover?(time.hour) && time.min >= 15 }
The easiest way to check the time is in the specific range would be to compare hours and minutes:
cst = time.in_time_zone("America/Guatemala") # CST whole year
(16..18).cover?(cst.hour) && cst.min >= 15
I'm looking to return the number of weeks since a user was created in my app..
My model is User.rb (id, created_at)
Given user.created_at:
2.4.0 :008 > user.created_at
=> Mon, 14 Aug 2017 15:51:23 UTC +00:00
How can I do something like: user.created_at.weeks_ago where it returns some integer which represents the number of weeks since the user was created?
If you just want to build a human-friendly string, I believe time_ago_in_words is what you are after.
https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
Otherwise, I would resort to something like the following:
(Time.new - user.created_at) / 1.week
If you want a more elegant solution, Subtract dates in Ruby and get the difference in minutes mentions the Time Difference gem for Ruby.
e.g.
TimeDifference.between(user.created_at.to_time, Time.now).in_weeks
I would use:
Time.current
=> Wed, 20 Sep 2017 03:56:15 UTC +00:00
Then
((Time.current - person.created_at)/604800).to_i
the subtraction gives you the number of seconds then divide it by 604800 which is the number of seconds in a week.
I am using DateTime.now and Time.now methods. And I store it in some variables. Now I want this time in minutes. Meaning, instead of hours and minutes I want to get time in minutes only.
2.2.2 :014 > datetime = DateTime.now
=> Fri, 11 Sep 2015 12:13:00 +0530
2.2.2 :015 > time = Time.now
=> 2015-09-11 12:13:06 +0530
2.2.2 :016 >
Now i want to calculate this time entirely in minutes. is there any method like to_minutes like below?
datetime_in_min = datetime.to_minutes
time_in_min = time.to_minutes
This must have already been done by now but just for anyone who's still looking for an answer, try doing something like below
lets assume the we wish to fetch the minutes and the hours from something like
s = Sat, 27 May 2017 02:30:00 UTC +00:00 (date time)
then,
hours = s.strftime("%H")
minutes = s.strftime("%M")
total_minutes((hours.to_i * 60) + minutes)
hence you'll get something like 150
Hope this helps.
You can use the following method from Time:
Time.now.to_i
to get number of seconds since the Epoch (January 1, 1970 00:00 UTC).
The strftime method can get you any piece of the time you want. The time in minutes (I'm assuming relative to the beginning of the day) you need to do some math:
hours = datetime.strftime('%k').to_i
hours_in_minutes = hours * 60
minutes = datetime.strftime('%M').to_i
minutes_since_start_of_day = hours_in_minutes + minutes
Same thing works for time.
Here is what I am trying to achieve - I have set up a scheduler to execute midnight of every friday, which collects the data from a service for the start date of last friday at 00:00:00 hrs and end time of last thursday at 23:59:59 hrs. Since it has to work every friday, I cannot hard code the dates so I thought of trying out DateTime.
So as per my requirement, if I am running the job on this Friday midnight i.e at "2014-12-12T03:00:00Z", then my start date should be "2014-12-05T00:00:00Z" and my end date should be "2014-12-11T23:59:59Z".
So to get start and end dates, I am trying to subtract days out of my now object. This is what I tried:
now = DateTime.now
p now.new_offset(0).to_s
startDate = now - 7
p startDate.new_offset(0).to_s
endDate = now - 1
p endDate.new_offset(0).to_s
This gives me the right date, but the time is wrong i.e. instead of start date with 00:00:00 and end date with 23:59:59 this would be start date with 03:00:00 and end date with 03:00:00.
How do I modify the DateTime object to get the start date with time at beginning of the day and end date with time at end of the day?
Sorry I am very bad in dealing with dates. Thanks in advance!!
You can use he beginning_of_day and end_of_day methods
1.9.3-p448 :001 > DateTime.now.beginning_of_day
=> Tue, 09 Dec 2014 00:00:00 +0300
1.9.3-p448 :002 > DateTime.now.end_of_day
=> Tue, 09 Dec 2014 23:59:59 +0300
I think what you are trying to do is easier done with the Date class :
require 'date'
start_date = (Date.today - 7).to_time
end_date = Date.today.to_time - 1
Instead of doing this manually, I will suggest a gem called Whenever: https://github.com/javan/whenever
It's a simple DSL for Ruby cron jobs.
Also remember that DateTime has beginning_of_day and end_of_day methods.