~ubuntu-branches/ubuntu/trusty/ntop/trusty

« back to all changes in this revision

Viewing changes to html/MochiKit/DateTime.js

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2008-06-15 14:38:28 UTC
  • mfrom: (2.1.11 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080615143828-oalh84nda2hje4do
Tags: 3:3.3-11
Correction of Polish translation encoding, closes: #479490. Thanks
to Christian Perrier <bubulle@debian.org> for the help.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***
 
2
 
 
3
MochiKit.DateTime 1.3.1
 
4
 
 
5
See <http://mochikit.com/> for documentation, downloads, license, etc.
 
6
 
 
7
(c) 2005 Bob Ippolito.  All rights Reserved.
 
8
 
 
9
***/
 
10
 
 
11
if (typeof(dojo) != 'undefined') {
 
12
    dojo.provide('MochiKit.DateTime');
 
13
}
 
14
 
 
15
if (typeof(MochiKit) == 'undefined') {
 
16
    MochiKit = {};
 
17
}
 
18
       
 
19
if (typeof(MochiKit.DateTime) == 'undefined') {
 
20
    MochiKit.DateTime = {};
 
21
}
 
22
 
 
23
MochiKit.DateTime.NAME = "MochiKit.DateTime";
 
24
MochiKit.DateTime.VERSION = "1.3.1";
 
25
MochiKit.DateTime.__repr__ = function () {
 
26
    return "[" + this.NAME + " " + this.VERSION + "]";
 
27
};
 
28
MochiKit.DateTime.toString = function () {
 
29
    return this.__repr__();
 
30
};
 
31
 
 
32
MochiKit.DateTime.isoDate = function (str) {
 
33
    str = str + "";
 
34
    if (typeof(str) != "string" || str.length === 0) {
 
35
        return null;
 
36
    }
 
37
    var iso = str.split('-');
 
38
    if (iso.length === 0) {
 
39
        return null;
 
40
    }
 
41
    return new Date(iso[0], iso[1] - 1, iso[2]);
 
42
};
 
43
 
 
44
MochiKit.DateTime._isoRegexp = /(\d{4,})(?:-(\d{1,2})(?:-(\d{1,2})(?:[T ](\d{1,2}):(\d{1,2})(?::(\d{1,2})(?:\.(\d+))?)?(?:(Z)|([+-])(\d{1,2})(?::(\d{1,2}))?)?)?)?)?/;
 
45
 
 
46
MochiKit.DateTime.isoTimestamp = function (str) {
 
47
    str = str + "";
 
48
    if (typeof(str) != "string" || str.length === 0) {
 
49
        return null;
 
50
    }
 
51
    var res = str.match(MochiKit.DateTime._isoRegexp);
 
52
    if (typeof(res) == "undefined" || res === null) {
 
53
        return null;
 
54
    }
 
55
    var year, month, day, hour, min, sec, msec;
 
56
    year = parseInt(res[1], 10);
 
57
    if (typeof(res[2]) == "undefined" || res[2] === '') {
 
58
        return new Date(year);
 
59
    }
 
60
    month = parseInt(res[2], 10) - 1;
 
61
    day = parseInt(res[3], 10);
 
62
    if (typeof(res[4]) == "undefined" || res[4] === '') {
 
63
        return new Date(year, month, day);
 
64
    }
 
65
    hour = parseInt(res[4], 10);
 
66
    min = parseInt(res[5], 10);
 
67
    sec = (typeof(res[6]) != "undefined" && res[6] !== '') ? parseInt(res[6], 10) : 0;
 
68
    if (typeof(res[7]) != "undefined" && res[7] !== '') {
 
69
        msec = Math.round(1000.0 * parseFloat("0." + res[7]));
 
70
    } else {
 
71
        msec = 0;
 
72
    }
 
73
    if ((typeof(res[8]) == "undefined" || res[8] === '') && (typeof(res[9]) == "undefined" || res[9] === '')) {
 
74
        return new Date(year, month, day, hour, min, sec, msec);
 
75
    }
 
76
    var ofs;
 
77
    if (typeof(res[9]) != "undefined" && res[9] !== '') {
 
78
        ofs = parseInt(res[10], 10) * 3600000;
 
79
        if (typeof(res[11]) != "undefined" && res[11] !== '') {
 
80
            ofs += parseInt(res[11], 10) * 60000;
 
81
        }
 
82
        if (res[9] == "-") {
 
83
            ofs = -ofs;
 
84
        }
 
85
    } else {
 
86
        ofs = 0;
 
87
    }
 
88
    return new Date(Date.UTC(year, month, day, hour, min, sec, msec) - ofs);
 
89
};
 
90
 
 
91
MochiKit.DateTime.toISOTime = function (date, realISO/* = false */) {
 
92
    if (typeof(date) == "undefined" || date === null) {
 
93
        return null;
 
94
    }
 
95
    var hh = date.getHours();
 
96
    var mm = date.getMinutes();
 
97
    var ss = date.getSeconds();
 
98
    var lst = [
 
99
        ((realISO && (hh < 10)) ? "0" + hh : hh),
 
100
        ((mm < 10) ? "0" + mm : mm),
 
101
        ((ss < 10) ? "0" + ss : ss)
 
102
    ];
 
103
    return lst.join(":");
 
104
};
 
105
 
 
106
MochiKit.DateTime.toISOTimestamp = function (date, realISO/* = false*/) {
 
107
    if (typeof(date) == "undefined" || date === null) {
 
108
        return null;
 
109
    }
 
110
    var sep = realISO ? "T" : " ";
 
111
    var foot = realISO ? "Z" : "";
 
112
    if (realISO) {
 
113
        date = new Date(date.getTime() + (date.getTimezoneOffset() * 60000));
 
114
    }
 
115
    return MochiKit.DateTime.toISODate(date) + sep + MochiKit.DateTime.toISOTime(date, realISO) + foot;
 
116
};
 
117
 
 
118
MochiKit.DateTime.toISODate = function (date) {
 
119
    if (typeof(date) == "undefined" || date === null) {
 
120
        return null;
 
121
    }
 
122
    var _padTwo = MochiKit.DateTime._padTwo;
 
123
    return [
 
124
        date.getFullYear(),
 
125
        _padTwo(date.getMonth() + 1),
 
126
        _padTwo(date.getDate())
 
127
    ].join("-");
 
128
};
 
129
 
 
130
MochiKit.DateTime.americanDate = function (d) {
 
131
    d = d + "";
 
132
    if (typeof(d) != "string" || d.length === 0) {
 
133
        return null;
 
134
    }
 
135
    var a = d.split('/');
 
136
    return new Date(a[2], a[0] - 1, a[1]);
 
137
};
 
138
 
 
139
MochiKit.DateTime._padTwo = function (n) {
 
140
    return (n > 9) ? n : "0" + n;
 
141
};
 
142
 
 
143
MochiKit.DateTime.toPaddedAmericanDate = function (d) {
 
144
    if (typeof(d) == "undefined" || d === null) {
 
145
        return null;
 
146
    }
 
147
    var _padTwo = MochiKit.DateTime._padTwo;
 
148
    return [
 
149
        _padTwo(d.getMonth() + 1),
 
150
        _padTwo(d.getDate()),
 
151
        d.getFullYear()
 
152
    ].join('/');
 
153
};
 
154
 
 
155
MochiKit.DateTime.toAmericanDate = function (d) {
 
156
    if (typeof(d) == "undefined" || d === null) {
 
157
        return null;
 
158
    }
 
159
    return [d.getMonth() + 1, d.getDate(), d.getFullYear()].join('/');
 
160
};
 
161
 
 
162
MochiKit.DateTime.EXPORT = [
 
163
    "isoDate",
 
164
    "isoTimestamp",
 
165
    "toISOTime",
 
166
    "toISOTimestamp",
 
167
    "toISODate",
 
168
    "americanDate",
 
169
    "toPaddedAmericanDate",
 
170
    "toAmericanDate"
 
171
];
 
172
 
 
173
MochiKit.DateTime.EXPORT_OK = [];
 
174
MochiKit.DateTime.EXPORT_TAGS = {
 
175
    ":common": MochiKit.DateTime.EXPORT,
 
176
    ":all": MochiKit.DateTime.EXPORT
 
177
};
 
178
 
 
179
MochiKit.DateTime.__new__ = function () {
 
180
    // MochiKit.Base.nameFunctions(this);
 
181
    var base = this.NAME + ".";
 
182
    for (var k in this) {
 
183
        var o = this[k];
 
184
        if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
 
185
            try {
 
186
                o.NAME = base + k;
 
187
            } catch (e) {
 
188
                // pass
 
189
            }
 
190
        }   
 
191
    }
 
192
};
 
193
 
 
194
MochiKit.DateTime.__new__();
 
195
 
 
196
if (typeof(MochiKit.Base) != "undefined") {
 
197
    MochiKit.Base._exportSymbols(this, MochiKit.DateTime);
 
198
} else {
 
199
    (function (globals, module) {
 
200
        if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
 
201
            || (typeof(MochiKit.__compat__) == 'boolean' && MochiKit.__compat__)) {
 
202
            var all = module.EXPORT_TAGS[":all"];
 
203
            for (var i = 0; i < all.length; i++) {
 
204
                globals[all[i]] = module[all[i]]; 
 
205
            }
 
206
        }   
 
207
    })(this, MochiKit.DateTime);  
 
208
}