Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Assistance needed with code (Dates)

Hi all,

I am reading a list of headers from online images and it is reading and the formats show right BUT
when a Match is found it's ignoring it.

Basically: if NewOnlineImg = StrToDate(SetDate) then
                .....
     
               is not working!

               I've changed things around a million times so you are my last resort!
               Please help!!!

              Thanks
   
              ST3VO



procedure TForm1.Button4Click(Sender: TObject);
var i: integer;
    OnlineImg, SetDate, VRShopImgPath: String;
    LocalDate, NewOnlineImg: TDate;
begin
 
 
  for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items
    begin
 
    try
 
      Idhttp1.Head(Listbox1.Items[i]); //The Images on the listbox
      OnlineImg:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get the Last Modified Stamp
      OnlineImg:=AnsiLeftStr(OnlineImg, 16); //Trim it to show as ddd, dd MMM yyyy
      ListBox4.Items.add(OnlineImg);
 
 
      SetDate:=FormatDateTime('ddd, dd MMM yyyy', DateTimePicker1.Date);
      ListBox3.Items.Add(SetDate);
 
      NewOnlineImg:=StrToDate(OnlineImg);
      ListBox4.Items.Add(DateToStr(NewOnlineImg));
 
 
      if NewOnlineImg = StrToDate(SetDate) then
        begin
      ShowMessage('Match Found');
        //listbox2.Items.Add(Listbox1.Items[i]); // Clear it not new
       // ListBox3.Items.add(NewAutoExposureImg+ ' ' +NewPath);
        //Edit1.Text:=IntToStr(i);
       end;
 
    Except
 
      //continue;
    end;
     Application.ProcessMessages;
end;
 
end;

Open in new window

Avatar of imitchie
imitchie
Flag of New Zealand image

before 26:       if NewOnlineImg = StrToDate(SetDate) then
add (25) : ShowMessage(FormatDateTime('d/m/yy', NewOnlineImg) + ' = ' +
FormatDateTime('d/m/yy', StrToDate(SetDate)));

and check the output. I'm guessing it has to do with the format of the 'Last-Modified' string
Avatar of ST3VO

ASKER

Tried it and no messages appeared at all.  And there are matches! :o/

     NewOnlineImg:=StrToDate(OnlineImg);
      ListBox4.Items.Add(DateToStr(NewOnlineImg));
 
      if NewOnlineImg = StrToDate(SetDate) then
        begin
      ShowMessage('Match Found');

change the section above, to
      NewOnlineImg:=StrToDate(OnlineImg);
      ListBox4.Items.Add(DateToStr(NewOnlineImg));
 
  ShowMessage(FormatDateTime('d/m/yy', NewOnlineImg) + ' = ' +
FormatDateTime('d/m/yy', StrToDate(SetDate)));
 
      if NewOnlineImg = StrToDate(SetDate) then
        begin
      ShowMessage('Match Found');

Open in new window

Avatar of ST3VO

ASKER

No Messages shown at all :o/
which means you have an error WAY before that point (of testing = ). try/except is hiding the error. you should turn on debugging mode and find out where it is breaking
Avatar of ST3VO

ASKER

I don't get any errors...

Here's the unit and DFM so you can see for yourself!

Hope this helps!!!



////The Unit
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, StrUtils, Mask, rxToolEdit, ComCtrls, COMMCTRL;
 
type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Button2: TButton;
    IdHTTP1: TIdHTTP;
    Edit1: TEdit;
    ListBox2: TListBox;
    Button4: TButton;
    ListBox3: TListBox;
    ListBox4: TListBox;
    DateTimePicker1: TDateTimePicker;
    procedure ListBox2DblClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FDestinationFolder:String;
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
Uses WinInet;
 
function ExtractURLFileName(aPath : string) : string;
var I : integer;
begin
   I:=length(aPath);
   while (I>0) and (aPath[I] <> '/') do dec(I);
   result:=Copy(aPath,I+1,length(aPath)-I);
end;
 
 
 
function GetInetFile (const fileURL, FileName: String): boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  sAppName: string;
begin
 result := false;
 sAppName := ExtractFileName(Application.ExeName) ;
 hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
 try
  hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0) ;
  try
   AssignFile(f, FileName) ;
   Rewrite(f,1) ;
   repeat
    InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
    BlockWrite(f, Buffer, BufferLen)
   until BufferLen = 0;
   CloseFile(f) ;
   result := True;
  finally
   InternetCloseHandle(hURL)
  end
 finally
  InternetCloseHandle(hSession)
 end
end;
 
 
function GetFileName(Path: String): String;
var TmpName:String;
    StartPos:Integer;
begin
   try
      StartPos:=length(Path);
      While StartPos>0 do
      begin
        if path[StartPos]='/' then Break;
        dec(StartPos);
      end;
      Result:=copy(Path,StartPos+1,Length(path));
   except
     Result := #0;
   end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var i:Integer;
