#1 2019-06-18 12:21:56

jaclas
Member
Registered: 2014-09-12
Posts: 215

UnixTimePeriodToString() - how to use?

How do works UnixTimePeriodToString() and UnixMSTimePeriodToString() functions from SynCommons unit?

I need convert some interval (period) of time to string. I try these functions, but I get strange results:

var
  u : TUnixTime;
  ums : TUnixMSTime;
  dt: TDateTime;
  s: string;
begin
  dt := Now() - IncHour(Now(), -3); // three hours period, I mean

  ums := DateTimeToUnixMSTime(dt);
  s := UnixMSTimePeriodToString(ums, ' ');
  Writeln('UnixMSTimePeriodToString: ' + s);

  u := DateTimeToUnixTime(dt);
  s := UnixTimePeriodToString(u, ' ');
  Writeln('UnixTimePeriodToString: ' + s);

and I get:

UnixMSTimePeriodToString:  21:00:00.000
UnixTimePeriodToString:  21:00:00

why 21 not 3 h?

Offline

#2 2019-06-18 13:36:36

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: UnixTimePeriodToString() - how to use?

You are decreasing the hours twice:

dt := Now() - IncHour(Now(), -3); // three hours period, I mean

Use

dt := IncHour(Now(), -3); 

Offline

#3 2019-06-18 15:03:56

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: UnixTimePeriodToString() - how to use?

I know, but I do it consciously.
What you have proposed is a full date and time from 3 hours ago, e.g. 2019-06-18 13:30
And what I mean is only 3 hours, a time segment (period/interval), not a point on the timeline.
For example, total working time in the last month.

Offline

#4 2019-06-18 16:37:17

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: UnixTimePeriodToString() - how to use?

Sorry. I understood what you want to do now.

Why not calculate using unixtime directly?

  u := DateTimeToUnixTime(Now) - (DateTimeToUnixTime(Now) - 60*60*3);
  //or 
  //  u := DateTimeToUnixTime(Now) - DateTimeToUnixTime(IncHour(Now,-3));

   Writeln('UnixTimePeriodToString: ', UnixTimePeriodToString(u, ' '));
UnixTimePeriodToString: 03:00:00

Last edited by macfly (2019-06-18 16:51:09)

Offline

#5 2019-06-19 15:17:49

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: UnixTimePeriodToString() - how to use?

You are right! Thx macfly :-)

Offline

Board footer

Powered by FluxBB