This fixes failing tests when the timezone is different than utc

There were several problems:
* Time.now returns the systems time, not the users time
* fixtures do not translate dates from timezone to utc, but stores the
  date verbatim
* calling a controller will set the timezone to the preference of the
  current_user. So it could be changed while you do not realize this. I
  fixed the failing test, but problems could be elsewhere
This commit is contained in:
Reinier Balt 2015-08-04 23:08:13 +02:00
parent 0b44fe3f08
commit e58379e81f
27 changed files with 221 additions and 214 deletions

View file

@ -151,7 +151,7 @@ module RecurringTodos
end
def get_next_date(previous)
raise "Should not call AbstractRecurrencePattern.get_next_date directly. Overwrite in subclass"
raise "Should not call AbstractRecurrencePattern.get_next_date directly. Override in subclass"
end
def continues_recurring?(previous)
@ -174,13 +174,13 @@ module RecurringTodos
# same day as the previous
def determine_start(previous, offset=0.day)
start = self.start_from || NullTime.new
now = Time.zone.now
if previous
# check if the start_from date is later than previous. If so, use
# start_from as start to search for next date
start > previous ? start : previous + offset
else
# skip to present
now = Time.zone.now
start > now ? start : now
end
end
@ -199,32 +199,24 @@ module RecurringTodos
end
def find_last_day_x_of_month(weekday, month, year)
# count backwards. use UTC to avoid strange timezone oddities
# where last_day -= 1.day seems to shift tz+0100 to tz+0000
last_day = Time.utc(year, month, Time.days_in_month(month))
last_day = Time.zone.local(year, month, Time.days_in_month(month))
while last_day.wday != weekday
last_day -= 1.day
end
# convert back to local timezone
Time.zone.local(last_day.year, last_day.month, last_day.day)
last_day
end
def find_xth_day_of_month(x, weekday, month, year)
# 1-4th -> count upwards last -> count backwards. use UTC to avoid strange
# timezone oddities where last_day -= 1.day seems to shift tz+0100 to
# tz+0000
start = Time.utc(year,month,1)
start = Time.zone.local(year,month,1)
n = x
while n > 0
while start.wday() != weekday
start+= 1.day
start += 1.day
end
n -= 1
start+= 1.day unless n==0
start += 1.day unless n==0
end
# convert back to local timezone
Time.zone.local(start.year, start.month, start.day)
start
end
end
end

View file

@ -82,18 +82,9 @@ module RecurringTodos
def find_specific_day_of_month(previous, start, n)
if (previous && start.mday >= every_x_day) || (previous.nil? && start.mday > every_x_day)
# there is no next day n in this month, search in next month
#
# start += n.months
#
# The above seems to not work. Fiddle with timezone. Looks like we hit a
# bug in rails here where 2008-12-01 +0100 plus 1.month becomes
# 2008-12-31 +0100. For now, just calculate in UTC and convert back to
# local timezone.
#
# TODO: recheck if future rails versions have this problem too
start = Time.utc(start.year, start.month, start.day)+n.months
start += n.months
end
Time.zone.local(start.year, start.month, every_x_day)
start.in_time_zone.change(day: every_x_day)
end
def find_relative_day_of_month(start, n)
@ -101,13 +92,7 @@ module RecurringTodos
if the_next.nil? || the_next <= start
# the nth day is already passed in this month, go to next month and try
# again
# fiddle with timezone. Looks like we hit a bug in rails here where
# 2008-12-01 +0100 plus 1.month becomes 2008-12-31 +0100. For now, just
# calculate in UTC and convert back to local timezone.
# TODO: recheck if future rails versions have this problem too
the_next = Time.utc(the_next.year, the_next.month, the_next.day)+n.months
the_next = Time.zone.local(the_next.year, the_next.month, the_next.day)
the_next += n.months
# TODO: if there is still no match, start will be set to nil. if we ever
# support 5th day of the month, we need to handle this case
@ -136,7 +121,5 @@ module RecurringTodos
day: day_of_week_as_text(day_of_week),
n_months: n_months)
end
end
end

View file

@ -19,7 +19,7 @@ module Stats
end
def first_action_at
first_action.created_at if first_action
first_action.try(:created_at)
end
def projects