How should I work with dates? - ruby-on-rails

its(:register_token_created_at){ should eq Time.zone.now }
results in this failure:
expected: Fri, 28 Mar 2014 13:31:53 UTC +00:00
got: Fri, 28 Mar 2014 13:31:53 UTC +00:00
(compared using ==)
Diff:
The Diff is even completely empty! Don't really know what I can do here...

Use the timecop gem.
Check out the railscast about this issue.

Related

Ruby gem timecop wrong delay between two dates after travel

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.

skip created_at field from to rspec response

Could you please help me,
When i run rspec, i got this response.body
"{:id=>1, :email=>\"email1#example.com\", :live=>true, :is_confirmed=>false, :is_pro=>false, :user_profile=>nil, :user_settings=>nil, :subscription_end_at=>nil, :is_trialing=>nil, :created_at=>Mon, 19 Mar 2018 16:45:25 UTC +00:00, :updated_at=>Mon, 19 Mar 2018 16:45:25 UTC +00:00}"
But I want this
"{:id=>1, :email=>\"email1#example.com\", :live=>true, :is_confirmed=>false, :is_pro=>false, :user_profile=>nil, :user_settings=>nil, :subscription_end_at=>nil, :is_trialing=>nil, :created_at=>"Mon, 19 Mar 2018 16:45:25 UTC +00:00", :updated_at=>"Mon, 19 Mar 2018 16:45:25 UTC +00:00"}"
I just want to removed created_at and updated_at attribute from the response because it's give the datetime object instead of string

Rails - Change TimeWithZone Timezone

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!

Ruby how to get a date to the default format as it's stored in the db

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

Iterating between two DateTimes, with a one hour step

I'm looking for an efficient way, in Ruby 1.9.x/Rails 3.2.x, to iterate between two DateTime objects, with a one-hour step.
('2013-01-01'.to_datetime .. '2013-02-01'.to_datetime).step(1.hour) do |date|
...
end
I understand that an issue with this is that 1.hour is just the number of seconds, but my attempts to convert that to a DateTime object and use that as the step doesn't work either.
I looked at "Beware of Ruby Sugar". It mentions, near the bottom, that DateTime has a direct step method. I confirmed this by running methods on a DateTime object, but I cannot find any documentation on step in DateTime, in either Ruby's or Rails' documents.
Similar to my answer in "How do I return an array of days and hours from a range?", the trick is to use to_i to work with seconds since the epoch:
('2013-01-01'.to_datetime.to_i .. '2013-02-01'.to_datetime.to_i).step(1.hour) do |date|
puts Time.at(date)
end
Note that Time.at() converts using your local time zone, so you may want to specify UTC by using Time.at(date).utc
Maybe late but, you can do it without Rails, for example to step with hours:
Ruby 2.1.0
require 'time'
hour_step = (1.to_f/24)
date_time = DateTime.new(2015,4,1,00,00)
date_time_limit = DateTime.new(2015,4,1,6,00)
date_time.step(date_time_limit,hour_step).each{|e| puts e}
2015-04-01T00:00:00+00:00
2015-04-01T01:00:00+00:00
2015-04-01T02:00:00+00:00
2015-04-01T03:00:00+00:00
2015-04-01T04:00:00+00:00
2015-04-01T05:00:00+00:00
2015-04-01T06:00:00+00:00
Or minutes:
#one_minute_step = (1.to_f/24/60)
fifteen_minutes_step = (1.to_f/24/4)
date_time = DateTime.new(2015,4,1,00,00)
date_time_limit = DateTime.new(2015,4,1,00,59)
date_time.step(date_time_limit,fifteen_minutes_step).each{|e| puts e}
2015-04-01T00:00:00+00:00
2015-04-01T00:15:00+00:00
2015-04-01T00:30:00+00:00
2015-04-01T00:45:00+00:00
I hope it helps.
Here's something I came up with recently:
require 'active_support/all'
def enumerate_hours(start, end_)
Enumerator.new { |y| loop { y.yield start; start += 1.hour } }.take_while { |d| d < end_ }
end
enumerate_hours(DateTime.now.utc, DateTime.now.utc + 1.day)
# returns [Wed, 20 Aug 2014 21:40:46 +0000, Wed, 20 Aug 2014 22:40:46 +0000, Wed, 20 Aug 2014 23:40:46 +0000, Thu, 21 Aug 2014 00:40:46 +0000, Thu, 21 Aug 2014 01:40:46 +0000, Thu, 21 Aug 2014 02:40:46 +0000, Thu, 21 Aug 2014 03:40:46 +0000, Thu, 21 Aug 2014 04:40:46 +0000, Thu, 21 Aug 2014 05:40:46 +0000, Thu, 21 Aug 2014 06:40:46 +0000, Thu, 21 Aug 2014 07:40:46 +0000, Thu, 21 Aug 2014 08:40:46 +0000, Thu, 21 Aug 2014 09:40:46 +0000, Thu, 21 Aug 2014 10:40:46 +0000, Thu, 21 Aug 2014 11:40:46 +0000, Thu, 21 Aug 2014 12:40:46 +0000, Thu, 21 Aug 2014 13:40:46 +0000, Thu, 21 Aug 2014 14:40:46 +0000, Thu, 21 Aug 2014 15:40:46 +0000, Thu, 21 Aug 2014 16:40:46 +0000, Thu, 21 Aug 2014 17:40:46 +0000, Thu, 21 Aug 2014 18:40:46 +0000, Thu, 21 Aug 2014 19:40:46 +0000, Thu, 21 Aug 2014 20:40:46 +0000, Thu, 21 Aug 2014 21:40:46 +0000]
I'm not entirely sure if this will help out but check out this stack overflow page, question seems similar.
calculate difference in time in days, hours and minutes

Resources