Discussion:
[fpc-devel] NowUTC in FPC
Denis Kozlov
2016-07-20 09:08:12 UTC
Permalink
Hello,

Is it possible to move NowUTC from LazUTF8SysUtils.NowUTC to FPC, e.g.
DateUtils?

LocalTimeToUniversal(Now) is inefficient and suffers from a race condition,
but can still be used as a fallback implementation.

Denis
Marco van de Voort
2016-07-20 12:31:57 UTC
Permalink
Post by Denis Kozlov
Is it possible to move NowUTC from LazUTF8SysUtils.NowUTC to FPC, e.g.
DateUtils?
LocalTimeToUniversal(Now) is inefficient and suffers from a race condition,
but can still be used as a fallback implementation.
Isn't that horribly misnamed? It seems to get system time without additional
timezone corrections by FPC, but there is no guarantee that that is UTC?

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-07-20 12:49:11 UTC
Permalink
Post by Denis Kozlov
Post by Denis Kozlov
Is it possible to move NowUTC from LazUTF8SysUtils.NowUTC to FPC, e.g.
DateUtils?
LocalTimeToUniversal(Now) is inefficient and suffers from a race
condition,
Post by Denis Kozlov
but can still be used as a fallback implementation.
Isn't that horribly misnamed? It seems to get system time without
additional
timezone corrections by FPC, but there is no guarantee that that is UTC?
Do you mean "NowUTC" is misnamed? Sure, just as many other things, but
consistency is gold-ish ;)

I'm not sure why you say there is no guarantee, because in example of
Windows implementation GetSystemTime API is explicitly expressed in UTC
according to MSDN.

Denis
Marco van de Voort
2016-07-20 13:47:15 UTC
Permalink
Post by Denis Kozlov
Post by Marco van de Voort
additional
timezone corrections by FPC, but there is no guarantee that that is UTC?
Do you mean "NowUTC" is misnamed? Sure, just as many other things, but
consistency is gold-ish ;)
I'm not sure why you say there is no guarantee, because in example of
Windows implementation GetSystemTime API is explicitly expressed in UTC
according to MSDN.
I was looking at the Unix implementation.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-07-20 14:26:56 UTC
Permalink
Post by Marco van de Voort
Post by Denis Kozlov
I'm not sure why you say there is no guarantee, because in example of
Windows implementation GetSystemTime API is explicitly expressed in UTC
according to MSDN.
I was looking at the Unix implementation.
Unix implementation also returns UTC time, i.e. fpgettimeofday returns
seconds since Epoch, which gets converted to TDateTime.

Don't mind another poorly chosen name "EpochToLocal", which in reality is
more like "UnixToDateTime".

Is there an issue I'm just not seeing?

Denis
Henry Vermaak
2016-07-20 14:33:26 UTC
Permalink
Post by Denis Kozlov
Post by Marco van de Voort
Post by Denis Kozlov
I'm not sure why you say there is no guarantee, because in example of
Windows implementation GetSystemTime API is explicitly expressed in UTC
according to MSDN.
I was looking at the Unix implementation.
Unix implementation also returns UTC time, i.e. fpgettimeofday returns
seconds since Epoch, which gets converted to TDateTime.
Don't mind another poorly chosen name "EpochToLocal", which in reality is
more like "UnixToDateTime".
Is there an issue I'm just not seeing?
I thought the same. Maybe the implementation predates UnixToDateTime()?
Worth a bug report, I think.

Henry
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-07-20 14:49:10 UTC
Permalink
Post by Henry Vermaak
Post by Denis Kozlov
Don't mind another poorly chosen name "EpochToLocal", which in reality is
more like "UnixToDateTime".
Is there an issue I'm just not seeing?
I thought the same. Maybe the implementation predates UnixToDateTime()?
Worth a bug report, I think.
Well, Unix implementation of NowUTC can be replaced with a tidy:

function NowUTC: TDateTime;
var
tp: timeval;
begin
fpgettimeofday(@tp,nil);
Result := UnixToDateTime(tp.tv_sec);
end;

Denis
Denis Kozlov
2016-07-20 14:52:34 UTC
Permalink
Post by Denis Kozlov
Is it possible to move NowUTC from LazUTF8SysUtils.NowUTC to FPC, e.g.
DateUtils?
LocalTimeToUniversal(Now) is inefficient and suffers from a race
condition, but can still be used as a fallback implementation.
So, below is a tidy cross-platform implementation of NowUTC:

function NowUTC: TDateTime;
{$ifdef WINDOWS}
var
SystemTime: TSystemTime;
begin
Windows.GetSystemTime(SystemTime);
Result := SystemTimeToDateTime(SystemTime);
end;
{$else}
{$ifdef UNIX}
var
tp: timeval;
begin
fpgettimeofday(@tp,nil);
Result := UnixToDateTime(tp.tv_sec);
end;
{$else}
begin
// Beware, it suffers from a race condition!
Result := LocalTimeToUniversal(Now);
end;
{$endif}


Denis
Denis Kozlov
2016-07-20 14:59:20 UTC
Permalink
Oops, forgot the milliseconds, here it is:

function NowUTC: TDateTime;
{$ifdef WINDOWS}
var
SystemTime: TSystemTime;
begin
Windows.GetSystemTime(SystemTime);
Result := SystemTimeToDateTime(SystemTime);
end;
{$else}
{$ifdef UNIX}
var
tp: timeval;
begin
fpgettimeofday(@tp,nil);
Result := UnixToDateTime(tp.tv_sec);
Result := IncMilliSecond(Result, tp.tv_usec div 1000);
end;
{$else}
begin
// WARNING: Suffers from a race condition!
Result := LocalTimeToUniversal(Now);
end;
{$endif}
{$endif}
Denis Kozlov
2016-07-21 08:37:46 UTC
Permalink
I've uploaded a patch to mantis:
http://bugs.freepascal.org/view.php?id=30394

Denis
Ondrej Pokorny
2016-07-21 09:04:15 UTC
Permalink
Post by Denis Kozlov
http://bugs.freepascal.org/view.php?id=30394
Why does your client ignore mailing list threads? (Or why does my
Thunderbird email client ignore threads on messages sent from you?)



Ondrej
Henry Vermaak
2016-07-21 09:24:02 UTC
Permalink
Post by Ondrej Pokorny
Post by Denis Kozlov
http://bugs.freepascal.org/view.php?id=30394
Why does your client ignore mailing list threads? (Or why does my
Thunderbird email client ignore threads on messages sent from you?)
His client doesn't set References: or Reply-to: headers. I have to keep
joining the messages to the thread, it's annoying.

Henry
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Henry Vermaak
2016-07-21 09:25:49 UTC
Permalink
Post by Henry Vermaak
Post by Ondrej Pokorny
Post by Denis Kozlov
http://bugs.freepascal.org/view.php?id=30394
Why does your client ignore mailing list threads? (Or why does my
Thunderbird email client ignore threads on messages sent from you?)
His client doesn't set References: or Reply-to: headers. I have to keep
joining the messages to the thread, it's annoying.
Sorry, meant In-Reply-To:
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-07-21 10:14:22 UTC
Permalink
Post by Ondrej Pokorny
Why does your client ignore mailing list threads? (Or why does my
Thunderbird email client ignore threads on messages sent from you?)
It's replying through Gmail web interface seems to screw it up. Can't
use it for mailing lists then. Bummer.

P.S. This one is sent through Thunderbird. Sorry for the mess.

Denis
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-07-30 09:55:30 UTC
Permalink
Can somebody take a look and apply the patch please?

Thanks
Post by Denis Kozlov
http://bugs.freepascal.org/view.php?id=30394
Denis
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Michael Van Canneyt
2016-07-30 10:10:28 UTC
Permalink
Post by Denis Kozlov
Can somebody take a look and apply the patch please?
I had a look at it.
Please modify the patch first, see comments in bugreport.

Michael.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-07-30 12:34:58 UTC
Permalink
I had a look at it. Please modify the patch first, see comments in
bugreport.
The reason why I put UniversalTime (a.k.a. NowUTC) function in DateUtils
is because of the dependency on LocalTimeToUniversal, UnixToDateTime,
IncMilliSecond functions.

I presumed that DateUtils cannot (or must not) be added to the uses
section of SysUtils.

What would you recommend?

Denis
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Michael Van Canneyt
2016-07-30 16:45:18 UTC
Permalink
I had a look at it. Please modify the patch first, see comments in
bugreport.
The reason why I put UniversalTime (a.k.a. NowUTC) function in DateUtils is
because of the dependency on LocalTimeToUniversal, UnixToDateTime,
IncMilliSecond functions.
As far as I can see, these perform almost the same function as EpochToLocal in
sysutils/unix.

You can perfectly do these additions manually.
I presumed that DateUtils cannot (or must not) be added to the uses section
of SysUtils.
Indeed it cannot.
What would you recommend?
I don't see why you cannot perform simple additions in sysutils.

