Reply To: Introduction to CMAC. 8: GetSystemTime & GetLocalTime

Product Compare Forums Multi-Edit User Created Macros Introduction to CMAC. 8: GetSystemTime & GetLocalTime Reply To: Introduction to CMAC. 8: GetSystemTime & GetLocalTime

#3853
deleyd
Participant

By now you’re asking, “How can I convert a weekday number and month number into a weekday name and month name?”

As far as I know, CMAC does not support “an array of strings”. Instead, here’s an old programming trick that does the job nicely. This demonstrates the use of:

1. The CMAC copy function for extracting a substring from a string.
2. The CMAC Shorten_Str function for trimming off trailing blanks.
3. The CMAC C-like sprintf macro.

[code:c4bv6dsl]#include Win32.sh //Must come before #include MsgLog.sh
//(defines struct Tmsg used by MsgLog.sh)
#include MsgLog.sh //DebugLog is defined here.
#include SPrintF.sh //Defines the SPrintF macro

/* NOTE: Win32.sh already contains the following,
so we don’t have to include this ourselves:
#define PSystemTime Pointer
struct TSystemTime {
Word wYear;
Word wMonth;
Word wDayOfWeek;
Word wDay;
Word wHour;
Word wMinute;
Word wSecond;
Word wMilliseconds;
}
*/
import void GetLocalTime( PSystemTime St )
kernel32 ‘GetLocalTime’;

//MACRO TESTTIME
void testtime()
{
struct TSystemTime St;
GetLocalTime( &St );
DebugLog(2, "testtime",
"wYear=" + str(St.wYear) +
" wMonth=" + str(St.wMonth) +
" wDayOfWeek=" + str(St.wDayOfWeek) +
" wDay=" + str(St.wDay) +
" wHour=" + str(St.wHour) +
" wMinute=" + str(St.wMinute) +
" wSecond=" + str(St.wSecond) +
" wMilliseconds=" + str(St.wMilliseconds) );

/*
wMonth – The month; January = 1, February = 2, and so on.
wDayOfWeek – The day of the week; Sunday = 0, Monday = 1,...
*/
str weekday_names = "Monday " + // 0
"Tuesday " + // 1
"Wednesday" + // 2
"Thursday " + // 3
"Friday " + // 4
"Saturday " + // 5
"Sunday "; // 6
int weekday_name_length = 9;

str month_names = "January " + // 1
"February " + // 2
"March " + // 3
"April " + // 4
"May " + // 5
"June " + // 6
"July " + // 7
"August " + // 8
"September" + // 9
"October " + // 10
"November " + // 11
"December "; // 12
int month_name_length = 9;

str weekday;
str month;
str msgstr;

weekday = copy( weekday_names, (1 + (St.wDayOfWeek * weekday_name_length)), weekday_name_length);

month = copy( month_names, (1 + ((St.wMonth-1) * month_name_length)), month_name_length);

weekday = Shorten_Str(weekday); //Trim any trailing blanks
month = Shorten_Str(month); //Trim any trailing blanks

//Demonstration of C-like sprintf macro
SPrintF(msgstr, "%s, %s %u", weekday, month, St.wDay);
make_message(msgstr);

//Note: the last two lines, SPrintF & make_message, could
//have been combined into the following single line
//by using the "messagef" macro also defined in SPrintF.sh:
// messagef("%s, %s %u", weekday, month, St.wDay);
}
[/code:c4bv6dsl]

See file SPrintF.s in the Multi-Edit\src folder to see how the SPrintF macro works. (Note: a bug in the processing of the seldom used %f control code will be fixed in version 9.10.02)