Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"From now" format for datetime columns #1043

Closed
pasha-zzz opened this issue Dec 19, 2017 · 11 comments
Closed

"From now" format for datetime columns #1043

pasha-zzz opened this issue Dec 19, 2017 · 11 comments

Comments

@pasha-zzz
Copy link

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).

@pasha-zzz
Copy link
Author

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

Only affected function - TMainForm.TorrentDateTimeToString

@pasha-zzz
Copy link
Author

pasha-zzz commented Jan 7, 2018

Localization and option to enable this format needed. But something like:

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(dt1*24), 'h') + PeriodName(Trunc(dt1*24*60) mod 60, 'm') + PeriodName(Trunc(dt1*24*60*60) mod 60, 's');
      if Length(s)=0 then s:='0 s';
      s:=s+'ago';
      Result:=s;
    end;
end;

@pasha-zzz
Copy link
Author

pasha-zzz commented Jan 7, 2018

Or (even better):

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;

@leonsoft-kras
Copy link
Contributor

ok, thanks 👍

@antekgla
Copy link
Contributor

antekgla commented Jan 7, 2018

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);
...

@leonsoft-kras
Copy link
Contributor

@antekgla - try :) good luck to you !

@pasha-zzz
Copy link
Author

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

@antekgla
Copy link
Contributor

antekgla commented Jan 7, 2018

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.

@pasha-zzz
Copy link
Author

pasha-zzz commented Jan 7, 2018

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"...

@antekgla
Copy link
Contributor

antekgla commented Jan 16, 2018

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

@PeterDaveHello
Copy link
Member

@antekgla awesome 👍

PeterDaveHello added a commit that referenced this issue Jan 19, 2018
New Feature: #1043 Implement the Display of Relative Dates/Times (to Now)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants