获取推文创建时间的问题

时间:2013-05-14 13:30:03

标签: twitter tweetsharp

我使用Tweetsharp库显示用户发布的最新5条推文。我在dev / twitter上创建了一个应用程序,并获得了所需的令牌。我想显示自推文发布以来已过去的时间。但是" tweet.CreatedDate"来自Tweetsharp库的方法提供了在推文之前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 个答案:

答案 0 :(得分: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;
        }
    }