QDateTime::fromString returns invalid Date, what am I missing? - c++

I have some code that reads a datetime from a sqlite database, the datetime is returned as a string. when I try to convert it to a date using QDateTime::FromString it returns an invalid date. Below is the time as returned from the database and conversion.
Why is this failing to parse?
// -this is the value returned from the DB currentServerTime=2012-01-17 19:20:27.0
QString format("yyyy/MM/dd hh:mm:ss");
QString qCurrentServerTime(currentServerTime);
now = QDateTime::fromString(qCurrentServerTime, format);

No expert in QT, but if QDateTime::fromString() works as one would (reasonably) expect and according to this, you're not using the correct pattern.
You indicate the string read from the sqllite database is like "2012-01-17 19:20:27.0", then your format should be like yyyy-MM-dd HH:mm:ss.z.
In detail:
Your separator should by '-' not '/' (as you show in the example)
The time seems to be in 24 hours format (19 -> 7 p.m.) (so use HH instead of hh)
You have one digit for milliseconds, so add .z.

Related

Unreliable DateTime parsing by COleDateTime

I am trying to parse the date using ParseDateTime method provided by COleDateTime class. But parsing of two different dates in the same program is returning inconsisent values for the month.
Code Snippet:
COleDateTime dtCreated;
dtCreated.ParseDateTime(safe_cast<CString>(strCreatedDate));
Inconsistent RESULTS:
if strCreatedDate = "10/7/2020" (in mm.dd.yyyy format)
then dtCreated.GetMonth() = 7 (but it should be 10)
if strCreatedDate = "7/29/2020" (in mm.dd.yyyy format)
then dtCreated.GetMonth() = 7 (in this case, it is correct)
UPDATE:
The value of date present in the strCreatedDate vairable could be "dd.mm.yyyy" OR "mm.dd.yyyy" format. But I do have the information about the data format available in a separate variable. Based on the format, I want COleDateTime to correctly parse the DateTime string. How can I do that?
Since String^ is your input you could use DateTime::ParseExact, and then convert DateTime to COleDateTime using DateTime.ToOADate:
COleDateTime dtCreated(DateTime::ParseExact(
strCreatedDate, isDMY ? "d.M.yyyy" : "M.d.yyyy", nullptr).ToOADate());

NiFi toDate() Function altering the content

i have been trying to convert a string to date using
${test:toString():toDate('dd-MMM-yy HH.mm.ss.SSSSSSSSS'):format('dd-MMM-yy HH.mm.ss.SSSSSSSSS')}
my value for test attribute is like 13-MAR-20 15.50.41.396000000
when i'm using the above mentioned expression to convert the string to Date, it actually is changing the date as below:
test (input value):
13-MAR-20 15.50.41.396000000
time (output value)
18-Mar-20 05.50.41.000000000
please advise!
I ran into a similar issue with date time encoded in ISO 8601.
The problem is, that the digits after the second are defined as fragment of a second, not milliseconds. If it has 3 digits, it equivalent to milliseconds. If more than 3, the toDate() function parse the fragment of a second as milliseconds. In your case 396000000 milliseconds = 4,58333333 days.
I solved my issue with replaceAll() by cutting digits to first 3.
${test:replaceAll('(\.[0-9]{3})([0-9]+)','$1')}
But my value was formatted as 18-06-20T05:50:41.396000000, so maybe you have to adjust the regex.

String to YYYY-MM-DD date format in Athena

So I've looked through documentation and previous answers on here, but can't seem to figure this out.
I have a STRING that represents a date. A normal output looks as such:
2018-09-19 17:47:12
If I do this, I get it to return in this format 2018-09-19 17:47:12.000:
SELECT
date_parse(click_time,'%Y-%m-%d %H:%i:%s') click_time
FROM
table.abc
But that's not the output I need. I was just trying to show that I'm close, but clearly missing something. When I change click_time to date_parse(click_time,'%Y-%m-%d'), it sends back INVALID_FUNCTION_ARGUMENT: Invalid format: "2018-09-19 17:47:12" is malformed at " 17:47:12"
So there's clearly something I'm not doing correctly to get it to simply return 2018-09-19.
date_parse converts a string to a timestamp. As per the documentation, date_parse does this:
date_parse(string, format) → timestamp
It parses a string to a timestamp using the supplied format.
So for your use case, you need to do the following:
cast(date_parse(click_time,'%Y-%m-%d %H:%i:%s')) as date )
For your further reference, you can go to the below link for prestodb online documentation https://prestodb.github.io/docs/current/functions/datetime.html

How to use fromString() with currentDate()

I'm trying to get current date from the system clock and change its format (i.e. to this format dd/MM/yyyy). After that I need to set QDateEdit using setDate(). My problem is that using fromString() with QDate::currentDate() returns an invalid date. This is the piece of code regarding this issue
QDate date = QDate::currentDate().fromString("dd/MM/yyyy");
qDebug() << date.toString(); // <-- returns empty string
StartDateEdit->setDate(date); // <-- adding invalid date shows 1-1-2000
My system clock has this format M/d/yyyy in Windows 10. Any suggestions?
QDate::currentDate() is a static function, this returns the current date, the object stores the date, not the format. If you want to display the form: "dd/MM/yyyy" you must use the setDisplayFormat function of QDateEdit.
QDate date = QDate::currentDate();
StartDateEdit->setDate(date);
StartDateEdit->setDisplayFormat("dd/MM/yyyy");
Note:The fromString function converts a string to QDate indicating the format of the string.

opening a file which has date in a formatted manner and get that date to compare

I am programming in c++,i have a requirement as to get the date only ,rest of the components are not needed,
i have contents in my file in the following way:-
2012-03-26T15:05:24.844Z - DEBUG: Logging_Test:test3 : Testing file logger
2012-03-26T15:05:24.844Z->this part is returned from a function GetDateTime(),which has a stringstream sDateStream and returing to the function GetDateTime() a string
My objective is just to get the date from this part and compare it with the system's date and for the system date i m using:-
void FileLogger::date()
{
SYSTEMTIME time;//variable time to get the system time
GetLocalTime(&time);//reference to the timek
int hour = time.wHour;
if (hour > 12) hour -= 12;
int year = time.wYear;
int month = time.wMonth;
int day = time.wDay;
//not returning anything just storing values of day ,month
//& year
}
now i need to make a comparision b/w two dates and if the date in the .txt is smaller than today's date a new file gets created
please help guys i know it is very simple for the masters but for a starter like me it is creating troubles , please provide help in form of some code so that i can understand it
If you've got the line as a string, and you're sure of the format, the
simplest solution would be:
std::string dateString(
line.begin(), std::find( line.begin(), line.end(), 'T' ) );
(I'd still do some extra checking; e.g. that the dateString is exactly
10 characters long, that it only contains digits or '-', etc.)
This is the ISO date format; it has been especially designed to support
comparison directly as a string, so you might want to consider getting
the system date as a string in ISO format as well. (This is very easy
if you use the standard functions time, localtime and strftime.)