[Home]WikiPatches/JavaScriptTimezoneHandling

UseModWiki | WikiPatches | RecentChanges | Preferences

Instead of cgi-script wiki.pl, display localtime using JavaScript? so that the user doesn't have to set the timezone or care about summar time at all.

The mechanism is simple. The server responds with UTC/GMT time as UNIX 32bit time_t (how many seconds after 1970/1/1). Then the JavaScript embedded in the HTML page displaies localized time. Actually the patch is pretty safe. Though it changes part related to time, it doesn't affect any pages stored in DataDir.

Note:

I found this patch cool even though depriving some freedome about timezone from the user.

This patch includes /TwentyFourHourFormat patch as well.

--- wiki_92.pl	2002-12-14 20:58:56.000000000 -0600
+++ wiki_timezone.cgi	2002-12-21 00:06:03.000000000 -0600
@@ -38,7 +38,7 @@
   $UseSubpage $UseCache $RawHtml $SimpleLinks $NonEnglish $LogoLeft
   $KeepDays $HtmlTags $HtmlLinks $UseDiffLog $KeepMajor $KeepAuthor
   $FreeUpper $EmailNotify $SendMail $EmailFrom $FastGlob $EmbedWiki
-  $ScriptTZ $BracketText $UseAmPm $UseConfig $UseIndex $UseLookup
+  $ScriptTZ $BracketText $UseConfig $UseIndex $UseLookup
   $RedirType $AdminPass $EditPass $UseHeadings $NetworkFile $BracketWiki
   $FreeLinks $WikiLinks $AdminDelete $FreeLinkPattern $RCName $RunCGI
   $ShowEdits $ThinLine $LinkPattern $InterLinkPattern $InterSitePattern
@@ -111,7 +111,6 @@
 $NonEnglish  = 0;       # 1 = extra link chars,   0 = only A-Za-z chars
 $ThinLine    = 0;       # 1 = fancy <hr> tags,    0 = classic wiki <hr>
 $BracketText = 1;       # 1 = allow [URL text],   0 = no link descriptions
-$UseAmPm     = 1;       # 1 = use am/pm in times, 0 = use 24-hour times
 $UseIndex    = 0;       # 1 = use index file,     0 = slow/reliable method
 $UseHeadings = 1;       # 1 = allow = h1 text =,  0 = no header formatting
 $NetworkFile = 1;       # 1 = allow remote file:, 0 = no file:// links
@@ -647,8 +646,8 @@
     next  if ((!$all) && ($ts < $changetime{$pagename}));
     next  if (($idOnly ne "") && ($idOnly ne $pagename));
     %extra = split(/$FS2/, $extraTemp, -1);