begin
   Button1.Enabled:=False ;
   Screen.Cursor:=crHourGlass;
   for i:=0 to ListBox2.Items.Count-1 do
   begin
    if not GetInetFile(ListBox2.Items.Strings[i],
               FDestinationFolder+GetFileName(ListBox2.Items.Strings[i])) then
       ShowMessage('Error while downloading ' + ListBox2.Items.Strings[i]);
   end;
   Button1.Enabled:=True;
   Screen.Cursor:=crDefault;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  Listbox1.Clear;
  ListBox1.Items.LoadFromFile('URLList2.txt');
end;
 
procedure TForm1.Button3Click(Sender: TObject);
 
 
var ImageStampDate, DataTimeNow : String;
i: integer;
begin
 
  for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items
    begin
      //if i > 0 then
      // Begin
 
    try
      sleep(2000);
      Idhttp1.Head(Listbox1.Items[i]); //Assign the current selection count
      ImageStampDate:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get Last Modified from image online
      //StrToDate(ImageStampDate, 'ddd, d mmm yyyy');
      DataTimeNow:=FormatDateTime('ddd, d mmm yyyy hh:mm:ss "GMT"', now); //Get Now FormatDate
      ImageStampDate:=AnsiLeftStr(ImageStampDate, 16); //Only leave the Date
      DataTimeNow:=AnsiLeftStr(DataTimeNow, 16);  //Only leave the Date
      if DataTimeNow <> ImageStampDate then
        begin
        listbox2.Items.Add(Listbox1.Items[i]); // Clear it not new
        listbox1.Items.Delete(i);
        Edit1.Text:=IntToStr(i);
       end;
 
    Except
      begin
        listbox1.Items.Delete(i); // Clear it not new
        Edit1.Text:=IntToStr(i);
      end;
      //continue;
    end;
     Application.ProcessMessages;
end;
 
end;
 
 
 
 
 
 
 
 
 
procedure TForm1.Button4Click(Sender: TObject);
var i: integer;
    OnlineImg, SetDate, VRShopImgPath: String;
    LocalDate, NewOnlineImg: TDate;
begin
 
 
  for i := 0 to ListBox1.Items.Count - 1 do //Loop the listbox items
    begin
 
    try
 
      Idhttp1.Head(Listbox1.Items[i]); //The Images on the listbox
      OnlineImg:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get the Last Modified Stamp
      OnlineImg:=AnsiLeftStr(OnlineImg, 16); //Trim it to show as ddd, dd MMM yyyy
      ListBox4.Items.add(OnlineImg);
 
 
      SetDate:=FormatDateTime('ddd, dd MMM yyyy', DateTimePicker1.Date);
      ListBox3.Items.Add(SetDate);
 
      NewOnlineImg:=StrToDate(OnlineImg);
      ListBox4.Items.Add(DateToStr(NewOnlineImg));
 
 
      if NewOnlineImg = StrToDate(SetDate) then
        begin
      //ShowMessage('Match Found');
      ShowMessage(FormatDateTime('d/m/yy', NewOnlineImg) + ' = ' +
           FormatDateTime('d/m/yy', StrToDate(SetDate)));
        //listbox2.Items.Add(Listbox1.Items[i]); // Clear it not new
       // ListBox3.Items.add(NewAutoExposureImg+ ' ' +NewPath);
        //Edit1.Text:=IntToStr(i);
       end;
 
    Except
 
      //continue;
    end;
     Application.ProcessMessages;
end;
 
end;
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  FDestinationFolder:= ExtractFileDir(ParamStr(0));
  if FDestinationFolder[Length(FDestinationFolder)]<>'\' then
     FDestinationFolder:=FDestinationFolder+'\';
  FDestinationFolder:=FDestinationFolder+'Files\';
 
  if not DirectoryExists(FDestinationFolder) then
    CreateDir(FDestinationFolder);
end;
 
procedure TForm1.ListBox1DblClick(Sender: TObject);
var s: string;
begin
 
ShowMessage(Listbox1.Items[ListBox1.ItemIndex]);
  Idhttp1.Head(Listbox1.Items[ListBox1.ItemIndex]); //Assign the current selection count
   Showmessage(Idhttp1.Response.RawHeaders.Values['Last-Modified']);
end;
 
procedure TForm1.ListBox2DblClick(Sender: TObject);
begin
ShowMessage(Listbox2.Items[ListBox2.ItemIndex]);
  Idhttp1.Head(Listbox2.Items[ListBox2.ItemIndex]); //Assign the current selection count
   Showmessage(Idhttp1.Response.RawHeaders.Values['Last-Modified']);
end;
 
