PDA

View Full Version : ThinBasic time library release [FreeBasic]



xLeaves
10-09-2019, 15:21
I have enhanced and encapsulated FreeBasic's time library so that ThinBasic can use these functions.

Now they are integrated into the development environment of LzRPA.

I will post these programming code here.

So that people with the same needs can use them.

The code also includes the implementation of MsgBox, InputBox and keyboard and mouse emulation.

Also note that it covers some of the original ThinBasic functions and may cause compatibility issues.

Because I have no historical burden when developing with ThinBasic.

Consider less compatibility issues.

Below is a screenshot of the test code:


TracePrint("当前日期和时间 : " & Now())
TracePrint("当前日期 : " & Date())
TracePrint("当前时间 : " & Time())
TracePrint("时间戳 : " & Timer())
TracePrint("当前年 : " & Year())
TracePrint("当前月份 : " & Month())
TracePrint("当前日期 : " & Day())
TracePrint("当前星期几 : " & WeekDay())
TracePrint("当前时 : " & Hour())
TracePrint("当前分 : " & Minute())
TracePrint("当前秒 : " & Second())
TracePrint("当前Unix时间戳 : " & ToUnixTime())
TracePrint("十天后的Unix时间戳 : " & ToUnixTime(Now()))
TracePrint("将当前Unix时间戳转换为时间 : " & FromUnixTime(ToUnixTime(Now())))
TracePrint("判断是否可以转换为时间 : " & IsDate("2018年12月12日 12:00:00"))
TracePrint("将字符串转换为时间 : " & CDate("2018年12月12日 12:00:00"))
TracePrint("当前时间添加10天 : " & DateAdd("d", 10, Now()))
TracePrint("获取当天与十天后的时间差(返回天) : " & DateDiff("d", DateAdd("d", 10, Now()), Now()))
TracePrint("构造日期 : " & DateSerial(2019, 9, 4))
TracePrint("构造时间 : " & TimeSerial(12, 0, 0))
TracePrint("构造时间和日期 : " & TimeSerial(2019, 9, 4, 12, 0, 0))
TracePrint("获取时间中天这部分的数据 : " & DatePart("d", Now()))
TracePrint("将时间格式化显示 : " & Format(Now(), "yyyy-mm-dd hh:mm:ss"))


10029

FreeBasic's time library uses a Double data store time information, where the integer part stores the number of days (how many days have elapsed since a certain time) and the fractional part stores the time of day, for example, 0.5 means 12:00:00.

The advantage of this is that time can be directly added and subtracted, very convenient, the disadvantage is that you can not directly calculate and output the display like VB, you must first Format, otherwise they look like a bunch of meaningless numbers.

Of course, ThinBasic does not provide an operator overloading mechanism for data types, so output and computation cannot be done at the same time.

I think that the computing power of time is the most important, so I gave up the idea of reconstructing a time system. FreeBasic's time library is quite good.

But it's not quite complete, so I added four functions, ToUnixTime, FromUnixTime, IsDate, and CDate, to make this time library better.

Of course, the function is also closer to the processing power of VB.



Below is the source code, developed using FreeBasic (FbEdit editor):

10030

xLeaves
10-09-2019, 15:37
The DLL file is already compiled in the source code package.



@ErosOlmi:

I encountered a weird problem when testing the time library,

As shown in the figure below, in some cases (no rules are found), the order in which the code is executed affects the execution result of the function.

I added a MessageBox before the Return of ThinBasic_LzRtl.DLL, and the return value pops up. The return value is normal, but when the problem occurs, the return value displayed by ThinBasic is indeed 0.

10031

10032

The code on the graph can reproduce this problem 100%,

This problem only occurs with the ToUnixTime function, and it only appears when the function is executed for the first time.



I don't know if it is because of the system language. On my computer, ThinBasic will execute halfway and exit suddenly.

This problem occasionally appears,

I have feedback this question before, which is the test code of many MsgBox. Since the author and many people have not feedback this question, I suspect that it is caused by system language compatibility.

ErosOlmi
11-09-2019, 21:13
Thanks a lot.

I will check asap your code and reported strage behaves.
I'm under heavy work load at the moment :oops:

ErosOlmi
14-09-2019, 09:53
Ciao xLeaves

first of all thanks a lot for using thninBasic SDK Core engine, great work.
I think you are one of the few I know doing that so, thanks again.
I've seen that SDK for FreeBasic is not much complete as SDK for PowerBasic. My fault.
I will try to implement it in future thinBasic versions in order to have all the thinCore exported functions available also for FreeBasic.


Anyway, I've tested your library and seems ok to me in terms of execution.
10036


Regarding strange behaves ... I've only few doubts on how you parse some values using thinCore SDK.
The fact that code exit without executing all the parsing steps seems related to the fact that some of your functions generate a runtime error (GPF) exiting before the end but without crashing thinCore engine.

For example in Rtl_ToUnixTime SDK function "thinBasic_ParseDouble(dTime)" is executed even if ToUnixTime in script is called passing no parameters like "ToUnixTime()" and this can bring to strange behaves:

Function Rtl_ToUnixTime() As UInteger
Dim ParensPresent As Long = thinBasic_CheckOpenParens_Optional
Dim dTime As Double
Dim bMS As Integer

thinBasic_ParseDouble(dTime)
If dTime = 0 Then
dTime = Now()
EndIf

If thinBasic_CheckComma_Optional() = TB_TRUE Then
thinBasic_ParseLong(bMS)
Else
bMS = FALSE
EndIf

