Author Topic: Slick-C equivalent for sprintf()  (Read 1834 times)

Marcel

  • Senior Community Member
  • Posts: 165
  • Hero Points: 11
Slick-C equivalent for sprintf()
« on: November 08, 2011, 03:14:23 pm »
Hi,
I probably didn't look hard enough, but I couldn't find a string formatting function akin to C sprintf().  At the very least, it should be able to do something like

bfr = format('%04d%c%02d%c%02d',year,'/',month,'/',day);

There are some doc references to nls() but it doesn't quite have that power.

Thanks

hs2

  • Senior Community Member
  • Posts: 2732
  • Hero Points: 284
Re: Slick-C equivalent for sprintf()
« Reply #1 on: November 09, 2011, 11:40:11 am »
AFAIK there is no such thing as sprintf. I've written this helper macro function time ago to handle formatted dates in a way I want. Could be a starting point at least..
Code: [Select]
/**
 * get compact date
 *
 * @param delim   opt. delimiter of date tokens
 * @param yyyy    true: 4 digit year - false: 2 digits
 * @param lzero   leading zero of each date token/number zero of year (was useful until 2009 and short 2 digit year format)
 *
 * @return  compact date string
 */
static _str getcdate ( _str delim = '', boolean yyyy = false, boolean lzero = true )
{
   _str month, day, year;
   parse _date() with month'/'day'/'year;
   if (lzero && length(month)<2) month='0'month;
   if (lzero && length(day)<2)   day='0'day;
   if (length(year)>2 && !yyyy)  year=substr(year,3);
   return ( year delim month delim day );
}

Example macro command to use it:
Code: [Select]
_command void cdate( void )
{
   _insert_text( getcdate( '-' ) );
}

Hope it helps,
HS2
« Last Edit: November 09, 2011, 12:46:03 pm by hs2 »