The reverse, introducing system dependent system calls in dateutils, is a
much worse solution. DateUtils is for simple date/time calculations, not for
performing OS calls.

Michael.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-08-01 01:21:59 UTC
Permalink
Post by Michael Van Canneyt
I don't see why you cannot perform simple additions in sysutils.
But that would create duplicated code of LocalTimeToUniversal,
UnixToDateTime, IncMilliSecond functions in SysUtils and DateUtils.
Post by Michael Van Canneyt
As far as I can see, these perform almost the same function as
EpochToLocal in
sysutils/unix.
EpochToLocal is equivalent to UnixToDateTime, except the later is more
efficient and better named. Also, I found at least 4 copies of
EpochToLocal in RTL for some reason, one of which suggests to use
UnixToDateTime instead ;)

Assuming that UniversalTime function must reside in SysUtils, I see
these options:

* Option A: Duplicate necessary functions from DateUtils to SysUtils.
* Option B: Move necessary functions from DateUtils to SysUtils.
* Option C: Include DateUtils in RTL, so that we cant use its functions
in SysUtils.

Option C seems to be the cleanest. Am I wrong to think that?

Denis
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Michael Van Canneyt
2016-08-01 06:59:05 UTC
Permalink
Post by Denis Kozlov
Post by Michael Van Canneyt
I don't see why you cannot perform simple additions in sysutils.
But that would create duplicated code of LocalTimeToUniversal,
UnixToDateTime, IncMilliSecond functions in SysUtils and DateUtils.
So what ? It is essentially A+B*C or some variant of that.

Since you seem reluctant, no problem, I will do it myself.

Michael.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Denis Kozlov
2016-08-01 09:31:53 UTC
Permalink
Post by Denis Kozlov
But that would create duplicated code of LocalTimeToUniversal,
UnixToDateTime, IncMilliSecond functions in SysUtils and DateUtils.
So what ? It is essentially A+B*C or some variant of that.
I try to avoid code duplication, which unnecessarily increases
maintenance efforts.
Post by Denis Kozlov
Since you seem reluctant, no problem, I will do it myself.
I'm asking for guidance to make sure I do it the right way.

Is it really so bad to include DateUtils in RTL?

Denis
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
Michael Van Canneyt
2016-08-01 09:53:53 UTC
Permalink
Post by Denis Kozlov
But that would create duplicated code of LocalTimeToUniversal,
UnixToDateTime, IncMilliSecond functions in SysUtils and DateUtils.
So what ? It is essentially A+B*C or some variant of that.
I try to avoid code duplication, which unnecessarily increases maintenance
efforts.
One must weigh always weigh the advantages over the disadvantages.
Consistency and design of the API have also their purpose.

If I didn't care about that, I would simply have added it to dateutils.
As it is now, dateutils is 100% portable. By introducing a dependency on the
OS, this is no longer true, and I think that should be avoided.
Post by Denis Kozlov
Since you seem reluctant, no problem, I will do it myself.
I'm asking for guidance to make sure I do it the right way.
In that case, just copy the implementation of the functions involved.
Is it really so bad to include DateUtils in RTL?
Yes. It was specially removed to keep the RTL as small as possible.

To move it back for the sake of some multiplications and additions is not an
option IMHO.

Michael.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Denis Kozlov
2016-07-21 09:19:22 UTC
Permalink
Post by Ondrej Pokorny
Why does your client ignore mailing list threads? (Or why does my
Thunderbird email client ignore threads on messages sent from you?)
I use latest Thunderbird too and Gmail web interface sometimes, but haven't
seen this behavior with my threads.

Mailing list archive also shows a correct thread structure:
http://lists.freepascal.org/pipermail/fpc-devel/2016-July/thread.html

[image: Inline images 1]

Maybe Thunderbird is having a bad day? Or your mail server is mangling
email headers somehow?

Denis
Ondrej Pokorny
2016-07-21 09:27:21 UTC
Permalink
Post by Ondrej Pokorny
Why does your client ignore mailing list threads? (Or why does my
Thunderbird email client ignore threads on messages sent from you?)
I use latest Thunderbird too and Gmail web interface sometimes, but
haven't seen this behavior with my threads.
http://lists.freepascal.org/pipermail/fpc-devel/2016-July/thread.html
It's strange. The mailing list archive doesn't show correct thread
structure either. Don't you see that your responses to other messages
are linked to the root and not to the message you answered?
My thunderbird is even worse, it creates new root message of almost all
(but not all) of your responses.

Ondrej
Loading...