Plugin Scripting BUG: Problem using dates and floating point

Post your questions and problem reports here

Moderator: kfury77

Forum rules
Please try to follow these guidelines. This will help to receive faster and more accurate response.

  • Check the Support section of the corresponding product first. Chances are you will find your answer there;
  • Do not create new topics for already reported problems. Add your comments to the existing topics instead;
  • Create separate topic for each problem request. Do NOT post a number of non-related problem reports in a single topic;
  • Give your topic a meaningful title. Titles such as "A question," "Bug report" and "Help!" provide others no clue what your message is about;
  • Include the version number of the software you are using;
  • This is not an official customer support helpdesk. If you need a prompt and official response, please contact our support team directly instead. It may take a while until you receive a reply in the forum;

Plugin Scripting BUG: Problem using dates and floating point

Postby pmk65 » Fri May 13, 2016 4:37 pm

In my latest plugin, I have a line:
var UnixStartDate = StrToDate("01/01/1970");

This works perfectly on my system, but on other systems it reports an error:
Plugin "Unix Timestamp": Line 28:5: "01/01/1970" is not a valid date

I had a similar problem a while ago, working with floating point values.
It looks like WeBuilder uses the system settings of the computer.

In Denmark where I live, we use comma as thousand seperator and point as decimal seperator and slash as date separator.
But in the US they use point as thousand separator and comma as decimal seperator.

So my plugin will work in the countries who uses the same separators as me, and fail with an error in all other countries.
Last edited by pmk65 on Sat May 14, 2016 9:34 pm, edited 2 times in total.
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Problem with using dates and float formats

Postby pmk65 » Sat May 14, 2016 10:16 am

I have discovered why this problem happens.
According to this site (which I use a LOT as reference when writing plugins), the default separator between dates is defined like this:
http://www.delphibasics.co.uk/RTL.asp?Name=DateSeparator

DateSeparator = LOCALE_SDATE by default.

Now this poses is a BIG problem, as FastScript/WeBuilder doesn't have the commands (like: DateSeparator, TimeSeparator, ShortTimeFormat, LongDateFormat ect.) to change that.
So the values used in a plugin script can change depending on the users language setup in Windows.

This makes it hard/impossible to create plugins that use floating point or date/time operations.
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Plugin Scripting BUG: Problem using dates and floating p

Postby Aivars » Mon May 16, 2016 4:23 pm

I'll add "DateSeparator, TimeSeparator, ShortTimeFormat, LongDateFormat" to plugins wishlist. We didn't figure someone would do a lot of date/time manipulations :)
Blumentals Software Programmer
User avatar
Aivars
Blumentals Software Developer
 
Posts: 2453
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Plugin Scripting BUG: Problem using dates and floating p

Postby Aivars » Mon May 16, 2016 4:26 pm

Btw there should be no problems with decimal values any more, WeBuilder just resets it to "." independent on locale (because Web standards e.g. CSS use . anyways). It's more complicated with the dates, I think your idea of exposing date format would be better in this case.
Blumentals Software Programmer
User avatar
Aivars
Blumentals Software Developer
 
Posts: 2453
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Plugin Scripting BUG: Problem using dates and floating p

Postby pmk65 » Wed May 18, 2016 1:26 pm

Good to know that the floading point problem has been fixed. :)

I got around the Date problem by using a numeric value instead of the date object. But that only worked when converting from Unixtimestamp to date.
The other way (date to untixtimestamp) requires a known dateformat.
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Plugin Scripting BUG: Problem using dates and floating p

Postby Aivars » Wed May 18, 2016 5:04 pm

The 4 listed functions will be added, but only in Read-only mode. There are some functions that have been there since the beginning, that might help you with dates:

Code: Select all
function EncodeDate(Year, Month, Day: Word): TDateTime
procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word)
function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime
procedure DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word)
function Date: TDateTime
function Time: TDateTime
function Now: TDateTime
function DayOfWeek(aDate: TDateTime): Integer
function IsLeapYear(Year: Word): Boolean
function DaysInMonth(nYear, nMonth: Integer): Integer
function FormatDateTime(Fmt: String; DateTime: TDateTime): String
function ValidDate(cDate: String): Boolean


and these are the ones that you know about, but I'm adding them here for reference:
Code: Select all
function DateToStr(e: Extended): String
function TimeToStr(e: Extended): String
function DateTimeToStr(e: Extended): String
function StrToDate(s: String): Extended
function StrToTime(s: String): Extended
function StrToDateTime(s: String): Extended
Blumentals Software Programmer
User avatar
Aivars
Blumentals Software Developer
 
Posts: 2453
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Plugin Scripting BUG: Problem using dates and floating p

Postby pmk65 » Wed May 18, 2016 6:48 pm

From the documentation of "StrToDateTime" (The other StrToXX functions is similar)

---
The first, date part of the string must adhere to the format of the ShortDateFormat value, and use the DateSeparator character to separate the day, month and year values.

The second, time part, separated by a blank from the date must adhere to the format of the LongTimeFormat value, and use the TimeSeparator character to separate the hour, minute and second values.
---

So I don't think a ReadOnly version will help. As without having a unified ShortDateFormat ,LongTimeFormat, TimeSeparator and DateSeparator it will be very hard to make a conversion that works on all setups.
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Plugin Scripting BUG: Problem using dates and floating p

Postby Aivars » Wed May 18, 2016 9:20 pm

In your specific case you could use EncodeDate(1970, 1, 1) and avoid StrToDate altogether. If you are targeting a specific, locale-independent format, you could use regular expressions to split the date/time string into the components and then use EncodeDate(...) + EncodeTime(...).
Blumentals Software Programmer
User avatar
Aivars
Blumentals Software Developer
 
Posts: 2453
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Plugin Scripting BUG: Problem using dates and floating p

Postby pmk65 » Thu May 19, 2016 9:29 pm

Aivars wrote:In your specific case you could use EncodeDate(1970, 1, 1) and avoid StrToDate altogether. If you are targeting a specific, locale-independent format, you could use regular expressions to split the date/time string into the components and then use EncodeDate(...) + EncodeTime(...).


Didn't think of that. Thanks.

BTW: Then I found a way to get hold of the locale settings of the system using a batch file. And then it's easy to pass the output to a plugin script.

Code: Select all
@ECHO OFF
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sTimeFormat') DO SET TimeFormat=%%B
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sLongDate') DO SET LongDate=%%B
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate') DO SET ShortDate=%%B
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sDate') DO SET DateDelimiter=%%B
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sTime') DO SET TimeDelimiter=%%B

ECHO TimeFormat    = %TimeFormat%
ECHO LongDate      = %LongDate%
ECHO ShortDate     = %ShortDate%
ECHO DateDelimiter = %DateDelimiter%
ECHO TimeDelimiter = %TimeDelimiter%
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
pmk65
 
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark


Return to HTMLPad / Rapid CSS / Rapid PHP / WeBuilder Support

Who is online

Users browsing this forum: No registered users and 11 guests