Re: [fpc-pascal] How to use StrToDateTime() to convert a string with no delimiters?

2023-04-25 Thread Bo Berglund via fpc-pascal
On Tue, 25 Apr 2023 08:25:01 +0200 (CEST), Michael Van Canneyt via fpc-pascal
 wrote:

>
>
>On Mon, 24 Apr 2023, Thomas Kurz via fpc-pascal wrote:
>
>> I always use ScanDateTime for such purposes:
>>
>> https://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html
>>
>> You can explicitly define the pattern.
>
>Correct. This is the only correct way to do this.
>
>StrToDateTime is very limited in what it accepts.
>
>Michael.

Thanks, I did not know about this before, but now it is inserted in my code and
works fine. :-)

So now I can just specify the pattern of data to be scanned using the y, m, h, n
and s letters.
But I am adding 20 on front of the telegram value because that uses only a
2-digit year and I had problems on my new RPiZero until I added the two yy in
front of the pattern making it a 4-char year and the 20 in front of the
telegram.

Then all works fine.

-- 
Bo Berglund
Developer in Sweden

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] How to use StrToDateTime() to convert a string with no delimiters?

2023-04-25 Thread Michael Van Canneyt via fpc-pascal




On Mon, 24 Apr 2023, Thomas Kurz via fpc-pascal wrote:


I always use ScanDateTime for such purposes:

https://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

You can explicitly define the pattern.


Correct. This is the only correct way to do this.

StrToDateTime is very limited in what it accepts.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] How to use StrToDateTime() to convert a string with no delimiters?

2023-04-24 Thread Thomas Kurz via fpc-pascal
I always use ScanDateTime for such purposes:

https://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

You can explicitly define the pattern.



- Original Message - 
From: Bo Berglund via fpc-pascal 
To: fpc-pascal@lists.freepascal.org 
Sent: Monday, April 24, 2023, 20:08:01
Subject: [fpc-pascal] How to use StrToDateTime() to convert a string with no 
delimiters?

I have a device that sends data over the serial port with a timestamp in this
format:

(230424194642W)

This is coded as yymmddhhnnss and the last char designates daylight savings
(S)ummer or normal (W)inter time. In the example above it is W for normal winter
time.

What I have done so far is:
- Remove the () at start and end
- Remove the W at the end
- Add 20 (as the century) up front
- Insert a space after the 6th char (so between date and time
- Set the TFormatsettings like this (to say that there are no separators):
  FS.DateSeparator := #0; (Is this how it is done?)
  FS.TimeSeparator := #0;
- Then the call:
  Value := StrToDateTime(sValue, FS); (Value is declared as TDateTime)

This triggers an exception with the message:
 "194522" is not a valid time

What is the proper way to decode a string as shown above?
Since the date part could be decoded, why not also the time part, after all I
separated the two with a space and set the same separators?


-- 
Bo Berglund
Developer in Sweden

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] How to use StrToDateTime() to convert a string with no delimiters?

2023-04-24 Thread Bo Berglund via fpc-pascal
I have a device that sends data over the serial port with a timestamp in this
format:

(230424194642W)

This is coded as yymmddhhnnss and the last char designates daylight savings
(S)ummer or normal (W)inter time. In the example above it is W for normal winter
time.

What I have done so far is:
- Remove the () at start and end
- Remove the W at the end
- Add 20 (as the century) up front
- Insert a space after the 6th char (so between date and time
- Set the TFormatsettings like this (to say that there are no separators):
  FS.DateSeparator := #0; (Is this how it is done?)
  FS.TimeSeparator := #0;
- Then the call:
  Value := StrToDateTime(sValue, FS); (Value is declared as TDateTime)

This triggers an exception with the message:
 "194522" is not a valid time

What is the proper way to decode a string as shown above?
Since the date part could be decoded, why not also the time part, after all I
separated the two with a space and set the same separators?


-- 
Bo Berglund
Developer in Sweden

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal