have you ever wondered, whether something like that is possible in FB ?
The answer is obviously: YES (otherwise, this post whould be pointless).
I'm not the person for a lot of (useless?) words I prefer it, that the code and the comments in it, do the explaining instead.
As this example shows, it is not only auto-updating but also auto-converting it's own internals
(US-Date to ISO-Date and Time) which probably all none-US-members here, will appreciate:
Code: Select all
' DtTm_t-test1.bas -- (c) 2021-01-31, MrSwiss
'
' compile: -s console
'
Type DtTm_t ' automatic US-/ISO-date conv. & update
Public: ' interface: externally accessible stuff
Declare Constructor() ' default constructor (type initializer)
Declare Destructor() ' default destructor
Declare Operator Cast () As String ' date & time (default return)
Declare Function sdate() As String ' date only
Declare Function stime() As String ' time only
Private: ' any external access prohibited
Declare Sub update() ' only internally callable (as below)
Declare Sub mkISO(ByVal As Const ZString Ptr, ByVal As ZString Ptr)
As ZString * 11 cdate, adate ' uses less memory than string, e.g. _
As ZString * 9 ctime ' with string/FBC 64: 72 bytes
As UByte padd ' type alignment only (to 32 bytes)
End Type
Constructor DtTm_t()
With This
.ctime = Time
.adate = Date
.mkISO(.adate, .cdate) ' convert US- to ISO-date and assign
End With
End Constructor
Destructor DtTm_t() ' default dtor (free allocated memory)
With This
DeAllocate(@.cDate) ' deallocate zstring's memory
DeAllocate(@.aDate)
DeAllocate(@.cTime)
End With
End Destructor
Operator DtTm_t.Cast() As String
With This
.update() ' assure data is current
Return .cdate + " " + .ctime
End With
End Operator
Private Sub DtTm_t.update() ' when none changed, do nothing
Var ts = Time ' instead of multiple 'Time' calls
With This
If ts > .ctime Then ' only new time
.ctime = ts
ElseIf ts < .ctime Then ' new time & date (date change!)
.ctime = ts
.adate = Date
.mkISO(.adate, .cdate) ' convert US- to ISO-date and assign
End If
End With
End Sub
' separated: to avoid redundant code (ctor() and update() use it)
Private Sub DtTm_t.mkISO( _ ' US-date to ISO-date converter
ByVal pad As Const ZString Ptr, _ ' source ptr, read only
ByVal pcd As ZString Ptr _ ' target ptr, read/write
) ' MANDATORY: ptr's must be dereferenced!
*pcd = Right(*pad, 4) + "-" + _ ' year (first in ISO)
Left(*pad, 2) + "-" + _ ' month (second in ISO)
Mid(*pad, 4, 2) ' day (last/third in ISO)
End Sub
Function DtTm_t.sdate() As String
This.update() ' assure data is current
Return This.cdate
End Function
Function DtTm_t.stime() As String
This.update() ' assure data is current
Return This.ctime
End Function
' end type procedures
' ===== test1 =====
Dim As DtTm_t ISO ' one instance of type (ctor call)
Print "DtTm_t size:"; SizeOf(DtTm_t); " Bytes" ' show size of type
Print
Print ISO ' Cast Operator call, date & time
Sleep(1750, 1) ' prove the 'automatic update' feature
Print : Print ISO.stime() ' just the time
Print ISO.sdate() ' just the date
Print : Print
Print "... done ... ";
Sleep
' ===== end test1 ===== ' ----- EOF -----
IOW, there is quite a lot of improving it to be done, which I'm leaving to: DIY extensions.
(I'm NOT going to post the other version here.)