convert wxString to time_t - c++

I have a wxString which has a date as its value. The date format is stored depending on the regional setting or locale settings.
For eg. wxString dateStr = "9/10/2013" [dd/mm/yyyy format for Italy as regional locale setting].
When I parse the date string using wxDateTime::ParseDate(dateStr) and try to convert it in time_t using wxDateTime::GetTicks() function. But it swaps the value of day and month when the day is less than or equal to 12 for example 3/10/2013 or 12/11/2013. I am getting month as 3 and 12, and day as 10 and 11 respectively. But it works fine if the date is greater than 12 i.e 14/10/2013 or 28/10/2013.
I want to convert the above date string into time_t depending upon the locale setting. I am using windows as well as linux for development env.
Please help me out from this problem with an example or code snippet.

I suggest you use wxDateTime::ParseDateFormat instead, then you can specify the exact format of the date-string.
The reason you have problem with ParseDate is that it first tries to parse the date-string in American format (where the format is mm/dd/yyyy), and if it fails it tries other formats.

Related

Cannot parse UTC date in Athena

I have the date string in the form: 2019-02-18 09:17:31.260000+00:00 and I am trying to convert it into date in Athena.
I have tried converting into timestamp as suggested in the SO answers but failed.
There is a discussion in https://github.com/prestodb/presto/issues/10567 but no answer to this particular date format.
I tried several format like 'YYYY-MM-dd HH:mm:ss.SSSSSSZ' but doesn't work and get error like INVALID_FUNCTION_ARGUMENT: Invalid format:..is malformed at "+00:00".
Been stuck for a while, any help is appreciated!
Athena is based on a very old version of Presto, and there is no straightforwad way of doing that with some string manipulation trick. For instance, you can use regexp_replace to extract the part of the string that's compatible with the built-in timestamp with timezone type and do:
SELECT cast(regexp_replace('2019-02-18 09:17:31.260000+00:00','(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\d{3}(.*)', '$1$2') AS timestamp with time zone)
Recent versions of Trino (formerly known as PrestoSQL) introduced support for variable-precision temporal types with up to nanosecond precision (12 decimals).
With that feature, you can just do:
trino> select cast('2019-02-18 09:17:31.260000+00:00' as timestamp(6) with time zone);
_col0
--------------------------------
2019-02-18 09:17:31.260000 UTC
(1 row)
A shorter version to Martin Traverso's answer is to sub string the extra characters:
select cast(substr('2019-02-18 09:17:31.260000+00:00',1,23) as timestamp);

Extract Date from epoch in NiFi

I have a CSV file with an attribute having epoch values like '1517334599.906'.
I want to convert/update the Epoch values into ISO timestamp 'yyyy-MM-dd HH:mm:ss.SSS' via NiFi.
That conversion is for Kibana to recognize the field as Timestamp. Is there a way to do this? If there is can anyone help me with the configuration?
Using NiFi's record capabilities you can use UpdateRecord with a CsvReader and CsvWriter.
See the "format" function in expression language for converting an epoch to a date string:
https://nifi.apache.org/docs/nifi-docs/html/expression-language-guide.html#format
In UpdateRecord you would do something like:
/eventDate = ${field.value:format("yyyy-MM-dd HH:mm:ss.SSS")}
This says take the value of /eventDate (change this to your field name) and set the value of that field to the result of the format function on the right.
The only thing I am not sure about is whether an epoch can have a decimal portion as shown in your example. I would expect it to be converted to a long which would be a whole number.

ColeDateTime::Format languagespecific different fields

I want to format a COleDateTime as a CString, so i use
COleDateTime dt = GetMyDateTimeObj();
dt.Format(LOCALE_NOUSEROVERRIDE, GetCurrentLocaleID());
Since my program is multilanguage, GetCurrentLocaleID gives me the LCID currently selected (not in windows but in my program).
So the above gives me the correctly formated string according to the selected user-language but its always in a format "date-with-year + time-with-seconds". I think this is the short format of a date and the long format of the time accordong to windows regional settings.
What can i do to get the String with
- the year as just 2 numbers?
- completely without the year?
- the time-part without seconds?
Are there any switches to COleDateTime::Format that can give me year-as-2-digits, time-without-seconds but keeps fieldorder and separators specified by the LCID?
Of course i dont want to change any windows-regional-settings to do this and i dont want to use any fixed format-strings like _T("%d.%m.%Y %H:%M:%S") as this would break language-specific order of fields and separators.
Thanks for your hints and answers
Micha

Modify date time to a specific format

I'm having trouble coding a conversion to a specific time format along with converting to string format. I have the following code.
txtMonday->Text = Convert::ToString(dtpMondayIn->Value);
I want txtMonday(text box) not only to display dtpMondayIn's value(datetimepicker), but in h.mm format. Currently, it will display the date and time, for example, January 1, 2014 8:40:30 AM. I want the text box to show only 8.40 as in hours and minutes. How could I code this? I'm using Visual Studio 2012 and c++ language.
I am not really sure but
try this,
txtMonday->Text = Convert::ToString(dtpMondayIn->Value.ToString("hh:mm tt",CultureInfo.InvariantCulture));
OR
Convert::ToString(dtpMondayIn->Value.ToString("hh:mm",CultureInfo.InvariantCulture));
Also check DATETIME PICKER VALUE PROP
and DATETIME FORMATS
Let me know in case it works.

What is the right way to convert into UNIX timestamp from the date and time in C/C++?

I have a lot of dates with time in this format:
day.mon.year - hour:min:sec
And I need to convert this dates with time into Unix timestamp.
I used tm structure, but I can't fill those fields:
tm_wday
tm_yday
And I don't must I fill those field, because I don't know do this field have any effect to the value of Unix timestamp.
Help me to choose rigth way to calculate Unix timestamp.
P.S. Dates with time aren't current, they can be date of the 20-th century or future dates (to 2038 year).
P.P.S. I use OS Windows.
POSIX has a formula for exactly what you want:
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15
tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
(tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
This works whenever you have a broken-down time in GMT, even if the underlying system's mktime, etc. functions do not use the same format time_t as "Unix timestamps".
If your original time is in local time, you can use mktime and gmtime to convert it to GMT using the system's notion of timezone rules. If you want to apply your own timezone offset rules, just do that manually before using the above formula.
If you are on unix, mktime() will get the second part of the timestamp. It ignores the tm_wday and tm_yday fields.