2013-05-14 48 views
1

我正在使用Tweetsharp庫來顯示用戶發佈的最新5條推文。我在dev/twitter上創建了一個應用程序,並獲得了所需的令牌。我想顯示發佈推文後的時間。但是tweetsharp庫中的「tweet.CreatedDate」方法在推文時間之前5-6小時提供。問題獲取推文的創建時間

public DataSet GenerateTweetDataSet() 
    {   
     string time = ""; 
     int hrs = 0, mins = 0; 
     int days = 0; 

     TwitterService service = new TwitterService(ConsumerKey, ConsumerSecret); 
     service.AuthenticateWith(AccessToken, AccessTokenSecret); 

     ListTweetsOnUserTimelineOptions tweetoptions = new ListTweetsOnUserTimelineOptions(); 
     tweetoptions.Count = 5; 
     tweetoptions.ScreenName = _twitter.ScreenName; 

     var tweets = service.ListTweetsOnUserTimeline(tweetoptions); 

     DataTable TweetsTable = new DataTable(); 
     TweetsTable.Columns.Add("Text"); 
     TweetsTable.Columns.Add("DateTime"); 
     TweetsTable.Columns.Add("Id"); 

     if (tweets != null) 
      foreach (var tweet in tweets) 
      { 
       string UserName = tweet.User.ScreenName; 
       string TweetText = tweet.Text; 
       DateTime tweetDate = tweet.CreatedDate; 
       DateTime currentDate = DateTime.Now; 

       time = (currentDate - tweetDate).Days.ToString(); 
       TimeSpan t1 = currentDate.Subtract(tweetDate); 

       mins = (Int32)t1.TotalMinutes; 
       hrs = (Int32)t1.TotalHours; 
       days = (Int32)t1.TotalDays; 

       if (mins < 60) 
       { 
        time = mins + " mins ago"; 
       } 
       else if (mins > 60 && hrs <= 24) 
       { 
        time = hrs + " hours ago"; 
       } 

       else if (hrs > 24 && days < 365) 
       { 
        time = days + " days ago"; 
       } 
       else if (days > 365) 
       { 
        time = "over a year ago"; 
       } 
       long id = tweet.Id; 
       TweetsTable.Rows.Add(TweetText, time, id); 
      } 

     DataSet ds = new DataSet(); 
     ds.Tables.Add(TweetsTable); 

     return ds; 
    }   

有人可以請指出我要去哪裏錯誤或如何解決這個問題。 如果沒有,那麼是否有更好/更簡單的方式來獲取推文?任何示例代碼不使用任何API庫? 任何幫助將不勝感激。

+1

你必須設置時區像**美國/紐約** – 2013-05-15 06:17:54

+0

@DarkWish,請你告訴我如何設置時區? – Namrata 2013-05-15 11:54:30

+0

如果您使用的是PHP,那麼請使用** date_default_timezone_set('America/New_York')**參見[here](http://php.net/manual/en/function.date-default-timezone-set.php) – 2013-05-15 11:59:34

回答

0
Literal litAuthor = (Literal)e.Item.FindControl("litAuthor"); 
    Literal litTime = (Literal)e.Item.FindControl("litTime"); 
    HyperLink btnTwitter = (HyperLink)e.Item.FindControl("btnTwitter"); 

    TwitterStatus twitterStatus = (TwitterStatus)e.Item.DataItem; 

    if (litAuthor != null) 
    { 
     if (twitterStatus != null) 
     { 
      TimeSpan diff = DateTime.Now - twitterStatus.CreatedDate; 

      if (diff.Days > 0) 
      { 
       litTime.Text = twitterStatus.CreatedDate.ToString("dd MMM yyyy"); 
      } 
      else if (diff.Hours < 24) 
      { 
       litTime.Text = diff.Hours.ToString() + "h"; 
      } 
      else 
      { 
       litTime.Text = diff.Minutes.ToString() + "m"; 
      } 

      litAuthor.Text = twitterStatus.Author.ScreenName; 
      btnTwitter.Text = twitterStatus.Text; 
      btnTwitter.NavigateUrl = "http://twitter.com/" + twitterStatus.User.ScreenName + "/statuses/" + twitterStatus.IdStr; 
     } 
    }