fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-12-17 08:34 am

[dw-free] investigate datetime manipulation in talkread vs. talkpost

[commit: http://hg.dwscoalition.org/dw-free/rev/4182da2bdc12]

http://bugs.dwscoalition.org/show_bug.cgi?id=3314

Refactor out into a function and use it where needed, so that our date
format is used consistently throughout the site.

Patch by [personal profile] kareila.

Files modified:
  • cgi-bin/LJ/S2.pm
  • cgi-bin/LJ/Talk.pm
  • htdocs/talkpost.bml
  • htdocs/talkread.bml
--------------------------------------------------------------------------------
diff -r e303c8fa89e6 -r 4182da2bdc12 cgi-bin/LJ/S2.pm
--- a/cgi-bin/LJ/S2.pm	Fri Dec 17 15:32:49 2010 +0800
+++ b/cgi-bin/LJ/S2.pm	Fri Dec 17 16:34:34 2010 +0800
@@ -2421,6 +2421,33 @@ sub nth_entry_seen {
     return $LJ::REQ_GLOBAL{'nth_entry_keys'}->{$key} = ++$LJ::REQ_GLOBAL{'nth_entry_ct'};
 }
 
+sub sitescheme_secs_to_iso {
+    my ( $secs, %opts ) = @_;
+    my $remote = LJ::get_remote();
+    my @ret;
+
+    # time format (12/24 hr)
+    my $fmt_time = "%%hh%%:%%min%% %%a%%m";  # 12-hr default
+    $fmt_time = "%%HH%%:%%min%%" if $remote && $remote->use_24hour_time;
+
+    # convert date to S2 object
+    my $s2_ctx = [];  # fake S2 context object
+    # if opts has a true tz key, get the remote user's timezone if possible
+    my $s2_datetime = $opts{tz} ? DateTime_tz( $secs, $remote ) : undef;
+    my $has_tz = defined $s2_datetime ? "(local)" : "UTC";
+    # if timezone execution failed, use GMT
+    $s2_datetime = DateTime_unix( $secs ) unless defined $s2_datetime;
+
+    my @s2_args = ( $s2_ctx, $s2_datetime );
+
+    # reformat date and time display for user
+    push @ret, S2::Builtin::LJ::Date__date_format( @s2_args, "iso" );
+    push @ret, S2::Builtin::LJ::DateTime__time_format( @s2_args, $fmt_time );
+    push @ret, $has_tz;
+
+    return join ' ', @ret;
+}
+
 # adectomy
 sub current_box_type {}
 sub curr_page_supports_ebox { 0 }
diff -r e303c8fa89e6 -r 4182da2bdc12 cgi-bin/LJ/Talk.pm
--- a/cgi-bin/LJ/Talk.pm	Fri Dec 17 15:32:49 2010 +0800
+++ b/cgi-bin/LJ/Talk.pm	Fri Dec 17 16:34:34 2010 +0800
@@ -14,14 +14,16 @@ package LJ::Talk;
 package LJ::Talk;
 use strict;
 
+use MIME::Words;
+use Carp qw(croak);
+
 use LJ::Constants;
 use LJ::Event::JournalNewComment;
 use LJ::Comment;
 use LJ::EventLogRecord::NewComment;
 use LJ::OpenID;
-use MIME::Words;
+use LJ::S2;
 use DW::Captcha;
-use Carp qw(croak);
 
 # dataversion for rate limit logging
 our $RATE_DATAVER = "1";
@@ -3863,7 +3865,8 @@ sub make_preview {
             } else {
                 $ret .= $ju->ljuser_display;
             }
-            $ret .= " @ " . $entry->eventtime_mysql;
+            my $etime = LJ::mysqldate_to_time( $entry->eventtime_mysql );
+            $ret .= " @ " . LJ::S2::sitescheme_secs_to_iso( $etime );
 
             $ret .= " p?>\n";
         }
diff -r e303c8fa89e6 -r 4182da2bdc12 htdocs/talkpost.bml
--- a/htdocs/talkpost.bml	Fri Dec 17 15:32:49 2010 +0800
+++ b/htdocs/talkpost.bml	Fri Dec 17 16:34:34 2010 +0800
@@ -263,7 +263,9 @@ body<=
     }
 
     unless ($init->{'replyto'}) {
-        $ret .= LJ::Talk::format_eventtime( $item->{eventtime}, $u );
+        my $etime = LJ::mysqldate_to_time( $item->{eventtime} );
+        $etime = LJ::S2::sitescheme_secs_to_iso( $etime );
+        $ret .= LJ::Talk::format_eventtime( $etime, $u );
     }
 
     $ret .= "</td></tr></table>";
