So, I am creating a C++ UWP application that when opened, displays current date and time. In addition, I need to also find a way so that I can display the day of the week as well. For the day of the week, I am using this code:
#include <ctime>
#include <time.h>
#include <chrono>
//under Mainpage::Mainpage()
time_t timeObj = time(nullptr);
tm aTime;
localtime_s(&aTime, &timeObj);
String ^ weekofdayout;
switch (aTime.tm_wday)
{
case 1:
weekofdayout = "Monday";
break;
case 2:
weekofdayout = "Tuesday";
break;
case 3:
weekofdayout = "Wednesday";
break;
case 4:
weekofdayout = "Thursday";
break;
case 5:
weekofdayout = "Friday";
break;
default:
weekofdayout = "(NULL)";
break;
}
timetext->Text += weekofdayout;
However, whenever I run it, the textbox returns: (NULL) the default case. I don't know whats causing it.
I have taken the system time before successfully in windows forms c++ with this code:
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
DateTime datetime = DateTime::Now;
this->label3->Text = datetime.ToString();}
I was unable to port this over to UWP C++ because I think it uses the system namespace. Any ideas on how I would be able to recreate the same effect in c++?
For the stopwatch, I haven't found any real documentation online regarding UWP C++ stopwatch but I did find this: https://iot-developer.net/windows-iot/uwp-programming-in-c/timer-and-timing/stopwatch It isn't for C++ UWP but it is for C# UWP. I tried to make my own timer with a global function but it just freezes the program until the code is done running:
int seconds = 0;
for (int i = 0; ; i++)
{
seconds++;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (stopwatch == false)
{
break;
}
}
return seconds;
I'm pretty stumped, usually, I would find the answer online but due to the low documentation of UWP C++, I have no idea where to find the answer.