Recent

Author Topic: Compare Now with given time?  (Read 1994 times)

JamesSci

  • New Member
  • *
  • Posts: 30
Compare Now with given time?
« on: March 01, 2020, 07:52:03 pm »
Hi,

I want my program to do something if a given time has been reached. I do this by comparing my time with now:

Code: Pascal  [Select][+][-]
  1. if Now < StrToDateTime('01/03/2020 18:52:03') then
  2.   { Do something }

If I'm not mistaken though, this won't work on some machines with a different date/time format, right? If so, how can I express my target time?

Thank you.

balazsszekely

  • Guest
Re: Compare Now with given time?
« Reply #1 on: March 01, 2020, 07:58:42 pm »
Hi JamesSci,

Quote
I want my program to do something if a given time has been reached. I do this by comparing my time with now:
Use the function EncodeDatetime from Dateutils, then compare the result with now.

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

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Compare Now with given time?
« Reply #2 on: March 01, 2020, 09:31:13 pm »
Or better, use the StrToDateTime overload that allows you to specify the string format:

Code: Pascal  [Select][+][-]
  1. var
  2.   MySettings: TFormatSettings;
  3.  
  4. {...}
  5.  
  6. with MySettings do begin
  7.   DateSeparator := '/';
  8.   TimeSeparator := ':';
  9.   ShortDateFormat := 'd/m/y'
  10.   ShortTimeFormat := 'h:n:s'
  11. end;
  12. if Now < StrToDateTime('01/03/2020 18:52:03', MySettings) then
  13.   { Do something }

Modify to suit your tastes ;)

HTH!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Compare Now with given time?
« Reply #3 on: March 01, 2020, 10:23:41 pm »
Or better, use the StrToDateTime overload that allows you to specify the string format:

Why would you say it is better than EncodeDateTime?

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Compare Now with given time?
« Reply #4 on: March 01, 2020, 10:33:53 pm »
Or better, use the StrToDateTime overload that allows you to specify the string format:

Why would you say it is better than EncodeDateTime?
because it includes the parsing of the string to its components and you do not have to reinvent the wheel.

balazsszekely

  • Guest
Re: Compare Now with given time?
« Reply #5 on: March 02, 2020, 06:21:05 am »
@eljo
Quote
because it includes the parsing of the string to its components and you do not have to reinvent the wheel.

TDateTime is a double, @lucamar first parse the string, then converts the string to double.
EncodeDateTime directly creates a double. Now please tell me wich method reinvents the wheel.

PS: Don't get me wrong, @lucamar's method it's a perfectly good one, use it if you like it, but saying that is better then EncodeDatetime is a slight exaggeration.

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Compare Now with given time?
« Reply #6 on: March 02, 2020, 09:02:52 am »
TDateTime is a double, @lucamar first parse the string, then converts the string to double.
EncodeDateTime directly creates a double. Now please tell me wich method reinvents the wheel.

Exactly.

PS: Don't get me wrong, @lucamar's method it's a perfectly good one, use it if you like it, but saying that is better then EncodeDatetime is a slight exaggeration.

Of course, Lucamar's post has its place, as it is nice to show that there is this way, for completeness.
However, including unnecessary string parsing can hardly be called a better solution.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Compare Now with given time?
« Reply #7 on: March 02, 2020, 10:37:00 am »
Demonstrate Bias:
Code: Pascal  [Select][+][-]
  1. begin
  2.   writeln(Now);
  3.   writeln(Now);
  4. end.
A slow processor differs (should differ) , but what happens? Think!
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Compare Now with given time?
« Reply #8 on: March 02, 2020, 11:27:32 am »
TDateTime is a double, @lucamar first parse the string, then converts the string to double.
EncodeDateTime directly creates a double. Now please tell me wich method reinvents the wheel.

Exactly.

PS: Don't get me wrong, @lucamar's method it's a perfectly good one, use it if you like it, but saying that is better then EncodeDatetime is a slight exaggeration.

Of course, Lucamar's post has its place, as it is nice to show that there is this way, for completeness.
However, including unnecessary string parsing can hardly be called a better solution.

Please, notice the context: the OP is already using StrToDateTime, presumably because he's getting the date/time (whether as a constant ot from elsewhere) as a string, and the question arised about the different default formats it may use to parse the string, which he would have to do anyway to use EncodeDateTime()

Since he's already using it and any other method implies parsing the string anyway, isn't it better (and IMHO the "correct" way) to use the correct overload of StrToDateTime?

