AX 2012 Technical - Help with the DateTimeUtil Class
Problem Description
Need a bit of clarification on the DateTimeUtil Class and the questions are:
1. Some functionalities of client's product needs to store Date/Time to the 100 Nano seconds unit like the System.DateTime type. Is it possible to get this functionality in the utcDateTime?
2. The function datetime2str() you have thefollowing code:
// Start
utcDT = DateTimeUtil::utcNow();
info(datetime2str(utcDT));
// End
Assume that I live in Brussels, the company and user settings have the time zone set to GMT+1. I expect now that infolog prints me the local time, instead it prints the utc value without zone conversion. Any Ideas?
Thanks again!
Solution to point#2
Can’t answer point1, but for point 2 they need to change the code like this:
utcDT = DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::utcNow(), DateTimeUtil::getUserPreferredTimeZone());
Solution to point#1
Answering the first question. The utcdatetime type supports a minimum precision of 1 second. It is not possible to go to 100 nano second values. I would be very interested in knowing the partner’s scenario. You can share that with me offline if you can.
Best Response
I had no problem (except a little .NET interop weirdness w/int64) creating a table and storing high precision date/time values in it. Create a table called “TestDateTime” with two columns “str Description” and “int64 DateTimeInt” and run the following job to try it out:
static void DateTimeInTable(Args _args)
{
TestDateTime dtTable;
void SaveRec(str _description, str _datetimeStr)
{
System.DateTime dt;
int64 dtInt;
dtTable.clear();
dtTable.Description = _description;
dt = System.DateTime::Parse(_datetimeStr);
dtInt = dt.ToBinary();
dtTable.DateTimeInt = dtInt;
dtTable.write();
}
void ShowRec(TestDateTime _rec)
{
System.DateTime datetime = System.DateTime::FromBinary(_rec.DateTimeInt);
str dtStr = datetime.ToString('o');
info(strfmt('%1=%2', _rec.Description, dtStr));
}
void ShowRecs(str _orderby)
{
info(_orderby);
while(true)
{
next dtTable;
if(dtTable)
ShowRec(dtTable);
else
break;
}
info('');
}
delete_from dtTable;
SaveRec('Rec 1','2009-01-01T12:12:12.1234567');
SaveRec('Rec 4','2009-02-01T12:12:12.1234567');
SaveRec('Rec 3','2009-06-01T12:12:12.1234567');
SaveRec('Rec 2','2009-01-02T12:12:12.1234567');
select nofetch dtTable order by Description;
ShowRecs('By Description');
select nofetch dtTable order by DateTimeInt;
ShowRecs('By DateTime');
}