Function overload(ing) is also shown, with a numeric to string conversion (same name, different parameter's).
<edit> updated code to prevent compiler warning with: -w pedantic (compiler switch) </edit>
Code: Select all
' D_TandArr_OFunc.bas -- 2017-11-04, by MrSwiss
' Data-Type, conversion Array's and Overloaded Function's (in a nutshell)"
' compile: -s console (FBC x64-Win, ver.1.05.0, tested, -lang FB)
#Include Once "datetime.bi" ' FB internal .bi file (Date/Time stuff)
' initialized arrays to convert numbers (array index), to String
Dim As String * 9 mnth(1 To 12) = { "January", "February", "March", "April", "May", _
"June", "July", "August", "September", _
"October", "November", "December" }
Dim As String * 9 wday(1 To 7) = { "Sunday", "Monday", "Tuesday", "Wednesday", _
"Thursday", "Friday", "Saturday" }
Dim As Double DT = Now ' Prog. start: Date & Time
' because: the Function is used, before it is implemented!
Declare Function LeapYear(ByVal As ULong) As Boolean
Type status
'As ULong stp ' step (currently ??? - not used)
As ULong clr ' color
As ULong d ' day
As ULong m ' month
As ULong y ' year
As ULong hr ' hour
As ULong mn ' minute
As ULong sc ' second
As ULong dow ' day of week
As ULong fdow ' first day of week
As Boolean lyr ' leap year (yes/no = TRUE/FALSE)
As Boolean vis ' visible
As Boolean ini ' initialized
As Boolean res ' reserved
End Type
Dim As status stat
With stat
'.stp = 0
.clr = &hFF000000
.d = Day(DT)
.m = Month(DT)
.y = Year(DT)
.dow = Weekday(DT)
.hr = Hour(DT)
.mn = Minute(DT)
.sc = Second(DT)
.fdow = 2 ' Monday (or Sunday = 1)
.lyr = LeapYear(stat.y)
.vis = TRUE
.ini = TRUE
.res = FALSE
End With
' here: only used as: Type initializer (see above)
Function LeapYear(ByVal yea As ULong) As Boolean
If (yea Mod 4) Then Return FALSE ' definitely NOT a leap Year
' below: exception test (a century but, not also clean dividable by 400)
If Not(yea Mod 100) AndAlso Not(yea Mod 400) Then Return FALSE
' below: all tests passed = it is, a leap Year
Return TRUE
End Function
Function TimeString OverLoad( _
ByVal DaTi As Double, _ ' DateTime serial
ByVal h2d As Boolean = FALSE _ ' show leading zero(hour's only)
) As String
Dim As String sh, sm, ss, sd = ":"
Dim As ULong hr = Hour(DaTi), mn = Minute(DaTi), sc = Second(DaTi)
If Not(h2d) Then ' single digit hour (if < 10), default
sh = Str(hr)
If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
Else ' hour always 2 digits (displays leading "0")
If hr < 10 Then sh = "0" + Str(hr) Else sh = Str(hr)
If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
End If
Return sh + sd + sm + sd + ss
End Function
Function TimeString OverLoad( _
ByVal hr As ULong, _ ' hour
ByVal mn As ULong, _ ' minute
ByVal sc As ULong, _ ' second
ByVal h2d As Boolean = FALSE _ ' show leading zero(hour's only)
) As String
Dim As String sh, sm, ss, sd = ":"
If Not(h2d) Then ' single digit hour (if < 10), default
sh = Str(hr)
If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
Else ' hour always 2 digits (displays leading "0")
If hr < 10 Then sh = "0" + Str(hr) Else sh = Str(hr)
If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
End If
Return sh + sd + sm + sd + ss
End Function
Function TimeString OverLoad( _
ByRef stt As Const status, _ ' Type "status"
ByVal h2d As Boolean = FALSE _ ' show leading zero(hour's only)
) As String
Dim As String sh, sm, ss, sd = ":"
With stt
If Not(h2d) Then ' single digit hour (if < 10), default
sh = Str(.hr)
If .mn < 10 Then sm = "0" + Str(.mn) Else sm = Str(.mn)
If .sc < 10 Then ss = "0" + Str(.sc) Else ss = Str(.sc)
Else ' hour always 2 digits (displays leading "0")
If .hr < 10 Then sh = "0" + Str(.hr) Else sh = Str(.hr)
If .mn < 10 Then sm = "0" + Str(.mn) Else sm = Str(.mn)
If .sc < 10 Then ss = "0" + Str(.sc) Else ss = Str(.sc)
End If
End With
Return sh + sd + sm + sd + ss
End Function
' ===== MAIN =====
Dim As String ttl = "Data-Type, conversion Array's and Overloaded Function's (in a nutshell)"
Width 80, 30
Print ttl : Print String(Len(ttl), "~") : Print
Print "DateTime: "; DT
Print "Type Size: "; SizeOf(status)
Print
' using the Type
With stat
'Print "stp: "; Str(.stp)
Print "clr: "; "hex: "; Hex(.clr, 8); " dec: "; str(.clr)
Print "d: "; Str(.d)
Print "m: "; Str(.m)
Print "y: "; Str(.y)
Print "dow: "; Str(.dow)
Print "fdow:"; Str(.fdow)
Print "hr: "; Str(.hr)
Print "mn: "; Str(.mn)
Print "sc: "; Str(.sc)
Print "lyr: "; UCase(Str(.lyr))
Print "vis: "; UCase(Str(.vis))
Print "ini: "; UCase(Str(.ini))
Print "res: "; UCase(Str(.res))
End With
Print
Print "Program-Start Time & Date was:"
Print "------------------------------"
' using the Type, again
Print "Date: "; Trim(wday(stat.dow)); ", "; Str(stat.d); _
". "; Trim(mnth(stat.m)); " "; Str(stat.y); _
" (using arrays to get the Strings)"
Print "Time(1): "; TimeString(DT, TRUE); " OverLoad(ed) Function(DateTime serial)"
Print "Time(2): "; TimeString(Hour(DT), Minute(DT), Second(DT)); " OverLoad(ed) Function(hour, minute, second)"
Print "Time(3): "; TimeString(stat); " OverLoad(ed) Function(Type)"
Print : Print : Print "press a key to quit ... ";
Sleep
' ===== END MAIN ===== ' ----- EOF -----