Convert string to date object in Visual Studio - c++

Need to convert a string, which stores the date in format like such: "Apr 23 2014 12:39:17" to a number or an object; Working with visual studio in a MS-specific environment.
In C++ is there a easy to use function that can achieve this?
I am doing this in order to do a comparison between the string date and now().
Thanks.

Here is the solution I found for my problem.
To clarify: A string representation of a date needed to be converted into some sort of date object, so that I could find the difference between 2 dates.
This works on MS VisualStudio2010 & uses the microsoft classes. (Basically; It won't work on a unix box!).
// Create 2 COleDateTime objects:
COleDateTime DateTime1;
COleDateTime DateTime2;
// 'Get' 2 string dates:
BSTR time1 = L"Apr 24 2014 09:20:20";
BSTR time2 = L"Apr 23 2014 12:39:17";
// Parse the string dates into the date objects (See! Its alot easier then I thought!)
DateTime1.ParseDateTime(time1);
DateTime2.ParseDateTime(time2);
// Calculate the time difference with a COleDateTimeSpan Object...
COleDateTimeSpan timeSpan = DateTime2 - DateTime1;
// Create integer with the difference in time in seconds...
CString str = timeSpan.Format(_T("%S"));
int differenceInSeconds = _tstoi(str);
Hope this helps someone!

Related

Extract Date and Time in ABAP via Regex

I wanted to separate the time and date from this string using REGEX because I feel like it is the only way I can separate it. But I am not really familiar on how to do it maybe someone can help me out here.
The original string: Your item was delivered in or at the mailbox at 3:34 pm on September 1, 2016 in TEXAS, MT 59102
The output i want to achieve/populate:
lv_time = 3:34 pm
lv_date = September 1, 2016
Here's the code I was trying to do but I am only able to cut it like this:
lv_status = Your item was delivered in or at the mailbox at
lv_time = 3
lv_date = :34 pm on September 1, 2016 in TEXAS, MT 59102.
Here's the code I have so far:
DATA: lv_status TYPE string,
lv_time TYPE string,
lv_date TYPE string,
lv_off TYPE i.
lv_status = 'Your item was delivered in or at the mailbox at 3:34 pm on September 1, 2016 in TEXAS, MT 59102.'.
FIND REGEX '(\d+)\s*(.*)' IN lv_status SUBMATCHES lv_time lv_date MATCH OFFSET lv_off.
lv_status = lv_status(lv_off).
You asked for it, here it comes:
\b((1[0-2]|0?[1-9]):([0-5][0-9]) ([AaPp][Mm])) on (January|February|March|April|May|June|July|August|September|October|November|December)\D?(\d{1,2}\D?)?\D?((?:19[7-9]\d|20\d{2})|\d{2})
This accepts time in HH:MM am/pm format, and dates in Jan-Dec, dd 1970-2999.
Each part is captured in its own group.
The demo shows a version that allows abbreviated month names:
Demo

Cannot parse datetime in pm format with booster, C++, graphlab

I've tried to convert datetime string into datetime of an SArray (uses C++ booster library), but it does not seem to understand the %p format specifier. http://www.boost.org/doc/libs/1_43_0/doc/html/date_time/date_time_io.html
This documentation says specifiers marked with ! do not currently work for input.
Does that mean that you cannot parse anything with pm or PM?
I was able to get the string-to-datetime conversion to work by making two small changes:
Use %I for the hour on a 12-hour clock (%H is for a 24 hour clock).
Use %P (upper case) for the AM/PM flag.
Here's what works for me:
sf = gl.SFrame({'date': ['2015-11-06 02:12:42 pm',
'2015-11-05 03:43:11 pm']})
sf['date2'] = sf['date'].str_to_datetime('%Y-%m-%d %I:%M:%S %p')

Checking two TDateTime variables

I am using C++ Builder and have the following question:
I am wanting to detect if a date/time is later than another date/time, and by how much.
Here is my current code:
TDateTime testFirstDate("11/09/2012");
TDateTime testFirstTime("14:00");
TDateTime testSecondDate("12/09/2012");
TDateTime testSecondTime("16:00");
TDateTime testCombined1 = testFirstDate + testFirstTime;
TDateTime testCombined2 = testSecondDate + testSecondTime;
TDateTime testDateDifference = testSecondDate - testFirstDate;
std::cout << testDateDifference;
In the above example, the following gets printed out: 31/12/1899
The difference between the two values is only 1 day. Why is: 31/12/1899 being printed, and not something like: 1?
The difference is 1 day, 22 hours.
TDateTime in Delphi and C++ Builder is a double, where the whole portion (the part to the left of the decimal point) stores the number of days since a base date of December 30, 1899 (see note below), and the fractional portion (the part to the right of the decimal point) is the time.
The 1899 you're seeing after the subtraction is because you have less than a full day, and therefore the whole portion of the number is zero, and as I mentioned a date of zero is the base date in December, 1899. Since your date is 1 day later than that base date (when represented as a TDateTime, the date is interpreted as December 31, 1899.
The time portion for 22 hours is approximately 0.9167 (actually, 0.916666666666667), which represents 22/24ths of a day.
Delphi's runtime library contains a unit called DateUtils, which IIRC is available to C++ Builder as well (there's a header file for it), which contains functions which may help you, like DaysBetween that you may find useful. There are C++ examples of it's use available here.
As far as equality (one date being after the other), you can use the standard >, <, >=, <=, !=, and == operators. I've demonstrated this below as well.
Here's a quick example (in Delphi, as I don't have C++ Builder installed on this machine) that might explain:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, DateUtils;
var
StartDate, EndDate, Diff: TDateTime;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
// Base date, formatted in US date format
WriteLn('BaseDate: ', FormatDateTime('mm/dd/yyyy hh:nn:ss', 0));
StartDate := EncodeDateTime(2012, 9, 11, 14, 0, 0, 0);
EndDate := EncodeDateTime(2012, 9, 12, 16, 0, 0, 0);
Diff := EndDate - StartDate;
WriteLn('Diff as String: ', DateToStr(Diff));
WriteLn('Diff as Double: ', Diff);
WriteLn('DaysBetween: ', DaysBetween(EndDate, StartDate));
// Equality
WriteLn('EndDate after StartDate`, EndDate > StartDate);
RegEx.Free;
ReadLn;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
This produces this output:
BaseDate: 12/30/1899 00:00:00
Diff as String: 12/31/1899
Diff as Double: 1.08333333332848E+0000
DaysBetween: 1
EndDate after StartDate: TRUE
NOTE: The base date was established by Microsoft for COM, and for compatibility reasons Delphi/C++ Builder adopted it.
You can use your normal -, +, <, >,== and = with TDateTime.
So to see if one date is ahead of another, you can, for example, subtract them and see if the result is greater or lesser than zero.

Checking if OSX is on a 24 hour clock

I would prefer to do this with Qt Methods if at all possible.
Currently in our code, we can distinguish that Windows is on a 24 hour clock; however not on Mac.
We have a method that returns a string such as: 1/9/2012 9:53:42 AM - Which is giving us a previous time, not the current one (Which is what we want), I do not want to mess with this method though.
I've been playing around with a way to determine if the current system clock is in military time; and to adjust the previous time returned from the string to reflect that. I can get this to work on Windows, but on Mac - it displays a normal 12-hour time regardless of whether we're on a 24-hour clock.
Ignore my crude-debugging messages or if I'm not particularly going at the problem correctly - I haven't been able to test it yet and tweak as necessary: (Explanation after code)
QLocale *ql = new QLocale();
QString qlTF = ql->timeFormat();
QString fileTime = QString::fromUtf8(str.GetSafeStringPtr());
if (qlTF.left(1) == (QString("H"))) // Our system clock is set to military time
{
QString newTime;
QStringList fileTimeDateSplit = fileTime.split(" ");
QStringList fileTimeSplit = fileTimeDateSplit.at(1).split(":");
m_editModified->setText(qlTF);
if (fileTimeSplit.at(0).toInt() < 12 && (fileTimeDateSplit.at(2) == "PM"))
{
int newHour = 12 + (fileTimeSplit.at(0).toInt()%12);
newTime.append(QString::number(newHour));
newTime.append(":");
newTime.append(fileTimeSplit.at(1));
newTime.append(":");
newTime.append(fileTimeSplit.at(2));
m_editModified->setText(QString("military after noon"));
}
}
else m_editModified->setText(qlTF);
Basically I'm grabbing the locale of the current machine to retrieve the system's time format.
fileTime is set to a string such as "1/9/2012 9:53:42 AM".
qlTF returns a format such as: HH:mm:ss , H:mm:ss, hh:mm:ss, or h:mm:ss - capital meaning it's a 24 hour clock.
I tokenize the different strings by the delimiters and then check to see if the time was greater than 12 and PM; then add the additional time and combine the new time string.
You can see that I did:
m_editModified->setText(qlTF);
for debugging purposes. On Windows, this will be set to HH:mm:ss; however even with a 24-hour clock enabled on a mac, it still returns h:mm:ss - which completely defeats the purpose.
Any ideas would be very much appreciated!
Why don't you just convert the string you have ("1/9/2012 9:53:42 AM") to QDateTime and then convert that QDateTime back to string in the format you want (I use ISODate in the example):
QString timeFormat = "M/d/yyyy h:m:s AP";
QDateTime dt = QDateTime::fromString("1/9/2012 9:53:42 AM", timeFormat);
QString text = "";
if (dt.isValid())
text = dt.toString(Qt::ISODate);

Check if string is of SortableDateTimePattern format

Is there any way I can easily check if a string conforms to the SortableDateTimePattern ("s"), or do I need to write a regular expression?
I've got a form where users can input a copyright date (as a string), and these are the allowed formats:
Year: YYYY (eg 1997)
Year and month: YYYY-MM (eg 1997-07)
Complete date: YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes: YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds: YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
I don't have much experience of writing regular expressions so if there's an easier way of doing it I'd be very grateful!
Not thoroughly tested and hence not foolproof, but the following seems to work:
var regex:RegExp = /(?<=\s|^)\d{4}(-\d{2}(-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{2})?)?\+\d{2}:\d{2})?)?)?(?=\s|$)/g;
var test:String = "23 1997 1998-07 1995-07s 1937-04-16 " +
"1970-0716 1993-07-16T19:20+01:01 1979-07-16T19:20+0100 " +
"2997-07-16T19:20:30+01:08 3997-07-16T19:20:30.45+01:00";
var result:Object
while(result = regex.exec(test))
trace(result[0]);
Traced output:
1997
1998-07
1937-04-16
1993-07-16T19:20+01:01
2997-07-16T19:20:30+01:08
3997-07-16T19:20:30.45+01:00
I am using ActionScript here, but the regex should work in most flavors. When implementing it in your language, note that the first and last / are delimiters and the last g stands for global.
I'd split the input field into many (one for year, month, day etc.).
You can use Javscript to advance from one field to the next once full (i.e. once four characters are in the year box, move focus to month) for smoother entry.
You can then validate each field independently and finally construct the complete date string.