The value of the last commit Github

So, I've done a bit of additional research, testing, etc., and got some help from a few people. and iv was able to come up with the following method:

public void GithubLastCommit(string apiLink)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("User-Agent",
                "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

            using (var response = client.GetAsync(apiLink).Result)
            {
                var json = response.Content.ReadAsStringAsync().Result;

                dynamic commits = JArray.Parse(json);
                DateTime lastCommit = commits[0].commit.author.date;
                DateTime now = DateTime.Now;
                int Days = (int)((now - lastCommit).TotalDays);
                int Hours = (int)((now - lastCommit).TotalHours);
                int Minutes = (int)((now - lastCommit).TotalMinutes);
                MessageBox.Show(Hours.ToString());
                if(Hours <=24)
                {
                    MessageBox.Show(Hours.ToString() + "Hours ago");
                }
            }
        }
    }

      

Now the apiLink im submit is a random github I found to check it out at: https://api.github.com/repos/Homebrew/homebrew/commits , however I don't seem to get the correct value (1 hour ago at the moment writing), and no matter what I change the index, it doesn't give me any correct values. What could I be doing wrong?

+3


source to share


1 answer


While there is no standard for JSON dates, most JSON dates are expressed in terms of UTC. The "Z" at the end of your time value, following ISO 8601 , indicates that it is:

    "date": "2015-04-27T03:58:52Z"

      

This is essentially what Json.NET expects by default , although you can configure it differently. Thus, you need to do:

DateTime now = DateTime.UtcNow;

      

And your calculations must be correct. This is because the subtraction operator DateTime

does not consider whether it is DateTime

in local or UTC units
and simply assumes that they are in the same timezone Kind

even if not. More details here and here .

Update



How does github format the "last commit" time? If you look at the HTML source on the homebrew page , you'll see the following:

       <span class="css-truncate css-truncate-target"><time datetime="2015-04-27T15:31:05Z" is="time-ago">Apr 27, 2015</time></span>

      

So what is this "time-ago"

thing? If you look at the javascript for the page, you find this:

n.prototype.timeAgo = function () {
var e = (new Date).getTime() - this.date.getTime(),
t = Math.round(e / 1000),
n = Math.round(t / 60),
r = Math.round(n / 60),
i = Math.round(r / 24),
o = Math.round(i / 30),
a = Math.round(o / 12);
return 0 > e ? 'just now' : 10 > t ? 'just now' : 45 > t ? t + ' seconds ago' : 90 > t ? 'a minute ago' : 45 > n ? n + ' minutes ago' : 90 > n ? 'an hour ago' : 24 > r ? r + ' hours ago' : 36 > r ? 'a day ago' : 30 > i ? i + ' days ago' : 45 > i ? 'a month ago' : 12 > o ? o + ' months ago' : 18 > o ? 'a year ago' : a + ' years ago'
},

      

Thus, if the time difference is more than 36 hours but less than 30 days, they are rounded (possibly up) to the nearest number of days. You will need to duplicate this logic to get a similar result in your code. So 3.5990 days will cost "4 days ago".

I found another version of the same logic here: github / time-elements

+1


source







All Articles