Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: exception class EConvertError with message "15:51:11.133" is not valid time


This question is answered.


Permlink Replies: 2 - Last Post: Sep 21, 2016 7:17 AM Last Post By: Kobus le Roux Threads: [ Previous | Next ]
Kobus le Roux

Posts: 2
Registered: 10/2/16
exception class EConvertError with message "15:51:11.133" is not valid time  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 20, 2016 7:00 AM
Hi all

I am playing around with Delphi XE6 and have run into the EConvertError (see subject)

I wrote a little program that will measure the time that a rat spend on a specific activity (down to milliseconds) (working with scientific students). The program is running smoothly on Windows 7 machines but gives me the mentioned error when ran on Windows 10.

The code that seems to be giving me the error is the following:

T1d := StrToTime(FormatDateTime('hh:mm:ss.z', Now - StartTime));

where T1d is a variable of type TTime

If I take out the milliseconds (.z) from the statement above the program runs. Unfortunately the program has to be accurate up to milliseconds. As soon as I add the .z in the above statement then I get the error. Also the time stated in the error message indicates for example that 15 hours have passed - which is absolutely not true. So the time in the error message is not accurate. Again if I leave out the .z part from the FormatDateTime statement the measurements are correct (just not accurate enough).

The only thing that changed was that we tried to install and run the program on a Win 10 machine. It works fine on Win 7.

Any ideas for me will be appreciated. I am not a professional software developer but have taught myself some basics.

Kobus

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: exception class EConvertError with message "15:51:11.133" is not validtime
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 20, 2016 9:31 AM   in response to: Kobus le Roux in response to: Kobus le Roux
Kobus wrote:

I am playing around with Delphi XE6 and have run into the
EConvertError (see subject)

This is a locale issue. You are formatting a TDateTime using a specific
format, but then you are using the version of StrToTime() that uses the OS
locale for parsing. The error means the string you are parsing does not
match what the OS is expecting. In particular, the OS's expected thousands
and/or decimal separator is likely different than what the string is using.

First off, why are you formatting a TDateTime to a string and then back to
a TDateTime? You don't need that at all. Just store the result as-is:

T1d := Now - StartTime;


In cases where you actually do need to parse a date/time string, use the
version of StrToTime() that takes a TFormatSettings as input so you can specify
the exact format of the string:

var
  fmt: TFormatSettings;
 
fmt := TFormatSettings.Create;
fmt.TimeSeparator := ':'
fmt.DecimalSeparator := '.';
 
T1d := StrToTime(FormatDateTime('hh:mm:ss.zzz', Now - StartTime, fmt), fmt);


I wrote a little program that will measure the time that a rat spend
on a specific activity (down to milliseconds) (working with scientific
students).

I would not suggest using TDateTime for that. I would suggest using GetTickCount/64()
or TStopWatch instead:

uses
  ..., Windows;
 
var
  Start, Elapsed: DWORD;
 
Start := GetTickCount;
// do activity ...
Elapsed := GetTickCount - Start;


uses
  ..., Windows;
 
var
  Start, Elapsed: DWORD;
 
Start := GetTickCount;
// do activity ...
Elapsed := GetTickCount - Start;


uses
  ..., System.Diagnostics;
    
var
  SW: TStopWatch;
  Elapsed: Int64;
 
SW := TStopWatch.StartNew;
// do activity ...
SW.Stop;
Elapsed := SW.ElapsedMilliseconds;


--
Remy Lebeau (TeamB)
Kobus le Roux

Posts: 2
Registered: 10/2/16
Re: exception class EConvertError with message "15:51:11.133" is not validtime  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 21, 2016 7:17 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you very much for this helpful answer.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02