[dw-free] better organization of LJ functions
[commit: http://hg.dwscoalition.org/dw-free/rev/6ebda1baf1b5]
http://bugs.dwscoalition.org/show_bug.cgi?id=3965
Move LJ::ago_text and LJ::diff_ago_text from ljtimeutil.pl to LJ/Lang.pm.
Should now be referred to as LJ::Lang::ago_text, LJ::Lang::diff_ago_text,
but aliasing means old method still works. Inline statushistory_time.
Patch by
kareila.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3965
Move LJ::ago_text and LJ::diff_ago_text from ljtimeutil.pl to LJ/Lang.pm.
Should now be referred to as LJ::Lang::ago_text, LJ::Lang::diff_ago_text,
but aliasing means old method still works. Inline statushistory_time.
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- cgi-bin/LJ/Lang.pm
- cgi-bin/ljtimeutil.pl
- htdocs/admin/statushistory.bml
-------------------------------------------------------------------------------- diff -r 341b82ccc061 -r 6ebda1baf1b5 cgi-bin/LJ/Lang.pm --- a/cgi-bin/LJ/Lang.pm Fri Nov 18 15:33:19 2011 +0800 +++ b/cgi-bin/LJ/Lang.pm Fri Nov 18 16:17:10 2011 +0800 @@ -90,6 +90,46 @@ return ""; } +# args: secondsold - The number of seconds ago something happened. +# returns: approximate English time span - "2 weeks", "20 hours", etc. + +sub ago_text { + my $secondsold = $_[0] || 0; + return LJ::Lang::ml( 'time.ago.never' ) unless $secondsold > 0; + + my $num; + if ( $secondsold >= 60*60*24*7 ) { + $num = int( $secondsold / (60*60*24*7) ); + return LJ::Lang::ml( 'time.ago.week', { num => $num } ); + } elsif ( $secondsold >= 60*60*24 ) { + $num = int( $secondsold / (60*60*24) ); + return LJ::Lang::ml( 'time.ago.day', { num => $num } ); + } elsif ( $secondsold >= 60*60 ) { + $num = int( $secondsold / (60*60) ); + return LJ::Lang::ml( 'time.ago.hour', { num => $num } ); + } elsif ( $secondsold >= 60 ) { + $num = int( $secondsold / 60 ); + return LJ::Lang::ml( 'time.ago.minute', { num => $num } ); + } else { + $num = $secondsold; + return LJ::Lang::ml( 'time.ago.second', { num => $num } ); + } +} +*LJ::ago_text = \&ago_text; + +# args: time in seconds of last activity; "current" time in seconds. +# returns: result of ago_text for the difference. + +sub diff_ago_text { + my ( $last, $time ) = @_; + return ago_text( 0 ) unless $last; + $time = time() unless defined $time; + + my $diff = ( $time - $last ) || 1; + return ago_text( $diff ); +} +*LJ::diff_ago_text = \&diff_ago_text; + #### ml_ stuff: my $LS_CACHED = 0; my %DM_ID = (); # id -> { type, args, dmid, langs => { => 1, => 0, => 1 } } diff -r 341b82ccc061 -r 6ebda1baf1b5 cgi-bin/ljtimeutil.pl --- a/cgi-bin/ljtimeutil.pl Fri Nov 18 15:33:19 2011 +0800 +++ b/cgi-bin/ljtimeutil.pl Fri Nov 18 16:17:10 2011 +0800 @@ -197,67 +197,6 @@ $wday); } -# <LJFUNC> -# name: LJ::statushistory_time -# des: Convert a time like "20070401120323" to "2007-04-01 12:03:23". -# class: time -# args: -# des-: -# info: Only [dbtable[statushistory]] currently formats dates like this. -# returns: -# </LJFUNC> -sub statushistory_time { - my $time = shift; - $time =~ s/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/$1-$2-$3 $4:$5:$6/; - return $time; -} - -# <LJFUNC> -# class: time -# name: LJ::ago_text -# des: Converts integer seconds to English time span -# info: Turns a number of seconds into the largest possible unit of -# time. "2 weeks", "4 days", or "20 hours". -# returns: A string with the number of largest units found -# args: secondsold -# des-secondsold: The number of seconds from now something was made. -# </LJFUNC> -sub ago_text -{ - my $secondsold = shift; - return $BML::ML{'time.ago.never'} unless $secondsold > 0; - my $num; - my $unit; - if ($secondsold >= 60*60*24*7) { - $num = int($secondsold / (60*60*24*7)); - return LJ::Lang::ml('time.ago.week', {'num' => $num}); - } elsif ($secondsold >= 60*60*24) { - $num = int($secondsold / (60*60*24)); - return LJ::Lang::ml('time.ago.day', {'num' => $num}); - } elsif ($secondsold >= 60*60) { - $num = int($secondsold / (60*60)); - return LJ::Lang::ml('time.ago.hour', {'num' => $num}); - } elsif ($secondsold >= 60) { - $num = int($secondsold / (60)); - return LJ::Lang::ml('time.ago.minute', {'num' => $num}); - } else { - $num = $secondsold; - return LJ::Lang::ml('time.ago.second', {'num' => $num}); - } -} - -# args: time in seconds of last activity; "current" time in seconds. -# returns: result of LJ::ago_text for the difference. - -sub diff_ago_text { - my ( $last, $time ) = @_; - return ago_text( 0 ) unless $last; - $time = time() unless defined $time; - - my $diff = ( $time - $last ) || 1; - return ago_text( $diff ); -} - # Given a year, month, and day; calculate the age in years compared to now. May return a negative number or # zero if called in such a way as would cause those. diff -r 341b82ccc061 -r 6ebda1baf1b5 htdocs/admin/statushistory.bml --- a/htdocs/admin/statushistory.bml Fri Nov 18 15:33:19 2011 +0800 +++ b/htdocs/admin/statushistory.bml Fri Nov 18 16:17:10 2011 +0800 @@ -114,7 +114,9 @@ if ($hist->{$_} && ($_ eq 'user' || $_ eq 'admin')) { $ret .= LJ::ljuser($hist->{$_}); } elsif ($_ eq 'shdate') { - $ret .= LJ::statushistory_time($hist->{$_}); + my $time = $hist->{$_}; + $time =~ s/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/$1-$2-$3 $4:$5:$6/; + $ret .= $time; } elsif ($_ eq 'notes') { # notes need to be ehtml'd, but afterwards, we can convert \n to <br /> my $enotes = LJ::ehtml($hist->{$_}); --------------------------------------------------------------------------------
no subject
no subject