1

I have the following code, using the twitter gem in ruby on rails. It does not return the correct time for tweets created, although I have set my time zone in application.rb and have also set it here. It returns three hours ahead of the time the actual tweet was created.

      <% TwitterFeed.userTweets.each do |tweet| %>
      <blockquote>
        <%= parsed_tweet tweet %>
        <% time_est = tweet.created_at.in_time_zone('Eastern Time (US & Canada)')%>
        <br><small><%= (time_est.strftime("%m/%d/%y %I:%M%p %z"))  %></small> 
      </blockquote>
      <% end %>

I have the following code in my application_helper.rb to parse the links from the tweet object that is returned

module ApplicationHelper
  def parsed_tweet tweet
    _parsed_tweet = tweet.full_text.dup

    tweet.urls.each do |entity|
      html_link = link_to(entity.display_url.to_s, entity.expanded_url.to_s, target: '_blank')
      _parsed_tweet.sub!(entity.url.to_s, html_link)
    end

    tweet.media.each do |entity|
      html_link = link_to(entity.display_url.to_s, entity.expanded_url.to_s, target: '_blank')
      _parsed_tweet.sub!(entity.url.to_s, html_link)
    end

    _parsed_tweet.html_safe
  end
end

I have the following code in my twitter_feed.rb file where I keep all my functions.

  def self.userTweets
    client.user_timeline("twitter", count: 5, exclude_replies: true, include_rts: false)
  end

I am using version 6.2.0 of the twitter gem.

Frank Doe
  • 192
  • 1
  • 3
  • 17
  • please include the server log – Fabrizio Bertoglio Nov 18 '17 at 18:27
  • It's possible that the time is being converted from twitter as if it were UTC. Try retrieving the time from twitter as UTC and see if that resolves the issue. Although you are specifying the time zone to twitter, rails has no way to make the relation, and just assumes the default, and tries to convert it. – DivXZero Nov 18 '17 at 18:56
  • @DivXZero Changing to UTC also does not show the correct time. – Frank Doe Nov 18 '17 at 20:10
  • [mcve], please. I don't know how to reproduce your problem, given the limited information provided. – Tom Lord Nov 19 '17 at 00:06
  • For example, what version of the gem are you using? [This change](https://github.com/sferik/twitter/commit/42e00f001f16f47e6b5e1c05e466ba1b3eb603ec) in 2014 made all times return in UTC rather than the local timezone. – Tom Lord Nov 19 '17 at 00:08
  • @TomLord I am using twitter (6.2.0) – Frank Doe Nov 19 '17 at 01:34

1 Answers1

0

Let's cut this down to a minimal (!!) example, since this issue is in fact totally unrelated to Twitter:

time_utc = Time.utc(2017, 'Nov', 19, 13, 30, 0)
#=> 2017-11-19 13:30:00 UTC

time_est = time_utc.in_time_zone('Eastern Time (US & Canada)')
#=> Sat, 19 Nov 2017 08:30:00 EST -05:00

time_est.strftime("%m/%d/%y %I:%M%p")
#=> "11/19/17 08:30AM"

time_est.strftime("%m/%d/%y %I:%M%p %z")
#=> "11/19/17 08:30AM -0500"

You are using strftime without displaying the time zone information.

So since you converted the created_at time to a different timezone, you may be displaying it falsely.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • For future reference on StackOverflow: I asked for a [mcve] for a very important reason - you never actually showed what `tweet` was, so I cannot actually *reproduce* your problem. The above answer is only a best guess. – Tom Lord Nov 19 '17 at 13:23
  • Returning the time zone showed my correct time zone, "-0500". So I believed it was indeed a Twitter problem. `tweet` is a tweet object returned by the API. – Frank Doe Nov 20 '17 at 00:34
  • @FrankDoe As I showed above, this has got nothing to do with Twitter. You were converting a UTC time into a different zone, and then displaying it *without* the timezone information - so of course it's going to be a different hour. – Tom Lord Nov 20 '17 at 09:18
  • It's obvious that `tweet` is going to be some such object, but you did not show *which* tweet it was, or what code you used to retrieve it. Your example was *incomplete* and *non-verifiable*. – Tom Lord Nov 20 '17 at 09:19
  • I am sorry if I offended you in any way. I am certainly not the youngest in the bunch, just trying to get a grasp on things. I am also fairly new to stack overflow. I have updated the question with all of the code that I use to pull tweets from a user. – Frank Doe Nov 20 '17 at 19:50