A strftime()-like function for GLB.

Previous topic - Next topic

Hatonastick

Here's a (rather klunky) function that emulates (for the most part) most of strftime() from C.  It's only been lightly tested so may contain bugs still, however is being used in a program of mine so if any appear I'll edit and post the fixes.

I was using an actual strftime() call using INLINE, but kept getting occasional, semi-random crashes on start-up that I never successfully tracked down, so I thought I'd make this function instead -- with the added bonus that at least this one is/should be cross-platform.

Code (glbasic) Select
// Example
PRINT Time$("Time: %h:%M%p"), 100, 200
SHOWSCREEN
MOUSEWAIT

// Return a time/date string based upon given format:
// %b: Abbreviated month name
// %B: Full month name
// %d: Day of the month (01-31)
// %H: Hour in 24-hour format
// %h: Hour in 12-hour format
// %m: Month (01-12)
// %M: Minute (00-59)
// %p: AM or PM
// %S: Second (00-59)
// %y: Year without century
// %Y: Year with century
// %%: Percent sign
FUNCTION Time$: fstring$
LOCAL output$, td$[], pos%, temp%

// Abbreviated month names
DIMDATA amonth$[], "NIL", "Jan.", "Feb.", "Mar.", "Apr.", "May", _
"June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."

// FUll month names
DIMDATA fmonth$[], "NIL", "January", "February", "March", "April", _
"May", "June", "July", "August", "September", "October", _
"November", "December"

// Get our local time and date, then split each section into an array
SPLITSTR(PLATFORMINFO$("TIME"), td$[], ":- ")

// Process our format string
pos% = 0
WHILE pos% < LEN(fstring$)
// Have we found a control character?
IF MID$(fstring$, pos%, 1) = "%"
pos% = pos% + 1
SELECT MID$(fstring$, pos%, 1)
// Abbreviated month name
CASE "b"
temp% = td$[1]
output$ = output$ + amonth$[temp%]
// Full month name
CASE "B"
temp% = td$[1]
output$ = output$ + fmonth$[temp%]
// Day of the month (01-31)
CASE "d"
output$ = output$ + td$[2]
// Hour in 24-hour format
CASE "H"
output$ = output$ + td$[3]
// Hour in 12-hour format
CASE "h"
// Convert from 24 hour time to 12 hour time
IF td$[3] > 12
temp% = td$[3]
output$ = output$ + (temp% - 12)
// 12 midnight
ELSEIF td$[3] = 0
output$ = output$ + "12"
// Handle all other instances
ELSE
output$ = output$ + td$[3]
ENDIF
// Month (01-12)
CASE "m"
output$ = output$ + td$[1]
// Minute (00-59)
CASE "M"
output$ = output$ + td$[4]
// AM or PM
CASE "p"
IF td$[3] >= 12
output$ = output$ + "PM"
ELSE
output$ = output$ + "AM"
ENDIF
// Second (00-59)
CASE "S"
output$ = output$ + td$[5]
// Year without century
CASE "y"
output$ = output$ + MID$(td$[0], 2, -1)
// Year with century
CASE "Y"
output$ = output$ + td$[0]
// Percent sign
CASE "%"
output$ = output$ + "%"
// Unrecognised format character, just copy it over
DEFAULT
output$ = output$ + MID$(fstring$, pos%, 1)
ENDSELECT
pos% = pos% + 1
// No control character, just copy it across
ELSE
output$ = output$ + MID$(fstring$, pos%, 1)
pos% = pos% + 1
ENDIF
WEND

RETURN output$
ENDFUNCTION

Kitty Hello

Why not use STRREPL and INSTR? :P
Good work!

Hatonastick

I was going to use INSTR and probably will when I do the rewrite. :)  What is STRREPL?  Isn't recognized by the editor and I can't find it in the help file.

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Hatonastick

Ahhhh, ok.  Where's my coffee...  ;/

CipCinue

Thanks for the VB code. I am trying to get it to work but the sub or function ErrorIn was not defined. COuld you post the ErrorIn sub or function. Thanks for the code it will make it easy for me to work with armadillo
Thanks
Ray

Hatonastick

Not quite sure what you are talking about.  Firstly this isn't Visual BASIC.  Secondly there is no ErrorIn sub or function in this code snippet. :)

amarliani

Thanks for that function. Makes my life easier, especially since it's very easy to adopt to german.