Bug in EncodeTime? lazarus / 31-LCL ---------------------------------------------------------------------- winni · 2020-08-18 12:00 Hi! Is this a bug in EncodeTime? Or is it too cautious? EncodeTime rejects every hour >= 24 and every minute/second >=60 as an error. Now think about the situation that you are computing the local time with the help of the UTC, timezone and the daylight saving: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i LocalTime := EndcodeTime (HourUTC+timezone+daylight, minUTC,secUTC,0); Here the hour can be equal-greater than 24 because in the east we might have already a new day. So that could EncodeDate do for me, but it refuses. And the new day would be easy to detect because the day part of TDateTime is now 1 and not zero. Now I have to do it like in old basic times: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i LocalTime := (HourUTC+timezone+daylight)/24 +MinUTC/24/60+secUTC/24/60/60;   The problem is not so easy to solve as it also concerns the minutes with the timezones with 30 or 45 minutes (India, Nepal, Iran, Australia ...). Is this a bug? Or is it too cautious??? Winni ---------------------------------------------------------------------- AlexTP · 2020-08-18 12:11 http://www.delphibasics.co.uk/RTL.asp?Name=EncodeTime quote: If you exceed these values, an EConvertError is raised. ---------------------------------------------------------------------- wp · 2020-08-18 12:14 I think you are abusing the EncodeTime() function. It is clearly stated in the docs that QuoteThe parameters must have a valid range (boundaries included): - Hour -- must be between 0 and 23. - Minute,second -- must both be between 0 and 59. - Millisecond -- must be between 0 and 999. For timezone correction there are decidated functions in DateUtils: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { UTC <-> Local time } Function UniversalTimeToLocal(UT: TDateTime): TDateTime;Function UniversalTimeToLocal(UT: TDateTime; TZOffset : Integer): TDateTime;Function LocalTimeToUniversal(LT: TDateTime): TDateTime;Function LocalTimeToUniversal(LT: TDateTime; TZOffset: Integer): TDateTime; ---------------------------------------------------------------------- winni · 2020-08-18 12:26 Hi! No, I am not abusing EncodeTime. I am using the intelligent format of TDateTime. With automatic wrapping which is prohibited by EncodeTime. They have not understood how to use TDateTime. But thanx to Alextp for the info: The trash again comes from Delphi. Winni ---------------------------------------------------------------------- wp · 2020-08-18 14:11 When you want to add hours to overflow into the next day, then you also must be allowed to subtract hours to underflow into the previous day. But this brings us into the range of negative TDateTimes, and this is where TDateTime does not work any more. Suppose the time 6:00:00.000 ( EncodeTime(6, 0, 0, 0) = 0.25 = 1 quarter day), and FormatDateTime('hh:nn', 0.25) yields '6:00'. OK. Now subtract 12 hours (half a day = 0.5). This brings us back to 18:00 on the previous day. We cannot calculate EncodeTime(-18,0,0,0), but let's subtract 0.5 (= 12 hours)  from 0.25 which results in -0.75. I would expect FormatDateTime('hh:nn', -0.75) do show '18:00', but in fact it displays '6:00'. q.e.d: Negative TDateTime is wrong. Is this "intelligent"? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; uses  SysUtils; var  d: TDateTime;begin  // Date/time calculation resulting in negative time  d := EncodeTime(6, 0, 0,0);  WriteLn(FormatDateTime('hh:nn', d));            // expected: 6:00  WriteLn(FormatdateTime('hh:nn', d-0.5));        // expected: 18:00, but is 6:00  WriteLn;   // Keep date/time always positive:  d := EncodeTime(6, 0, 0, 0) + 1;  // 6:00 on the next day  WriteLn(FormatDateTime('hh:nn', d));            // expected: 6:00  WriteLn(FormatdateTime('hh:nn', d-0.5));        // expected: 18:00, ok   ReadLn;end. ---------------------------------------------------------------------- winni · 2020-08-18 17:21 Hi Negative Date or Time is not wrong because they are just the two parts of a double - integer and frac . As I said above: They have not understood how fine TDateTime works. Everything is ok and correct until you convert TDateTime to a string. So as you say: FormatDateTime, TimeToStr, DateToStr and others are buggy because they don't interpret negativ values correct. This is double trouble because they did  not chose 01.01.0001 as offset date but 30.12.1899. So whenever you want to calculate something from the times of the industrial revolution then you ran in trouble with the date. I assume that they were just lazybones: This way they got around the calendar change from Julian to Gregorian (Oktober 1582). Winni ---------------------------------------------------------------------- Bart · 2020-08-18 20:03 IMO '32-13-2020' or '-15--11-1984' are not a valid dates (in dd-mm- yyyy notation), so they should raise an error when trying to convert. The internal representation of a TDateTime is just an implementation detail. There are dedicated routines for adding and subtracting time/date's. Bart ---------------------------------------------------------------------- winni · 2020-08-18 20:52 Hi! In the Julian Date time routines this is a well know algorithm while checking a legal date: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Function IsJulianDateLegal (s: string) : boolean;var tmp : string;     J: TJulianDate;beginJ := StrToJulian (s);tmp := JulianToStr(J);result := tmp = s;end;  You see: overflows and negative days/month/years are allowed. That makes the handling of computing with dates a lot easier. The Delphi logic results in a lot of unnecessary procedures and complicates the working with dates. Not to speak how to work with dates before 1900. Winni