It would, of course, be otherwise if he got the date(s) as separate integers: Then the better solution would in fact be EncodeDateTime. But that was not the question.
« Last Edit: March 02, 2020, 11:36:56 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Compare Now with given time?
« Reply #9 on: March 02, 2020, 11:32:48 am »
@eljo
Quote
because it includes the parsing of the string to its components and you do not have to reinvent the wheel.

TDateTime is a double, @lucamar first parse the string, then converts the string to double.
EncodeDateTime directly creates a double. Now please tell me wich method reinvents the wheel.

PS: Don't get me wrong, @lucamar's method it's a perfectly good one, use it if you like it, but saying that is better then EncodeDatetime is a slight exaggeration.
TDateTime is a double, @lucamar first parse the string, then converts the string to double.
EncodeDateTime directly creates a double. Now please tell me wich method reinvents the wheel.

Exactly.

PS: Don't get me wrong, @lucamar's method it's a perfectly good one, use it if you like it, but saying that is better then EncodeDatetime is a slight exaggeration.

Of course, Lucamar's post has its place, as it is nice to show that there is this way, for completeness.
However, including unnecessary string parsing can hardly be called a better solution.


You missed the point. Now is a TdateTime that needs to be compared against a string of a datetime. See the first post.
So the problem becomes how to convert the string to a tdatetime to compare it against "now".

Do you propose to convert the value of now to string and compare strings?

balazsszekely

  • Guest
Re: Compare Now with given time?
« Reply #10 on: March 02, 2020, 11:51:54 am »
@eljo
Quote
You missed the point. Now is a TdateTime that needs to be compared against a string of a datetime. See the first post.
Maybe we missed the point, but if we did, we have an even bigger problem.

Quote
Do you propose to convert the value of now to string and compare strings?
I propose to store the datetime as double not string. Nowadays every decent database has DateTime format, more over if you wish to avoid databases, you can save the datetime into a stream as double, then save the stream to file. I see no reason to store the date as string, it will lead to confusion and converting error as this conversation demonstrates. With that said, I don't want to force my opinion on others, so I will deatach myself from this thread.
« Last Edit: March 02, 2020, 12:05:13 pm by GetMem »

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Compare Now with given time?
« Reply #11 on: March 02, 2020, 12:06:05 pm »
@eljo
Quote
You missed the point. Now is a TdateTime that needs to be compared against a string of a datetime. See the first post.
Maybe we miss the point, but if we did, we have an even bigger problem.

Quote
Do you propose to convert the value of now to string and compare strings?
I propose to store the datetime as double not string. Nowadays every decent database has DateTime format, more over if you wish to avoid databases, you can save the datetime into a stream as double, then save the stream to file. I see no reason to store the date as string, it will lead to confusion and converting error as this conversation demonstrates. With that said, I don't want to force my opinion on others, so I will deatach myself from this thread.
you assume that he is control of how the data are saved or even retrieved. You might be right and the OP will be better off changing those processes but that is not what he asked for and we can only answer what it was asked not impose our assumptions.

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Compare Now with given time?
« Reply #12 on: March 02, 2020, 12:45:53 pm »
I do agree that, if the times are gotten from previously prepared strings, from some file with formatted dates or something like that, then your proposal is the way to go.

However, in the original question, I still fail to see that parsing from string was needed.

you assume that he is control of how the data are saved or even retrieved.

I don't see that GetMem and I assumed anything. You assumed that dates were previously packed as strings. Why?

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Compare Now with given time?
« Reply #13 on: March 02, 2020, 12:58:20 pm »
I do agree that, if the times are gotten from previously prepared strings, from some file with formatted dates or something like that, then your proposal is the way to go.

However, in the original question, I still fail to see that parsing from string was needed.

you assume that he is control of how the data are saved or even retrieved.

I don't see that GetMem and I assumed anything. You assumed that dates were previously packed as strings. Why?
The original example was well prepared. He used the function strtodatetime and even provided a string constant to show the format of the string and he was worried that the string format might no work on different locales. There is no code to retrieve or even to hint that there is a conversion from a datetime to string, not even a hint of a database back that returns the datetime as string. So no there is no assumption in my part. If my interpretation is bad then the OP can refine hes question as needed.

PS: sorry this is OT for this thread so I"m not going to continue with new posts just one last thing as obvious as it might be.
It was not my proposal it was lucamar's proposal I'm only addressing the "even better" part of his post as I understand it.

« Last Edit: March 02, 2020, 01:04:46 pm by eljo »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Compare Now with given time?
« Reply #14 on: March 02, 2020, 01:54:02 pm »
I don't see that GetMem and I assumed anything. You assumed that dates were previously packed as strings. Why?

Because, as eljo says, that's how the original example code represents it. We don't know how he's getting the date/time so we can assume only that he knows what he is doing when he uses a string and StrToDateTime.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018