Is it possible to work with DateTime value in C++? - c++

I'm working with an API which offers date and time data as a double precision decimal value, which is compatible with the Windows Date/Time format. I'm writing a program in C++ and I'd like to be able to access elements of the data contained within the double Date/Time value and format them as human readable. For example take the Date/Time value like so 41411.583333 and print a string as DD/MM/YYYY HH:MM:SS using C++. Please can someone explain if/how this can be done?

The COleDateTime class (provided in the MFC and ATL libraries) provides everything you need. It includes a Format member function that can print date/time in numerous formats:
http://msdn.microsoft.com/en-us/library/c1ayhyhk(v=vs.80).aspx

There's also the Boost.Date_Time library. Documentation here.

Related

Delete record by time in InfluxDB-CXX

Reference: https://github.com/offa/influxdb-cxx
It is easy to delete record by time using CLI interface,
delete from imagetable where time='2022-11-16T19:42:41.945508272Z'
but I am not able to figure out how to do the same with influxdb-cxx. i.e. not able to access the time through C++ interface.
e.g. Tags can be accessed with function points[0].getTags() but how to access the time ?
Have already tried to access it with points[0].getTimestamp() but not able to print it in this format in C++ 2022-11-17T03:37:25.934547412Z
can anyone please help ? Thanks in advance.
In influxdb-cxx you can use InfluxDB::execute method to execute InfluxQL statements like from your example for CLI interface. Regarding timestamps, they are saved as std::chrono::time_point<std::chrono::system_clock> (source) in the library's Point class, which denotes Unix (epoch) time excluding leap seconds (which is what timestamps in InfluxDB represent). Your example uses RFC3339 notation to provide timestamp, but InfluxQL also directly understands "nanosecond count since epoch" notation for it (example). So, it isn't necessary to represent Point's timepoint in RFC3339 notation to use it in execute command (which is possible, but harder and reduntant), you can just use standard C++ chrono library functions to get nanoseconds since epoch for given timepoint. Example:
using namespace std::chrono;
auto nsEpoch = duration_cast<nanoseconds>(points[0].getTimestamp().time_since_epoch()).count();
idb->execute("delete from imagetable where time=" + std::to_string(nsEpoch));

Parsing a date and time from a string in the current system format on Windows

I need to parse a string that is supposed to consist of a date and time in the system's current date/time format settings. I can use, for example, std::get_time(), but the problem is that I don't know what date/time format is set in the system.
I have found several ways to get the date/time format using:
GetLocaleInfo/GetLocaleInfoEx with LOCALE_SSHORTDATE and LOCALE_SSHORTDATE
or std::time_get<char>::dateorder()
but in this way, I get "incompatible format" with functions that parse date/time (std::get_time()) and it needs to be converted:
GetLocaleInfo() gives me "MM/dd/yyyy H:mm:ss", and this needs to be converted to something like "%m/%d/%Y %H:%M:%S".
std::time_get<char>::dateorder() gives a more accurate order in date format:
std::time_get<char>::dmy
std::time_get<char>::mdy
std::time_get<char>::ymd
std::time_get<char>::ydm
but it is still necessary to define the separator and the time format.
In general, I don't mind manual conversion, but I'm wondering if there is a more direct way that gives the system date/time format that is compatible with the date/time parsing functions.
std::get_time uses the std::time_get facet.
std::time_get uses the POSIX strptime interface for its flags.
The POSIX strptime specification states that:
%x The date, using the locale's date format.
%X The time, using the locale's time format.
Whether or not this will actually work in your environment is another matter. However, it is worth giving this a try.

How to convert imported date variable to the original format in Stata?

My original date variable is like this 19jun2015 16:52:04. After importing, it looks like this: 1.77065e+12
The storage type for the new imported variable is str11 and display format is %11s
I wonder how I can restore it back to date format?
William Lisowski gives excellent advice in his comment. For anyone using date-times in Stata, there is a minimal level of understanding without which confusion and outright error are unavoidable. Only study of the help so that your specific needs are understood can solve your difficulty.
There is a lack of detail in the question which makes precise advice difficult (imported -- from what kind of file? using which commands and/or third party programs?), except to diagnose that your dates are messed up and can only be corrected by going back to the original source.
Date strings such as "19jun2015 16:52:04" can be held in Stata as strings but to be useful they need to be converted to double numeric variables which hold the number of milliseconds since the beginning of 1960. This is a number that people cannot interpret, but Stata provides display formats so that displayed dates are intelligible.
Your example is when converted a number of the order of a trillion but if held as a string with only 6 significant figures you have, at a minimum, lost detail irretrievably.
These individual examples make my points concrete. di is an abbreviation for the display command.
clock() (and also Clock(), not shown or discussed here: see the help) converts string dates to milliseconds since Stata's origin. With a variable, you would use generate double.
. di %23.0f clock("19jun2015 16:52:04", "DMY hms")
1750351924000
If displayed with a specific format, you can check that Stata is interpreting your date-times correctly. There are also many small variations on the default %tc format to control precise display of date-time elements.
. di %tc clock("19jun2015 16:52:04", "DMY hms")
19jun2015 16:52:04
The first example shows that even date-times which are recent dates (~2016) and in integer seconds need 10 significant figures to be accurate; the default display gives 4; somehow you have 6, but that is not enough.
. di clock("19jun2015 16:52:04", "DMY hms")
1.750e+12
You need to import the dates again. If you import them exactly as shown, the rest can be done in Stata.
See https://en.wikipedia.org/wiki/Significant_figures if that phrase is unfamiliar.

Excel international date formatting

I am having problems formatting Excel datetimes, so that it works internationally. Our program is written in C++ and uses COM to export data from our database to Excel, and this includes datetime fields.
If we don't supply a formatting mask, some installations of Excel displays these dates as Serial numbers (days since 1900.01.01 followed by time as a 24-hour fraction). This is unreadable to a human, so we ha found out that we MUST supply a date formatting mask to be sure that it displays readable.
The problem - as I see it - is that Excel uses international formatting masks. For example; the UK datetime format mask might be "YYYY-MM-DD HH:MM".
But if the format mask is sent to an Excel that is installed in Sweden, it fails since the Swedish version of the Excel uses "ÅÅÅÅ-MM-DD tt:mm".
It's highly impractical to have 150 different national datetime formatting masks in our application to support different countries.
Is there a way to write formatting masks so that they include locale, such that we would be allowed to use ONE single mask?
Unless you are using the date functionality in Excel, the easiest way to handle this is to decide on a format and then create a string yourself in that format and set the cell accordingly.
This comic: http://xkcd.com/1179/ might help you choose a standard to go with. Otherwise, clients that open your file in different countries will have differently formatted data. Just pick a standard and force your data to that standard.
Edited to add: There are libraries that can make this really easy for your as well... http://www.libxl.com/read-write-excel-date-time.html
Edited to add part2: Basically what I'm trying to get at is to avoid asking for the asmk and just format the data yourself (if that makes sense).
I recommend doing the following: Create an excel with date formatting on a specific cell and save this for your program to use.
Now when the program runs it will open this use this excel file to retrieve the local date formatting from the excel and the specified cell.
When you have multiple formats to save just use different cells for them.
It is not a nice way but will work afaik.
Alteratively you could consider creating an xla(m) file that will use vba and a command to feed back the local formatting characters through a function like:
Public Function localChar(charIn As Range) As String
localChar = charIn.NumberFormatLocal
End Function
Also not a very clean method, but it might do the trick for you.

Setting Excel NumberFormat for Dates ignoring the locale in native C++

I'm using Excel COM automation from native C++. My goal is to set the NumberFormat of a range for dates. The Problem is, the format string Excel expects depends on the user's locale.
Example:
If the locale is english i have to set the NumberFormat to "dd.mm.yyyy"
If the locale is german i need to use "TT.MM.JJJJ" to get the same result.
I'v found a lot of solutions for .NET, unfortunately i don't have that luxury. The usual solution was to temporarily set the locale of the the Excel thread to english, but it doesn't seem possible with native C++.
I could read the default system locale and then hardcode different format strings, but this won't work as soon as someone uses a locale i haven't included explicitely, so this is a bad solution.
So, how do i solve this problem so it works no matter which locale is used?
One possible solution is to use the system time formatter (like: _tcsftime, strftime, wcsftime) to format the date/time structure to the locale of the running system. Since both your program and excel is expected to run on the same PC, it should work properly without even knowing how the local date/time string look like.
As a fall-back solution, you may have to provide a series of hard-coded format string and/or a user-manually-entered string in the option-configuration section of your program, just in case the default locale format fails.
Additional informations after first reply comment from OP: --------------
locale setting affect the text-display presentation of a datetime-binary-object in the UI only. Such datetime-binary-object is alway stored as a binary structure in a very consistent way, irregardless of locale setting even in its each different contexts, be it in Excel (Date/Time format cell), C++ (various structure depends on api), VisualBasic (Variant).
So if your intention is to export the datetime binary structure to excel, not as a strng, you can alway use the "variant" type to store the date value as excel-automation uses the IDispatch interface. You can forget about the locale setting totally.
Adding of code fragment to export datetime variant and format the cell by code: ----------------
I am using MFC so the the code is very simple, you may have to convert to winapi youself
_Application app;
if (!app.CreateDispatch("Excel.Application"))
{ AfxMessageBox("Cannot start Excel !");
return;
}
app.SetVisible(TRUE);
COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
Workbooks objbooks = app.GetWorkbooks();
_Workbook objbook = objbooks.Add(VOptional);
Worksheets objsheets = objbook.GetWorksheets();
_Worksheet objsheet = objsheets.GetItem(COleVariant((short)1));
// get current date time
COleDateTime timeCurrent = COleDateTime::GetCurrentTime();
// setting date values
Range range;
range = objsheet.GetRange(COleVariant("A2"), COleVariant("B3"));
range.SetValue(COleVariant(timeCurrent));
// range.SetNumberFormat(COleVariant("dd/mm/yyyy"));
range.SetNumberFormat(COleVariant("dddd, mmmm dd, yyyy"));
AfxMessageBox("Done...");