Recent

Author Topic: convert time/date from HTTP-header [partially SOLVED]  (Read 921 times)

xint

  • New Member
  • *
  • Posts: 33
convert time/date from HTTP-header [partially SOLVED]
« on: February 20, 2025, 05:59:21 pm »
I have a Date string from a HTTP-header "Thu, 20 Feb 2025 16:09:13 GMT", I have to compare against a more regular TimeDate-String, which is "2025-03-06".

Does someone has an idea how to convert the HTTP string in a more simple way then using lots of pos, copy's and length's ?


Thanks !
« Last Edit: February 21, 2025, 10:36:50 am by xint »

Nimbus

  • Jr. Member
  • **
  • Posts: 84
Re: convert time/date from HTTP-header
« Reply #1 on: February 20, 2025, 06:31:17 pm »
Well there are certainly methods to parse/format datetimes in the RTL

Code: Pascal  [Select][+][-]
  1. program Hello;
  2.  
  3. uses
  4.   SysUtils, DateUtils;
  5.  
  6. var
  7.   InputStr, OutputStr: String;
  8.   DateTime: TDateTime;
  9.  
  10. const
  11.   RFC1123_TimePattern = 'ddd, dd mmm yyyy hh:nn:ss "GMT"';
  12.  
  13. begin
  14.   InputStr := 'Thu, 20 Feb 2025 16:09:13 GMT';
  15.   DateTime := ScanDateTime(RFC1123_TimePattern, InputStr);
  16.   OutputStr := FormatDateTime('yyyy-MM-dd', DateTime);
  17.   Writeln(OutputStr);
  18. end.

cdbc

  • Hero Member
  • *****
  • Posts: 2609
    • http://www.cdbc.dk
Re: convert time/date from HTTP-header
« Reply #2 on: February 20, 2025, 06:32:19 pm »
Hi
edit: Nimbus beat me to it, with a better answer...
Yes, use a "poor man's state-machine", i.e.: you write a function with a simple state-parser inside...
Being an internet-date-format, I don't think it'll change format on you...
Should be doable ...and pretty reusable  :D
You'll need some good sample specimens to practice on, I'd say 25..50 different inet-datestamps, should do it for a start...

1) analyze the input string, you've got 1 - need more for precision
2) run through the string char by char
3) depending on where you are in the string, pick out the relevant string
4) lookup in a couple of arrays and convert to your format
5) when done, assemble the return string - and Bob's your uncle :)

Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: convert time/date from HTTP-header
« Reply #3 on: February 20, 2025, 06:36:00 pm »
I am by no means an expert,  but I think that https://www.freepascal.org/docs-html/rtl/sysutils/strtodatetime.html with the format settings appropriately localised is probably the way to do it.

I'd caution against making any assumptions about time resolutions finer than a second, since FPC (and Delphi before it) considers the ISO to be wrong.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

xint

  • New Member
  • *
  • Posts: 33
Re: convert time/date from HTTP-header
« Reply #4 on: February 20, 2025, 06:45:22 pm »
I am by no means an expert,  but I think that https://www.freepascal.org/docs-html/rtl/sysutils/strtodatetime.html with the format settings appropriately localised is probably the way to do it.

I'd caution against making any assumptions about time resolutions finer than a second, since FPC (and Delphi before it) considers the ISO to be wrong.

MarkMLl

StrToDateTime constantly fails with this particular string - I already wasted a significant amount of time to give this approach a try. Thank you anyway !

xint

  • New Member
  • *
  • Posts: 33
Re: convert time/date from HTTP-header
« Reply #5 on: February 20, 2025, 06:47:09 pm »
Well there are certainly methods to parse/format datetimes in the RTL

So glad to see your reply. Thanks a million !

MarkMLl

  • Hero Member
  • *****
  • Posts: 8533
Re: convert time/date from HTTP-header
« Reply #6 on: February 20, 2025, 07:08:33 pm »
StrToDateTime constantly fails with this particular string - I already wasted a significant amount of time to give this approach a try. Thank you anyway !

Which suggests that the long-format for date and times chosen by your OS's locale is wrong. since the function I cited allows you to override that with a parameter (i.e. does not change the program's defaults) it should be possible to fix it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: convert time/date from HTTP-header
« Reply #7 on: February 20, 2025, 10:35:35 pm »

