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

unix timestamp support #136

Closed
wants to merge 1 commit into from
Closed

unix timestamp support #136

wants to merge 1 commit into from

Conversation

goker-dev
Copy link

I added unix timestamp support:
Ex: 1372218564

I added unix timestamp support:
Ex: <abbr class="timeago" title="1372218564">1372218564</abbr>
@rmm5t
Copy link
Owner

rmm5t commented Jun 26, 2013

Thanks for the effort on this, but this violates the HTML5 time tag. Timestamps should be in ISO8601 format. Same for the time microformat. Plus, this PR is missing tests.

@rmm5t rmm5t closed this Jun 26, 2013
@goker-dev
Copy link
Author

Yes, you are right. But, I can use the plugin with any tag.

<span class="timeago" title="1372218564">1372218564</span>

I'm working with foursquare api and it send me a timestamp. I mean, it can be more useful if it can handle timestamp.

@rmm5t
Copy link
Owner

rmm5t commented Jun 26, 2013

Sure, but timeago can handle Date objects too. If you're already looking at unix timestamps like this in Javascript, you can just assign the Date value to the element itself. No need to use the title attribute.

Here's a quick example. http://jsfiddle.net/Sq5gJ/

@goker-dev
Copy link
Author

Thanks for explain but I think it's a little bit lame solution. I just wanted to use a plugin.
The below is a native example which can handle all of them, if I write JS code

http://jsfiddle.net/goker/3J9Dz/

(function timeAgo(selector) {

    var templates = {
        prefix: "",
        pastSuffix: " ago",
        featureSuffix: " later",
        seconds: "less than a minute",
        minute: "about a minute",
        minutes: "%d minutes",
        hour: "about an hour",
        hours: "about %d hours",
        day: "a day",
        days: "%d days",
        month: "about a month",
        months: "%d months",
        year: "about a year",
        years: "%d years"
    };
    var template = function(t, n) {
        return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n)));
    };

    var timer = function(time) {
        if (!time)
            return;
        time = time.replace(/\.\d+/, ""); // remove milliseconds
        time = time.replace(/-/, "/").replace(/-/, "/");
        time = time.replace(/T/, " ").replace(/Z/, " UTC");
        time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
        time = new Date(time * 1000 || time);

        var now = new Date();
        var seconds = ((now.getTime() - time) * .001) >> 0;
        var minutes = seconds / 60;
        var hours = minutes / 60;
        var days = hours / 24;
        var years = days / 365;

        return templates.prefix + (
                seconds < 45 && template('seconds', seconds) ||
                seconds < 90 && template('minute', 1) ||
                minutes < 45 && template('minutes', minutes) ||
                minutes < 90 && template('hour', 1) ||
                hours < 24 && template('hours', hours) ||
                hours < 42 && template('day', 1) ||
                days < 30 && template('days', days) ||
                days < 45 && template('month', 1) ||
                days < 365 && template('months', days / 30) ||
                years < 1.5 && template('year', 1) ||
                template('years', years)
                ) + templates.suffix;
    };

    var elements = document.getElementsByClassName('timeago');
    for (var i in elements) {
        var $this = elements[i];
        if (typeof $this === 'object') {
            $this.innerText = timer($this.getAttribute('title') || $this.getAttribute('datetime'));
        }
    }
    // update time every minute
    setTimeout(timeAgo, 60000);

})();

Thanks for interesting.
Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants