Recent

Author Topic: How do I make an array of datetimes?  (Read 1326 times)

Marion

  • Full Member
  • ***
  • Posts: 123
How do I make an array of datetimes?
« on: May 06, 2023, 04:58:58 pm »
I need a list/array of datetimes.

I tried to make an array as:

Code: Pascal  [Select][+][-]
  1. datearray : array of TDateTime;
  2.  
  3. //and
  4.  
  5. datearray : array of Double;

In both cases it works but when I try to use the list for comparisons nothing seems to pass. Here is what I've tried.

Code: Pascal  [Select][+][-]
  1. if CompareDate(date, datearray[i]) = 0 then
  2.  
  3. if CompareDate(date, TDateTime(datearray[i])) = 0 then
  4.  
  5. if (date = datearray[i]) then


Suggestions?
Thank you,
Marion
(A recovering Windows programmer.)

alpine

  • Hero Member
  • *****
  • Posts: 1062
Re: How do I make an array of datetimes?
« Reply #1 on: May 06, 2023, 05:22:27 pm »
  • Are you sure that date is really equal to datearray[ i ] ?
  • Have you checked that i < Length(datearray) ?
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

TRon

  • Hero Member
  • *****
  • Posts: 2506
Re: How do I make an array of datetimes?
« Reply #2 on: May 06, 2023, 07:02:46 pm »
Suggestions?
Not really as the relevant parts are missing to be able to determine what might be the issue that you ran into.

So, as an example:
Code: Pascal  [Select][+][-]
  1. program dating;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils, DateUtils;
  7.  
  8. const
  9.   template = 'YYYY-MM-DD';
  10. var
  11.   Dates : array of TDateTime;
  12.   Date  : TDateTime;
  13.   i     : integer;
  14. begin
  15.   Dates := [StrToDate('2021-01-01', template), StrToDate('2022-01-01', template), StrToDate('2023-01-01', template)];
  16.   // or
  17.   SetLength(Dates,3);
  18.   Dates[0] := StrToDate('2021-01-01', template);
  19.   Dates[1] := StrToDate('2022-01-01', template);
  20.   Dates[2] := StrToDate('2023-01-01', template);
  21.  
  22.   // display the dates
  23.   for Date in Dates
  24.     do WriteLn(FormatDateTime('YYYY MM DD hh:nn:ss.zzz', Date));
  25.  
  26.   // search for specific date
  27.   Date := StrToDate('2022-01-01', template);
  28.   for i := Low(Dates) to High(Dates) do
  29.     if CompareDate(Date, Dates[i]) = 0
  30.      then WriteLn('dates[', i, '] matches')
  31.       else WriteLn('mismatch');
  32.  
  33.   // another way to search for a specific date
  34.   for Date in Dates do
  35.     if CompareDate(StrToDate('2022-01-01', template), Date) = 0
  36.      then WriteLn('date matches')
  37.       else WriteLn('mismatch');
  38. end.
  39.  

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: How do I make an array of datetimes?
« Reply #3 on: May 06, 2023, 08:24:38 pm »
Are we so sure that the index being used is taking into account for the fact that dynamic arrays start at 0 and end at 1 less than the size ?

Maybe showing some more code here may help, but I think using Low and High of the Array may help avoid that.

 Also, is it possible the function is failing to trim off the floating point errors ?

EDIT:
 
 Wanted to add, is the size of the array being set?
 :D
« Last Edit: May 06, 2023, 08:43:31 pm by jamie »
The only true wisdom is knowing you know nothing

jcmontherock

  • Full Member
  • ***
  • Posts: 236
Re: How do I make an array of datetimes?
« Reply #4 on: May 07, 2023, 05:47:35 pm »
I always believe that "Date" is a reserved word and could not be used as variable name. See:

https://www.freepascal.org/docs-html/rtl/sysutils/date.html

Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: How do I make an array of datetimes?
« Reply #5 on: May 07, 2023, 09:06:55 pm »
So what is the difference between Date and Now ?
The only true wisdom is knowing you know nothing

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: How do I make an array of datetimes?
« Reply #6 on: May 07, 2023, 09:41:38 pm »
So what is the difference between Date and Now ?

Date has the date with "00:00:00" as the time.
Now has the date with the current time.   

-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

tetrastes

  • Sr. Member
  • ****
  • Posts: 481
Re: How do I make an array of datetimes?
« Reply #7 on: May 07, 2023, 09:47:57 pm »
I always believe that "Date" is a reserved word and could not be used as variable name. See:

https://www.freepascal.org/docs-html/rtl/sysutils/date.html

It is not a reserved word, it is a function defined in sysutils, so it can be used as variable name.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: How do I make an array of datetimes?
« Reply #8 on: May 07, 2023, 09:48:30 pm »
I always believe that "Date" is a reserved word and could not be used as variable name. See:

https://www.freepascal.org/docs-html/rtl/sysutils/date.html

Date is an ordinary function and not a reserved word, so one is free to use it as one wants. To use SysUtils.Date in that case one needs to use exactly that.

So what is the difference between Date and Now ?

Now is the current date and time. Date is essentially Trunc(Now) (and Time is essentially Frac(Now)).

tetrastes

  • Sr. Member
  • ****
  • Posts: 481
Re: How do I make an array of datetimes?
« Reply #9 on: May 07, 2023, 09:59:20 pm »
Time is time of December 30 1899  :D
« Last Edit: May 07, 2023, 10:03:22 pm by tetrastes »

Marion

  • Full Member
  • ***
  • Posts: 123
Re: How do I make an array of datetimes?
« Reply #10 on: May 08, 2023, 07:24:18 pm »
This is what I finally did.

Code: Pascal  [Select][+][-]
  1. TDatesArray = array of TDateTime;
  2.  
  3. // Inside my component.
  4.  
  5. private
  6.   FDates: TDatesArray;
  7.   procedure SetDates(AValue: TDatesArray);
  8. public
  9.   property HighlightDates : TDatesArray read FDates write SetDates;
  10.  
  11. implementation
  12.  
  13. procedure SetDates(AValue: TDatesArray)
  14. begin
  15.   if FDates = AValue then Exit;
  16.   FDates := AValue;
  17.   Invalidate;
  18. end;
  19.  
  20.  
Thank you,
Marion
(A recovering Windows programmer.)

Marion

  • Full Member
  • ***
  • Posts: 123
Re: How do I make an array of datetimes?
« Reply #11 on: May 09, 2023, 12:41:27 am »
This is an update. I changed to using generics. Here is the code.

Code: Pascal  [Select][+][-]
  1. uses
  2.   fgl;
  3.  
  4. type
  5.   TDatesList = specialize TFPGList<TDateTime>;
  6.  
  7. TGizmoCalendar  = class(TCustomPanel)
  8. private
  9.   FDates: TDatesList;
  10.   procedure SetDates(AValue: TDatesList);
  11. public
  12.   property HighlightDates : TDatesList read FDates write SetDates;
  13.  
  14.  

Now, when I need to test if the current date is in the list of dates to be highlighted all I need to do is this.

Code: Pascal  [Select][+][-]
  1.  
  2. if FDates.IndexOf(CurrentDate) > -1 then
  3. begin
  4.   // Highlight the date
  5. end;
  6.  
  7.  

If anyone is interested in the project, it can be found here: https://github.com/titleofliberty/gizmos
Thank you,
Marion
(A recovering Windows programmer.)

 

TinyPortal © 2005-2018