0

I would like to get the current date, including time AND milliseconds. Since the Now function returns the time without milliseconds, I decided to use the DecodeDateTime function, but Delphi10 doesn´t seem to know what is that.

enter image description here

Is it gone? If yes, what should I use instead of it?

Community
  • 1
  • 1
Oh nooo
  • 478
  • 5
  • 19
  • 1
    I suggest that you stop using Delphi Basics, and instead use the documentation. Admittedly, the Delphi Basics site does list the unit that contains the function, but you are better off using the product documentation. – David Heffernan Oct 06 '15 at 07:50
  • 1
    Even using `DecodeDateTime` you won't get any more precision out of the `Now` function. If you need to know the current time of day with miliseconds, consider [`MilliSecondOfTheDay`](http://docwiki.embarcadero.com/Libraries/Seattle/en/System.DateUtils.MilliSecondOfTheDay). http://stackoverflow.com/q/29790895/327083 – J... Oct 06 '15 at 09:05
  • 1
    That said, if you don't actually need the date and time of day but only want a precise way to measure a relative interval between two events then consider using a [`TStopwatch`](http://docwiki.embarcadero.com/Libraries/XE2/en/System.Diagnostics.TStopwatch) – J... Oct 06 '15 at 09:06
  • "*the Now function returns the time without milliseconds*" - I don't know where you got that idea from, but `Now()` has millisecond precision. I use `Now()` all the time and it includes milliseconds just fine. And looking at its source code, it definitely gets milliseconds from the OS on all platforms. I just did a test with calling `Now()` twice with a `Sleep(10)` in between and then extracted the milliseconds from both values, they were 10ms apart, as expected. – Remy Lebeau Oct 06 '15 at 17:30
  • @RemyLebeau Emba's documentation states : [Note: Although TDateTime values can represent milliseconds, Now is accurate only to the nearest second.](http://docwiki.embarcadero.com/Libraries/en/System.SysUtils.Now) For whatever that's worth... maybe a legacy thing that used to be true but works now (and nobody updated the docs). 0_o – J... Oct 06 '15 at 19:29
  • @J...: It is odd that the documentation would ever say that at all, since `Now()` and `Time()` have always included milliseconds, at least as far back as D5 anyway (I can't check earlier versions). `GetLocalTime()` on Windows includes milliseconds (although on Windows CE, it is not guaranteed depending on hardware, but Delphi never supported CE anyway), as does `gettimeofday()` (actually microseconds) on POSIX systems. – Remy Lebeau Oct 06 '15 at 20:15
  • @RemyLebeau Well, it says it's only *accurate* to the nearest second, but one could be forgiven for interpreting that to mean that it only returns to the nearest second (answering the `I don't know where you got that idea from` question). As for why they say that at all, perhaps the implementation details would shed some light. – J... Oct 06 '15 at 21:30
  • @J...: Well, the implementation details clearly show that it takes milliseconds into account. And looking at older VCL source code shows it has done so for a LONG time. So there was no reason for the documentation to ever say that. Unless it is trying to take floating-point rounding into account (since `TDateTime` is implemented as a `Double`, afterall). – Remy Lebeau Oct 06 '15 at 22:06
  • @RemyLebeau I guess Emba just has rubbish for documentation, then ;) – J... Oct 06 '15 at 22:08
  • 1
    As far as I remember Now() always had up to one millisecond precision. In fact I used it often when doing code time measurement (before TStopWatch was implemented). Now I guess that since Delphi now also supports mobile platforms it is possible that some of them does not support retrieving time with up to one millisecond accuracy. – SilverWarior Oct 07 '15 at 21:40

1 Answers1

5

DecodeDateTime resides in System.DateUtils

To obtain a string with date, time and milliseconds, you can consider this solution too:

FormatDateTime('dd/mm/yyyy hh:nn:ss,zzz', Now);

SysUtils.FormatDateTime reference


As pointed out in the Remy Lebeau's comment, Now does include milliseconds and it corresponds actually to:

var
  SystemTime: TSystemTime;
begin
  GetLocalTime(SystemTime);
  with SystemTime do
    Result := EncodeDate(wYear, wMonth, wDay) + EncodeTime(wHour, wMinute, wSecond, wMilliseconds);
end;
Community
  • 1
  • 1
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
  • `Now()` is effectively the same as `Date()+Time()`. They all get their data from the same source. In any case, both `Now()` and `Time()` include milliseconds. – Remy Lebeau Oct 06 '15 at 17:36