Why do my temporary unit tests break every day at 16:00?

For a long time I had this problem, which at certain times of the day is interrupted by TON of my tests. I have many tests that make simple date comparisons, and everything works fine from midnight to 4:00 p.m. Any idea why this is happening? I also set my timezone in the environment file.

It seems that some of my calls, such as 5.days.from_now.to_date, add an extra day.

Edit

For example, this test fails:

# Widget that creates items for how many days the trip is gone.
def test_should_create_correct_amount_of_days_for_trip
  w = DayWidget.create(:trip => trips(:hawaii))
  assert_equal w.days.size, 5
end

# Code in trip model that calculates amount of days
def number_of_days
  (self.return_date.to_date - self.depart_date.to_date).to_i + 1
end

# Test fixture yaml for Hawaii
hawaii:
  depart_date: <%= Time.now.tomorrow.to_s(:db) %>
  return_date: <%= 5.days.from_now.to_s(:db) %>

After 4:00 p.m. the test above does not work and says that it created 6 days instead of 5 .: (

+3
source share
3 answers

, , 8 UTC ( 4: 00p , , UTC).

/ , , , / ( UTC UTC localtime to localtime).

: , , Time.now Time, XXX.days.from_now ActiveSupport::TimeWithZone, :

ruby-1.9.2-p136 :009 > (Time.now+5.days).to_s(:db)
 => "2011-02-08 19:40:24" 
ruby-1.9.2-p136 :010 > 5.days.from_now.to_s(:db)
 => "2011-02-09 03:40:29" 

, .utc :

ruby-1.9.2-p136 :017 > 5.days.from_now.utc.to_s(:db)
 => "2011-02-09 03:42:39" 
ruby-1.9.2-p136 :018 > (Time.now+5.days).utc.to_s(:db)
 => "2011-02-09 03:42:39" 

1.day.from_now Time.now.tomorrow, .

+8

Time.current Time.now.

Time.current ActiveSupport::TimeWithZone , #ago #from_now, , .

+1

If you really only deal with dates, do not forget to set the hours, minutes and seconds to 0, otherwise you are at the mercy of the time of day when you create the dates.

0
source

Source: https://habr.com/ru/post/1789903/


All Articles