annotate date.js @ 6:29becdd02dea

Fix server file handling.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 19 Sep 2011 02:13:42 -0700
parents 82905edac9d8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 Date.prototype.set8601 = function (string) {
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 var d = string.match(new RegExp(regexp));
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 var offset = 0;
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 var date = new Date(d[1], 0, 1);
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 if (d[3]) { date.setMonth(d[3] - 1); }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 if (d[5]) { date.setDate(d[5]); }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 if (d[7]) { date.setHours(d[7]); }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 if (d[8]) { date.setMinutes(d[8]); }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 if (d[10]) { date.setSeconds(d[10]); }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 if (d[14]) {
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 offset = (Number(d[16]) * 60) + Number(d[17]);
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 offset *= ((d[15] == '-') ? 1 : -1);
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 offset -= date.getTimezoneOffset();
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 var time = (Number(date) + (offset * 60 * 1000));
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 this.setTime(Number(time));
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 return this;
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 Date.prototype.to8601 = function(){
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 function pad(n){return n<10 ? '0'+n : n}
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 return this.getUTCFullYear()+'-'
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 + pad(this.getUTCMonth()+1)+'-'
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 + pad(this.getUTCDate())+'T'
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 + pad(this.getUTCHours())+':'
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 + pad(this.getUTCMinutes())+':'
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 + pad(this.getUTCSeconds())+'Z';
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 }
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 Date.prototype.getRelative = function(){
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 const s = 1000, m = s*60, h = m*60, d = h*24, y = d*365, M = y/12,
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 ref = Date.now(),
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 input = this.getTime(),
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 delta = ref - input,
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 year = Math.round((delta/y)*10)/10,
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 month = Math.round((delta/M)*10)/10,
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 day = Math.round((delta/d)*10)/10,
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 hour = Math.round((delta/h)*10)/10,
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 minute = Math.round((delta/m)*10)/10;
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 if (year == 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 return "a year ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 if (year > 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 return Math.round(year)+" years ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 if (month == 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 return "a month ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 if (month > 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 return Math.round(month)+" months ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 if (day == 1)
1
82905edac9d8 Fix some things.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 0
diff changeset
60 return "yesterday";
0
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 if (day > 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 return Math.round(day)+" days ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 if (hour == 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 return "an hour ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 if (hour > 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 return Math.round(hour)+" hours ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 if (minute == 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 return "a minute ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 if (minute > 1)
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 return Math.round(minute)+" minutes ago";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 return "just now";
f62b5c395a48 Initial commit.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 };