If bMS Then
Function = DateDiff("s", 25569.3333333333, dTime, 0, 0) * 1000
Else
Function = DateDiff("s", 25569.3333333333, dTime, 0, 0)
EndIf
If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory
End Function


I would have written something like the following that is a little more longer but more secure and can prevent some behaves from programming style:


Function Rtl_ToUnixTime() As UInteger
Dim dTime As Double
Dim bMS As Integer

'---Set default values
dTime = Now()
bMS = FALSE

'---Start parsing ...
'---First of all check if there is a (
If thinBasic_CheckOpenParens_Optional <> 0 Then

'---Check if user just called function with () without passing any parameter
If thinBasic_CheckCloseParens_Optional <> 0 Then
'---In this case do nothing! Function called as ToUnixTime()
Else
'---If there is a ( then we check if parameters were passed
'---OK start pasing some parameter
thinBasic_ParseDouble(dTime)

'---If optional , then we have another parameter
If thinBasic_CheckComma_Optional() = TB_TRUE Then
thinBasic_ParseLong(bMS)
End If

'---We anetered here because there was ( so there must e a )
thinBasic_CheckCloseParens_Mandatory
End If
End If
'---End parsing

'---Before executing we need to check if a runtime error occurred during parsing
If thinBasic_GetLastError = 0 Then
If bMS Then
Function = DateDiff("s", 25569.3333333333, dTime, 0, 0) * 1000
Else
Function = DateDiff("s", 25569.3333333333, dTime, 0, 0)
EndIf
End If

End Function


Let me know if it makes any difference.

Ciao
Eros

xLeaves
15-09-2019, 10:19
Dear ErosOlmi:

I will try to check if you can solve the problem according to your way.

FreeBasic does not support 64-bit numbers for return values (I tested the LongInt type and can't return, don't know why, please tell me if you know).

So the second parameter here is invalid, or the current data precision is not enough to zoom in to the millisecond timestamp.

I can consider removing this parameter (the same result can be achieved by multiplying the data by 1000 in ThinBasic)

xLeaves
15-09-2019, 10:22
Thanks a lot.

I will check asap your code and reported strage behaves.
I'm under heavy work load at the moment :oops:

Thank you
I will try to finalize any problem I find to a sample code that can be completely reproduced.
Minimize the cost of troubleshooting your mistakes

jack
16-09-2019, 08:49
hello xLeaves (https://www.thinbasic.com/community/member.php?1914-xLeaves)
regarding FreeBasic not being able to return a 64-bit integer, would you give an example that shows the problem?

primo
18-09-2019, 08:10
jack, i have checked the FB 1.07 on windows 7_x64

sub add(a as ULONGINT, b as ULONGINT)
print a+b
end sub

add 18446744073709551610, 5

EDIT: this is better:

Declare Function add (a as ULONGINT, b as ULONGINT) As ULONGINT
Function add(a as ULONGINT, b as ULONGINT) As ULONGINT
dim total as ULONGINT
total = a + b
return total
End Function

dim c as ULONGINT
c = add(18446744073709551610, 5)
print c
10038

it return the highest unsigned number for x64: 18446744073709551615
as reported here http://ctp.mkprog.com/en/freebasic/unsigned_64bit_integer/
this is the largest number for unsigned number x64
look also https://stackoverflow.com/questions/46442411/how-big-can-a-64-bit-unsigned-integer-be

note: a new FB version 1,07 released in 2019-08-27:
https://sourceforge.net/projects/fbc/files/Binaries%20-%20Windows/

xLeaves
12-10-2019, 11:24
Dear ErosOlmi:

I tried to change the code to look like the following:


Function Rtl_ToUnixTime() As UInteger
Dim ParensPresent As Long = thinBasic_CheckOpenParens_Optional
Dim dTime As Double
thinBasic_ParseDouble(dTime)
If dTime = 0 Then
dTime = Now()
EndIf
Function = DateDiff("s", 25569.3333333333, dTime, 0, 0)
If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory
End Function

And no longer pass optional parameters, the problem remains

The code reproduced by BUG is as follows:


Function Main() As Long
TracePrint("获取当天与十天后的时间差(返回天) : " & DateDiff("d", DateAdd("d", 10, Now()), Now()))
TracePrint("获取时间中天这部分的数据 : " & DatePart("d", Now()))
TracePrint("将时间格式化显示 : " & Format(Now(), "yyyy-mm-dd hh:mm:ss"))
TracePrint("当前Unix时间戳 : " & ToUnixTime(Now()))
TracePrint("当前Unix时间戳 : " & ToUnixTime(Now()))
End Function

The value of the fourth output is still 0. I tested it in the same way as before. I added a MessageBox function before the FreeBasic function returns. The data I returned is not 0.

xLeaves
12-10-2019, 11:30
Dear jack & primo :

I mean that in the development of the FreeBasic standard extension library, using the thinBasic_ReturnCodeQuad constant to specify the type of the return value is invalid, even if the function returns LongInt, the resulting data is incorrect.

For example the following FreeBasic code:


Function Rtl_Test() As LongInt
Dim ParensPresent As Long = thinBasic_CheckOpenParens_Optional
Function = 12345678901234
If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory
End Function

FUNCTION LoadLocalSymbols Cdecl ALIAS "LoadLocalSymbols" (BYVAL sPath AS STRING) AS Long EXPORT

thinBasic_LoadSymbol("Test" , thinBasic_ReturnCodeQuad , @Rtl_Test , thinBasic_ForceOverWrite)

Return 0
END FUNCTION

Load and run under ThinBasic, I get the result always -9.22337203685477581E+18