Author |
Topic: FORMAT$ (Read 507 times) |
|
joker
Global Moderator
member is offline
Gender:
Posts: 157
|
|
FORMAT$
« Thread started on: Nov 7th, 2015, 11:00am » |
|
It is a shame there isn't a "format$" or even a "using$" in LB or LBB.
Date input has to be parsed out. All dates needed are not always "today's" date.
Sometimes "a" date is used to create file names, too.
Oh, a function we will go, a function we will go, hi ho the merry oh, a function we will go.
PS. That diddy will be going around in your head all day and night. ... You're welcome.
|
« Last Edit: Nov 7th, 2015, 11:01am by joker » |
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: FORMAT$
« Reply #1 on: Nov 7th, 2015, 11:20am » |
|
on Nov 7th, 2015, 11:00am, pnlawrence wrote:It is a shame there isn't a "format$" or even a "using$" in LB or LBB. |
|
Both LB and LBB have using$(), and LBB's version has a number of extensions, but it isn't particularly suited to date formatting.
Quote:Date input has to be parsed out. All dates needed are not always "today's" date. |
|
The GetDateFormat API can format dates in almost any way you might like, and isn't limited to today's date.
Commonly you must resort to using Windows API calls in LB/LBB. You shouldn't necessarily consider that to be a limitation - including all the things the API can do as native features of BASIC would be impractical and result in a hugely bloated language. It's arguable that Liberty BASIC doesn't strike the right balance between native and API functions, but at least the power of the API is available, and fairly straightforwardly so.
Richard.
|
|
|
|
joker
Global Moderator
member is offline
Gender:
Posts: 157
|
|
Re: FORMAT$
« Reply #2 on: Nov 7th, 2015, 11:37am » |
|
Quote:... result in a hugely bloated language.... |
|
Oh, you mean like a MS product?
I was just wishing before I parsed it out into year, month and day variables.
|
|
Logged
|
|
|
|
joker
Global Moderator
member is offline
Gender:
Posts: 157
|
|
Re: FORMAT$
« Reply #3 on: Nov 7th, 2015, 11:41am » |
|
This is what I've been working on. Untested as of now. Code:
function DateCode$(year,month,day,code$)
'function to format a date into YYYY-MMM-DD-X (e.g. X=“1”, “2”, “F”, “Y”, etc.)
year = int(abs(year)) : month = int(abs(month)) : day = int(abs(day)) : code$ = left$(code$,1) ' normalize values
code = asc(code$) : if (code < 48 and code > 57) and (code < 65 and code > 90) then code$ = "0" ' normalize value
select case ' work on the year entered
case year < 100 ' 2 digits "2000 - 2099"
year$ = "20" + right$("00" + str$(year),2) ' YYYY
case year < 2000 ' "1900 - 1999"
year$ = str$(year)
case year < 2100 ' "2000 - 2099"
year$ = str$(year)
case else
year$ = date$("yyyy")
end select
select case ' work on the month entered
case month > 0 and month < 10 ' 1 digit "1 - 9"
month$ = right$("00" + str$(month),2) ' MM
case month < 13 ' "10 - 12"
month$ = str$(month)
case else
month$ = date$("mm")
end select
select case ' work on the day entered
case day > 0 and day < 10 ' 1 digit "1 - 9"
day$ = right$("00" + str$(day),2) ' DD
case day < 32 ' "10 - 31"
day$ = str$(day)
case else
day$ = date$("dd")
end select
DateCode$ = year$ + "-" + month$ + "-" + day$ + "-" + code$
end function
|
|
Logged
|
|
|
|
Richard Russell
Administrator
member is offline
Posts: 1348
|
|
Re: FORMAT$
« Reply #4 on: Nov 7th, 2015, 12:00pm » |
|
on Nov 7th, 2015, 11:41am, pnlawrence wrote:This is what I've been working on. |
|
For comparison, an API date-formatting function:
Code: print DateFormat$(2015, 11, 7, "yyyy-MM-dd")
print DateFormat$(2015, 11, 7, "yyyy-MMM-dd")
print DateFormat$(2015, 11, 7, "yyyy-MMMM-dd")
print DateFormat$(2015, 11, 7, "dddd yyyy MMMM dd")
end
function DateFormat$(year,month,day,format$)
struct st, Year as short, Month as short, DayOfWeek as short, Day as short, _
Hour as short, Minute as short, Second as short, Milliseconds as short
st.Year.struct = year
st.Month.struct = month
st.Day.struct = day
date$ = space$(100) + chr$(0)
cch = len(date$)
calldll #kernel32, "GetDateFormatA", 0 as long, 0 as long, st as struct, _
format$ as ptr, date$ as ptr, cch as long, r as long
DateFormat$ = trim$(date$)
end function Richard.
|
|
|
|
joker
Global Moderator
member is offline
Gender:
Posts: 157
|
|
Re: FORMAT$
« Reply #5 on: Nov 7th, 2015, 1:03pm » |
|
Fantastic!
All I have to do is add a bit to the code$, and I have the custom string that I want.
Good show, Richard! Thanks!
EDIT: Added silly error checking code. Who makes errors in data entry? :D
Code:
customNum = 99
code$ = "yyyy-MM-dd"
print DateCode$(2015, 11, 7, "dd-MM-yyyy")
print DateCode$(2015, 11, 7, "dd-MMM-yyyy")
print DateCode$(2015, 11, 7, "dd-MMMM-yyyy")
print DateCode$(2015, 11, 7, "dddd dd MMMM yyyy")
' test for bad numbers or simplified way to use today's date (put in nonsense data)
result$ = DateCode$(2014, 12, 12, code$ +"-"+str$(customNum))
if result$ = "" then
result$ = date$(code$) + "-" + str$(customNum) ' use today's date
end if
print result$
end
function DateCode$(year,month,day,code$)
struct st, Year as short, Month as short, DayOfWeek as short, Day as short, _
Hour as short, Minute as short, Second as short, Milliseconds as short
st.Year.struct = year
st.Month.struct = month
st.Day.struct = day
date$ = space$(100) + chr$(0)
cch = len(date$)
calldll #kernel32, "GetDateFormatA", 0 as long, 0 as long, st as struct, _
code$ as ptr, date$ as ptr, cch as long, r as long
DateCode$ = trim$(date$)
end function
|
« Last Edit: Nov 7th, 2015, 1:33pm by joker » |
Logged
|
|
|
|
|