Unreliable DateTime parsing by COleDateTime - c++

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());

Related

Finding time and date with Regular Expression (RegEx) in Dart language

I'm writing an application with Flutter. I read the times and dates from a source. The date and time format string sent by the resource is:
(Day, Month, Year, Hour, Minute, Second)
07.04.2021 13:30:00
03.04.2021 11:30:00
04.04.2021 17:30:00
03.04.2021 17:30:00
I want to convert this date and time format to DateTime data type with DateTime.parse() function. Here are some examples of what this function accepts as strings and what I need:
"2012-02-27 13:27:00"
"20120227 13:27:00"
"20120227T132700"
I have to convert the string type data coming to me from the source into one of these formats. But in Dart language I couldn't create the Regular Expression needed to do this and couldn't find it anywhere.
I would be very grateful if anyeone could help me understand what I should do.
If you have to play a lot with the dates, you could use the Jiffy package to ease your development.
DateTime yourDatetime = Jiffy("07.04.2021 13:30:00", "dd.MM.yyyy hh:mm:ss").dateTime;
This is a piece a cake by using regular expressions:
var regExp = RegExp(r'(\d{4}-?\d\d-?\d\d(\s|T)\d\d:?\d\d:?\d\d)');
use DateFormat.parse and DateFormat.format from intl package:
https://api.flutter.dev/flutter/intl/DateFormat/parse.html
https://api.flutter.dev/flutter/intl/DateFormat/format.html
final date = DateFormat("yyyy.MM.dd HH:mm:ss").parse("07.04.2021 13:30:00");
DateFormat("yyyy-MM-dd HH:mm:ss").format(date);
DateTime.parse accepts only a subset of ISO 8601 formats: https://api.flutter.dev/flutter/dart-core/DateTime/parse.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.

C++ , using strptime and strftime to convert date formats including invalid dates

Am using strptime and strftime to convert date from one format to another in C++,
Everything works fine for valid input dates but in case of invalid dates it just can't work.
Using mktime 'fixes' or changes dates like if there is an invalid day, it automatially changes the month as well. What I want is to convert any input date from one format to another like
if it is 12/22/99 and i want to convert from mm/dd/yy to dd-mm-yyyy, it shall convert to 22-12-1999.
If there is any invalid date, it shall emit 00 and keep the other things in required format, like 12/45/01 (in mm/dd/yy) shall convert to 00-12-2001 (in dd/mm/yyyy format since day can't be 45), the problem is strptime, mktime, all changes the value since the tm timestruct works that way, is there any possibility to do that?
A simple use case can be
convert mm/dd/yy to dd-mm-yyyy
03/33/99 -> 00-03-1999
I tried my best but strptime just changes the corresponding month in case of a wrong input day. I tried putting 00 in case of a wrong date, but strptime handles 00 differently too, so for each format, it can't work. Really stuck now.
////////////////////////////////////////////
// For those who wants a programming question
// i repeat, it is a programming question
/////////////////////////////////////////////
struct tm timestruct = {0};
// it can be in any other supported format as well
char *in = "80-00-11";
string fromStr("%Y-%d-%m ");
string toStr("%d/%m/%y");
char buf[255];
if(strptime(in, fromStr.c_str(), &timestruct) == NULL ) // invalid date
strftime(buf, sizeof(buf), toStr.c_str(), &timestruct);
puts(buf);
/* The problem is in case of wrong date like the supplied date in char *in.
i want to leave out the correct parameters of the supplied data, like
00/**11/98**, but it changes the **11** to some other value, my problem is i may have day as empty field like given in the code snippet
*/

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

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.

C++ boost date format

I have a vector string of dates in the from "dd-mmm-yyyy" so for example
todays date would be:
std::string today("07-Sep-2010");
I'd like to use the date class in boost but to create a date object the
constructor for date needs to be called as follows:
date test(2010,Sep,07);
Is there any easy/elegant way of passing dates in the format "dd-mmm-yyyy"?
My first thought was to use substr and then cast it? But I've read that there's also
the possibility of using 'date facets'?
Thanks!
include "boost/date_time/gregorian/parsers.hpp"
date test = boost::gregorian::from_us_string("07-Sep-2010")
There is a builtin parser for this form of date in Boost itself, check out the docs here:
http://www.boost.org/doc/libs/1_44_0/doc/html/date_time/date_time_io.html#date_time.io_objects
date_type parse_date(...)
Parameters:
string_type input
string_type format
special_values_parser
Parse a date from the given input using the given format.
string inp("2005-Apr-15");
string format("%Y-%b-%d");
date d;
d = parser.parse_date(inp,
format,
svp);
// d == 2005-Apr-15
with inp adjusted for your needs.