Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-04

Converting http date back to TDateTime

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Zamrony P. Juhara

Hi,
How do I convert string with HTTP date format such as Wed, 29 Apr 2020 10:35:50 GMT
back to its TDateTime value?
I try with ScanDateTime()
adateTime := ScanDateTime(     'ddd", "dd" "mmm" "yyyy" "hh:mm:ss" GMT"',     'Wed, 29 Apr 2020 10:35:50 GMT'); but EConvertError exception is raised with message  Mismatch char " " "," at pattern position 5, string position 5

ZamroniFano Framework https://fanoframework.github.io

Zamrony P. Juhara

Alexander Grotewohl
to Zamrony P. Juhara

Can't test, but from the docs shouldn't it be something like:

adateTime := ScanDateTime(
'ddd, dd mmm yyyy hh:mm:ss GMT',
'Wed, 29 Apr 2020 10:35:50 GMT');
Alexander Grotewohl https://dcclost.com ________________________________
Earlier message 31 lines
From: fpc-pascal on behalf of Zamrony P. Juhara via fpc-pascal Sent: Tuesday, April 28, 2020 10:29:27 PM To: FPC-Pascal Users Discussions Cc: Zamrony P. Juhara Subject: [fpc-pascal] Converting http date back to TDateTime Hi, How do I convert string with HTTP date format such as Wed, 29 Apr 2020 10:35:50 GMT back to its TDateTime value? I try with ScanDateTime() adateTime := ScanDateTime( 'ddd", "dd" "mmm" "yyyy" "hh:mm:ss" GMT"', 'Wed, 29 Apr 2020 10:35:50 GMT'); but EConvertError exception is raised with message Mismatch char " " "," at pattern position 5, string position 5 Zamroni Fano Framework https://fanoframework.github.io Zamrony P. Juhara
Alexander Grotewohl
to Zamrony P. Juhara

"Note that to include any of the above characters literally in the result string, they must be enclosed in double quotes."

This only makes sense for FormatDateTime which returns a string you can display.

For example:
writeln(FormatDateTime(' "yyyy" = yyyy ', now));

To print:
yyyy = 2020

Alexander Grotewohl https://dcclost.com ________________________________
Earlier message 11 lines
From: Zamrony P. Juhara Sent: Tuesday, April 28, 2020 10:44:07 PM To: FPC-Pascal users discussions ; Alexander Grotewohl ; FPC-Pascal Users Discussions Subject: Re: [fpc-pascal] Converting http date back to TDateTime No it does not work. Docs says literal string needs to be quoted with " Zamrony P. Juhara On Wed, Apr 29, 2020 at 9:37, Alexander Grotewohl wrote:
Zamrony P. Juhara
to Alexander Grotewohl

I try with your suggested pattern format but it does not work either. Same EConvertError with different error message. I guess, I have to parse it manually

Zamrony P. Juhara
https://v3.juhara.com
https://github.com/zamronypj

Fano Framework
https://fanoframework.github.io

mod_pascal
https://zamronypj.github.io/mod_pascal

On Wed, Apr 29, 2020 at 10:00, Alexander Grotewohl wrote: _______________________________________________ fpc-pascal maillist  - 
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Santiago A.
to Zamrony P. Juhara: No it does not work. Docs says literal string needs to be quoted with " Zamrony P. Juhara…
Zamrony P. Juhara wrote 15 lines
No it does not work. Docs says literal string needs to be quoted with " Zamrony P. Juhara On Wed, Apr 29, 2020 at 9:37, Alexander Grotewohl wrote: _______________________________________________ fpc-pascal maillist  - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

This format works for me:
'ddd, dd mmm yyyy hh:nn:ss'
Fpc 3.04, windows 7, 32bits, and fpc 3.04, Linux, 64bits.

Some points:

* "m" is allowed for minutes , nevertheless, I use for minutes "n", not "m".
* I have suppressed the time zone information, I couldn't see how to set it in formatStrings, nevertheless, it ignores the GMT, or anything after time in input string with no error.
* I think that the scanner ignores the character after weekday, it expects a separator, as long as it is not a "d" if will accept and skip anything. So the double quote after the three "d" of 'ddd", "dd is ignored and everything breaks. It's a little confusing.
* The weekday and month depend on you local.
* using non-ascii characters breaks things. In Spanish Wednesday is "Miércoles" (e with acute accent) and Tuesday is "Martes": "mar, 28 abr 2020" works, but "Mié, 29 Abr 2020" fails. I think that the problem is that é  uses two bytes in UTF-8, but scan only deals properly with single byte characters

Saludos Santiago A.
Zamrony P. Juhara
to Santiago A.

Yes, it works if I remove timezone part from pattern as suggested by Santiago. This one works adatetime := scanDateTime(     'ddd, dd mmm yyyy hh:mm:ss',     'Tue, 28 Apr 2020 10:11:12 GMT'); So i guess, we must convert timezone information to local timezone manually.

Thank you
Zamrony P. Juhara
https://v3.juhara.com
https://github.com/zamronypj

Fano Framework
https://fanoframework.github.io

mod_pascal
https://zamronypj.github.io/mod_pascal

On Wed, Apr 29, 2020 at 16:40, Santiago A. wrote: _______________________________________________ fpc-pascal maillist  - 
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Santiago A.
to Zamrony P. Juhara
Zamrony P. Juhara wrote 9 lines
Yes, it works if I remove timezone part from pattern as suggested by Santiago. This one works adatetime := scanDateTime(      'ddd, dd mmm yyyy hh:mm:ss',      'Tue, 28 Apr 2020 10:11:12 GMT'); So i guess, we must convert timezone information to local timezone manually.

yes.
In unit Dateutils
https://www.freepascal.org/docs-html/rtl/dateutils/index-5.html You have a lot of handy functions:

function UniversalTimeToLocal(

  UT: TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>

):TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;
function UniversalTimeToLocal(
   UT: TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;

  TZOffset: Integer
<https://www.freepascal.org/docs-html/rtl/system/integer.html>

):TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;
function LocalTimeToUniversal(

  LT: TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>

):TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;
function LocalTimeToUniversal(
   LT: TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;

  TZOffset: Integer
<https://www.freepascal.org/docs-html/rtl/system/integer.html>

):TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;
function UniversalTimeToLocal(

  UT: TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>

):TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;
function UniversalTimeToLocal(
   UT: TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;

  TZOffset: Integer
<https://www.freepascal.org/docs-html/rtl/system/integer.html>

):TDateTime
<https://www.freepascal.org/docs-html/rtl/system/tdatetime.html>;
function GetLocalTimeOffset: Integer
<https://www.freepascal.org/docs-html/rtl/system/integer.html>;
Show quoted text 24 lines
Thank you Zamrony P. Juhara https://v3.juhara.com https://github.com/zamronypj Fano Framework https://fanoframework.github.io mod_pascal https://zamronypj.github.io/mod_pascal On Wed, Apr 29, 2020 at 16:40, Santiago A. wrote: _______________________________________________ fpc-pascal maillist  - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Saludos Santiago A.