end.
 
 
////The DFM
object Form1: TForm1
  Left = 135
  Top = 173
  Caption = 'Form1'
  ClientHeight = 583
  ClientWidth = 851
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ListBox1: TListBox
    Left = 8
    Top = 8
    Width = 265
    Height = 553
    ItemHeight = 13
    Items.Strings = (
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV677' +
        '41770_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV788' +
        '79418_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV879' +
        '99924_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV902' +
        '19618_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV916' +
        '90881_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV952' +
        '44787_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10120/AETV985' +
        '75806_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV147' +
        '15833_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV163' +
        '95440_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV170' +
        '12568_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV229' +
        '61767_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV304' +
        '45530_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV464' +
        '84350_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV477' +
        '08321_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV492' +
        '75484_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV525' +
        '12354_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV558' +
        '35343_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV603' +
        '05166_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV622' +
        '07174_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV681' +
        '18793_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV704' +
        '81607_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV705' +
        '47077_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV719' +
        '32075_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV731' +
        '38048_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV816' +
        '51924_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV817' +
        '90523_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV817' +
        '92210_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV834' +
        '68679_1b.jpg'
      
        'http://imageserver.autoexposure.co.uk/autoedit/AETA10185/AETV847' +
        '54068_1b.jpg')
    TabOrder = 0
    OnDblClick = ListBox1DblClick
  end
  object Button1: TButton
    Left = 768
    Top = 550
    Width = 75
    Height = 25
    Caption = 'Download'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 824
    Top = 488
    Width = 19
    Height = 25
    Caption = 'Load'
    TabOrder = 2
    OnClick = Button2Click
  end
  object Edit1: TEdit
    Left = 752
    Top = 88
    Width = 73
    Height = 21
    TabOrder = 3
    Text = '0'
  end
  object ListBox2: TListBox
    Left = 279
    Top = 8
    Width = 274
    Height = 553
    ItemHeight = 13
    TabOrder = 4
    OnDblClick = ListBox2DblClick
  end
  object Button4: TButton
    Left = 768
    Top = 519
    Width = 75
    Height = 25
    Caption = 'Process'
    TabOrder = 5
    OnClick = Button4Click
  end
  object ListBox3: TListBox
    Left = 559
    Top = 127
    Width = 284
    Height = 154
    ItemHeight = 13
    TabOrder = 6
    OnDblClick = ListBox2DblClick
  end
  object ListBox4: TListBox
    Left = 559
    Top = 287
    Width = 284
    Height = 186
    ItemHeight = 13
    TabOrder = 7
    OnDblClick = ListBox2DblClick
  end
  object DateTimePicker1: TDateTimePicker
    Left = 559
    Top = 61
    Width = 266
    Height = 21
    Date = 39419.439478796300000000
    Format = 'ddd, dd MMM yyyy'
    Time = 39419.439478796300000000
    DateFormat = dfLong
    TabOrder = 8
  end
  object IdHTTP1: TIdHTTP
    AllowCookies = True
    ProxyParams.BasicAuthentication = False
    ProxyParams.ProxyPort = 0
    Request.ContentLength = -1
    Request.Accept = 'text/html, */*'
    Request.BasicAuthentication = False
    Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'
    HTTPOptions = [hoForceEncodeParams]
    Left = 776
    Top = 32
  end
end

Open in new window

Avatar of ST3VO

ASKER

Found anything?
Avatar of ST3VO

ASKER

Anyone please???
hmm...

Some things you can try:

1) TDateTime is actually just a number for the amount of days that have passed since X (usually 31/12/1899 or something), with fractions of numbers to represent the hours minutes and seconds. Therefore if you have two dates that differ by even 1 second, they will not match.
So trim your TDateTimes to isolate the date only!
Add DateUtils to your unit's uses clause, and use DateOf.
Syntax:
DateOf(const AValue: TDateTime): TDateTime;
Example:
if DateOf(NewOnlineImg) = DateOf(StrToDate(SetDate)) then

2) Instead of saying '=', try using comparedate
Add Dateutils to the uses clause of your unit.
Syntax:
function CompareDate(const A: TDateTime; const B: TDateTime): TValueRelationship;
Example:
if CompareDate(NewOnlineImg, StrToDate(SetDate)) = 0 then
OR
if CompareDate(DateOf(NewOnlineImg), DateOf(StrToDate(SetDate))) = 0 then

3) Diagnostics. Get the system to tell you the dates it is trying to compare
Examples:
Showmessage(IntToStr(CompareDate(NewOnlineImg, StrToDate(SetDate))));
OR
Showmessage(DateToString(NewOnlineImg));
Showmessage(DateToString(StrToDate(SetDate)))
OR
Showmessage(DateTimeToString(NewOnlineImg));
Showmessage(DateTimeToString(StrToDate(SetDate)))
Why do you insist on moving my debug code??

      ShowMessage(FormatDateTime('d/m/yy', NewOnlineImg) + ' = ' +
           FormatDateTime('d/m/yy', StrToDate(SetDate)));      if NewOnlineImg = StrToDate(SetDate) then
        begin
      //ShowMessage('Match Found');


