Hi,
Last week I was working with MediaWiki ( see [
www.mediawiki.org] ) and this thread reminded me how MediaWiki prompts for the timezone in the user preferences, displaying the server time, the local time and the Offset. The Offset field [ name='wpHourDiff' id='wpHourDiff' ] has the button underneath it to 'Fill in from browser', here is the javascript how it does it through [ onclick='javascript:guessTimezone()' id='guesstimezonebutton' ]. The code is extracted from ../skins/common/wikibits.js:
function unhidetzbutton() {
var tzb = document.getElementById('guesstimezonebutton');
if (tzb) {
tzb.style.display = 'inline';
}
}
// in [-]HH:MM format...
// won't yet work with non-even tzs
function fetchTimezone() {
// FIXME: work around Safari bug
var localclock = new Date();
// returns negative offset from GMT in minutes
var tzRaw = localclock.getTimezoneOffset();
var tzHour = Math.floor( Math.abs(tzRaw) / 60);
var tzMin = Math.abs(tzRaw) % 60;
var tzString = ((tzRaw >= 0) ? "-" : ""
+ ((tzHour < 10) ? "0" : ""
+ tzHour +
":" + ((tzMin < 10) ? "0" : ""
+ tzMin;
return tzString;
}
function guessTimezone(box) {
document.getElementsByName("wpHourDiff"
[0].value = fetchTimezone();
}
Very clever, IMHO
Regards,
toivo
Sydney, Australia