PageSourceSearch

https://events.wesleyan.edu/livewhale/scripts/lib/date/formatter.js

js wesleyan.edu collected 2026-10-01 08:46:06 UTC 5,684 bytes, 194 lines download raw bytes

1/*globals livewhale: true */
2
3if (typeof livewhale === 'undefined') {
4	window.livewhale = {};
5}
6if (!livewhale.lib) livewhale.lib = {};
7
8(function() {
9	var $ = livewhale.jQuery || jQuery;
10
11	// Formats dates using PHP date format strings
12	// IX adapted code from http://phpjs.org/functions/date/
13	var Formatter = function(date_obj) {
14		this._dt = date_obj;
15	};
16
17	Formatter.prototype = {
18		format: function(format) {
19			return format.replace(this._formatChr, $.proxy(this._formatChrCb, this));
20		},
21		_txt_words: [
22			"Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
23			"January", "February", "March", "April", "May", "June", "July",
24			"August", "September", "October", "November", "December"
25		],
26		// trailing backslash -> (dropped)
27		// a backslash followed by any character (including backslash) -> the character
28		// empty string -> empty string
29		_formatChr: /\\?(.?)/gi,
30		_formatChrCb: function(t, s) {
31			return this[t] ? this[t]() : s;
32		},
33		_pad: function(n, c) {
34			n = String(n);
35			while (n.length < c) {
36				n = '0' + n;
37			}
38			return n;
39		},
40
41		// Day
42		d: function() { // Day of month w/leading 0; 01..31
43			return this._pad(this.j(), 2);
44		},
45		D: function() { // Shorthand day name; Mon...Sun
46			return this.l().slice(0, 3);
47		},
48		j: function() { // Day of month; 1..31
49			return this._dt.getDate();
50		},
51		l: function() { // Full day name; Monday...Sunday
52			return this._txt_words[this.w()] + 'day';
53		},
54		N: function() { // ISO-8601 day of week; 1[Mon]..7[Sun]
55			return this.w() || 7;
56		},
57		S: function() { // Ordinal suffix for day of month; st, nd, rd, th
58			var j = this.j(),
59				i = j % 10;
60			if (i <= 3 && parseInt((j % 100) / 10, 10) === 1) {
61				i = 0;
62			}
63			return ['st', 'nd', 'rd'][i - 1] || 'th';
64		},
65		w: function() { // Day of week; 0[Sun]..6[Sat]
66			return this._dt.getDay();
67		},
68		z: function() { // Day of year; 0..365
69			var a = new Date(this.Y(), this.n() - 1, this.j()),
70				b = new Date(this.Y(), 0, 1);
71			return Math.round((a - b) / 864e5);
72		},
73
74		// Week
75		W: function() { // ISO-8601 week number
76			var a = new Date(this.Y(), this.n() - 1, this.j() - this.N() + 3),
77					b = new Date(a.getFullYear(), 0, 4);
78			return this._pad(1 + Math.round((a - b) / 864e5 / 7), 2);
79		},
80
81		// Month
82		F: function() { // Full month name; January...December
83			return this._txt_words[6 + this.n()];
84		},
85		m: function() { // Month w/leading 0; 01...12
86			return this._pad(this.n(), 2);
87		},
88		M: function() { // Shorthand month name; Jan...Dec
89			return this.F().slice(0, 3);
90		},
91		n: function() { // Month; 1...12
92			return this._dt.getMonth() + 1;
93		},
94		t: function() { // Days in month; 28...31
95			return (new Date(this.Y(), this.n(), 0)).getDate();
96		},
97
98		// Year
99		L: function() { // Is leap year?; 0 or 1
100			var j = this.Y();
101			return j % 4 === 0 & j % 100 !== 0 | j % 400 === 0;
102		},
103		o: function() { // ISO-8601 year
104			var n = this.n(),
105				W = this.W(),
106				Y = this.Y();
107			return Y + (n === 12 && W < 9 ? 1 : n === 1 && W > 9 ? -1 : 0);
108		},
109		Y: function() { // Full year; e.g. 1980...2010
110			return this._dt.getFullYear();
111		},
112		y: function() { // Last two digits of year; 00...99
113			return this.Y().toString().slice(-2);
114		},
115
116		// Time
117		a: function() { // am or pm
118			return this._dt.getHours() > 11 ? "pm" : "am";
119		},
120		A: function() { // AM or PM
121			return this.a().toUpperCase();
122		},
123		B: function() { // Swatch Internet time; 000..999
124			// not supported
125			return '';
126		},
127		g: function() { // 12-Hours; 1..12
128			return this.G() % 12 || 12;
129		},
130		G: function() { // 24-Hours; 0..23
131			return this._dt.getHours();
132		},
133		h: function() { // 12-Hours w/leading 0; 01..12
134			return this._pad(this.g(), 2);
135		},
136		H: function() { // 24-Hours w/leading 0; 00..23
137			return this._pad(this.G(), 2);
138		},
139		i: function() { // Minutes w/leading 0; 00..59
140			return this._pad(this._dt.getMinutes(), 2);
141		},
142		s: function() { // Seconds w/leading 0; 00..59
143			return this._pad(this._dt.getSeconds(), 2);
144		},
145		u: function() { // Microseconds; 000000-999000
146			return this._pad(this._dt.getMilliseconds() * 1000, 6);
147		},
148
149		// Timezone
150		e: function() { // Timezone identifier; e.g. Atlantic/Azores, ...
151			// The following works, but requires inclusion of the very large
152			// timezone_abbreviations_list() function.
153			/*							return that.date_default_timezone_get();
154			 */
155			throw 'Not supported (see source code of date() for timezone on how to add support)';
156		},
157		I: function() { // DST observed?; 0 or 1
158			// Compares Jan 1 minus Jan 1 UTC to Jul 1 minus Jul 1 UTC.
159			// If they are not equal, then DST is observed.
160			var a = new Date(this.Y(), 0),
161				// Jan 1
162				c = Date.UTC(this.Y(), 0),
163				// Jan 1 UTC
164				b = new Date(this.Y(), 6),
165				// Jul 1
166				d = Date.UTC(this.Y(), 6); // Jul 1 UTC
167			return ((a - c) !== (b - d)) ? 1 : 0;
168		},
169		O: function() { // Difference to GMT in hour format; e.g. +0200
170			var tzo = this._dt.getTimezoneOffset(),
171				a = Math.abs(tzo);
172			return (tzo > 0 ? "-" : "+") + this._pad(Math.floor(a / 60) * 100 + a % 60, 4);
173		},
174		P: function() { // Difference to GMT w/colon; e.g. +02:00
175			var O = this.O();
176			return (O.substr(0, 3) + ":" + O.substr(3, 2));
177		},
178		Z: function() { // Timezone offset in seconds (-43200...50400)
179			return -this._dt.getTimezoneOffset() * 60;
180		},
181
182		// Full Date/Time
183		c: function() { // ISO-8601 date.
184			return 'Y-m-d\\TH:i:sP'.replace(this._formatChr, this._formatChrCb);
185		},
186		r: function() { // RFC 2822
187			return 'D, d M Y H:i:s O'.replace(this._formatChr, this._formatChrCb);
188		},
189		U: function() { // Seconds since UNIX epoch
190			return this._dt._date / 1000 | 0;
191		}
192	};
193	livewhale.lib.DateFormatter = Formatter;
194}());

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.