~mrqtros/ubuntu-rssreader-app/ubuntu-rssreader-app-to-tabs

« back to all changes in this revision

Viewing changes to shorts/qml/utils/dateutils.js

  • Committer: Roman Shchekin
  • Date: 2015-07-04 08:38:18 UTC
  • Revision ID: mrqtros@gmail.com-20150704083818-ecojm3nmy5bpkxrb
Merge with the Reboot project.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This file is part of ubuntu-rssreader-app.
 
5
 *
 
6
 * ubuntu-rssreader-app is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * ubuntu-rssreader-app is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
.pragma library
 
20
 
 
21
var MILLIS_IN_SECOND = 1000
 
22
var SECONDS_LIMIT = 60 * MILLIS_IN_SECOND; // used as reference
 
23
var MINUTES_LIMIT = 60 * SECONDS_LIMIT;    // print minutes up to 1h
 
24
var HOURS_LIMIT = 12 * MINUTES_LIMIT;      // print hours up to 12h
 
25
var DAY_LIMIT = 24 * MINUTES_LIMIT;        // print '<val> days ago' up to 30 days back
 
26
var DAYS_LIMIT = 30 * DAY_LIMIT;           // print '<val> days ago' up to 30 days back
 
27
 
 
28
// Convert date string to a number of seconds since 1970-01-01
 
29
function parseDate(dateAsStr) {
 
30
    return new Date(dateAsStr).getTime()/MILLIS_IN_SECOND;
 
31
}
 
32
 
 
33
// date is a number of seconds since  1970-01-01
 
34
function formatRelativeTime(i18n, date) {
 
35
    // fallback if none of the other formatters matched
 
36
    function defaultFallbackFormat(then) {
 
37
        // TRANSLATORS: this is a time formatting string,
 
38
        // see http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-date.html#details for valid expressions
 
39
        return Qt.formatDateTime(then, i18n.tr("MMMM d"))
 
40
    }
 
41
 
 
42
    // Simple matches all diffs < limit, formats using the format function.
 
43
    function SimpleFormatter(limit, format) {
 
44
        this.matches = function (now, then, diff) { return diff < limit }
 
45
        this.format = format
 
46
    }
 
47
 
 
48
    // Matches yesterday date
 
49
    function YesterdayFormatter() {
 
50
        this.matches = function (now, then, diff) {
 
51
            return diff < DAY_LIMIT && now.getDate() !== then.getDate();
 
52
        }
 
53
        this.format = function (now, then, diff) {
 
54
            return i18n.tr("Yesterday at %1").arg(
 
55
                        /* TRANSLATORS: this is a time formatting string,
 
56
                           see http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-date.html#details for valid expressions */
 
57
                        Qt.formatDateTime(then, i18n.tr("h:mm AP")))
 
58
        }
 
59
    }
 
60
 
 
61
    // Matches up to 7 days ago (formats date as a weekday + time)
 
62
    function WeekFormatter() {
 
63
        this.matches = function (now, then, diff) {
 
64
            return diff < 7 * DAY_LIMIT
 
65
        }
 
66
        this.format = function (now, then, diff) {
 
67
            // TRANSLATORS: this is a time formatting string,
 
68
            // see http://qt-project.org/doc/qt-5.0/qtqml/qml-qtquick2-date.html#details for valid expressions
 
69
            return Qt.formatDateTime(then, i18n.tr("ddd, h:mm AP"));
 
70
        }
 
71
    }
 
72
 
 
73
    // An array of formatting object processed from 0 up to a matching object.
 
74
    // If none of the object matches a default fallback formatter will be used.
 
75
    var FORMATTERS = [
 
76
                new SimpleFormatter(SECONDS_LIMIT, function (now, then, diff) { return i18n.tr("A few seconds ago...") }),
 
77
                new SimpleFormatter(MINUTES_LIMIT, function (now, then, diff) {
 
78
                    var val = Math.floor(diff / SECONDS_LIMIT)
 
79
                    return i18n.tr("%1 minute ago", "%1 minutes ago", val).arg(val)
 
80
                }),
 
81
                new SimpleFormatter(HOURS_LIMIT, function (now, then, diff) {
 
82
                    var val = Math.floor(diff / MINUTES_LIMIT)
 
83
                    return i18n.tr("%1 hour ago", "%1 hours ago", val).arg(val)
 
84
                }),
 
85
                new YesterdayFormatter(),
 
86
                new WeekFormatter(),
 
87
                new SimpleFormatter(DAYS_LIMIT, function (now, then, diff) {
 
88
                    var val = Math.floor(diff / DAY_LIMIT)
 
89
                    return i18n.tr("%1 day ago", "%1 days ago", val).arg(val)
 
90
                })
 
91
            ]
 
92
 
 
93
    function formatDiff(now, then, diff) {
 
94
        for (var i=0; i<FORMATTERS.length; ++i) {
 
95
            var formatter = FORMATTERS[i]
 
96
            if (formatter.matches(now, then, diff)) {
 
97
                return formatter.format(now, then, diff)
 
98
            }
 
99
        }
 
100
        return defaultFallbackFormat(then)
 
101
    }
 
102
 
 
103
    var now = new Date();
 
104
    var then = new Date(date*MILLIS_IN_SECOND);
 
105
    var diff = now - then;
 
106
    var formattedDiff = formatDiff(now, then, diff);
 
107
    return formattedDiff;
 
108
}