Jump to content
David Schwartz

TDateTimeHelper from D11 that can be used in D10.4?

Recommended Posts

I'm running into code that's being created in D11 that is full of references to new time-related stuff. Seems these things are quite popular, making me wonder what has taken so long for them to be added to the Delphi DateUtils library.

 

Anyway, TDateTimeHelper is a record helper that was added, along with other stuff, that seems quite useful.

 

Is there any way to get access for these things in earlier versions of Delphi? I'm using D10.4.2. It would be really nice to be able to use these new things without having to keep creating workarounds or spending several hundred dollars to upgrade Delphi.

Share this post


Link to post

The only way I see is to (re-)create this record helper yourself (or find code from somebody else who did).

Share this post


Link to post

You could try to copy the new helper from DateUtils.pas D11.2 into a separate unit, like DateUtils.Helper.pas

type
  TDateTimeHelper = record helper for TDateTime
    /// <summary>Now returns the current date and time</summary>
    class function Now: TDateTime; static; inline;
    /// <summary>Now returns the current UTC date and time</summary>
    class function NowUTC: TDateTime; static;
    /// <summary>Strips the time portion from a TDateTime value</summary>
    function GetDate: TDate; inline;

So that you can use it in D10.4.2 too.

There should be only a few changes needed, if any, to make that run in D10.4.2 too.

 

I would encapsule the content of this new helper unit with a Compiler directive, to ensure this is only used below version D11.2.

Edited by Rollo62

Share this post


Link to post
5 minutes ago, Rollo62 said:

You could try to copy the new helper from DateUtils.pas D11.2

Not sure this is legal if he didn't upgraded first to D11.2.

Share this post


Link to post
1 minute ago, FPiette said:

Not sure this is legal if he didn't upgraded first to D11.2.

That was exactly my thought.  👍

Share this post


Link to post

Copying the code from D11.x would definitively be a no-no.

Recreating the functionality where needed with your own code - no problem.

Share this post


Link to post
24 minutes ago, FPiette said:
24 minutes ago, FPiette said:

You could try to copy the new helper from DateUtils.pas D11.2

Not sure this is legal if he didn't upgraded first to D11.2.

That's a non issue: If he didn't upgrade, he wouldn't have the code in the first place.

 

Share this post


Link to post
4 hours ago, FPiette said:

Not sure this is legal if he didn't upgraded first to D11.2.

I assume that he has the D11 code, so it should be legal to rework this unit to make his code-base backwards-compatible.

 

If it would be true what you say, this would forbid to fix any buggy system unit for the same reason.

 

Share this post


Link to post
9 minutes ago, Rollo62 said:

I assume that he has the D11 code

I doubt that:

4 hours ago, David Schwartz said:

I'm using D10.4.2. It would be really nice to be able to use these new things without having to keep creating workarounds or spending several hundred dollars to upgrade Delphi.

 

Share this post


Link to post
53 minutes ago, Uwe Raabe said:

I doubt that:

Oh yes, then better upgrade to D11, its worth it.

Share this post


Link to post

Yes, and we hit direct the problem with these class/record helpers.

We had our own TDateTimeHelper, so we could change all code. Because it's never clear which helper it would pick, yours or theirs.

 

 

Share this post


Link to post
5 minutes ago, mvanrijnen said:

Because it's never clear which helper it would pick, yours or theirs.

Last one in uses wins.

Share this post


Link to post
4 minutes ago, Rollo62 said:

Last one in uses wins.

i know, but i find it a hassle to keep this in sight which one (in this case System.DateUtils or our own Helpers.DateTime) is where in the uses clausules, in all our sourcecodes.

But it's not a big problem, only hope there will be a solution for this problem.

 

Edited by mvanrijnen

Share this post


Link to post
On 1/4/2023 at 8:23 AM, Rollo62 said:

Oh yes, then better upgrade to D11, its worth it.

Worth it? $900 upgrade to get a few class helpers they copied from Microsoft? Does that seem worthwhile to anybody here? They should make these library upgrades free for all Delphi users. Maybe they've been ported to FPC or Lazarus? If I had a copy of the interface spec, I'd write the implementations myself, as most of them are probably just one or two lines of code.

Share this post


Link to post
1 hour ago, David Heffernan said:

I'm not even sure what you are asking us for here. What would a positive outcome to this thread look like? 

Something like "Here is an implementation of these helpers for earlier Delphi versions that somebody with the same problem wrote." ?

 

I wrote my own version of the TStopwatch records (actually it originally was a class + an interface but I changed that to be compatible to TStopwatch) just so I could use that for earlier Delphi versions. So it's not unreasonable that somebody else did the same for these helpers just in case he got annoyed enough that they were not available for (some of) the Delphi version(s) he uses?

Share this post


Link to post
3 hours ago, dummzeuch said:

Something like "Here is an implementation of these helpers for earlier Delphi versions that somebody with the same problem wrote." ?

Would be quicker just to write them yourself

Share this post


Link to post
5 minutes ago, David Heffernan said:

Would be quicker just to write them yourself

Which seems to be a no brainer since we just learned that you can just copy that code from Microsoft.

Share this post


Link to post
3 hours ago, David Heffernan said:

Would be quicker just to write them yourself

We have been there before, haven't we? If I write something myself, I might introduce bugs (Yes, I'm not infallible and I know that). That's why we use library functions even though the functionality might not be that complex.

Share this post


Link to post
15 hours ago, dummzeuch said:

If I write something myself, I might introduce bugs

I mean, aren't we all people who write code? I find it odd to hear programmers that don't want to program because they aren't able to do it right. 

  • Like 1

Share this post


Link to post
1 hour ago, David Heffernan said:

I mean, aren't we all people who write code? I find it odd to hear programmers that don't want to program because they aren't able to do it right. 

Experience has taught me that I make mistakes (Yes, there was a time where I thought I was the Über-Programmer that does not make mistakes,. But that was about 30 years ago.). And there is still so much to program even if I don't re-write anything that already exists that I don't get bored. Of course if it turns out that the existing code is buggy, I will try to fix it or rewrite it.

On top of that: One of the main reasons why I ended up as a programmer was that I was am lazy, so code reuse is a godsent.

Edited by dummzeuch

Share this post


Link to post
On 1/6/2023 at 1:36 AM, David Heffernan said:

I'm not even sure what you are asking us for here. What would a positive outcome to this thread look like? 

Ahh, thanks for that. 🙂

 

I don't know if the Community Edition of D11 is available yet or not (the page doesn't say), but if these units are included there, then perhaps they can be used by earlier Delphi licensees?

 

Maybe someone from Embt could get involved and see about making things like this more widely accessible to previous Delphi users? It's not like code mostly copied from other platforms is proprietary to either Delphi or EMBT. 


If these fail, how about getting access to a copy of the Interface portion of the DateUtils unit, or just the parts that are new in D11? Since I don't know what's there, simply grabbing stuff from some other platform wouldn't be terribly useful. The implementation part seems almost trivial since the methods I've seen can be rewritten using earlier syntax with very little effort.

 

(If they'd make it so class helpers can inherit previous helpers, then they could simply be published as separate units available to every Delphi user.)

 

Finally, if these changes have been ported to FPC or Lazarus, then I can use or adapt them. Does anybody know?

 

Share this post


Link to post
7 hours ago, David Schwartz said:

Finally, if these changes have been ported to FPC or Lazarus, then I can use or adapt them. Does anybody know?

These are open source projects 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×