Transgui: "From now" format for datetime columns

Created on 19 Dec 2017  路  11Comments  路  Source: transmission-remote-gui/transgui

Maybe optional.
Added/completed/last activity columns format like a "1h 33m ago" or "1d 11h 5m ago" and so on (w - week, m - month, y - year).

enhancement help wanted

Most helpful comment

Wel... I finally implement this.
I make a Pull Request (like @PeterDaveHello suggest) to merge the code from this new feature.

If you want to display the Date/Times in a Relative Way (to Now) add a new value in _transgui.ini_ in the [Mainform] section:

FromNow=1

where 1= Relative Times and 0 or missing= MM/DD/YY HH:MM:SS

In both cases I implement the possibility of view the Date/Time in the other way.
I did that thru hints:

If you view Relative Times the hint shows you Absolute Times:

hin1

hint2

If you view Absolute Times the hints shows Relative Times:

hint3

hint4

All 11 comments

For date delta use PeriodBetween function:
https://www.freepascal.org/docs-html/rtl/dateutils/periodbetween.html

Only affected function - TMainForm.TorrentDateTimeToString

Localization and option to enable this format needed. But something like:
```pascal
function PeriodName(per: word; pername: string): string;
begin
if per=0 then Result:='' else Result:=IntToStr(per) + ' ' + pername + ' ';
end;

function TMainForm.TorrentDateTimeToString(d: Int64): string;
var dt, dt1: TDateTime;
dd, dm, dy: word;
s: string;
begin
if d = 0 then
Result:=''
else
begin
// Result:=DateTimeToStr(UnixToDateTime(d) + GetTimeZoneDelta);
dt:=UnixToDateTime(d) + GetTimeZoneDelta;
dt1:=Now;
PeriodBetween(dt1, dt, dy, dm, dd);
dt1:=Abs(Frac(dt-dt1));
s:=PeriodName(dy, 'y') + PeriodName(dm, 'mo') + PeriodName(dd, 'd') + PeriodName(Trunc(dt124), 'h') + PeriodName(Trunc(dt12460) mod 60, 'm') + PeriodName(Trunc(dt1246060) mod 60, 's');
if Length(s)=0 then s:='0 s';
s:=s+'ago';
Result:=s;
end;
end;
```

Or (even better):
pascal function TMainForm.TorrentDateTimeToString(d: Int64): string; var dt, dt1: TDateTime; dd, dmo, dy, dh, dm, ds: word; s: string; begin if d = 0 then Result:='' else begin // Result:=DateTimeToStr(UnixToDateTime(d) + GetTimeZoneDelta); dt:=UnixToDateTime(d) + GetTimeZoneDelta; dt1:=Now; PeriodBetween(dt1, dt, dy, dmo, dd); dt1:=Abs(Frac(dt-dt1)); s:=PeriodName(dy, 'y') + PeriodName(dmo, 'mo') + PeriodName(dd, 'd'); if dy+dmo=0 then begin dh:=Trunc(dt1*24); s:=s+PeriodName(dh, 'h'); if dd=0 then begin dm:=Trunc(dt1*24*60) mod 60; s:=s+PeriodName(dm, 'm'); if dh=0 then begin ds:=Trunc(dt1*24*60*60) mod 60; PeriodName(ds, 's'); end; end; end; if Length(s)=0 then s:='0 s'; s:=s+'ago'; Result:=s; end; end;

ok, thanks 馃憤

I'm working on this...
To make it optional there is not other way what modify the TorrentDateTimeToString function adding a new optional parameter:

function TorrentDateTimeToString(d: Int64; FromNow:Boolean = false): string;

And modify the function like this:

function TMainForm.TorrentDateTimeToString(d: Int64; FromNow: Boolean): string;
begin
  if d = 0 then
    Result:=''
  else
      if FromNow then
         Result := HumanReadableTime(Now,UnixToDateTime(d) + GetTimeZoneDelta) else
             Result:=DateTimeToStr(UnixToDateTime(d) + GetTimeZoneDelta);
end; 

Then make a new boolean value in transgui.ini and read it in Oncreate of the form.

  FFromNow := Ini.ReadBool('MainForm','FromNow',false);

To make localizable you have to define sYears, sMonths (sDays, sHours, sMins, sSecs are already defined)

sMonths = '%dmo';
sYears  = '%dy';

And then make the HumanReadableTime function:

function HumanReadableTime(ANow,AThen: TDateTime): string;
var
  Years, Months, Days, Hours, Minutes, Seconds, Discard: Word;
begin
  Try
    PeriodBetween(ANow,AThen,Years,Months,Days);
    DecodeDateTime(Anow-AThen,Discard,Discard,Discard,Hours,Minutes,Seconds,Discard);
    if Years > 0 then begin
       Result := Format(sYears,[Years]) + ' ' + Format(sMonths,[Months]);
    end else if Months > 0 then begin
      Result := Format(sMonths,[Months]) + ' ' + Format(sDays,[Days]);
     end else if Days > 0 then begin
       Result := Format(sDays,[Days]) + ' ' + Format(sHours,[Hours]);
    end else if Hours > 0 then begin
      Result := Format(sHours,[Hours]) + ' ' + Format(sMins,[Minutes]);
    end else if Minutes > 0 then begin
      Result := Format(sMins,[Minutes]) + ' ' + Format(sSecs,[Seconds]);
    end else begin
      Result := Format(sSecs,[Seconds])
    end;
  Except
    Result := 'An Eternity';
  End;
end;

So, then you have to replace every call to TorrentDateTimeToString with the new optional parameter like this:

procedure TMainForm.gTorrentsCellAttributes(Sender: TVarGrid; ACol, ARow, ADataCol: integer; 
AState: TGridDrawState; var CellAttribs: TCellAttributes);
...
     Text:=TorrentDateTimeToString(Sender.Items[ADataCol, ARow],FFromNow);
...

@antekgla - try :) good luck to you !

I wrote that it needs localization and option support. And I think there is no need to add additional parameter to function, we can read option directly in TMainForm.TorrentDateTimeToString function

And I think there is no need to add additional parameter to function, we can read option directly in TMainForm.TorrentDateTimeToString function

I also consider that, but for example in my current Transmission setup with 300 torrents and columns Completed On, Added On & Last Active that would be 900 reads to the ini file every time TransGUI draw the torrent grid (in my case each 5 seconds)
I think what is better read it only once at form creation.

I think what is better read it only once at form creation

New property like TMainForm.FFromNow solves this. And arguments of TMainForm.TorrentDateTimeToString untouched. And again, I dont wrote "read directly from file"...

Wel... I finally implement this.
I make a Pull Request (like @PeterDaveHello suggest) to merge the code from this new feature.

If you want to display the Date/Times in a Relative Way (to Now) add a new value in _transgui.ini_ in the [Mainform] section:

FromNow=1

where 1= Relative Times and 0 or missing= MM/DD/YY HH:MM:SS

In both cases I implement the possibility of view the Date/Time in the other way.
I did that thru hints:

If you view Relative Times the hint shows you Absolute Times:

hin1

hint2

If you view Absolute Times the hints shows Relative Times:

hint3

hint4

@antekgla awesome 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ploink picture ploink  路  7Comments

Pusha1d picture Pusha1d  路  7Comments

hekm77 picture hekm77  路  13Comments

LYCX2015 picture LYCX2015  路  11Comments

mrplumber picture mrplumber  路  13Comments