-    if ($date ne &CalcDay($ts)) {
-      $date = &CalcDay($ts);
+    if ($date ne &CalcServerDay($ts)) {
+      $date = &CalcServerDay($ts);
       if ($inlist) {
         $html .= "</UL>\n";
         $inlist = 0;
@@ -991,6 +990,39 @@
   return $q->header();
 }
 
+sub GetJavaScript {
+  my ($t);
+
+  $t = '<script><!--
+function dt(t){var d = new Date(t*1000);
+document.write(["January","February","March","April","May","June","July",
+"August","September","October","November","December"][d.getMonth()]
++" "+d.getDate()+", "+d.getYear());}
+function tm(t){
+var d = new Date(t*1000);var h = d.getHours();var m = d.getMinutes();
+if(m<10) m = "0"+m;
+';
+
+if (&GetParam('24hour', 0)) {
+  $t .= 'document.write(h+":"+m);}';
+}
+else {
+  $t .= 'var ampm = " am";
+if (h > 11) {
+  if (h == 12) h = 0; else h = h - 12;
+  ampm = " pm";
+}
+document.write(h+":"+m+ampm);}';
+}
+
+  $t .= '
+function dtm(t){dt(t);document.write(" ");tm(t);}
+// -->
+</script>';
+
+  return $t;
+}
+
 sub GetHtmlHeader {
   my ($title) = @_;
   my ($dtd, $bgcolor, $html, $bodyExtra);
@@ -1008,6 +1040,7 @@
     $html .= qq(<LINK REL="stylesheet" HREF="$StyleSheet">\n);
   }
   # Insert other header stuff here (like inline style sheets?)
+  $html .= &GetJavaScript;
   $bodyExtra = '';
   if ($bgcolor ne '') {
     $bodyExtra = qq( BGCOLOR="$bgcolor");
@@ -2329,7 +2362,7 @@
   return @IndexList;
 }
 
-sub CalcDay {
+sub CalcServerDay {
   my ($ts) = @_;
 
   $ts += $TimeZoneOffset;
@@ -2340,11 +2373,7 @@
           "December")[$mon]. " " . $mday . ", " . ($year+1900);
 }
 
-sub CalcDayNow {
-  return CalcDay($Now);
-}
-
-sub CalcTime {
+sub CalcServerTime {
   my ($ts) = @_;
   my ($ampm, $mytz);
 
@@ -2356,7 +2385,7 @@
     $mytz = " " . $ScriptTZ;
   }
   $ampm = "";
-  if ($UseAmPm) {
+  if (!&GetParam('24hour', 0)) {
     $ampm = " am";
     if ($hour > 11) {
       $ampm = " pm";
@@ -2368,10 +2397,25 @@
   return $hour . ":" . $min . $ampm . $mytz;
 }
 
-sub TimeToText {
-  my ($t) = @_;
+sub CalcDay {
+  my ($ts) = @_;
+  return "<script><!--\ndt($ts);\n//--></script><noscript>" . &CalcServerDay($ts) . '</noscript>';
+}
 
-  return &CalcDay($t) . " " . &CalcTime($t);
+sub CalcTime {
+  my ($ts) = @_;
+  return "<script><!--\ntm($ts);\n//--></script><noscript>" . &CalcServerTime($ts) . '</noscript>';
+}
+
+sub TimeToText {
+  my ($ts) = @_;
+  return "<script><!--\ndtm($ts);\n//--></script><noscript>" . &CalcServerDay($ts) .
+         ' ' . &CalcServerTime($ts) . '</noscript>';
+}
+
+sub TimeToTextWithoutScript {
+  my ($ts) = @_;
+  return &CalcServerDay($ts) . ' ' . &CalcServerTime($ts);
 }
 
 sub GetParam {
@@ -2718,10 +2762,12 @@
                          -default=>&GetParam("defaultdiff", 1));
   }
   print '<hr><b>', T('Misc:'), "</b>\n";
-  # Note: TZ offset is added by TimeToText, so pre-subtract to cancel.
-  print '<br>', T('Server time:'), ' ', &TimeToText($Now-$TimeZoneOffset);
+  # Note: TZ offset is added by TimeToText, so pre-subtract to cancel.
+  print '<br><noscript>';
+  print T('Server time:'), ' ', &TimeToTextWithoutScript($Now-$TimeZoneOffset);
   print '<br>', T('Time Zone offset (hours):'), ' ',
-        &GetFormText('tzoffset', 0, 4, 9);
+        &GetFormText('tzoffset', 0, 4, 9),
+	'</noscript>', &GetFormCheck('24hour', 0, T('24 hour time display'));
   print '<br>', &GetFormCheck('editwide', 1,
                               T('Use 100% wide edit area (if supported)'));
   print '<br>',
@@ -2836,11 +2882,14 @@
   }
   &UpdatePrefNumber("rcshowedit", 1, 0, 2);
   &UpdatePrefNumber("tzoffset", 0, -999, 999);
+  &UpdatePrefCheckbox('24hour');
   &UpdatePrefNumber("editrows", 1, 1, 999);
-  &UpdatePrefNumber("editcols", 1, 1, 999);
-  print T('Server time:'), ' ', &TimeToText($Now-$TimeZoneOffset), '<br>';
+  &UpdatePrefNumber("editcols", 1, 1, 999);
+  print '<noscript>';
+  print T('Server time:'), ' ', &TimeToTextWithoutScript($Now-$TimeZoneOffset), '<br>';
   $TimeZoneOffset = &GetParam("tzoffset", 0) * (60 * 60);
-  print T('Local time:'), ' ', &TimeToText($Now), '<br>';
+  print T('Local time:'), ' ', &TimeToTextWithoutScript($Now), '<br>';
+  print '</noscript>';
 
   &SaveUserData();
   print '<b>', T('Preferences saved.'), '</b>';

--TakuyaMurata


UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited December 21, 2002 4:18 pm by TakuyaMurata (diff)
Search: