How to display date using C++? - function

How can I display the date using the function "MessageBox"?

Here is a link for several different ways to get the date and time:
Date & Time
Copied from site above:
Definition (from windows):
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
Implementation:
SYSTEMTIME st;
GetSystemTime(&st);
// You format how you want

DateTime dateTime = DateTime::Now;
MessageBox::Show(dateTime.ToString());
Other ToXString() functions can be found here

For example like this (I assumed you asked about native Windows API):
// Get current time
SYSTEMTIME now;
GetLocalTime(&now);
// Format the date using the default user language
TCHAR buffer[1024];
GetDateFormat(
MAKELCID(LANG_USER_DEFAULT, SORT_DEFAULT),
0,
&now,
NULL,
buffer,
1024
);
// Show it in a message box
MessageBox(HWND_DESKTOP, buffer, _T("Today"), MB_OK);
It's also possible to ask GetDateFormat to calculate the buffer length required to store the output. To do that pass NULL and 0 as last two parameters:
int length = GetDateFormat(
MAKELCID(LANG_USER_DEFAULT, SORT_DEFAULT),
0,
&now,
NULL,
NULL,
0
);

Related

What should I set the flags field of CUDA_BATCH_MEM_OP_NODE_PARAMS?

The CUDA graph API exposes a function call for adding a "batch memory operations" node to a graph:
CUresult cuGraphAddBatchMemOpNode (
CUgraphNode* phGraphNode,
CUgraph hGraph,
const CUgraphNode* dependencies,
size_t numDependencies,
const CUDA_BATCH_MEM_OP_NODE_PARAMS* nodeParams
);
but the documentation for this API call does not explain what the flags field of ... is used for, and what one should set the flags to. So what value should I be passing?
A related API function is cuStreamBatchMemOp
CUresult cuStreamBatchMemOp (
CUstream stream,
unsigned int count,
CUstreamBatchMemOpParams* paramArray,
unsigned int flags
);
it essentially takes the fields of CUDA_BATCH_MEM_OP_NODE_PARAMS as its separate parameters. Its documentation says that flags is "reserved for future expansion; must be 0".

Print a ULONG value with DbgPrint in kernel mode driver

I try to figure out how to print a certain value in a driver. In my case it is a ULONG value. At https://www.osronline.com/showthread.cfm?link=187470, it states that one should use the %U format specifier. So, I have the following code (only the relevant parts):
ULONG value;
value = 5;
DbgPrint("The value is: %U", value);
Compiling and loading works fine. But the "DbgView" output is not what I expected as you can see below:
The value is U
I hope someone can help. Thanks in advance.
Best regards
That is wrong
Per https://learn.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=vs-2017#type-field-characters
To format ULONG, please use:
%u, for decimal integer.
%x, for unsigned hexadecimal integer; uses "abcdef."
%X, for unsigned hexadecimal integer; uses "ABCDEF."

Use the same buffer for separate DATE and TIME

I am using the MySQL C api along with prepared statements. When issuing a INSERT query with a TIME field the result is in HHH:MM:SS format, where the hours are the amount of hours elapsed this month. For instance if the date is 2015-02-21, and the time is 21:30:00 the time would be displayed as 525:30:00 but I want to use the HH:MM:SS format instead (e.g 21:30:00), which would be the actual time of the day.
sbind[3].buffer_type=MYSQL_TYPE_DATE;
sbind[3].buffer= (char *)&ts; // Pointer to a MYSQL_TIME data structure
sbind[3].is_null= 0;
sbind[3].length= 0;
sbind[4] = sbind[3];
sbind[4].buffer_type=MYSQL_TYPE_TIME;
mysql_stmt_bind_param(stmt, sbind); // sbind is an array of MYSQL_BIND structures
ts.year= 1900+tm_info->tm_year; // tm_info is a pointer to a tm structure
ts.month= 1+tm_info->tm_mon;
ts.day= tm_info->tm_mday;
ts.hour= tm_info->tm_hour;
ts.minute= tm_info->tm_min;
ts.second= tm_info->tm_sec;
This code will prepare the date field as yyyy-mm-dd and fill it with the date in tm_info. Likewise it will do the same thing for the time field but in the HHH:MM:SS format.
A unfashionable way which works is to use a separate MYSQL_TIME structure for the time, but I aim for a more elegant way to handle this.
(EDIT: Here I have included the relevant client side code
MYSQL_TIME ts;
MYSQL_STMT *stmt;
MYSQL_BIND sbind[2];
...
char query[QUERY_BUFFER_SIZE];
strcpy(query, "INSERT INTO `mytable` (date,time) VALUES(?,?)");
if(mysql_stmt_prepare(stmt, query, strlen(query))){
return mysql_stmt_errno(stmt);
}
...
time_t rawtime;
time(&rawtime); // get current time
struct tm *tm_info = localtime ( &rawtime );
...
memset(sbind,0,sizeof(sbind));
sbind[0].buffer_type=MYSQL_TYPE_DATE;
sbind[0].buffer= (char *)&ts; // Pointer to a MYSQL_TIME data structure
sbind[0].is_null= 0;
sbind[0].length= 0;
sbind[1] = sbind[0];
sbind[1].buffer_type=MYSQL_TYPE_TIME;
mysql_stmt_bind_param(stmt, sbind); // sbind is an array of MYSQL_BIND structures
ts.year= 1900+tm_info->tm_year; // tm_info is a pointer to a tm structure
ts.month= 1+tm_info->tm_mon;
ts.day= tm_info->tm_mday;
ts.hour= tm_info->tm_hour;
ts.minute= tm_info->tm_min;
ts.second= tm_info->tm_sec;
if(mysql_stmt_execute(stmt)){
return mysql_stmt_errno(stmt);
}
This assumes mysql is a valid connection. The table mytable in this case only contains a DATE type and a TIME type.
)
That's the natural representation for the TIME type - it, as the name suggests, only holds time, and this is the natural way to express time larger than a day. It does, as the documentation suggests, use HH:MM:SS for smaller values.
Since TIME does not care about shenanigans like leap seconds, to exclude full days, just take tm_hour%24. But, to allow for shenanigans like DST transitions, you have nothing to do but add the TIME to the starting point of the specific month and do DATETIME arithmetic with the stock functions.
#c45602234:
Trying to outsmart libmysql FAILED (mysql-connector-c-6.1.5/libmysql/libmysql.c:1964):
static void store_param_date(NET *net, MYSQL_BIND *param)
{
MYSQL_TIME tm= *((MYSQL_TIME *) param->buffer);
tm.hour= tm.minute= tm.second= tm.second_part= 0;
net_store_datetime(net, &tm);
}
As you can see, it always uses up the entire structure (apparently, so that relevant functions always work as expected).

