I am using the helpful gem timecop (https://github.com/travisjeffery/timecop) for my tests with rspec and today an old unchanged test is breaking.
I might be mistaken in the way I am using it but using pry I printed the following:
Time.zone.now => Wed, 31 Mar 2021 15:09:45 CEST +02:00
6.months.from_now => Thu, 30 Sep 2021 15:09:56 CEST +02:00
Timecop.travel(6.months.from_now)
Time.zone.now => Thu, 30 Sep 2021 15:10:10 CEST +02:00
6.months.ago => Tue, 30 Mar 2021 15:10:15 CEST +02:00
Thank you in advance for any idea to understand or solve this.
Have a nice day.
Ok it is my mistake, there is no 31st in September so it falls back to 30th and then 6 months ago is 30th of March.
I want to generate all the dates of a particular month starting from the 1st of that month to the 30 or 31st of that month with out using a trivial for loop:
for i in 1..Time.days_in_month(Date.today.month, Date.today.year)
date = Date.new(y, m, i)
end
I was getting dates like this but I'm not supposed to use this trivial for loop.
Although there are quite a few ways you could accomplish this, one way would be to use a Ruby Range combined with the Time#beginning_of_month and Time#end_of_month methods to generate an object that contains all of the days of the month and then convert it into an array:
(Date.today.beginning_of_month..Date.today.end_of_month).to_a
=> [Fri, 01 Nov 2019, Sat, 02 Nov 2019, Sun, 03 Nov 2019, Mon, 04 Nov 2019, Tue, 05 Nov 2019, Wed, 06 Nov 2019, ...., Fri, 29 Nov 2019, Sat, 30 Nov 2019]
Another option would be to use the Time#all_month helper which is as simple as:
Date.today.all_month.to_a
=> [Fri, 01 Nov 2019, Sat, 02 Nov 2019, Sun, 03 Nov 2019, Mon, 04 Nov 2019, Tue, 05 Nov 2019, Wed, 06 Nov 2019, ...., Fri, 29 Nov 2019, Sat, 30 Nov 2019]
(Plain Ruby) The Date class uses negative numbers as a way of counting from the end (like in Arrays):
require "date"
today = Date.today
m, y = today.month, today.year
p (Date.new(y,m,1) .. Date.new(y,m,-1)).to_a
I've been banging my head against this for a while and I can't seem to understand how rails timezones and in_time_zone works.
Here is some rails c output that I'd like to understand:
[26] VMM(bby - main - dev)> Time.zone.now
=> Wed, 14 Mar 2018 09:13:17 CDT -05:00
[27] VMM(bby - main - dev)> MyModel.first.started_at
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
[28] VMM(bby - main - dev)> MyModel.first.started_at.in_time_zone(Time.zone)
=> Fri, 09 Mar 2018 09:17:00 CST -06:00
So:
From the first line, the Time.zone seems to be CDT -5.
From the second line, the started_at attribute seem to be CST -6
On the third line, my intention is to change that atribute to use CDT -5, so I'd expect an output of Fri, 09 Mar 2018 10:17:00 CDT -05:00
Why does this behave as it does instead of how I expect it to?
Thanks in advance!
I have date as "Wed, 29 Jun 2016" and time in "11:35 PM" format
how can i create a date time object with it?
something like what Time.current does.
It's usually pretty simple if your date can be parsed:
DateTime.parse("Wed, 29 Jun 2016 11:35 PM")
# => Wed, 29 Jun 2016 23:35:00 +0000
You can then use that in any capacity you'd use any other date/time.
Why you don't use next:
Time.parse('Wed, 29 Jun 2016 11:35 PM')
=> 2016-06-29 23:35:00 +0300
My db is by default storing times as such:
Object.last.created_at
# => Fri, 03 Jul 2015 23:27:50 UTC +00:00
I looked at the strftime docs and I can build that myself, but it seems there must be an easy way to get a regular Date object to that format? Just wondering if there is...
to_datetime gets really close, but not exactly all the way there.
Date.today.to_datetime
# => Sat, 04 Jul 2015 00:00:00 +0000
Any other ideas?
Try this
Time.zone.now
#=> Sat, 04 Jul 2015 20:32:44 UTC +00:00
UPDATE
DateTime.now.in_time_zone
#=> Sat, 04 Jul 2015 20:43:57 UTC +00:00
Oh silly me... it's just
Date.today.in_time_zone
# => Mon, 06 Jul 2015 00:00:00 UTC +00:00