Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined method `utc' for String #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/que/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def format_error(job)
end

def relative_time(time)
time = Time.utc((t = Date._parse(time))[:year], t[:mon], t[:mday], t[:hour], t[:min], t[:sec]) if time.is_a?(String)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using Time::parse from stdlib for parsing to a time like your original proposal?

# Top of file
require 'time'


def relative_time(time)
  time = Time.parse(time) if time.is_a?(String)
  %{<time class="timeago" datetime="#{time.utc.iso8601}">#{time.utc}</time>}
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time.parse() parses using local time with the result that

time = '2020-09-06 22:17:03'
relative_time(time)

returns <time class="timeago" datetime="2020-09-07T02:17:03Z">2020-09-07 02:17:03 UTC</time>
for my current timezone (EDT).

However, I believe run_at is not meant to be treated as local.

When I add a job with a run_at in utc (example 1.year.from_now.utc => Fri, 29 Oct 2021 20:43:40 UTC +00:00), run_at is returned as "2021-10-29 20:30:01.405839" using Que::Web::SQL[:scheduled_jobs]

When I add a job with run at in local time (example Time.now + 1.year => 2021-10-29 16:44:14 -0400), , run_at is returned as "2021-10-29 20:44:14.053852" again using Que::Web::SQL[:scheduled_jobs]

In my setup of Postgres, pg, Rails and que, I think run_at is returned as a string without timezone and is assumed to be in UTC, not local.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

time = Time.use_zone('UTC') { Time.zone.parse time } if time.is_a?(String)

might be more expressive and clearer.

%{<time class="timeago" datetime="#{time.utc.iso8601}">#{time.utc}</time>}
end

Expand Down
16 changes: 16 additions & 0 deletions spec/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,20 @@ def error_job(last_error)
end
end
end

describe '#relative_time' do
it 'renders a Time object as a <time> HTML element' do
time = Time.now
_(subject.relative_time(time)).must_equal(
%Q(<time class="timeago" datetime="#{time.utc.iso8601}">#{time.utc}</time>)
)
end

it 'renders a String object without timezone in the format "YYYY-MM-DD HH:MM:SS" (e.g. "2020-09-06 22:17:03") as a <time> HTML element' do
time = '2020-09-06 22:17:03'
_(subject.relative_time(time)).must_equal(
%Q(<time class="timeago" datetime="2020-09-06T22:17:03Z">2020-09-06 22:17:03 UTC</time>)
)
end
end
end