Recent

Author Topic: Milisecond precision unix timestamp  (Read 3956 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Milisecond precision unix timestamp
« on: August 07, 2018, 04:24:05 pm »
Im unable to figure out how i could get a unix timestamp with millisecond precision.

I found this long time ago, which i used in many projects without fail:

Code: Pascal  [Select][+][-]
  1. function GetLocalTimestamp():string;
  2. Begin
  3. GetLocalTimestamp := inttostr(Trunc((Now - EncodeDate(1970, 1 ,1)) * 24 * 60 * 60));
  4. end;

But it is only seconds precision.  :(
Any idea? Link? Unit?

Thanks.
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Milisecond precision unix timestamp
« Reply #1 on: August 07, 2018, 04:47:13 pm »
f you want a timestamp with millisecond precision do not use:
1. string conversions.like inttostr
2. encodedate

Both are for representing the time in human readable format, not for actually registering the time for further processing.....

Anyway, Look at the baseunix unit.
You can be very precise with timing...
« Last Edit: August 07, 2018, 04:49:59 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

guest60499

  • Guest
Re: Milisecond precision unix timestamp
« Reply #2 on: August 07, 2018, 04:55:30 pm »
clock_gettime(3) will give you up to nanosecond resolution. The older gettimeofday(2) will give you up to microsecond resolution. If you have no preference the former is better and is still POSIX compliant and if I remember correctly a lot faster.

Keep in mind that downwards from ~0.1s the time may only be useful for keeping track of event order on that machine.

Use CLOCK_RealTime.
Code: Pascal  [Select][+][-]
  1. unit LinuxTime;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, CTypes;
  8.  
  9. type
  10.   // From linux/time.h.
  11.   TClockID = (
  12.     CLOCK_RealTime = 0,
  13.     CLOCK_Monotonic = 1,
  14.     CLOCK_Process_CPUTime_ID = 2,
  15.     CLOCL_Thread_CPUTIME_ID = 3,
  16.     CLOCK_Monotonic_Raw = 4,
  17.     CLOCK_RealTime_Coarse = 5,
  18.     CLOCK_Monotonic_Coarse = 6,
  19.     CLOCK_BootTime = 7,
  20.     CLOCK_RealTime_Alarm = 8,
  21.     CLOCK_BootTime_Alarm = 9,
  22.     CLOCK_SGI_Cycle = 10,
  23.     CLOCK_TAI = 11
  24.   );
  25.  
  26.   csize = Int64;
  27.   cssize = QWord;
  28.   ctime = QWord;
  29.  
  30.   PTimeSpecification = ^TTimeSpecification;
  31.   TTimeSpecification = record
  32.     tv_sec: QWord;
  33.     tv_nsec: LongWord;
  34.   end;
  35.  
  36. function clock_gettime(
  37.   clk_id: TClockID;
  38.   var tp: TTimeSpecification): cint;
  39. cdecl; external;
  40.  
  41. implementation
  42.  
  43. end.

If you are on Windows, looking at this MSDN article should indicate the use of either GetSystemTime or GetLocalTime depending on if you wish it to be corrected to appear in the user's timezone. The SYSTEMTIME structure looks as linked.

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Milisecond precision unix timestamp
« Reply #3 on: August 14, 2018, 04:12:01 pm »
Look at this thread maybe you could find something.

ccrause

  • Hero Member
  • *****
  • Posts: 862
Re: Milisecond precision unix timestamp
« Reply #4 on: August 14, 2018, 05:20:29 pm »
Code: [Select]
GetLocalTimestamp := inttostr(Trunc((Now - EncodeDate(1970, 1 ,1)) * 24 * 60 * 60));
But it is only seconds precision.  :(

Yes, because that is what the code as written is supposed to do, convert a floating point value (where the fractional part represents a fraction of a day) from day representation to second representation, then trunc cuts off the remaining fraction of a second.  If you want an integer representation of milliseconds then also multiply your timestamp value with 1000 before calling trunc.

Note that this is not yet the same as having an actual time stamp value with ms resolution as mentioned by guest and Thaddy.

I have used Now() with DecodeTime() on a modest laptop with Linux to time stamp text log entries and achieved a resolution of 10 - 20 ms (of course this includes the text formatting and time conversion overhead).

 

TinyPortal © 2005-2018