]> Lady’s Gitweb - Gitweb/commitdiff
gitweb: JavaScript ability to adjust time based on timezone
authorJohn 'Warthog9' Hawley <redacted>
Thu, 28 Apr 2011 19:04:09 +0000 (21:04 +0200)
committerLady <redacted>
Mon, 6 Apr 2026 04:51:31 +0000 (00:51 -0400)
This patch is based on Kevin Cernekee's <redacted>
patch series entitled "gitweb: introduce localtime feature".  While
Kevin's patch changed the server side output so that the timezone
was output from gitweb itself, this has a number of drawbacks, in
particular with respect to gitweb-caching.

This patch takes the same basic goal, display the appropriate times in
a given common timezone, and implements it in JavaScript.  This
requires adding / using a new class, "datetime", to be able to find
elements to be adjusted from JavaScript.  Appropriate dates are
wrapped in a span with this class.

Timezone to be used can be retrieved from "gitweb_tz" cookie, though
currently there is no way to set / manipulate this cookie from gitweb;
this is left for later commit.

Valid timezones, currently, are: "utc", "local" (which means that
timezone is taken from browser), and "+/-ZZZZ" numeric timezone as in
RFC-2822.  Default timezone is "local" (currently not configurable,
left for later commit).

Fallback (should JavaScript not be enabled) is to treat dates as they
have been and display them, only, in UTC.

Pages affected:
* 'summary' view, "last change" field (commit time from latest change)
* 'log' view, author time
* 'commit' and 'commitdiff' views, author/committer time
* 'tag' view, tagger time

Based-on-code-from: Kevin Cernekee <redacted>
Signed-off-by: John 'Warthog9' Hawley <redacted>
Signed-off-by: Jakub Narebski <redacted>
Signed-off-by: Junio C Hamano <redacted>
Makefile
gitweb.perl
static/js/adjust-timezone.js [new file with mode: 0644]
static/js/lib/datetime.js

index 8d84f4973bfb6ea91779d7900ccd6bed9d132776030b65deeb8f963ea52e788d..2236555d2a3483490a258afaf5d94f7395d2a8999dc8bcd93c0a49c529d2b5c2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -120,6 +120,7 @@ GITWEB_JSLIB_FILES += static/js/lib/common-lib.js
 GITWEB_JSLIB_FILES += static/js/lib/datetime.js
 GITWEB_JSLIB_FILES += static/js/lib/cookies.js
 GITWEB_JSLIB_FILES += static/js/javascript-detection.js
+GITWEB_JSLIB_FILES += static/js/adjust-timezone.js
 GITWEB_JSLIB_FILES += static/js/blame_incremental.js
 
 
index ca3d1a95c570b1a381a6c65e246da9d5e94156c40fc471771d8e422c80c3986f..ddf8ad212a98d2704ccb7858978c5217c83ebc91f7704da79c6d5fe635a011fa 100755 (executable)
@@ -3886,9 +3886,14 @@ sub git_footer_html {
                      qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.
                      qq!           "!. href() .qq!");\n!.
                      qq!</script>\n!;
-       } elsif (gitweb_check_feature('javascript-actions')) {
+       } else {
                print qq!<script type="text/javascript">\n!.
-                     qq!window.onload = fixLinks;\n!.
+                     qq!window.onload = function () {\n!.
+                     (gitweb_check_feature('javascript-actions') ?
+                     qq!       fixLinks();\n! : '').
+                     # last parameter to onloadTZSetup must be CSS class used by format_timestamp_html
+                     qq!       onloadTZSetup('local', 'gitweb_tz', 'datetime');\n!.
+                     qq!};\n!.
                      qq!</script>\n!;
        }
 
@@ -4094,7 +4099,7 @@ sub git_print_section {
 
 sub format_timestamp_html {
        my $date = shift;
-       my $strtime = $date->{'rfc2822'};
+       my $strtime = '<span class="datetime">'.$date->{'rfc2822'}.'</span>';
 
        my $localtime_format = '(%02d:%02d %s)';
        if ($date->{'hour_local'} < 6) {
diff --git a/static/js/adjust-timezone.js b/static/js/adjust-timezone.js
new file mode 100644 (file)
index 0000000..5b80fa6
--- /dev/null
@@ -0,0 +1,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 */
index 0d1ee872922b04119a5207d2f663a55438dd246cc31641dd7f3f1b90b74c761b..e1a304eb57ff34ab8a1f53b682b59a5d55b63cdd4555e516ea287388ecdc8df2 100644 (file)
@@ -104,6 +104,21 @@ function formatTimezoneInfo(hours, minutes, sep) {
        return tzSign + padLeft(hours, 2, '0') + sep + padLeft(minutes, 2, '0');
 }
 
+/**
+ * translate 'utc' and 'local' to numerical timezone
+ * @param {String} timezoneInfo: might be 'utc' or 'local' (browser)
+ */
+function normalizeTimezoneInfo(timezoneInfo) {
+       switch (timezoneInfo) {
+       case 'utc':
+               return '+0000';
+       case 'local': // 'local' is browser timezone
+               return localTimezoneInfo();
+       }
+       return timezoneInfo;
+}
+
+
 /**
  * return date in local time formatted in iso-8601 like format
  * 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
This page took 0.357903 seconds and 4 git commands to generate.