Debug message comes BEFORE the test to validate the test.  I gotta go off anyway, so good luck for many hours to come.
Avatar of ST3VO

ASKER

Right....Some more info I found:

The Original image result I get is: ddd, dd MMM yyyy hh:mm:ss GMT

So I used this to take off the excess and match the DateTimePicker format which is:  ddd, dd MMM yyyy

OnlineImg:=AnsiLeftStr(OnlineImg, 16); //Trim it to show as ddd, dd MMM yyyy

This gives me an error message " 'Thu, 30 Nov 2007' is not a valid date

This is returned by the CompareDate Function.

Any ideas??



Well you need to get it to TDateTime, you can do that using StrToDateTime. Look up in your Delphi help file StrToDateTime - there are two ways it can be used, the second way allows you to specify TFormatSettings, which will allow you to convert your string to a TDateTime.

Or alternatively, if you managed to convert from ddd, dd MMM yyyy hh:mm:ss GMT to a TDateTime before, then do that, just trim it using DateOf() which will remove the time part (hh:mm:ss GMT) and just keep the date (technically it sets the time to '0' which is midnight).
Avatar of ST3VO

ASKER

Is this not allowed?

Idhttp1.Head(Listbox1.Items[i]); //The Images on the listbox
      OnlineImg:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get the Last Modified Stamp
      OnlineImg:=AnsiLeftStr(StrToDateTime(OnlineImg, 16)); //Trim it to show as ddd, dd MMM yyyy

Won't compile :o/
What is this? StrToDateTime(OnlineImg, 16) ???
The first parameter is a string ("OnlineImg") the second parameter can only be TFormatSettings -- and "16" is not a TFormatSetting.
You can try this:
OnlineImg := StrToDateTime(AnsiLeftSr(OnlineImg, 16));
Avatar of ST3VO

ASKER

Trying this but get message invalid date too...Grrr :o(

Idhttp1.Head(Listbox1.Items[i]); //The Images on the listbox
      OnlineImg:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Get the Last Modified Stamp
      OnlineImg:=AnsiLeftStr(OnlineImg, 16); //Trim it to show as ddd, dd MMM yyyy
      NewOnlineImg:=StrToDateTime(OnlineImg);
      Showmessage(DateToStr(NewOnlineImg)); //Error Not a valid Date
Ooh... problem with my exampleabove. What type of variable is OnlineImg?
I think you mean: NewOnlineImg := StrToDateTime(AnsiLeftSr(OnlineImg, 16));
Avatar of ST3VO

ASKER

Hmmm:

[Pascal Error] Unit1.pas(187): E2003 Undeclared identifier: 'AnsiLeftSr'

Why is this sooo hard ? :o/
Avatar of ST3VO

ASKER

Can anyone help pleaseee????

Avatar of ST3VO

ASKER

In basic terms all I need to do is:

Var OnlineImg: String

A: OnlineImg:=(Idhttp1.Response.RawHeaders.Values['Last-Modified']); //Returns a String

Convert that string to Format : ddd, dd MMM yyyy

Then I have a TDateTimePicker - Format must also be: ddd, dd MMM yyyy // which it is

Then If OnlineImg = DateTimePicker.date then
  showmessage('Match Found');

Hope this is eaiser to understand!

I spent quite a while searching for ways to convert your string Thursday, 30 Nov 2007
into TDateTime format or even TDate format.
I came close, but not close enough to get it right.
I obviously did tests without the 'Thursday' because you can lose that, and only use '30 Nov 2007'.
This uses the format d MMM yyyy and it refused to convert it using StrtoDateTime(String, TFormatSettings).

I am going to bed, and will try look some more tomorrow.

One other alternative is to use EncodeDate (see DateUtils.EncodeDate).
For this you will need to isolate your string into its little components:
Day string
Month String
Year String
then do something like NewOnlineImg := EncodeDate(strtoint(YearString), strtoint(MonthString), strtoint(DayString))
(check the helpfile for proper syntax).
And then you will have a problem with your Month String, it needs an integer. So you have to convert Nov to 11. I do believe there is a DateUtils function that can do that for you too.

You can use the EncodeDate method as a last resort.. There has to be a way to use StrToDateTime with your date format, i.e. ddd, d MMM yyyy)

Good luck!
Avatar of ST3VO

ASKER

Thanks! I'll keep on trying!!! :o)
ASKER CERTIFIED SOLUTION
Avatar of Steven-Fernandez
Steven-Fernandez
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

Woohhoo...it's works perfect!!!

Thanks a million!!!