FormatStr() on Linux (ppc) - unexpected result

Report any Hollywood bugs here
Post Reply
gerograph
Posts: 69
Joined: Thu Mar 04, 2010 9:38 pm

FormatStr() on Linux (ppc) - unexpected result

Post by gerograph »

If using this code on windows and Hollywood 10:

Code: Select all

Function p_getDateYYYYMMDD()
	mo$ = "1" 
	mo$ = FormatStr("%02s", mo$)
	tag$ = "12" 
	tag$ = FormatStr("%02s", tag$)
	Return ("2026"..mo$..tag$)
EndFunction

str$ = p_getDateYYYYMMDD()
DebugPrint(str$)
It will properly output: 20260112

Using the same code in Void Linux on my X1000 it gives me: 2026 112. Seems like Linux interpretes "0" as "space"
Flinx
Posts: 351
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: FormatStr() on Linux (ppc) - unexpected result

Post by Flinx »

Your syntax is incorrect. You are attempting to output a string with two digits and a leading zero. But this is only defined for numbers, so to fix just this line:

Code: Select all

mo$ = FormatStr("%02d", ToNumber(mo$))
But your function doesn't seem to make sense to me at all. Why don't you use FormatDate()?

I don't know why the incorrect format string produces different results in Linux and Windows, but that doesn't really matter.
gerograph
Posts: 69
Joined: Thu Mar 04, 2010 9:38 pm

Re: FormatStr() on Linux (ppc) - unexpected result

Post by gerograph »

@Flinx
Thanx :-)
I don't know why the incorrect format string produces different results in Linux and Windows, but that doesn't really matter.
True, but still strange...
But your function doesn't seem to make sense to me at all. Why don't you use FormatDate()?
Didn't know this function existed (yeee! RTFM)...I found part of my code somewhere here on the forum... but seems FormatDate() does what I want. Thanx for the hint.
User avatar
airsoftsoftwair
Posts: 5871
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: FormatStr() on Linux (ppc) - unexpected result

Post by airsoftsoftwair »

gerograph wrote: Fri Jan 16, 2026 10:06 am Seems like Linux interpretes "0" as "space"
And that is correct according to C printf formatting rules. It's Microsoft's implementation which is off-standard here. I'm afraid there are a few platform-specific gotchas when dealing with printf string and you've just hit one. To workaround it, just use %d instead, e.g.

Code: Select all

Function p_getDateYYYYMMDD()
	mo$ = "1" 
	mo$ = FormatStr("%02d", Val(mo$))
	tag$ = "12" 
	tag$ = FormatStr("%02d", Val(tag$))
	Return ("2026"..mo$..tag$)
EndFunction

str$ = p_getDateYYYYMMDD()
DebugPrint(str$)
This will work safely everywhere.
Post Reply