How to create an hstring from C++/WinRT DateTime? - c++-winrt

I would like to know what's the simplest way to convert
Windows::Foundation::DateTime dt = winrt::clock::now(); into an hstring?

There is no "conversion" per se. I suspect what you're looking for is some kind of date/time formatting utility.
C++20 will bring greatly improved support in this domain, but it's not here yet.
The portable, but feature-poor way to do this is to convert to time_t and use the C library. https://en.cppreference.com/w/cpp/chrono/c
The full-featured way of doing this in WinRT is to use the date formatting utilities here https://learn.microsoft.com/en-us/uwp/api/windows.globalization.datetimeformatting

It is not pretty, but you could do this:
winrt::Windows::Foundation::DateTime dt = winrt::clock::now();
std::time_t t = dt.time_since_epoch().count();
std::wstringstream wss;
wss << std::put_time(std::localtime(&t), L"%m/%d/%Y %H:%M:%S");
winrt::hstring hs{ wss.str() };

Related

Convert table to string, then back again

I would like to know how to convert a table to a string, and then back again.
I want to use the sockets module to send a table, but I must do it through a string.
I would like to do it like this:
a = { 1, 2, 3 } -- create table
b = tostring(a) -- convert table to string
c = totable(b) -- convert string back to table
There are lots of existing Lua libraries for this.
See http://lua-users.org/wiki/TableSerialization
Table serialization functions are pretty straight forward, writing your own is a good learning exercise.
PS. Just checked...The love2D API has a table serialization library in it already.
As others have said, you can't serialize everything easily, but you can serialize a great many things. For this kind of IPC, JSON is the current lingua franca, and I highly recommend it, especially since you can fairly safely interchange with many other languages out there.
Lua has several implementations, but check out this one especially, as it works well, is pretty stable, and there's a good level of maintenance activity on github. Example code:
json = require("json")
encoded = json.encode(someVar)
decoded = json.decode(someStr)

cmake: add defines of data types handled as string

Standard C
I need to add data types to project because GNU do not understand some C51 data types. Example need a BYTE types as:
#define BYTE unsigned char
Have tried following examples:
add_definitions(-DBYTE=\"unsigned char\")
add_definitions(-DBYTE="unsigned short")
add_definitions(-DBYTE="\"unsigned long\"")
Some other ideas?
thanks :-)
This should work:
add_definitions("-DBYTE=unsigned char")
Antonio's suggestion is pretty good. But in case you are looking for using the defines at configure time, you can use the approach mentioned in your question.
To add more information, you can use something like:
set(MYDEFINES -DVAR1=value1 -DVAR2=value2)
add_definitions(${MYDEFINES})
add_definitions() accepts a list which is very useful sometimes. You don't have to convert to a string.

Converting std::int to System::Single

Apologies if there's an answer out there already; but all I seem to be getting is a bunch of "I want to turn my 1 into a 1.0" chaff from my Google searches.
First things first. No, I'm not talking about a simple Convert::ToSingle() call. Rather, I need to convert the representation of the data to a System::Single.
So in other words, I'd like to take int myInt = 1065353216;, and the result should be something like 1.000. I know the pure c++ method would be something like float myFloat=*(float *)&myInt;;but I need the managed version.
Thanks in advance for your help.
If you're in C++/CLI, you can do it the same way as you do in C++: float myFloat=*(float*)&myInt;
In pure managed-land, there are built-in methods to do this for double & Int64 (DoubleToInt64Bits and Int64BitsToDouble, but not for single & Int32. However, if you look at the implementation of those methods (MS Reference Source), you'll see that they're doing the exact same thing as you have listed, so that's also the managed way to do it. The only difference is if you do it in C#, you have to tag the method as unsafe.

Pig - How to cast datetime to chararray

I'm using CurrentTime(), which is a datetime data type. However, I need it as a chararray. I have the following:
A = LOAD ...
B = FOREACH A GENERATE CurrentTime() AS todaysDate;
I've tried various approaches, such as the following:
B = FOREACH A GENERATE (chararray)CurrentTime() AS todaysDate;
However, I always get ERROR 1052: Cannot cast datetime to chararray.
Anyone know how I can do this? By the way, I'm very new to pig. Thanks in advance!
I had a similar issue and I didn't want to use a custom UDF as described in the other answer. I am pretty new with Pig but it seems a pretty basic operation to justify the need of an UDF. This command works great for me:
B = FOREACH A GENERATE ToString(yourdatetimeobject, 'yyyy-MM-dd\'T\'HH:mm:ssz') AS yourfieldname;
You can select the format you want by looking at the SimpleDateFormat javadoc
You need to create a custom UDF which does the conversion
(e.g: see CurrentTime() implementation). Alternatively you may check out my answer on a similar topic for workarounds.
If you are on AWS, then use their DATE_TIME UDF.

How to convert Boolean type into "ON" "OFF" using String.Format

Is there are any way to convert Boolean type into "ON"/"OFF" using string formatter, like:
Dim inpValue as Boolean
Dim outValue = String.Format("0:ON;OFF", inpValue)
' should show OFF as output
without code or IFormatProvider?
What is wrong with:
Dim inpValue as Boolean
Dim outValue = If(inpValue, "ON", "OFF")
Here's a crazy trick from another question but it's completely unreadable. I strongly advise you not to do this! If you are of nervous disposition, stop reading now.
String.Format("{0:ON;ON;OFF}", CDbl(inpValue))
This converts the Boolean to -1 or 0 and then uses a custom numeric format.
This should help you:
http://www.developmentnow.com/g/38_2004_4_0_0_238356/iformatprovider-for-boolean.htm
There isn't any built-in support that I am aware of. However, depending on how much control you have, you can create your own format provider (IFormatProvider). Here is a link to a discussion on this: bool format provider.
You can't do this using String.Format (without implementing IFormatProvider which seems like a overkill for your purposes), but you can do it in one line using IIf:
IIf(inpValue, outValue=True, outValue=False)
Personally, I recommend using an If/Else statement instead of IIf, as IIf is not intuitive for some programmers coming from other languages.
c# - not the most elegant but effective:
string outValue = inpValue.ToString().Replace("False", "OFF").Replace("True", "ON");