summaryrefslogtreecommitdiff
path: root/gitweb/static/js/adjust-timezone.js
blob: 1577d780f0f34a2e910d67bdfd726f32af56d244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (C) 2011, John 'Warthog9' Hawley <warthog9@eaglescrag.net>
//               2011, Jakub Narebski <jnareb@gmail.com>

/**
 * @fileOverview Manipulate dates in gitweb output, adjusting timezone
 * @license GPLv2 or later
 */

/**
 * Get common timezone and adjust dates to use this common timezone.
 *
 * This function is called during onload event (added to window.onload).
 *
 * @param {String} tzDefault: default timezone, if there is no cookie
 * @param {String} tzCookieName: name of cookie to store timezone
 * @param {String} tzClassName: denotes elements with date to be adjusted
 */
function onloadTZSetup(tzDefault, tzCookieName, tzClassName) {
	var tzCookie = getCookie(tzCookieName);
	var tz = tzCookie ? tzCookie : tzDefault;

	// server-side of gitweb produces datetime in UTC,
	// so if tz is 'utc' there is no need for changes
	if (tz !== 'utc') {
		fixDatetimeTZ(tz, tzClassName);
	}
}


/**
 * Replace RFC-2822 dates contained in SPAN elements with tzClassName
 * CSS class with equivalent dates in given timezone.
 *
 * @param {String} tz: numeric timezone in '(-|+)HHMM' format, or 'utc', or 'local'
 * @param {String} tzClassName: specifies elements to be changed
 */
function fixDatetimeTZ(tz, tzClassName) {
	// sanity check, method should be ensured by common-lib.js
	if (!document.getElementsByClassName) {
		return;
	}

	// translate to timezone in '(-|+)HHMM' format
	tz = normalizeTimezoneInfo(tz);

	// NOTE: result of getElementsByClassName should probably be cached
	var classesFound = document.getElementsByClassName(tzClassName, "span");
	for (var i = 0, len = classesFound.length; i < len; i++) {
		var curElement = classesFound[i];

		// we use *.firstChild.data (W3C DOM) instead of *.innerHTML
		// as the latter doesn't always work everywhere in every browser
		var epoch = parseRFC2822Date(curElement.firstChild.data);
		var adjusted = formatDateRFC2882(epoch, tz);

		curElement.firstChild.data = adjusted;
	}
}

/* end of adjust-timezone.js */