Code: Pascal  [Select][+][-]
  1.   OutputStr := FormatDateTime('yyyy-MM-dd', DateTime);
  2. end.
In case waning to make sure that the separator is actually going to be "-" ...
Code: Pascal  [Select][+][-]
  1.   OutputStr := FormatDateTime('yyyy"-"MM"-"dd', DateTime);
.. otherwise the locale is able to alter that to whatever it is configure to use.
Today is tomorrow's yesterday.

xint

  • New Member
  • *
  • Posts: 33
Re: convert time/date from HTTP-header [SOLVED]
« Reply #8 on: February 21, 2025, 10:15:00 am »
Unfortunately I have to take up this problem again.
Since I switched back to my German development system, Nimbus solution fails - I assume it's because of the different short names for months and days for each language.

If so, the final software would encounter the same problem depending on where in the world it runs. Is there any way to override the default language settings of the operating system?


xint

  • New Member
  • *
  • Posts: 33
Re: convert time/date from HTTP-header [SOLVED]
« Reply #9 on: February 21, 2025, 11:02:45 am »
finally found the solution:
Code: Pascal  [Select][+][-]
  1. LCID_English = 2057; // british
  2. GetLocaleFormatSettings(LCID_English,fs);
  3.  

xint

  • New Member
  • *
  • Posts: 33
Re: convert time/date from HTTP-header [SOLVED]
« Reply #10 on: February 21, 2025, 11:02:56 am »
I have a Date string from a HTTP-header "Thu, 20 Feb 2025 16:09:13 GMT", I have to compare against a more regular TimeDate-String, which is "2025-03-06".

Does someone has an idea how to convert the HTTP string in a more simple way then using lots of pos, copy's and length's ?


Thanks !

silvercoder70

  • Full Member
  • ***
  • Posts: 200
    • Tim Coates
Re: convert time/date from HTTP-header [partially SOLVED]
« Reply #11 on: February 21, 2025, 12:15:52 pm »
Or with Indy...
Code: Pascal  [Select][+][-]
  1.   HttpDate := 'Sun, 06 Nov 1994 08:49:37 GMT';
  2.   PascalDateTime := InternetTimeToDateTime(HttpDate);
  3.  
🔥 Pascal Isn’t Dead -> See What It Can Do: @silvercoder70 on YouTube

wp

  • Hero Member
  • *****
  • Posts: 13352
Re: convert time/date from HTTP-header [SOLVED]
« Reply #12 on: February 21, 2025, 12:39:50 pm »
finally found the solution:
Code: Pascal  [Select][+][-]
  1. LCID_English = 2057; // british
  2. GetLocaleFormatSettings(LCID_English,fs);
  3.  
This is Windows-only. If you need a cross-platform solution do this:
Code: Pascal  [Select][+][-]
  1. function RFC1123DateTime(s: String): TDateTime;
  2. const
  3.   MONTH_NAMES: array[1..12] of string = (
  4.     'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'
  5.   );
  6.   DAY_NAMES: array[1..7] of string = (
  7.     'Son', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
  8.   );
  9. var
  10.   fs: TFormatSettings;
  11. begin
  12.   fs := DefaultFormatSettings;
  13.   fs.ShortMonthNames := MONTH_NAMES;
  14.   fs.ShortDayNames := DAY_NAMES;
  15.   Result := ScanDateTime('ddd, dd mmm yyyy hh:nn:ss "GMT"', s, fs);
  16. end;
« Last Edit: February 21, 2025, 07:21:05 pm by wp »

duralast

  • New Member
  • *
  • Posts: 43
Re: convert time/date from HTTP-header [partially SOLVED]
« Reply #13 on: February 21, 2025, 04:28:19 pm »
Using AWK  8) :D
Code: [Select]
$ echo "Thu, 20 Feb 2025 16:09:13 GMT" | awk '{sub(/Jan/,"01");sub(/Feb/,"02");sub(/Mar/,"03");sub(/Apr/,"04");sub(/May/,"05");sub(/Jun/,"06");sub(/Jul/,"07");sub(/Aug/,"08");sub(/Sep/,"09");sub(/Oct/,"10");sub(/Nov/,"11");sub(/Dec/,"12");print $4"-"$3"-"$2}'
2025-02-20
$

 

TinyPortal © 2005-2018