dodicat wrote:The most versatile is Format, but does it work in Linux?
I've just done a speed test, one of the tested methods is
Format( Now, "dd/MM/yyyy" )
and, the
most surprising *outcome* was, Format's return =
08.04.2018 and not:
08/04/2018, as expected (after consulting FB-Doc).
It seems that, after testing with both current WIN-FBC versions x32 (GAS) and, x64 (GCC),
that Format isn't working properly, with
*some of the options*, at least!!
Ohhh, btw. it was also, by far, the slowest method ... (which was expected!)
dates_timing_test.bas:
Code: Select all
' dartes_timng_test.bas -- 2018-04-08, MrSwiss
'
' compile: -s console (or use 'quick run' from IDE)
'
' IMPORTANT: never ever use compiler's optimizers, in any timing-tests !!!
' (it might 'optimize out' parts, of the following code!)
'
#Include "vbcompat.bi"
Declare Function MyDate( ByVal nChr As UByte=0 ) ByRef As Const String
Dim As Double t, te, tt1, tt2, tt3 ' timer var's
Dim As UInteger n, z ' loop counter's
Dim As String sam, sch, sfr ' target string's (tests)
#Define min 1 ' timing loop start (n & z loop)
#Define max 10000 ' timing loop end (n loop)
#Define nRun 100 ' number of run's (z loop) end
Const As UByte delim = Asc("/") ' delimiter defintion
' inform user: to wait (until tests concluded)
Print "please wait ... test in progress"
Print String(32, "~")
Print
' we don't want to measure the empty loop timing <-- sensitive to 'optimizations'!
te = Timer
For n = min To max ' optimizers 'love it', to 'kick out' empty loops, _
Next ' to them, they 'never have a purpose' (wrong here!)
te = Timer - te ' establish: empty loop time (te)
' all print statements, removed from timing loop's (corrupting results)
' increase (t) start, by empty loop time (te) = decrease to 'real' runtime
For z = min To nRun ' z loop's time isn't considered (not needed!)
t = Timer + te ' adding (te) to start timer (t)
For n = min To max
sam = date ' actual procedure under test
Next
tt1 += Timer - t ' pure 'real' runtime totalizer (tt1)
t = Timer + te ' adding (te) to start timer (t)
For n = min To max
sch = MyDate( delim ) ' actual procedure under test
Next
tt2 += Timer - t ' pure 'real' runtime totalizer (tt2)
t = Timer + te ' adding (te) to start timer (t)
For n = min To max
sfr = Format( Now, "dd/mm/yyyy" ) ' actual procedure under test
Next
tt3 += Timer - t ' pure 'real' runtime totalizer (tt3)
Next
' show results to user ...
print "datatype"; Tab(31); "time average (100 run's * 10'000)"
Print String(64, "-")
print "String's" : Print String(18, "-")
' the following calculations don't influence the test itself!
print sam; Tab(30); (tt1 / nRun) * 1000; Tab(50); "mS -- Date"
print sch; Tab(30); (tt2 / nRun) * 1000; Tab(50); "mS -- MyDate( delim )"
Print sfr; Tab(30); (tt3 / nRun) * 1000; Tab(50); "mS -- Format( Now, "; """dd/mm/yyyy"""; " )"
Print String(64, "-")
Print
print "... done ... press a key, to exit ! ";
Sleep
' ===== End test/demo code =====
' ----- start implementation -----
Function MyDate( _ ' day/month swap (optional: 'user defined' delim.)
ByVal nChr As UByte=0 _ ' default = american delim.
) ByRef As Const String ' reference to cDate (Static, read only!)
Static As String cDate, aDate ' local static var's
' check for: delimitter- or date-change (update not made otherwise!)
If nChr <> Asc(Mid(cDate, 3, 1)) OrElse aDate <> Date Then
aDate = Date ' update aDate (american, for comparison!)
If nChr > 0 Then ' 'user defined' delim.? --> build cDate
cDate = Mid(aDate, 4, 2) + Chr(nChr) + Left(aDate, 2) + _
Chr(nChr) + Right(aDate, 4)
Else ' default, american delim. used
nChr = Asc(Mid(aDate, 3, 1)) ' get delim. --> build cDate
cDate = Mid(aDate, 4, 2) + Chr(nChr) + Left(aDate, 2) + _
Chr(nChr) + Right(aDate, 4)
End If
End If
' return pre-made or newly created cDate-String (on change, only!)
MyDate = cDate ' the same as: Function = cDate / Return cDate
End Function
' ----- end implementation ----- ' ----- EOF -----