diff -r e303c8fa89e6 -r 4182da2bdc12 htdocs/talkread.bml
--- a/htdocs/talkread.bml	Fri Dec 17 15:32:49 2010 +0800
+++ b/htdocs/talkread.bml	Fri Dec 17 16:34:34 2010 +0800
@@ -152,13 +152,6 @@ body<=
     # get currently logged in user
     my $remote = LJ::get_remote();
 
-    my $tz_remote;
-    my $s2_ctx = [];  # fake S2 context object
-    if ($remote) {
-        my $tz = $remote->prop("timezone");
-        $tz_remote = $tz ? eval { DateTime::TimeZone->new( name => $tz); } : undef;
-    }
-
     #
     # Lots of logic to determine permissions for what can be viewed
     #
@@ -262,18 +255,8 @@ body<=
                                                 'userlink' => LJ::ljuser($up) });
     }
 
-    my $fmt_time_short;
-    if ( $remote && $remote->use_24hour_time ) {
-        $fmt_time_short = "%%HH%%:%%min%%";
-    } else {
-        $fmt_time_short = "%%hh%%:%%min%% %%a%%m";
-    }
-
-    my $etime = $item->{eventtime};
-    $etime = LJ::mysqldate_to_time( $etime );
-    $etime = LJ::S2::DateTime_unix( $etime );
-    $etime = S2::Builtin::LJ::Date__date_format( $s2_ctx, $etime, "iso" ) .
-            " " . S2::Builtin::LJ::DateTime__time_format( $s2_ctx, $etime, $fmt_time_short );
+    my $etime = LJ::mysqldate_to_time( $item->{eventtime} );
+    $etime = LJ::S2::sitescheme_secs_to_iso( $etime );
     $ret .= LJ::Talk::format_eventtime( $etime, $u );
     $ret .= "</td></tr></table>";
 
@@ -404,13 +387,7 @@ body<=
         my $dtid = $tid * 256 + $init->{'anum'};
         my $LJci = $LJ_cmtinfo{$dtid} = { rc => [], u => '', full => $post->{_loaded} };
 
-        my $s2_datetime = $tz_remote ?
-            LJ::S2::DateTime_tz($post->{'datepost_unix'}, $tz_remote) :
-            LJ::S2::DateTime_unix($post->{'datepost_unix'});
-
-        my $datepost = S2::Builtin::LJ::Date__date_format($s2_ctx, $s2_datetime, "iso") .
-            " " . S2::Builtin::LJ::DateTime__time_format($s2_ctx, $s2_datetime, $fmt_time_short) .
-            ($tz_remote ? " (local)" : " UTC");
+        my $datepost = LJ::S2::sitescheme_secs_to_iso( $post->{datepost_unix}, tz => 1 );
 
         my $level = ($opts->{'depth'} % 2) ? "even" : "odd";
         my $bgcolor = ($opts->{'depth'} % 2) ? "emcolorlite" : "emcolor";
@@ -472,14 +449,7 @@ body<=
                 my $edittime;
                 my $editreason = "";
                 if ($comment->is_edited) {
-                    my $s2_datetime_edittime = $tz_remote ?
-                        LJ::S2::DateTime_tz($comment->edit_time, $tz_remote) :
-                        LJ::S2::DateTime_unix($comment->edit_time);
-
-                    $edittime = S2::Builtin::LJ::Date__date_format($s2_ctx, $s2_datetime_edittime, "iso") .
-                        " " . S2::Builtin::LJ::DateTime__time_format($s2_ctx, $s2_datetime_edittime, $fmt_time_short) .
-                        ($tz_remote ? " (local)" : " UTC");
-
+                    $edittime = LJ::S2::sitescheme_secs_to_iso( $comment->edit_time, tz => 1 );
                     $editreason = LJ::ehtml( $comment->edit_reason );
                     $editreason = "($editreason)" if $editreason;
                 }
--------------------------------------------------------------------------------