Using strcmp() in my cgi code, for an html webpage, is causing a server error

I am making an html webpage that uses cgi to access a table/database in mysql. I input a .csv file containing info on my class schedule and the html displays it in the usual schedule table.
My problem is that I can't seem to use strcmp in my parsing cgi as it causes a server error. here is an excerpt of my code where I uses strcmp.
void parse2(char *queu)
{
//---------------------------------------------------------------
char *saveptr[1024];
char *subtoken;
char *Subject;
char *Day;
char *Start;
char *End;
char *Room;
char *Teacher;
int check = 1;
//---------------------------------------------------------------
subtoken = strtok_r(queu, ",", saveptr);
check = strcmp(subtoken, "\0");
printf("%d<br>", check);
if(check == 0){
printf("Error!");
} else {
Subject = subtoken;
Day = strtok_r(NULL, ",", saveptr);
Start = strtok_r(NULL, ",", saveptr);
End = strtok_r(NULL, ",", saveptr);
Room = strtok_r(NULL, ",", saveptr);
Teacher = strtok_r(NULL, ",", saveptr);
printf("%s\n<br/>%s\n<br/>%s\n<br/>%s\n<br/>%s\n<br/>%s\n", Subject, Day, Start, End, Room, Teacher);
//inputsql(Subject, Day, Start, End, Room, Teacher);
}
//---------------------------------------------------------------
}
Note that, I have tested this code and it works fine without me calling strcmp().
I am using strcmp() to prevent a line of unwanted characters, generated after the info when retrieved using POST method, from being entered into my database.
As you can see from the above code, I used strtok() to parse the line of info. Since the line of unwanted characters do not contain a comma (which is my delimiter) it should return a NULL value. correct?
Can anyone help me out? I welcome suggestions to use a different way on solving the problem I chose to solve using strcmp().
I think you should be checking subtoken == NULL, not strcmp(subtoken, "\0") == 0.
"\0" is a string containing a NUL byte, then another NUL (the terminator), so the standard library's string functions will just see an empty string. That's different to a NULL pointer (i.e. a pointer with value zero).
From STRTOK(3):
Each call to strtok() returns a pointer to a null-terminated string
containing the next token. This string does not include the
delimiting byte. If no more tokens are found, strtok() returns NULL.

MySQL C API using results

I am using the MySQL C API to query the database and I have the results stored in MYSQL_ROW types. I am able to print the results to the console with
printf("%s", row[0]);
however, according to the MySQL C API documentation, I cannot use them as null-terminated strings.
At the bottom of the function overview, they say I can "extract" the information with mysql_store_result() or mysql_use_result(). However, I am still confused as to how this is done.
Ideally, I want to use the results as a string so I can do stuff like strcmp, but otherwise I definitely need to use the information somehow with those two functions.
Can somebody show me an example of how to do this?
Basically, you call mysql_store_result() or mysql_use_result() to access the result set, the former loads all the rows into memory on the client side, the latter accesses rows one at a time from the server. If you use mysql_use_result(), you need to call mysql_fetch_row() to access each row until the function returns NULL. Each successful call to mysql_fetch_row() will return a MYSQL_ROW which you can use to access the individual field values.
Since the fields are not nul-terminated, you need to use mysql_fetch_lengths() to get the lengths of each of the fields so that you can copy them somewhere else via memcpy, etc.
Since the field values are not nul-terminated you will need to add your own NUL character when you make the copy if you want to use it as a string. Be aware that the field values may contain binary data, so if you do treat it as a string, functions that expect a C string will stop processing data if it encounters a nul-character in the data.
Here is an example from the documentation that should help you put all this together:
MYSQL_ROW row;
unsigned int num_fields;
unsigned int i;
num_fields = mysql_num_fields(result);
while ((row = mysql_fetch_row(result)))
{
unsigned long *lengths;
lengths = mysql_fetch_lengths(result);
for(i = 0; i < num_fields; i++)
{
printf("[%.*s] ", (int) lengths[i],
row[i] ? row[i] : "NULL");
}
printf("\n");
}